The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { T, useGT } from "gt-react-native"; |
| 3 | import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; |
| 4 | import { AppText as Text, AppTextInput } from "../../components/ui/AppText"; |
| 5 | import { Button } from "../../components/ui/Button"; |
| 6 | import { ProjectFavicon } from "../../components/ProjectFavicon"; |
| 7 | import * as Dialog from "../../components/ui/Dialog"; |
| 8 | import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; |
| 9 | import { planProjectRemoval } from "@t3tools/client-runtime/state/projects"; |
| 10 | import { |
| 11 | isAtomCommandInterrupted, |
| 12 | squashAtomCommandFailure, |
| 13 | } from "@t3tools/client-runtime/state/runtime"; |
| 14 | import { |
| 15 | deriveProjectGroupLabel, |
| 16 | type ProjectGroup, |
| 17 | } from "@t3tools/client-runtime/state/project-grouping"; |
| 18 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 19 | import { useNavigation } from "@react-navigation/native"; |
| 20 | import { useRef, useState } from "react"; |
| 21 | import { Platform, Pressable, View } from "react-native"; |
| 22 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 23 | |
| 24 | import { projectEnvironment } from "../../state/projects"; |
| 25 | import { useThreadShells } from "../../state/entities"; |
| 26 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 27 | import { |
| 28 | clearComposerDraft, |
| 29 | composerDraftsAtom, |
| 30 | findNewTaskDraftKeys, |
| 31 | waitForComposerDraftsLoaded, |
| 32 | } from "../../state/use-composer-drafts"; |
| 33 | import { |
| 34 | readEnvironmentScope, |
| 35 | useEnvironmentScope, |
| 36 | useEnvironmentsWithScope, |
| 37 | } from "../../state/session"; |
| 38 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 39 | import { SettingsScreen } from "./components/SettingsScreen"; |
| 40 | import { SettingsSection } from "./components/SettingsSection"; |
| 41 | import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow"; |
| 42 | import { SettingsSwitchRow } from "./components/SettingsSwitchRow"; |
| 43 | import { useSettingsEnvironmentMenu } from "./components/SettingsEnvironmentFilterHeader"; |
| 44 | import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-environment-filter"; |
| 45 | import { leaveSettingsPage } from "./settings-navigation"; |
| 46 | import { workspaceHasColumns } from "../layout/workspace/workspace-selection-store"; |
| 47 | import { DEFAULT_SETTINGS_PAGE } from "../layout/workspace/workspace-tab-navigation"; |
| 48 | import { readUnloadedThreadCount } from "../../state/olderSettledShelf"; |
| 49 | import { m } from "../../i18n/messages"; |
| 50 | |
| 51 | export function SettingsProjectOverviewRouteScreen() { |
| 52 | const gt = useGT(); |
| 53 | const environmentMenu = useSettingsEnvironmentMenu(); |
| 54 | const insets = useSafeAreaInsets(); |
| 55 | const [isRemoving, setIsRemoving] = useState(false); |
| 56 | const removalPending = useRef(false); |
| 57 | const startRemoval = () => { |
| 58 | if (removalPending.current) return false; |
| 59 | removalPending.current = true; |
| 60 | return true; |
| 61 | }; |
| 62 | const finishRemoval = () => { |
| 63 | removalPending.current = false; |
| 64 | setIsRemoving(false); |
| 65 | }; |
| 66 | const navigation = useNavigation(); |
| 67 | const { |
| 68 | availableTargets, |
| 69 | selectedTargets, |
| 70 | projectGroups, |
| 71 | selectedProjectKey, |
| 72 | selectProject, |
| 73 | getSelectedProjectKey, |
| 74 | } = useSettingsEnvironmentFilter(); |
| 75 | // The filter can move to another project while a removal is still running. |
| 76 | const finishProjectRemoval = (projectKey: string | null) => { |
| 77 | if (getSelectedProjectKey() !== projectKey) return; |
| 78 | if (navigation.isFocused()) |
| 79 | leaveSettingsPage(navigation, workspaceHasColumns(), DEFAULT_SETTINGS_PAGE); |
| 80 | // Otherwise the filter keeps pointing at the removed project as an unavailable one. |
| 81 | selectProject(null); |
| 82 | }; |
| 83 | const group = projectGroups.find((entry) => entry.key === selectedProjectKey); |
| 84 | const selectedEnvironmentIds = new Set(selectedTargets.map((entry) => entry.environmentId)); |
| 85 | const members = |
| 86 | group?.members |
| 87 | .map((entry) => entry.project) |
| 88 | .filter((project) => selectedEnvironmentIds.has(project.environmentId)) ?? []; |
| 89 | |
| 90 | return ( |
| 91 | <SettingsScreen title={gt("Projects")} menus={[environmentMenu]}> |
| 92 | <ScrollView |
| 93 | contentInsetAdjustmentBehavior="automatic" |
| 94 | showsVerticalScrollIndicator={false} |
| 95 | className="flex-1" |
| 96 | contentContainerClassName="gap-6 px-5 pt-4" |
| 97 | contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 98 | > |
| 99 | {selectedProjectKey === null ? ( |
| 100 | <Text className="px-2 text-base text-foreground-muted"> |
| 101 | <T>Choose a project with the filter above to manage its name, checkouts and removal.</T> |
| 102 | </Text> |
| 103 | ) : !group || members.length === 0 ? ( |
| 104 | <Text className="px-2 text-base text-foreground-muted"> |
| 105 | <T> |
| 106 | This project has no checkout on the selected connected environments. Change the filter |
| 107 | above. |
| 108 | </T> |
| 109 | </Text> |
| 110 | ) : ( |
| 111 | <ProjectOverviewContent |
| 112 | key={`${selectedProjectKey}:${members.map((member) => member.id).join(",")}`} |
| 113 | members={members} |
| 114 | group={group} |
| 115 | connectedTargets={availableTargets} |
| 116 | environments={selectedTargets} |
| 117 | hasOtherMembers={members.length < (group?.members.length ?? 0)} |
| 118 | isRemoving={isRemoving} |
| 119 | setIsRemoving={setIsRemoving} |
| 120 | startRemoval={startRemoval} |
| 121 | finishRemoval={finishRemoval} |
| 122 | projectKey={selectedProjectKey} |
| 123 | onProjectRemoved={finishProjectRemoval} |
| 124 | /> |
| 125 | )} |
| 126 | </ScrollView> |
| 127 | </SettingsScreen> |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | function ProjectOverviewContent(props: { |
| 132 | readonly members: readonly EnvironmentProject[]; |
| 133 | /** Pin and archive belong to the whole project, even when the filter picks one machine. */ |
| 134 | readonly group: ProjectGroup; |
| 135 | readonly connectedTargets: readonly SettingsTarget[]; |
| 136 | readonly environments: readonly SettingsTarget[]; |
| 137 | readonly hasOtherMembers: boolean; |
| 138 | readonly isRemoving: boolean; |
| 139 | readonly setIsRemoving: (isRemoving: boolean) => void; |
| 140 | readonly startRemoval: () => boolean; |
| 141 | readonly finishRemoval: () => void; |
| 142 | readonly projectKey: string | null; |
| 143 | readonly onProjectRemoved: (projectKey: string | null) => void; |
| 144 | }) { |
| 145 | const gt = useGT(); |
| 146 | const { isRemoving, setIsRemoving, startRemoval, finishRemoval } = props; |
| 147 | const representative = props.members[0]!; |
| 148 | const displayName = deriveProjectGroupLabel({ |
| 149 | groupKey: props.group.key, |
| 150 | representative, |
| 151 | members: props.members, |
| 152 | }); |
| 153 | const [draftName, setDraftName] = useState<string | null>(null); |
| 154 | const [isSaving, setIsSaving] = useState(false); |
| 155 | const threads = useThreadShells(); |
| 156 | const allMembers = props.group.members.map((member) => member.project); |
| 157 | const removableEnvironments = useEnvironmentsWithScope(allMembers, AuthOrchestrationOperateScope); |
| 158 | const canRemoveGroup = props.members.every((member) => |
| 159 | removableEnvironments.has(member.environmentId), |
| 160 | ); |
| 161 | const deleteProject = useAtomCommand(projectEnvironment.delete, { reportFailure: false }); |
| 162 | const updateProject = useAtomCommand(projectEnvironment.update, { |
| 163 | label: "project name update", |
| 164 | reportFailure: true, |
| 165 | }); |
| 166 | const organizeProject = useAtomCommand(projectEnvironment.update, { |
| 167 | label: "project update", |
| 168 | reportFailure: true, |
| 169 | }); |
| 170 | const [isOrganizing, setIsOrganizing] = useState(false); |
| 171 | const memberTargets = allMembers.map((member) => |
| 172 | props.connectedTargets.find((entry) => entry.environmentId === member.environmentId), |
| 173 | ); |
| 174 | const offline = memberTargets.some((target) => target === undefined); |
| 175 | // Older servers drop pin and archive, so their projects do not offer them. |
| 176 | // Offline members are checked when the write runs, so their reason can show. |
| 177 | const canOrganize = |
| 178 | allMembers.every( |
| 179 | (member, index) => |
| 180 | memberTargets[index] === undefined || removableEnvironments.has(member.environmentId), |
| 181 | ) && |
| 182 | memberTargets.every( |
| 183 | (target) => |
| 184 | target === undefined || |
| 185 | target.serverConfig.environment.capabilities.projectOrganization === true, |
| 186 | ); |
| 187 | // One machine at a time, stopping at the first failure, as web does. |
| 188 | const organize = async (input: { readonly pinned?: boolean; readonly archived?: boolean }) => { |
| 189 | setIsOrganizing(true); |
| 190 | for (const member of allMembers) { |
| 191 | if (!readEnvironmentScope(member.environmentId, AuthOrchestrationOperateScope)) { |
| 192 | Dialog.alert( |
| 193 | gt("Failed to update project"), |
| 194 | gt("This connection can no longer change it."), |
| 195 | ); |
| 196 | break; |
| 197 | } |
| 198 | const result = await organizeProject({ |
| 199 | environmentId: member.environmentId, |
| 200 | input: { projectId: member.id, ...input }, |
| 201 | }); |
| 202 | if (result._tag === "Failure") { |
| 203 | if (!isAtomCommandInterrupted(result)) { |
| 204 | const error = squashAtomCommandFailure(result); |
| 205 | Dialog.alert( |
| 206 | gt('Failed to update "{project}"', { project: member.title }), |
| 207 | error instanceof Error |
| 208 | ? (errorDisplayMessage(error, m) ?? error.message) |
| 209 | : gt("An error occurred."), |
| 210 | ); |
| 211 | } |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | setIsOrganizing(false); |
| 216 | }; |
| 217 | const nextName = (draftName ?? displayName).trim(); |
| 218 | const canSave = !isSaving && nextName.length > 0 && nextName !== displayName; |
| 219 | |
| 220 | const checkRemovalAccess = (members: readonly EnvironmentProject[]) => { |
| 221 | const denied = members.find( |
| 222 | (member) => !readEnvironmentScope(member.environmentId, AuthOrchestrationOperateScope), |
| 223 | ); |
| 224 | if (!denied) return true; |
| 225 | const environment = props.environments.find( |
| 226 | (entry) => entry.environmentId === denied.environmentId, |
| 227 | ); |
| 228 | Dialog.alert( |
| 229 | gt("Failed to remove project"), |
| 230 | environment |
| 231 | ? gt("This connection cannot change projects in {environment}.", { |
| 232 | environment: environment.label, |
| 233 | }) |
| 234 | : gt("This connection cannot change projects in this environment."), |
| 235 | ); |
| 236 | return false; |
| 237 | }; |
| 238 | |
| 239 | const removeMembers = (members: readonly EnvironmentProject[]) => { |
| 240 | if (!checkRemovalAccess(members) || !startRemoval()) return; |
| 241 | const projectKey = props.projectKey; |
| 242 | const plan = planProjectRemoval( |
| 243 | { |
| 244 | members: members.map((member) => ({ |
| 245 | ...member, |
| 246 | environmentLabel: props.environments.find( |
| 247 | (entry) => entry.environmentId === member.environmentId, |
| 248 | )?.label, |
| 249 | })), |
| 250 | groupMemberCount: props.members.length, |
| 251 | hasOtherMembers: props.hasOtherMembers, |
| 252 | displayName, |
| 253 | threads, |
| 254 | unloadedThreadCount: readUnloadedThreadCount(members, threads), |
| 255 | }, |
| 256 | m, |
| 257 | ); |
| 258 | Dialog.confirm({ |
| 259 | title: plan.targetKind === "checkout" ? gt("Remove checkout") : gt("Remove project"), |
| 260 | message: plan.confirmationMessage, |
| 261 | confirmText: plan.targetKind === "checkout" ? gt("Remove checkout") : gt("Remove project"), |
| 262 | destructive: true, |
| 263 | onCancel: finishRemoval, |
| 264 | onConfirm: () => { |
| 265 | void (async () => { |
| 266 | setIsRemoving(true); |
| 267 | if (!checkRemovalAccess(members)) return; |
| 268 | for (const member of members) { |
| 269 | if (!checkRemovalAccess([member])) return; |
| 270 | const result = await deleteProject({ |
| 271 | environmentId: member.environmentId, |
| 272 | input: { projectId: member.id, force: true }, |
| 273 | }); |
| 274 | if (result._tag === "Failure") { |
| 275 | if (!isAtomCommandInterrupted(result)) { |
| 276 | const error = squashAtomCommandFailure(result); |
| 277 | Dialog.alert( |
| 278 | gt('Failed to remove "{project}"', { project: member.title }), |
| 279 | error instanceof Error |
| 280 | ? (errorDisplayMessage(error, m) ?? error.message) |
| 281 | : gt("An error occurred."), |
| 282 | ); |
| 283 | } |
| 284 | return; |
| 285 | } |
| 286 | await waitForComposerDraftsLoaded() |
| 287 | .then(() => { |
| 288 | for (const draftKey of findNewTaskDraftKeys( |
| 289 | appAtomRegistry.get(composerDraftsAtom), |
| 290 | { |
| 291 | environmentId: member.environmentId, |
| 292 | projectId: member.id, |
| 293 | }, |
| 294 | )) { |
| 295 | clearComposerDraft(draftKey); |
| 296 | } |
| 297 | }) |
| 298 | .catch((error) => { |
| 299 | console.warn("[project-removal] could not clear composer drafts", error); |
| 300 | }); |
| 301 | } |
| 302 | // Nothing is left to show once the whole project is gone. |
| 303 | if (plan.targetKind === "project") props.onProjectRemoved(projectKey); |
| 304 | })().finally(finishRemoval); |
| 305 | }, |
| 306 | }); |
| 307 | }; |
| 308 | |
| 309 | const saveName = () => { |
| 310 | if (!canSave) return; |
| 311 | setIsSaving(true); |
| 312 | void (async () => { |
| 313 | const results = await Promise.all( |
| 314 | props.members.map((member) => |
| 315 | updateProject({ |
| 316 | environmentId: member.environmentId, |
| 317 | input: { projectId: member.id, title: nextName }, |
| 318 | }), |
| 319 | ), |
| 320 | ); |
| 321 | setIsSaving(false); |
| 322 | if (results.every((result) => result._tag !== "Failure")) setDraftName(null); |
| 323 | })(); |
| 324 | }; |
| 325 | |
| 326 | return ( |
| 327 | <> |
| 328 | <View className="flex-row items-center gap-4 px-2"> |
| 329 | <ProjectFavicon |
| 330 | environmentId={representative.environmentId} |
| 331 | projectTitle={representative.title} |
| 332 | workspaceRoot={representative.workspaceRoot} |
| 333 | faviconPath={representative.faviconPath} |
| 334 | projectIcon={representative.projectIcon} |
| 335 | size={48} |
| 336 | /> |
| 337 | <View className="min-w-0 flex-1"> |
| 338 | <Text className="text-xl font-t3-semibold text-foreground" numberOfLines={2}> |
| 339 | {displayName} |
| 340 | </Text> |
| 341 | <Text className="text-sm text-foreground-muted"> |
| 342 | {gt("{count, plural, one {{count} checkout} other {{count} checkouts}}", { |
| 343 | count: props.members.length, |
| 344 | })} |
| 345 | </Text> |
| 346 | </View> |
| 347 | </View> |
| 348 | |
| 349 | <SettingsSection title={gt("Project")}> |
| 350 | <View className="gap-3 p-4"> |
| 351 | <Text className="text-sm font-t3-medium text-foreground-muted"> |
| 352 | <T>Name</T> |
| 353 | </Text> |
| 354 | <View className="flex-row items-center gap-3"> |
| 355 | <AppTextInput |
| 356 | accessibilityLabel={gt("Project name")} |
| 357 | className="min-h-11 min-w-0 flex-1 rounded-xl border-continuous bg-card px-3 text-base text-foreground" |
| 358 | value={draftName ?? displayName} |
| 359 | onChangeText={setDraftName} |
| 360 | onSubmitEditing={saveName} |
| 361 | returnKeyType="done" |
| 362 | editable={!isSaving} |
| 363 | /> |
| 364 | {canSave ? ( |
| 365 | <Button |
| 366 | label={gt("Save")} |
| 367 | accessibilityHint={gt("Save project name")} |
| 368 | tone="secondary" |
| 369 | onPress={saveName} |
| 370 | /> |
| 371 | ) : null} |
| 372 | </View> |
| 373 | </View> |
| 374 | </SettingsSection> |
| 375 | |
| 376 | {canOrganize ? ( |
| 377 | <SettingsSection title={gt("Organize")}> |
| 378 | <SettingsSwitchRow |
| 379 | icon="pin" |
| 380 | label={gt("Pin", { $context: "verb, keep the project at the top" })} |
| 381 | subtitle={ |
| 382 | offline |
| 383 | ? gt("Connect every machine with this project to change this.") |
| 384 | : gt("Pinned projects come first in project pickers.") |
| 385 | } |
| 386 | value={props.group.pinned} |
| 387 | disabled={isOrganizing || offline} |
| 388 | onValueChange={(pinned) => void organize({ pinned })} |
| 389 | /> |
| 390 | <SettingsActionRow |
| 391 | icon="archivebox" |
| 392 | label={props.group.archived ? gt("Unarchive project") : gt("Archive project")} |
| 393 | description={ |
| 394 | props.group.archived |
| 395 | ? gt("Hidden from project pickers. Threads and files are kept.") |
| 396 | : gt("Hides the project from project pickers. Threads and files are kept.") |
| 397 | } |
| 398 | disabled={isOrganizing || offline} |
| 399 | onPress={() => void organize({ archived: !props.group.archived })} |
| 400 | /> |
| 401 | </SettingsSection> |
| 402 | ) : null} |
| 403 | |
| 404 | <SettingsSection title={gt("Checkouts")}> |
| 405 | {props.members.map((member, index) => { |
| 406 | const environment = props.environments.find( |
| 407 | (entry) => entry.environmentId === member.environmentId, |
| 408 | ); |
| 409 | return ( |
| 410 | <View |
| 411 | key={`${member.environmentId}:${member.id}`} |
| 412 | className={index === 0 ? "gap-1 p-4" : "gap-1 border-t border-border-subtle p-4"} |
| 413 | > |
| 414 | <Text |
| 415 | className={ |
| 416 | Platform.OS === "android" |
| 417 | ? "text-base text-foreground" |
| 418 | : "text-lg text-foreground" |
| 419 | } |
| 420 | > |
| 421 | {environment?.label ?? gt("Environment")} |
| 422 | </Text> |
| 423 | {environment?.displayUrl ? ( |
| 424 | <Text className="text-sm leading-normal text-foreground-muted"> |
| 425 | {environment.displayUrl} |
| 426 | </Text> |
| 427 | ) : null} |
| 428 | <Text className="text-sm leading-normal text-foreground-muted" selectable> |
| 429 | {member.workspaceRoot} |
| 430 | </Text> |
| 431 | {props.members.length > 1 || props.hasOtherMembers ? ( |
| 432 | <RemoveCheckoutAction |
| 433 | member={member} |
| 434 | disabled={isRemoving} |
| 435 | onPress={() => removeMembers([member])} |
| 436 | /> |
| 437 | ) : null} |
| 438 | </View> |
| 439 | ); |
| 440 | })} |
| 441 | </SettingsSection> |
| 442 | <SettingsSection |
| 443 | title={gt("Danger", { $context: "section heading for destructive actions" })} |
| 444 | > |
| 445 | <SettingsActionRow |
| 446 | icon="trash" |
| 447 | label={ |
| 448 | props.hasOtherMembers |
| 449 | ? gt("Remove checkout") |
| 450 | : props.members.length > 1 |
| 451 | ? gt("Remove this project everywhere") |
| 452 | : gt("Remove project") |
| 453 | } |
| 454 | description={ |
| 455 | props.hasOtherMembers |
| 456 | ? gt( |
| 457 | "Deletes the selected machine's checkout entries and their threads. Other machines and files on disk are not touched.", |
| 458 | ) |
| 459 | : props.members.length > 1 |
| 460 | ? gt( |
| 461 | "{count, plural, other {Deletes all {count} checkout entries and their threads on every machine. Files on disk are not touched.}}", |
| 462 | { count: props.members.length }, |
| 463 | ) |
| 464 | : gt("Deletes the project entry and its threads. Files on disk are not touched.") |
| 465 | } |
| 466 | tone="danger" |
| 467 | disabled={!canRemoveGroup || isRemoving} |
| 468 | loading={isRemoving} |
| 469 | onPress={() => removeMembers(props.members)} |
| 470 | /> |
| 471 | </SettingsSection> |
| 472 | </> |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | function RemoveCheckoutAction(props: { |
| 477 | readonly member: EnvironmentProject; |
| 478 | readonly disabled: boolean; |
| 479 | readonly onPress: () => void; |
| 480 | }) { |
| 481 | const gt = useGT(); |
| 482 | const canRemove = useEnvironmentScope(props.member.environmentId, AuthOrchestrationOperateScope); |
| 483 | return ( |
| 484 | <Pressable |
| 485 | accessibilityRole="button" |
| 486 | accessibilityLabel={gt("Remove checkout {path}", { path: props.member.workspaceRoot })} |
| 487 | disabled={props.disabled || !canRemove} |
| 488 | onPress={props.onPress} |
| 489 | className="self-start rounded-full border border-border bg-card px-3 py-1.5 disabled:opacity-40" |
| 490 | > |
| 491 | <Text className="font-t3-medium text-xs text-danger-foreground"> |
| 492 | <T>Remove checkout</T> |
| 493 | </Text> |
| 494 | </Pressable> |
| 495 | ); |
| 496 | } |