Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DatabaseStorageNotice.tsx

60 lines2.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import { shouldRecommendDatabaseCompaction, type EnvironmentId } from "@t3tools/contracts";
3import * as Schema from "effect/Schema";
4​
5import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
6import { useLocalStorage } from "../../hooks/useLocalStorage";
7import { serverEnvironment } from "../../state/server";
8import { Button } from "../ui/button";
9import { SettingsGroup } from "./SettingsGroup";
10import { SettingsRow, SettingsSection } from "./settingsLayout";
11​
12/** Suggests `t3 doctor` for a database that still holds at least 1 GiB of freed pages. */
13export function DatabaseStorageNotice({
14 environmentId,
15 label,
16}: {
17 environmentId: EnvironmentId;
18 label: string;
19}) {
20 const result = useAtomValue(serverEnvironment.databaseStorage({ environmentId, input: {} }));
21 const [dismissed, setDismissed] = useLocalStorage(
22 `database-storage-notice-dismissed:${environmentId}`,
23 false,
24 Schema.Boolean,
25 );
26 const { copyToClipboard } = useCopyToClipboard({ target: "command" });
27 const status = result._tag === "Success" ? result.value : null;
28 if (dismissed || status === null || !shouldRecommendDatabaseCompaction(status)) return null;
29 return (
30 <SettingsSection id={`storage-database-${environmentId}`} title="Database">
31 <SettingsGroup>
32 <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 control={
43 <div className="flex flex-wrap items-center gap-1.5">
44 <Button
45 variant="outline"
46 size="sm"
47 onClick={() => copyToClipboard(status.compactCommand, undefined)}
48 >
49 Copy command
50 </Button>
51 <Button variant="ghost" size="sm" onClick={() => setDismissed(true)}>
52 Dismiss
53 </Button>
54 </div>
55 }
56 />
57 </SettingsGroup>
58 </SettingsSection>
59 );