diff --git a/apps/mobile/src/features/settings/ScheduledTaskPickerScreens.tsx b/apps/mobile/src/features/settings/ScheduledTaskPickerScreens.tsx index f07ffea096ec8..5c9faf5b466f8 100644 --- a/apps/mobile/src/features/settings/ScheduledTaskPickerScreens.tsx +++ b/apps/mobile/src/features/settings/ScheduledTaskPickerScreens.tsx @@ -2,8 +2,8 @@ import { msg, T, useGT, useMessages } from "gt-react-native"; import { NativeScrollView } from "../../native/NativeScrollViewMarker"; import { LegendList } from "@legendapp/list/react-native"; import { useNavigation } from "@react-navigation/native"; +import { scheduledTaskDestinationRows } from "@t3tools/client-runtime/scheduled-task-destination"; import { scheduledTaskWorktreeBaseRef } from "@t3tools/client-runtime/scheduled-task-worktree"; -import { isListedThreadLineage } from "@t3tools/contracts"; import * as Haptics from "expo-haptics"; import { useMemo, useState } from "react"; import { Platform } from "react-native"; @@ -23,12 +23,7 @@ import { useDebouncedValue, usePaginatedBranches, useThreadSearch } from "../../ import { BranchPickerScreen } from "../threads/NewTaskContextPickerScreens"; import { ThreadSettingsPickerScreen } from "../threads/ThreadSettingsSheet"; import { SettingsScreen } from "./components/SettingsScreen"; -import { - NEW_THREAD_DESTINATION_LABEL, - useScheduledTaskDestinationLabel, - useScheduledTaskEditor, -} from "./scheduled-task-editor"; -import { moveDraftToThread, unbindDraftFromThread } from "./scheduledTaskDraft"; +import { useScheduledTaskDestinationLabel, useScheduledTaskEditor } from "./scheduled-task-editor"; function MissingTaskDraft() { const gt = useGT(); @@ -225,40 +220,21 @@ export function ScheduledTaskThreadPickerRouteScreen() { if (!editor) return ; - const needle = query.trim().toLocaleLowerCase(); - const rows = [ - ...(needle - ? [] - : [{ key: "new-thread", title: m(NEW_THREAD_DESTINATION_LABEL), thread: null }]), - ...(threadId !== null && currentLabel.toLocaleLowerCase().includes(needle) - ? [{ key: threadId, title: currentLabel, thread: null }] - : []), - ...shells - .filter( - (thread) => - thread.environmentId === environmentId && - thread.projectId === projectId && - thread.id !== threadId && - thread.archivedAt === null && - isListedThreadLineage(thread.lineage) && - thread.title.toLocaleLowerCase().includes(needle), - ) - .sort((left, right) => right.updatedAt.localeCompare(left.updatedAt)) - .map((thread) => ({ - key: thread.id, - title: thread.title.trim() || gt("Untitled thread"), - thread, - })), - ]; - const selectedKey = threadId ?? (editor.draft.threadMode === "new_thread" ? "new-thread" : null); + const rows = scheduledTaskDestinationRows(shells, { + environmentId, + projectId, + current: threadId === null ? null : { id: threadId, title: currentLabel }, + query, + untitledTitle: gt("Untitled thread"), + }); return ( row.key} - extraData={selectedKey} + keyExtractor={(row) => row.id} + extraData={threadId} estimatedItemSize={56} recycleItems className="flex-1" @@ -282,20 +258,15 @@ export function ScheduledTaskThreadPickerRouteScreen() { renderItem={({ item, index }) => ( { void Haptics.selectionAsync(); - if (item.key !== selectedKey) { + if (item.id !== threadId) { setEditor((current) => current - ? { - ...current, - draft: item.thread - ? moveDraftToThread(current.draft, item.thread) - : { ...unbindDraftFromThread(current.draft), threadMode: "new_thread" }, - } + ? { ...current, draft: { ...current.draft, threadId: item.id } } : current, ); } diff --git a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx index 8e15dad08ac23..3a9d96623fbcc 100644 --- a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx @@ -1029,59 +1029,64 @@ function TaskForm({ /> - - {offersThreadModes ? ( - ({ - id: mode, - title: m(THREAD_MODE_LABELS[mode]), - state: draft.threadMode === mode ? "on" : undefined, - }), - )} - onSelect={(id) => { - if (id === "new_thread") - setDraft((current) => ({ ...unbindDraftFromThread(current), threadMode: id })); - else if (id === "continue" || id === "fresh_session") - setDraft((current) => ({ ...current, threadMode: id })); - }} - /> - ) : null} - {!offersThreadModes || draft.threadMode !== "new_thread" ? ( - navigation.navigate("SettingsScheduledTaskThread")} - disabled={!draft.projectId || saving || dictationPending || environmentUnavailable} - /> - ) : null} - + {offersThreadModes || draft.threadMode !== "new_thread" ? ( + + {offersThreadModes ? ( + ({ + id: mode, + title: m(THREAD_MODE_LABELS[mode]), + state: draft.threadMode === mode ? "on" : undefined, + }), + )} + onSelect={(id) => { + if (id === "new_thread") + setDraft((current) => ({ ...unbindDraftFromThread(current), threadMode: id })); + else if (id === "continue" || id === "fresh_session") + setDraft((current) => ({ ...current, threadMode: id })); + }} + /> + ) : null} + {draft.threadMode !== "new_thread" ? ( + navigation.navigate("SettingsScheduledTaskThread")} + // Without thread modes there is no way back to a new thread each run, so stay read-only. + disabled={ + !offersThreadModes || + !draft.projectId || + saving || + dictationPending || + environmentUnavailable + } + /> + ) : null} + + ) : null} = { worktree: msg("Create a new worktree"), root: msg("Use the project checkout"), @@ -1309,7 +1305,7 @@ function ScheduledTaskDestination({ readonly threadId: ThreadId | null; readonly threadMode: ScheduledTaskThreadMode; readonly disabled: boolean; - readonly onSelect: (thread: { id: ThreadId; projectId: ProjectId } | null) => void; + readonly onSelect: (threadId: ThreadId) => void; /** Absent on servers that predate thread modes. */ readonly onThreadModeChange: ((mode: ScheduledTaskThreadMode) => void) | undefined; }) { @@ -1331,18 +1327,14 @@ function ScheduledTaskDestination({ titlesOnly: true, projectIds: [projectId as ProjectId], }); - const threads = scheduledTaskDestinationThreads(shells, { + const rows = scheduledTaskDestinationRows(shells, { environmentId, projectId, - currentThreadId: threadId, + current: threadId === null ? null : { id: threadId, title: currentLabel }, query, + untitledTitle: gt("Untitled thread"), }); - const search = query.trim().toLocaleLowerCase(); - const items: ReadonlyArray = [ - ...(search ? [] : [NEW_THREAD_DESTINATION]), - ...(threadId !== null && currentLabel.toLocaleLowerCase().includes(search) ? [threadId] : []), - ...threads.map((row) => row.id), - ]; + const items = rows.map((row) => row.id); const handleOpenChange = (next: boolean) => { setOpen(next); if (!next) setQuery(""); @@ -1380,7 +1372,7 @@ function ScheduledTaskDestination({ ) : null} - {!onThreadModeChange || threadMode !== "new_thread" ? ( + {threadMode !== "new_thread" ? ( { - if (value === NEW_THREAD_DESTINATION) { - onSelect(null); - handleOpenChange(false); - return; - } - const picked = threads.find((row) => row.id === value); - if (picked) onSelect(picked); + if (value !== null && value !== threadId) onSelect(value); handleOpenChange(false); }} > @@ -1426,15 +1412,9 @@ function ScheduledTaskDestination({ No threads found. - {items.map((value, index) => ( - - - {value === NEW_THREAD_DESTINATION - ? gt("New thread each run") - : value === threadId - ? currentLabel - : threads.find((row) => row.id === value)?.title || gt("Untitled thread")} - + {rows.map((row, index) => ( + + {row.title} ))} @@ -1814,14 +1794,9 @@ function ScheduledTaskEditorForm({ projectId={selectedProjectId} threadId={draft.threadId ? (draft.threadId as ThreadId) : null} threadMode={draft.threadMode} - disabled={saving || !connected} - onSelect={(thread) => - setDraft((current) => - thread === null - ? { ...unbindDraftFromThread(current), threadMode: "new_thread" } - : moveDraftToThread(current, thread), - ) - } + // Without thread modes there is no way back to a new thread each run, so stay read-only. + disabled={saving || !connected || !offersThreadModes} + onSelect={(threadId) => setDraft((current) => ({ ...current, threadId }))} onThreadModeChange={ offersThreadModes ? (threadMode) => @@ -1838,7 +1813,7 @@ function ScheduledTaskEditorForm({