|
| 1 | +import { useNavigation } from "@react-navigation/native"; |
| 2 | +import * as Haptics from "expo-haptics"; |
| 3 | +import { useMemo, useState } from "react"; |
| 4 | + |
| 5 | +import { AppText as Text } from "../../components/AppText"; |
| 6 | +import { buildModelOptions, groupByProvider } from "../../lib/modelOptions"; |
| 7 | +import { resolveProviderOptionDescriptors } from "../../lib/providerOptions"; |
| 8 | +import { useEnvironmentServerConfig, useProjects } from "../../state/entities"; |
| 9 | +import { useDebouncedValue, usePaginatedBranches } from "../../state/queries"; |
| 10 | +import { BranchPickerScreen } from "../threads/NewTaskContextPickerScreens"; |
| 11 | +import { ThreadSettingsPickerScreen } from "../threads/ThreadSettingsSheet"; |
| 12 | +import { SettingsScreen } from "./components/SettingsScreen"; |
| 13 | +import { useScheduledTaskEditor } from "./scheduled-task-editor"; |
| 14 | + |
| 15 | +function MissingTaskDraft() { |
| 16 | + return ( |
| 17 | + <SettingsScreen title="Scheduled task"> |
| 18 | + <Text className="p-5 text-base text-foreground-muted">Open a scheduled task form first.</Text> |
| 19 | + </SettingsScreen> |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +export function ScheduledTaskModelPickerRouteScreen() { |
| 24 | + const navigation = useNavigation(); |
| 25 | + const { editor, setEditor } = useScheduledTaskEditor(); |
| 26 | + const config = useEnvironmentServerConfig(editor?.environmentId ?? null); |
| 27 | + const selectedModel = editor?.draft.modelSelection ?? null; |
| 28 | + const models = useMemo(() => buildModelOptions(config, selectedModel), [config, selectedModel]); |
| 29 | + const providerGroups = useMemo(() => groupByProvider(models), [models]); |
| 30 | + const selectedOption = models.find( |
| 31 | + (option) => |
| 32 | + option.selection.instanceId === selectedModel?.instanceId && |
| 33 | + option.selection.model === selectedModel.model, |
| 34 | + ); |
| 35 | + const optionDescriptors = useMemo( |
| 36 | + () => |
| 37 | + resolveProviderOptionDescriptors({ |
| 38 | + capabilities: selectedOption?.capabilities, |
| 39 | + selections: selectedModel?.options, |
| 40 | + }), |
| 41 | + [selectedOption?.capabilities, selectedModel?.options], |
| 42 | + ); |
| 43 | + |
| 44 | + if (!editor) return <MissingTaskDraft />; |
| 45 | + |
| 46 | + return ( |
| 47 | + <ThreadSettingsPickerScreen |
| 48 | + environmentId={editor.environmentId} |
| 49 | + providerGroups={providerGroups} |
| 50 | + selectedModel={selectedModel} |
| 51 | + onSelectModel={(option) => |
| 52 | + setEditor((current) => |
| 53 | + current |
| 54 | + ? { |
| 55 | + ...current, |
| 56 | + draft: { |
| 57 | + ...current.draft, |
| 58 | + modelSelection: option.selection, |
| 59 | + modelSelectionIsExplicit: true, |
| 60 | + }, |
| 61 | + } |
| 62 | + : current, |
| 63 | + ) |
| 64 | + } |
| 65 | + optionDescriptors={optionDescriptors} |
| 66 | + onUpdateOptionSelections={(options) => |
| 67 | + setEditor((current) => |
| 68 | + current?.draft.modelSelection |
| 69 | + ? { |
| 70 | + ...current, |
| 71 | + draft: { |
| 72 | + ...current.draft, |
| 73 | + modelSelection: { ...current.draft.modelSelection, options }, |
| 74 | + modelSelectionIsExplicit: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + : current, |
| 78 | + ) |
| 79 | + } |
| 80 | + runtimeMode={editor.draft.runtimeMode} |
| 81 | + onUpdateRuntimeMode={(runtimeMode) => |
| 82 | + setEditor((current) => |
| 83 | + current |
| 84 | + ? { |
| 85 | + ...current, |
| 86 | + draft: { ...current.draft, runtimeMode }, |
| 87 | + } |
| 88 | + : current, |
| 89 | + ) |
| 90 | + } |
| 91 | + onClose={() => navigation.goBack()} |
| 92 | + /> |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +export function ScheduledTaskBranchPickerRouteScreen() { |
| 97 | + const navigation = useNavigation(); |
| 98 | + const { editor, setEditor } = useScheduledTaskEditor(); |
| 99 | + const projects = useProjects(); |
| 100 | + const project = |
| 101 | + projects.find( |
| 102 | + (entry) => |
| 103 | + entry.environmentId === editor?.environmentId && entry.id === editor.draft.projectId, |
| 104 | + ) ?? null; |
| 105 | + const [query, setQuery] = useState(""); |
| 106 | + const debouncedQuery = useDebouncedValue(query, 150); |
| 107 | + const branches = usePaginatedBranches({ |
| 108 | + environmentId: editor?.environmentId ?? null, |
| 109 | + cwd: project?.workspaceRoot ?? null, |
| 110 | + query: debouncedQuery, |
| 111 | + }); |
| 112 | + const visibleBranches = branches.refs.filter( |
| 113 | + (branch) => !branch.isRemote && branch.name.toLowerCase().includes(query.trim().toLowerCase()), |
| 114 | + ); |
| 115 | + |
| 116 | + if (!editor) return <MissingTaskDraft />; |
| 117 | + |
| 118 | + return ( |
| 119 | + <BranchPickerScreen |
| 120 | + title="Base branch" |
| 121 | + project={project} |
| 122 | + branches={visibleBranches} |
| 123 | + selectedBranchName={editor.draft.baseRef} |
| 124 | + query={query} |
| 125 | + onQueryChange={setQuery} |
| 126 | + loading={ |
| 127 | + query.trim() !== debouncedQuery.trim() || (branches.isPending && branches.data === null) |
| 128 | + } |
| 129 | + error={project ? branches.error : "This project is no longer available."} |
| 130 | + refreshing={branches.isFetchingNextPage} |
| 131 | + hasMore={branches.data?.nextCursor != null} |
| 132 | + onRefresh={branches.refresh} |
| 133 | + onLoadMore={branches.loadNext} |
| 134 | + onSelect={(branch) => { |
| 135 | + void Haptics.selectionAsync(); |
| 136 | + setEditor((current) => |
| 137 | + current ? { ...current, draft: { ...current.draft, baseRef: branch.name } } : current, |
| 138 | + ); |
| 139 | + navigation.goBack(); |
| 140 | + }} |
| 141 | + worktree={{ |
| 142 | + startFromOrigin: editor.draft.startFromOrigin, |
| 143 | + onChangeStartFromOrigin: (startFromOrigin) => |
| 144 | + setEditor((current) => |
| 145 | + current |
| 146 | + ? { |
| 147 | + ...current, |
| 148 | + draft: { ...current.draft, startFromOrigin }, |
| 149 | + } |
| 150 | + : current, |
| 151 | + ), |
| 152 | + }} |
| 153 | + /> |
| 154 | + ); |
| 155 | +} |
0 commit comments