@@ -2,16 +2,38 @@ import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScroll
22import { AppText as Text , AppTextInput } from "../../components/ui/AppText" ;
33import { Button } from "../../components/ui/Button" ;
44import { ProjectFavicon } from "../../components/ProjectFavicon" ;
5+ import * as Dialog from "../../components/ui/Dialog" ;
6+ import { AuthOrchestrationOperateScope } from "@t3tools/contracts" ;
7+ import { planProjectRemoval } from "@t3tools/client-runtime/state/projects" ;
8+ import {
9+ isAtomCommandInterrupted ,
10+ squashAtomCommandFailure ,
11+ } from "@t3tools/client-runtime/state/runtime" ;
512import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-grouping" ;
613import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell" ;
7- import { useState } from "react" ;
8- import { Platform , View } from "react-native" ;
14+ import { useNavigation } from "@react-navigation/native" ;
15+ import { useEffect , useRef , useState } from "react" ;
16+ import { Platform , Pressable , View } from "react-native" ;
917import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1018
1119import { projectEnvironment } from "../../state/projects" ;
20+ import { useThreadShells } from "../../state/entities" ;
21+ import { appAtomRegistry } from "../../state/atom-registry" ;
22+ import {
23+ clearComposerDraft ,
24+ composerDraftsAtom ,
25+ findNewTaskDraftKeys ,
26+ waitForComposerDraftsLoaded ,
27+ } from "../../state/use-composer-drafts" ;
28+ import {
29+ readEnvironmentScope ,
30+ useEnvironmentScope ,
31+ useEnvironmentsWithScope ,
32+ } from "../../state/session" ;
1233import { useAtomCommand } from "../../state/use-atom-command" ;
1334import { SettingsScreen } from "./components/SettingsScreen" ;
1435import { SettingsSection } from "./components/SettingsSection" ;
36+ import { SettingsActionRow } from "./components/SettingsActionRow" ;
1537import {
1638 AndroidSettingsEnvironmentFilter ,
1739 SettingsEnvironmentFilterHeader ,
@@ -20,7 +42,31 @@ import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-en
2042
2143export function SettingsProjectOverviewRouteScreen ( ) {
2244 const insets = useSafeAreaInsets ( ) ;
23- const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter ( ) ;
45+ const [ isRemoving , setIsRemoving ] = useState ( false ) ;
46+ const removalPending = useRef ( false ) ;
47+ const startRemoval = ( ) => {
48+ if ( removalPending . current ) return false ;
49+ removalPending . current = true ;
50+ return true ;
51+ } ;
52+ const finishRemoval = ( ) => {
53+ removalPending . current = false ;
54+ setIsRemoving ( false ) ;
55+ } ;
56+ const navigation = useNavigation ( ) ;
57+ const { selectedTargets, projectGroups, selectedProjectKey, selectProject } =
58+ useSettingsEnvironmentFilter ( ) ;
59+ const latestProjectKey = useRef ( selectedProjectKey ) ;
60+ useEffect ( ( ) => {
61+ latestProjectKey . current = selectedProjectKey ;
62+ } , [ selectedProjectKey ] ) ;
63+ // The filter can move to another project while a removal is still running.
64+ const finishProjectRemoval = ( projectKey : string | null ) => {
65+ if ( latestProjectKey . current !== projectKey ) return ;
66+ if ( navigation . isFocused ( ) ) navigation . goBack ( ) ;
67+ // Otherwise the filter keeps pointing at the removed project as an unavailable one.
68+ selectProject ( null ) ;
69+ } ;
2470 const group = projectGroups . find ( ( entry ) => entry . key === selectedProjectKey ) ;
2571 const selectedEnvironmentIds = new Set ( selectedTargets . map ( ( entry ) => entry . environmentId ) ) ;
2672 const members =
@@ -49,6 +95,13 @@ export function SettingsProjectOverviewRouteScreen() {
4995 key = { `${ selectedProjectKey } :${ members . map ( ( member ) => member . id ) . join ( "," ) } ` }
5096 members = { members }
5197 environments = { selectedTargets }
98+ hasOtherMembers = { members . length < ( group ?. members . length ?? 0 ) }
99+ isRemoving = { isRemoving }
100+ setIsRemoving = { setIsRemoving }
101+ startRemoval = { startRemoval }
102+ finishRemoval = { finishRemoval }
103+ projectKey = { selectedProjectKey }
104+ onProjectRemoved = { finishProjectRemoval }
52105 />
53106 ) }
54107 </ ScrollView >
@@ -60,18 +113,114 @@ export function SettingsProjectOverviewRouteScreen() {
60113function ProjectOverviewContent ( props : {
61114 readonly members : readonly EnvironmentProject [ ] ;
62115 readonly environments : readonly SettingsTarget [ ] ;
116+ readonly hasOtherMembers : boolean ;
117+ readonly isRemoving : boolean ;
118+ readonly setIsRemoving : ( isRemoving : boolean ) => void ;
119+ readonly startRemoval : ( ) => boolean ;
120+ readonly finishRemoval : ( ) => void ;
121+ readonly projectKey : string | null ;
122+ readonly onProjectRemoved : ( projectKey : string | null ) => void ;
63123} ) {
124+ const { isRemoving, setIsRemoving, startRemoval, finishRemoval } = props ;
64125 const representative = props . members [ 0 ] ! ;
65126 const displayName = deriveProjectGroupLabel ( { representative, members : props . members } ) ;
66127 const [ draftName , setDraftName ] = useState < string | null > ( null ) ;
67128 const [ isSaving , setIsSaving ] = useState ( false ) ;
129+ const threads = useThreadShells ( ) ;
130+ const removableEnvironments = useEnvironmentsWithScope (
131+ props . members ,
132+ AuthOrchestrationOperateScope ,
133+ ) ;
134+ const canRemoveGroup = props . members . every ( ( member ) =>
135+ removableEnvironments . has ( member . environmentId ) ,
136+ ) ;
137+ const deleteProject = useAtomCommand ( projectEnvironment . delete , { reportFailure : false } ) ;
68138 const updateProject = useAtomCommand ( projectEnvironment . update , {
69139 label : "project name update" ,
70140 reportFailure : true ,
71141 } ) ;
72142 const nextName = ( draftName ?? displayName ) . trim ( ) ;
73143 const canSave = ! isSaving && nextName . length > 0 && nextName !== displayName ;
74144
145+ const checkRemovalAccess = ( members : readonly EnvironmentProject [ ] ) => {
146+ const denied = members . find (
147+ ( member ) => ! readEnvironmentScope ( member . environmentId , AuthOrchestrationOperateScope ) ,
148+ ) ;
149+ if ( ! denied ) return true ;
150+ const environment = props . environments . find (
151+ ( entry ) => entry . environmentId === denied . environmentId ,
152+ ) ;
153+ Dialog . alert (
154+ "Failed to remove project" ,
155+ `This connection cannot change projects in ${ environment ?. label ?? "this environment" } .` ,
156+ ) ;
157+ return false ;
158+ } ;
159+
160+ const removeMembers = ( members : readonly EnvironmentProject [ ] ) => {
161+ if ( ! checkRemovalAccess ( members ) || ! startRemoval ( ) ) return ;
162+ const projectKey = props . projectKey ;
163+ const plan = planProjectRemoval ( {
164+ members : members . map ( ( member ) => ( {
165+ ...member ,
166+ environmentLabel : props . environments . find (
167+ ( entry ) => entry . environmentId === member . environmentId ,
168+ ) ?. label ,
169+ } ) ) ,
170+ groupMemberCount : props . members . length ,
171+ hasOtherMembers : props . hasOtherMembers ,
172+ displayName,
173+ threads,
174+ } ) ;
175+ Dialog . confirm ( {
176+ title : plan . targetKind === "checkout" ? "Remove checkout" : "Remove project" ,
177+ message : plan . confirmationMessage ,
178+ confirmText : plan . targetKind === "checkout" ? "Remove checkout" : "Remove project" ,
179+ destructive : true ,
180+ onCancel : finishRemoval ,
181+ onConfirm : ( ) => {
182+ void ( async ( ) => {
183+ setIsRemoving ( true ) ;
184+ if ( ! checkRemovalAccess ( members ) ) return ;
185+ for ( const member of members ) {
186+ if ( ! checkRemovalAccess ( [ member ] ) ) return ;
187+ const result = await deleteProject ( {
188+ environmentId : member . environmentId ,
189+ input : { projectId : member . id , force : true } ,
190+ } ) ;
191+ if ( result . _tag === "Failure" ) {
192+ if ( ! isAtomCommandInterrupted ( result ) ) {
193+ const error = squashAtomCommandFailure ( result ) ;
194+ Dialog . alert (
195+ `Failed to remove "${ member . title } "` ,
196+ error instanceof Error ? error . message : "An error occurred." ,
197+ ) ;
198+ }
199+ return ;
200+ }
201+ await waitForComposerDraftsLoaded ( )
202+ . then ( ( ) => {
203+ for ( const draftKey of findNewTaskDraftKeys (
204+ appAtomRegistry . get ( composerDraftsAtom ) ,
205+ {
206+ environmentId : member . environmentId ,
207+ projectId : member . id ,
208+ } ,
209+ ) ) {
210+ clearComposerDraft ( draftKey ) ;
211+ }
212+ } )
213+ . catch ( ( error ) => {
214+ console . warn ( "[project-removal] could not clear composer drafts" , error ) ;
215+ } ) ;
216+ }
217+ // Nothing is left to show once the whole project is gone.
218+ if ( plan . targetKind === "project" ) props . onProjectRemoved ( projectKey ) ;
219+ } ) ( ) . finally ( finishRemoval ) ;
220+ } ,
221+ } ) ;
222+ } ;
223+
75224 const saveName = ( ) => {
76225 if ( ! canSave ) return ;
77226 setIsSaving ( true ) ;
@@ -162,10 +311,59 @@ function ProjectOverviewContent(props: {
162311 < Text className = "text-sm leading-normal text-foreground-muted" selectable >
163312 { member . workspaceRoot }
164313 </ Text >
314+ { props . members . length > 1 || props . hasOtherMembers ? (
315+ < RemoveCheckoutAction
316+ member = { member }
317+ disabled = { isRemoving }
318+ onPress = { ( ) => removeMembers ( [ member ] ) }
319+ />
320+ ) : null }
165321 </ View >
166322 ) ;
167323 } ) }
168324 </ SettingsSection >
325+ < SettingsSection title = "Danger" >
326+ < SettingsActionRow
327+ icon = "trash"
328+ label = {
329+ props . hasOtherMembers
330+ ? "Remove checkout"
331+ : props . members . length > 1
332+ ? "Remove this project everywhere"
333+ : "Remove project"
334+ }
335+ description = {
336+ props . hasOtherMembers
337+ ? "Deletes the selected machine's checkout entries and their threads. Other machines and files on disk are not touched."
338+ : props . members . length > 1
339+ ? `Deletes all ${ props . members . length } checkout entries and their threads on every machine. Files on disk are not touched.`
340+ : "Deletes the project entry and its threads. Files on disk are not touched."
341+ }
342+ tone = "danger"
343+ disabled = { ! canRemoveGroup || isRemoving }
344+ loading = { isRemoving }
345+ onPress = { ( ) => removeMembers ( props . members ) }
346+ />
347+ </ SettingsSection >
169348 </ >
170349 ) ;
171350}
351+
352+ function RemoveCheckoutAction ( props : {
353+ readonly member : EnvironmentProject ;
354+ readonly disabled : boolean ;
355+ readonly onPress : ( ) => void ;
356+ } ) {
357+ const canRemove = useEnvironmentScope ( props . member . environmentId , AuthOrchestrationOperateScope ) ;
358+ return (
359+ < Pressable
360+ accessibilityRole = "button"
361+ accessibilityLabel = { `Remove checkout ${ props . member . workspaceRoot } ` }
362+ disabled = { props . disabled || ! canRemove }
363+ onPress = { props . onPress }
364+ className = "self-start rounded-full border border-border bg-card px-3 py-1.5 disabled:opacity-40"
365+ >
366+ < Text className = "font-t3-medium text-xs text-danger-foreground" > Remove checkout</ Text >
367+ </ Pressable >
368+ ) ;
369+ }
0 commit comments