Skip to content

Commit 8cd561e

Browse files
fix(web,mobile): list database space for all machines in one notice (#22575)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2085ea3 commit 8cd561e

5 files changed

Lines changed: 112 additions & 80 deletions

File tree

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,14 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
434434

435435
{props.page === "maintenance" ? (
436436
<>
437-
{!projectSelected &&
438-
selectedTargets
439-
.filter(
437+
{!projectSelected ? (
438+
<DatabaseStorageNotice
439+
targets={selectedTargets.filter(
440440
(target) =>
441441
target.serverConfig.environment.capabilities.databaseStorage === true,
442-
)
443-
.map((target) => (
444-
<DatabaseStorageNotice key={target.environmentId} target={target} />
445-
))}
442+
)}
443+
/>
444+
) : null}
446445
{!projectSelected ? (
447446
<SettingsSection title="Manage environments">
448447
{selectedTargets.map((target) => (

‎apps/mobile/src/features/settings/components/DatabaseStorageNotice.tsx‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
import { useAtomSet, useAtomValue } from "@effect/atom-react";
2-
import { shouldRecommendDatabaseCompaction } from "@t3tools/contracts";
32
import { AsyncResult } from "effect/reactivity";
4-
import { View } from "react-native";
53

6-
import { AppText as Text } from "../../../components/ui/AppText";
74
import { copyTextWithHaptic } from "../../../lib/copyTextWithHaptic";
85
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../../state/preferences";
96
import { serverEnvironment } from "../../../state/server";
107
import type { SettingsTarget } from "../settings-environment-filter";
118
import { SettingsActionRow } from "./SettingsActionRow";
129
import { SettingsSection } from "./SettingsSection";
1310

14-
/** Suggests `t3 doctor` for a database that still holds at least 1 GiB of freed pages. */
15-
export function DatabaseStorageNotice({ target }: { readonly target: SettingsTarget }) {
16-
const result = useAtomValue(
17-
serverEnvironment.databaseStorage({ environmentId: target.environmentId, input: {} }),
11+
/** Suggests `t3 doctor` on every machine whose database still holds at least 1 GiB of freed pages. */
12+
export function DatabaseStorageNotice({
13+
targets,
14+
}: {
15+
readonly targets: readonly SettingsTarget[];
16+
}) {
17+
const recommendations = useAtomValue(
18+
serverEnvironment.databaseCompactionRecommendations(
19+
JSON.stringify(targets.map((target) => target.environmentId)),
20+
),
1821
);
1922
const preferences = useAtomValue(mobilePreferencesAtom);
2023
const updatePreferences = useAtomSet(updateMobilePreferencesAtom);
21-
const dismissed =
22-
AsyncResult.isSuccess(preferences) &&
23-
preferences.value.dismissedDatabaseStorageNotices?.includes(target.environmentId) === true;
24-
const status = result._tag === "Success" ? result.value : null;
25-
if (dismissed || status === null || !shouldRecommendDatabaseCompaction(status)) return null;
26-
const gib = (status.reclaimableBytes / 1024 ** 3).toFixed(1);
24+
const dismissed = AsyncResult.isSuccess(preferences)
25+
? (preferences.value.dismissedDatabaseStorageNotices ?? [])
26+
: [];
27+
const visible = recommendations.filter(
28+
(recommendation) => !dismissed.includes(recommendation.environmentId),
29+
);
30+
if (visible.length === 0) return null;
2731
return (
28-
<SettingsSection title={`Reclaim ${gib} GiB on ${target.label}`}>
29-
<View className="gap-3 p-4">
30-
<Text className="text-sm text-muted-foreground">
31-
Old history is cleaned up automatically, but this database still holds the freed space.
32-
Run this command on {target.label} to return it to disk; it pauses and restarts the
33-
background service. Quit the desktop app first if it hosts this environment.
34-
</Text>
35-
<Text selectable className="font-mono text-sm text-foreground">
36-
{status.compactCommand}
37-
</Text>
38-
</View>
39-
<SettingsActionRow
40-
icon="doc.on.doc"
41-
label="Copy command"
42-
onPress={() => copyTextWithHaptic(status.compactCommand, { target: "command" })}
43-
/>
32+
<SettingsSection
33+
title="Reclaim database space"
34+
footer="Old history is cleaned up automatically, but these databases still hold the freed space. Run the command on each machine to return it to disk; it pauses and restarts the background service. Quit the desktop app first if it hosts the environment."
35+
>
36+
{visible.map(({ environmentId, status }) => (
37+
<SettingsActionRow
38+
key={environmentId}
39+
icon="doc.on.doc"
40+
label={targets.find((target) => target.environmentId === environmentId)?.label ?? ""}
41+
description={status.compactCommand}
42+
detail={`${(status.reclaimableBytes / 1024 ** 3).toFixed(1)} GiB`}
43+
onPress={() => copyTextWithHaptic(status.compactCommand, { target: "command" })}
44+
/>
45+
))}
4446
<SettingsActionRow
4547
icon="xmark"
4648
label="Dismiss"
@@ -50,7 +52,7 @@ export function DatabaseStorageNotice({ target }: { readonly target: SettingsTar
5052
dismissedDatabaseStorageNotices: [
5153
...new Set([
5254
...(current.dismissedDatabaseStorageNotices ?? []),
53-
target.environmentId,
55+
...visible.map((recommendation) => recommendation.environmentId),
5456
]),
5557
],
5658
}),
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,70 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { shouldRecommendDatabaseCompaction, type EnvironmentId } from "@t3tools/contracts";
2+
import type { EnvironmentId } from "@t3tools/contracts";
33
import * as Schema from "effect/Schema";
44

55
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
66
import { useLocalStorage } from "../../hooks/useLocalStorage";
77
import { serverEnvironment } from "../../state/server";
88
import { Button } from "../ui/button";
9-
import { SettingsGroup } from "./SettingsGroup";
109
import { SettingsRow, SettingsSection } from "./settingsLayout";
1110

12-
/** Suggests `t3 doctor` for a database that still holds at least 1 GiB of freed pages. */
11+
const DismissedEnvironmentIds = Schema.Array(Schema.String);
12+
13+
/** Suggests `t3 doctor` on every machine whose database still holds at least 1 GiB of freed pages. */
1314
export function DatabaseStorageNotice({
14-
environmentId,
15-
label,
15+
environments,
1616
}: {
17-
environmentId: EnvironmentId;
18-
label: string;
17+
environments: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }>;
1918
}) {
20-
const result = useAtomValue(serverEnvironment.databaseStorage({ environmentId, input: {} }));
19+
const recommendations = useAtomValue(
20+
serverEnvironment.databaseCompactionRecommendations(
21+
JSON.stringify(environments.map((environment) => environment.environmentId)),
22+
),
23+
);
2124
const [dismissed, setDismissed] = useLocalStorage(
22-
`database-storage-notice-dismissed:${environmentId}`,
23-
false,
24-
Schema.Boolean,
25+
"database-storage-notice-dismissed",
26+
[],
27+
DismissedEnvironmentIds,
2528
);
2629
const { copyToClipboard } = useCopyToClipboard({ target: "command" });
27-
const status = result._tag === "Success" ? result.value : null;
28-
if (dismissed || status === null || !shouldRecommendDatabaseCompaction(status)) return null;
30+
const visible = recommendations.filter(
31+
(recommendation) => !dismissed.includes(recommendation.environmentId),
32+
);
33+
if (visible.length === 0) return null;
2934
return (
30-
<SettingsSection id={`storage-database-${environmentId}`} title="Database">
31-
<SettingsGroup>
35+
<SettingsSection id="storage-database" title="Database">
36+
<SettingsRow
37+
title="Reclaim freed database space"
38+
description="Old history is cleaned up automatically, but these databases still hold the freed space. Run the command on each machine to return it to disk; it pauses and restarts the background service. Quit the desktop app first if it hosts the environment."
39+
/>
40+
{visible.map(({ environmentId, status }) => (
3241
<SettingsRow
33-
title={`Reclaim ${(status.reclaimableBytes / 1024 ** 3).toFixed(1)} GiB on ${label}`}
34-
description={
35-
<>
36-
Old history is cleaned up automatically, but this database still holds the freed
37-
space. Run the command on {label} to return it to disk; it pauses and restarts the
38-
background service. Quit the desktop app first if it hosts this environment.
39-
<code className="mt-2 block select-text break-all">{status.compactCommand}</code>
40-
</>
41-
}
42+
key={environmentId}
43+
title={`${(status.reclaimableBytes / 1024 ** 3).toFixed(1)} GiB on ${
44+
environments.find((environment) => environment.environmentId === environmentId)
45+
?.label ?? environmentId
46+
}`}
47+
description={<code className="select-text break-all">{status.compactCommand}</code>}
4248
control={
4349
<div className="flex flex-wrap items-center gap-1.5">
4450
<Button
4551
variant="outline"
46-
size="sm"
52+
size="xs"
4753
onClick={() => copyToClipboard(status.compactCommand, undefined)}
4854
>
4955
Copy command
5056
</Button>
51-
<Button variant="ghost" size="sm" onClick={() => setDismissed(true)}>
57+
<Button
58+
variant="ghost"
59+
size="xs"
60+
onClick={() => setDismissed((current) => [...current, environmentId])}
61+
>
5262
Dismiss
5363
</Button>
5464
</div>
5565
}
5666
/>
57-
</SettingsGroup>
67+
))}
5868
</SettingsSection>
5969
);
6070
}

‎apps/web/src/components/settings/StorageSettings.tsx‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,19 +491,14 @@ export function StorageSettingsPanel() {
491491

492492
return (
493493
<SettingsPageContainer>
494-
{!isProjectScope &&
495-
connectedEnvironments
496-
.filter(
494+
{!isProjectScope && (
495+
<DatabaseStorageNotice
496+
environments={connectedEnvironments.filter(
497497
(environment) =>
498498
environment.serverConfig?.environment.capabilities.databaseStorage === true,
499-
)
500-
.map((environment) => (
501-
<DatabaseStorageNotice
502-
key={environment.environmentId}
503-
environmentId={environment.environmentId}
504-
label={environment.label}
505-
/>
506-
))}
499+
)}
500+
/>
501+
)}
507502
<SettingsSection id="storage-worktrees" title="Worktrees">
508503
{!isProjectScope && <WorktreesDirectoryRow />}
509504
{isProjectScope && (

‎packages/client-runtime/src/state/server.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type ServerLifecycleStreamReadyEvent,
77
type ServerSelfUpdateProgressEvent,
88
type ServerSelfUpdateResult,
9+
shouldRecommendDatabaseCompaction,
910
WS_METHODS,
1011
} from "@t3tools/contracts";
1112
import * as Cause from "effect/Cause";
@@ -954,6 +955,34 @@ export function createServerEnvironmentAtoms<R, E>(
954955
: AsyncResult.success(result.value, result);
955956
}).pipe(Atom.withLabel(`environment-data:server:welcome:${environmentId}`)),
956957
);
958+
const databaseStorage = createEnvironmentRpcSubscriptionAtomFamily(runtime, {
959+
label: "environment-data:server:database-storage",
960+
tag: WS_METHODS.serverGetDatabaseStorage,
961+
idleTtlMs: 0,
962+
});
963+
// Keyed by a JSON array of environment ids so one notice can list every machine.
964+
const databaseCompactionRecommendations = Atom.family((environmentIdsKey: string) => {
965+
const sources = (JSON.parse(environmentIdsKey) as EnvironmentId[]).map((environmentId) => ({
966+
environmentId,
967+
atom: databaseStorage({ environmentId, input: {} }),
968+
}));
969+
return Atom.make((get) =>
970+
sources
971+
.flatMap(({ environmentId, atom }) => {
972+
const result = get(atom);
973+
return result._tag === "Success" &&
974+
result.value !== null &&
975+
shouldRecommendDatabaseCompaction(result.value)
976+
? [{ environmentId, status: result.value }]
977+
: [];
978+
})
979+
.sort((left, right) => right.status.reclaimableBytes - left.status.reclaimableBytes),
980+
).pipe(
981+
Atom.withLabel(
982+
`environment-data:server:database-compaction-recommendations:${environmentIdsKey}`,
983+
),
984+
);
985+
});
957986
const welcome = (target: {
958987
readonly environmentId: EnvironmentId;
959988
readonly input: EnvironmentRpcInput<typeof WS_METHODS.subscribeServerLifecycle>;
@@ -972,11 +1001,8 @@ export function createServerEnvironmentAtoms<R, E>(
9721001
tag: WS_METHODS.serverRunStorageCleanup,
9731002
concurrency: { mode: "singleFlight", key: ({ environmentId }) => environmentId },
9741003
}),
975-
databaseStorage: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
976-
label: "environment-data:server:database-storage",
977-
tag: WS_METHODS.serverGetDatabaseStorage,
978-
idleTtlMs: 0,
979-
}),
1004+
databaseStorage,
1005+
databaseCompactionRecommendations,
9801006
storageCleanupReport: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
9811007
label: "environment-data:server:storage-cleanup-report",
9821008
tag: WS_METHODS.serverGetStorageCleanupReport,

0 commit comments

Comments
 (0)