Skip to content

Commit ce18559

Browse files
committed
fix(mobile): finish a project removal only while that project is still selected
1 parent 82a96ac commit ce18559

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

‎apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-grouping";
1313
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
1414
import { useNavigation } from "@react-navigation/native";
15-
import { useRef, useState } from "react";
15+
import { useEffect, useRef, useState } from "react";
1616
import { Platform, Pressable, View } from "react-native";
1717
import { useSafeAreaInsets } from "react-native-safe-area-context";
1818

@@ -53,7 +53,20 @@ export function SettingsProjectOverviewRouteScreen() {
5353
removalPending.current = false;
5454
setIsRemoving(false);
5555
};
56-
const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
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+
};
5770
const group = projectGroups.find((entry) => entry.key === selectedProjectKey);
5871
const selectedEnvironmentIds = new Set(selectedTargets.map((entry) => entry.environmentId));
5972
const members =
@@ -87,6 +100,8 @@ export function SettingsProjectOverviewRouteScreen() {
87100
setIsRemoving={setIsRemoving}
88101
startRemoval={startRemoval}
89102
finishRemoval={finishRemoval}
103+
projectKey={selectedProjectKey}
104+
onProjectRemoved={finishProjectRemoval}
90105
/>
91106
)}
92107
</ScrollView>
@@ -103,14 +118,14 @@ function ProjectOverviewContent(props: {
103118
readonly setIsRemoving: (isRemoving: boolean) => void;
104119
readonly startRemoval: () => boolean;
105120
readonly finishRemoval: () => void;
121+
readonly projectKey: string | null;
122+
readonly onProjectRemoved: (projectKey: string | null) => void;
106123
}) {
107124
const { isRemoving, setIsRemoving, startRemoval, finishRemoval } = props;
108125
const representative = props.members[0]!;
109126
const displayName = deriveProjectGroupLabel({ representative, members: props.members });
110127
const [draftName, setDraftName] = useState<string | null>(null);
111128
const [isSaving, setIsSaving] = useState(false);
112-
const navigation = useNavigation();
113-
const { selectProject } = useSettingsEnvironmentFilter();
114129
const threads = useThreadShells();
115130
const removableEnvironments = useEnvironmentsWithScope(
116131
props.members,
@@ -144,6 +159,7 @@ function ProjectOverviewContent(props: {
144159

145160
const removeMembers = (members: readonly EnvironmentProject[]) => {
146161
if (!checkRemovalAccess(members) || !startRemoval()) return;
162+
const projectKey = props.projectKey;
147163
const plan = planProjectRemoval({
148164
members: members.map((member) => ({
149165
...member,
@@ -198,12 +214,8 @@ function ProjectOverviewContent(props: {
198214
console.warn("[project-removal] could not clear composer drafts", error);
199215
});
200216
}
201-
// Nothing is left to show once the whole project is gone, and its filter would
202-
// otherwise keep pointing at it as an unavailable project.
203-
if (plan.targetKind === "project") {
204-
if (navigation.isFocused()) navigation.goBack();
205-
selectProject(null);
206-
}
217+
// Nothing is left to show once the whole project is gone.
218+
if (plan.targetKind === "project") props.onProjectRemoved(projectKey);
207219
})().finally(finishRemoval);
208220
},
209221
});

0 commit comments

Comments
 (0)