11import { useAtomSet , useAtomValue } from "@effect/atom-react" ;
2- import {
3- isAtomCommandInterrupted ,
4- squashAtomCommandFailure ,
5- } from "@t3tools/client-runtime/state/runtime" ;
6- import {
7- databaseReclaimMinutes ,
8- type DatabaseStorageStatus ,
9- type EnvironmentId ,
10- } from "@t3tools/contracts" ;
11- import { Atom , AsyncResult } from "effect/reactivity" ;
12- import { useState } from "react" ;
13- import { Alert } from "react-native" ;
2+ import { AsyncResult } from "effect/reactivity" ;
143
154import { copyTextWithHaptic } from "../../../lib/copyTextWithHaptic" ;
165import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../../state/preferences" ;
176import { serverEnvironment } from "../../../state/server" ;
18- import { useAtomCommand } from "../../../state/use-atom-command" ;
197import type { SettingsTarget } from "../settings-environment-filter" ;
208import { ListActionRow as SettingsActionRow } from "../../../components/ui/ListActionRow" ;
219import { SettingsSection } from "./SettingsSection" ;
2210
23- const formatGiB = ( bytes : number ) => `${ ( bytes / 1024 ** 3 ) . toFixed ( 1 ) } GiB` ;
24-
25- // Keyed by a JSON array of environment ids, like the recommendations it pairs with.
26- const reclaimPermissionsAtom = Atom . family ( ( environmentIdsKey : string ) =>
27- Atom . make ( ( get ) =>
28- ( JSON . parse ( environmentIdsKey ) as EnvironmentId [ ] ) . map ( ( environmentId ) =>
29- get ( serverEnvironment . reclaimDatabaseSpace . permissionAtom ( environmentId ) ) ,
30- ) ,
31- ) ,
32- ) ;
33-
34- /** Offers to reclaim the space on every machine whose database still holds at least 1 GiB of freed pages. */
11+ /** Suggests `t3 doctor` on every machine whose database still holds at least 1 GiB of freed pages. */
3512export function DatabaseStorageNotice ( {
3613 targets,
3714} : {
3815 readonly targets : readonly SettingsTarget [ ] ;
3916} ) {
40- const environmentIds = targets . map ( ( target ) => target . environmentId ) ;
41- const environmentIdsKey = JSON . stringify ( environmentIds ) ;
4217 const recommendations = useAtomValue (
43- serverEnvironment . databaseCompactionRecommendations ( environmentIdsKey ) ,
18+ serverEnvironment . databaseCompactionRecommendations (
19+ JSON . stringify ( targets . map ( ( target ) => target . environmentId ) ) ,
20+ ) ,
4421 ) ;
45- const permissions = useAtomValue ( reclaimPermissionsAtom ( environmentIdsKey ) ) ;
46- const reclaimable = ( environmentId : EnvironmentId , status : DatabaseStorageStatus ) =>
47- status . canReclaim === true && permissions [ environmentIds . indexOf ( environmentId ) ] === true ;
4822 const preferences = useAtomValue ( mobilePreferencesAtom ) ;
4923 const updatePreferences = useAtomSet ( updateMobilePreferencesAtom ) ;
5024 const dismissed = AsyncResult . isSuccess ( preferences )
@@ -57,25 +31,18 @@ export function DatabaseStorageNotice({
5731 return (
5832 < SettingsSection
5933 title = "Reclaim database space"
60- footer = { `These databases still hold space freed by history cleanup.${
61- visible . every ( ( recommendation ) =>
62- reclaimable ( recommendation . environmentId , recommendation . status ) ,
63- )
64- ? ""
65- : " Run the command on each machine to reclaim it. It restarts the background service. Quit the desktop app first if it hosts the environment."
66- } `}
34+ footer = "These databases still hold space freed by history cleanup. Run the command on each machine to reclaim it. It restarts the background service. Quit the desktop app first if it hosts the environment."
6735 >
68- { visible . map ( ( { environmentId, status } ) => {
69- const target = targets . find ( ( candidate ) => candidate . environmentId === environmentId ) ;
70- return target ? (
71- < DatabaseRow
72- key = { environmentId }
73- target = { target }
74- status = { status }
75- reclaimable = { reclaimable ( environmentId , status ) }
76- />
77- ) : null ;
78- } ) }
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+ ) ) }
7946 < SettingsActionRow
8047 icon = "xmark"
8148 label = "Dismiss"
@@ -95,69 +62,3 @@ export function DatabaseStorageNotice({
9562 </ SettingsSection >
9663 ) ;
9764}
98-
99- function DatabaseRow ( {
100- target,
101- status,
102- reclaimable,
103- } : {
104- readonly target : SettingsTarget ;
105- readonly status : DatabaseStorageStatus ;
106- readonly reclaimable : boolean ;
107- } ) {
108- const { environmentId, label } = target ;
109- const reclaim = useAtomCommand ( serverEnvironment . reclaimDatabaseSpace , { reportFailure : false } ) ;
110- const [ requested , setRequested ] = useState ( false ) ;
111- const amount = formatGiB ( status . reclaimableBytes ) ;
112- if ( ! reclaimable ) {
113- return (
114- < SettingsActionRow
115- icon = "doc.on.doc"
116- label = { label }
117- description = { status . compactCommand }
118- detail = { amount }
119- onPress = { ( ) => copyTextWithHaptic ( status . compactCommand , { target : "command" } ) }
120- />
121- ) ;
122- }
123- // A failed repair reports back after the restart, which ends the wait.
124- const restarting = requested && status . reclaimFailure === undefined ;
125- const minutes = databaseReclaimMinutes ( status ) ;
126- const start = async ( ) => {
127- setRequested ( true ) ;
128- const result = await reclaim ( { environmentId, input : { } } ) ;
129- if ( result . _tag === "Success" ) return ;
130- setRequested ( false ) ;
131- if ( isAtomCommandInterrupted ( result ) ) return ;
132- const error = squashAtomCommandFailure ( result ) ;
133- Alert . alert (
134- `Could not reclaim space on ${ label } ` ,
135- error instanceof Error ? error . message : "An error occurred." ,
136- ) ;
137- } ;
138- return (
139- < SettingsActionRow
140- icon = "arrow.clockwise"
141- label = { label }
142- description = {
143- restarting
144- ? `Restarting. Back in about ${ minutes } min.`
145- : status . reclaimFailure === undefined
146- ? `Restarts T3 Code there. It is offline for about ${ minutes } min.`
147- : `The last attempt failed: ${ status . reclaimFailure } `
148- }
149- detail = { amount }
150- loading = { restarting }
151- onPress = { ( ) =>
152- Alert . alert (
153- `Reclaim ${ amount } on ${ label } ?` ,
154- `T3 Code on ${ label } stops, rebuilds its database, and starts again. It is offline for about ${ minutes } min. Running agents stop when it restarts.` ,
155- [
156- { text : "Cancel" , style : "cancel" } ,
157- { text : "Restart and reclaim" , onPress : ( ) => void start ( ) } ,
158- ] ,
159- )
160- }
161- />
162- ) ;
163- }
0 commit comments