Open History to see the last commit
1import { useAtomSet, useAtomValue } from "@effect/atom-react";
2import { shouldRecommendDatabaseCompaction } from "@t3tools/contracts";
3import { AsyncResult } from "effect/reactivity";
4import { View } from "react-native";
5
6import { AppText as Text } from "../../../components/ui/AppText";
7import { copyTextWithHaptic } from "../../../lib/copyTextWithHaptic";
8import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../../state/preferences";
9import { serverEnvironment } from "../../../state/server";
10import type { SettingsTarget } from "../settings-environment-filter";
11import { SettingsActionRow } from "./SettingsActionRow";
12import { SettingsSection } from "./SettingsSection";
13
14/** Suggests `t3 doctor` for a database that still holds at least 1 GiB of freed pages. */
15export function DatabaseStorageNotice({ target }: { readonly target: SettingsTarget }) {
16 const result = useAtomValue(
17 serverEnvironment.databaseStorage({ environmentId: target.environmentId, input: {} }),
18 );
19 const preferences = useAtomValue(mobilePreferencesAtom);
20 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);
27 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 />
44 <SettingsActionRow
45 icon="xmark"
46 label="Dismiss"
47 onPress={() =>
48 updatePreferences({
49 transform: (current) => ({
50 dismissedDatabaseStorageNotices: [
51 ...new Set([
52 ...(current.dismissedDatabaseStorageNotices ?? []),
53 target.environmentId,
54 ]),
55 ],
56 }),
57 })
58 }
59 />
60 </SettingsSection>
61 );
62}