The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | type SavedViewIdentity as SavedViewIdentitySchema, |
| 4 | savedViewNameError, |
| 5 | } from "@t3tools/client-runtime/saved-views"; |
| 6 | import { useGT, useMessages } from "gt-react-native"; |
| 7 | import { AsyncResult } from "effect/reactivity"; |
| 8 | import { useState } from "react"; |
| 9 | import { Alert } from "react-native"; |
| 10 | |
| 11 | import { prompt as showTextInputDialog } from "../../../components/ui/Dialog"; |
| 12 | import type { MenuProps } from "../../../components/ui/Menu.types"; |
| 13 | import { uuidv4 } from "../../../lib/uuid"; |
| 14 | import type { Preferences } from "../../../persistence/mobile-preferences"; |
| 15 | import { mobilePreferencesAtom, saveMobilePreferences } from "../../../state/preferences"; |
| 16 | import { useAtomCommand } from "../../../state/use-atom-command"; |
| 17 | import { ListFilterMenu } from "./ListFilterMenu"; |
| 18 | |
| 19 | type SavedViewIdentity = typeof SavedViewIdentitySchema.Type; |
| 20 | const EMPTY_SAVED_VIEWS: ReadonlyArray<never> = []; |
| 21 | |
| 22 | export function SavedViewsMenu<Scope extends object>({ |
| 23 | read, |
| 24 | write, |
| 25 | list, |
| 26 | suggestedName, |
| 27 | scope, |
| 28 | matches, |
| 29 | onApply, |
| 30 | }: { |
| 31 | read: (preferences: Preferences) => ReadonlyArray<Scope & SavedViewIdentity> | undefined; |
| 32 | write: (views: ReadonlyArray<Scope & SavedViewIdentity>) => Partial<Preferences>; |
| 33 | /** Picks whole translated sentences for this list; never inject it into copy. */ |
| 34 | list: "pullRequest" | "issue"; |
| 35 | suggestedName: string; |
| 36 | scope: Scope; |
| 37 | matches: (view: Scope, scope: Scope) => boolean; |
| 38 | onApply: (view: Scope & SavedViewIdentity) => Promise<void> | void; |
| 39 | }) { |
| 40 | type View = Scope & SavedViewIdentity; |
| 41 | const gt = useGT(); |
| 42 | const messages = useMessages(); |
| 43 | const preferences = useAtomValue(mobilePreferencesAtom); |
| 44 | const savePreferences = useAtomCommand(saveMobilePreferences, { reportFailure: false }); |
| 45 | const ready = AsyncResult.isSuccess(preferences) && !preferences.waiting; |
| 46 | const views: ReadonlyArray<View> = AsyncResult.isSuccess(preferences) |
| 47 | ? (read(preferences.value) ?? EMPTY_SAVED_VIEWS) |
| 48 | : EMPTY_SAVED_VIEWS; |
| 49 | const [selectedId, setSelectedId] = useState<string | null>(null); |
| 50 | const [busy, setBusy] = useState(false); |
| 51 | const selectedView = |
| 52 | views.find((view) => view.id === selectedId) ?? views.find((view) => matches(view, scope)); |
| 53 | if (selectedId === null && selectedView) setSelectedId(selectedView.id); |
| 54 | const modified = selectedView !== undefined && !matches(selectedView, scope); |
| 55 | |
| 56 | // These avoid `finally`, which the React Compiler cannot lower; every path clears `busy`. |
| 57 | async function applyView(view: View) { |
| 58 | setBusy(true); |
| 59 | try { |
| 60 | await onApply(view); |
| 61 | setSelectedId(view.id); |
| 62 | } catch { |
| 63 | Alert.alert( |
| 64 | gt( |
| 65 | "{list, select, pullRequest {Could not apply PR view} other {Could not apply issue view}}", |
| 66 | { |
| 67 | list, |
| 68 | }, |
| 69 | ), |
| 70 | gt("The device could not save your filters. Try again."), |
| 71 | ); |
| 72 | } |
| 73 | setBusy(false); |
| 74 | } |
| 75 | |
| 76 | async function persist(transform: (current: ReadonlyArray<View>) => ReadonlyArray<View>) { |
| 77 | setBusy(true); |
| 78 | const saved = |
| 79 | ( |
| 80 | await savePreferences({ |
| 81 | transform: (current) => write(transform(read(current) ?? EMPTY_SAVED_VIEWS)), |
| 82 | }) |
| 83 | )._tag === "Success"; |
| 84 | if (!saved) { |
| 85 | Alert.alert( |
| 86 | gt( |
| 87 | "{list, select, pullRequest {Could not save PR views} other {Could not save issue views}}", |
| 88 | { |
| 89 | list, |
| 90 | }, |
| 91 | ), |
| 92 | gt("The device could not save your views. Try again."), |
| 93 | ); |
| 94 | } |
| 95 | setBusy(false); |
| 96 | return saved; |
| 97 | } |
| 98 | |
| 99 | function editName(view?: View) { |
| 100 | showTextInputDialog({ |
| 101 | title: view |
| 102 | ? gt("{list, select, pullRequest {Rename PR view} other {Rename issue view}}", { list }) |
| 103 | : gt("{list, select, pullRequest {Save PR view} other {Save issue view}}", { list }), |
| 104 | initialValue: view?.name ?? suggestedName, |
| 105 | confirmText: gt("Save"), |
| 106 | onConfirm: (value) => { |
| 107 | const error = savedViewNameError(value, views, view?.id, messages); |
| 108 | if (error) { |
| 109 | Alert.alert(gt("Could not save view"), error); |
| 110 | return; |
| 111 | } |
| 112 | const id = view?.id ?? uuidv4(); |
| 113 | const name = value.trim(); |
| 114 | void persist((current) => |
| 115 | view |
| 116 | ? current.map((item) => (item.id === view.id ? { ...item, name } : item)) |
| 117 | : [...current, { ...scope, id, name }], |
| 118 | ).then((saved) => { |
| 119 | if (saved) setSelectedId(id); |
| 120 | }); |
| 121 | }, |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | const actions: MenuProps["actions"] = [ |
| 126 | ...views.map((view) => ({ |
| 127 | id: `apply:${view.id}`, |
| 128 | title: view.name, |
| 129 | state: selectedView?.id === view.id && !modified ? ("on" as const) : ("off" as const), |
| 130 | })), |
| 131 | { id: "save", title: gt("Save current view…") }, |
| 132 | ...(selectedView |
| 133 | ? [ |
| 134 | ...(modified ? [{ id: "update", title: gt("Update saved view") }] : []), |
| 135 | { id: "rename", title: gt("Rename view…") }, |
| 136 | { id: "delete", title: gt("Delete view"), attributes: { destructive: true } }, |
| 137 | ] |
| 138 | : []), |
| 139 | ]; |
| 140 | |
| 141 | return ( |
| 142 | <ListFilterMenu |
| 143 | label={ |
| 144 | selectedView |
| 145 | ? `${selectedView.name}${modified ? " *" : ""}` |
| 146 | : gt("Views", { $context: "saved filter views menu" }) |
| 147 | } |
| 148 | active={selectedView !== undefined} |
| 149 | accessibilityLabel={gt( |
| 150 | "{list, select, pullRequest {Saved pull request views} other {Saved issue views}}", |
| 151 | { list }, |
| 152 | )} |
| 153 | actions={actions.map((action) => ({ |
| 154 | ...action, |
| 155 | attributes: { ...action.attributes, disabled: !ready || busy }, |
| 156 | }))} |
| 157 | onSelect={(id) => { |
| 158 | if (!ready || busy) return; |
| 159 | const view = views.find((item) => `apply:${item.id}` === id); |
| 160 | if (view) { |
| 161 | void applyView(view); |
| 162 | return; |
| 163 | } |
| 164 | if (id === "save") editName(); |
| 165 | else if (id === "rename" && selectedView) editName(selectedView); |
| 166 | else if (id === "update" && selectedView) |
| 167 | void persist((current) => |
| 168 | current.map((item) => (item.id === selectedView.id ? { ...item, ...scope } : item)), |
| 169 | ); |
| 170 | else if (id === "delete" && selectedView) |
| 171 | void persist((current) => current.filter((item) => item.id !== selectedView.id)).then( |
| 172 | (saved) => { |
| 173 | if (saved) setSelectedId(null); |
| 174 | }, |
| 175 | ); |
| 176 | }} |
| 177 | /> |
| 178 | ); |
| 179 | } |