Open History to see the last commit
1import { useAtomSet, useAtomValue } from "@effect/atom-react";
2import { AsyncResult } from "effect/reactivity";
3
4import { copyTextWithHaptic } from "../../../lib/copyTextWithHaptic";
5import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../../state/preferences";
6import { serverEnvironment } from "../../../state/server";
7import type { SettingsTarget } from "../settings-environment-filter";
8import { ListActionRow as SettingsActionRow } from "../../../components/ui/ListActionRow";
9import { SettingsSection } from "./SettingsSection";
10
11/** Suggests `t3 doctor` on every machine whose database still holds at least 1 GiB of freed pages. */
12export 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 ),
21 );
22 const preferences = useAtomValue(mobilePreferencesAtom);
23 const updatePreferences = useAtomSet(updateMobilePreferencesAtom);
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;
31 return (
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 ))}
46 <SettingsActionRow
47 icon="xmark"
48 label="Dismiss"
49 onPress={() =>
50 updatePreferences({
51 transform: (current) => ({
52 dismissedDatabaseStorageNotices: [
53 ...new Set([
54 ...(current.dismissedDatabaseStorageNotices ?? []),
55 ...visible.map((recommendation) => recommendation.environmentId),
56 ]),
57 ],
58 }),
59 })
60 }
61 />
62 </SettingsSection>
63 );
64}