The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | |
| 5 | import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences"; |
| 6 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 7 | import { DEFAULT_PR_LIST_CONTROLS, type PullRequestListControls } from "./pullRequestList"; |
| 8 | import type { PullRequestSavedView } from "../../persistence/mobile-preferences"; |
| 9 | import { pullRequestSavedViewPreferences } from "./pullRequestSavedViews"; |
| 10 | |
| 11 | export function usePullRequestListControls() { |
| 12 | const preferences = useAtomValue(mobilePreferencesAtom); |
| 13 | const savePreferences = useAtomCommand(saveMobilePreferences, { reportFailure: false }); |
| 14 | const saved = AsyncResult.isSuccess(preferences) |
| 15 | ? preferences.value.pullRequestListFilters |
| 16 | : undefined; |
| 17 | const environmentId = saved?.environmentId ?? null; |
| 18 | const controlsFor = (environment: string | null) => |
| 19 | saved?.controls[environment ?? ""] ?? DEFAULT_PR_LIST_CONTROLS; |
| 20 | const setEnvironmentId = (next: string | null) => { |
| 21 | void savePreferences({ |
| 22 | transform: (current) => ({ |
| 23 | pullRequestListFilters: { |
| 24 | controls: current.pullRequestListFilters?.controls ?? {}, |
| 25 | environmentId: next, |
| 26 | }, |
| 27 | }), |
| 28 | }); |
| 29 | }; |
| 30 | const setControls = (environment: string | null, controls: PullRequestListControls) => { |
| 31 | void savePreferences({ |
| 32 | transform: (current) => ({ |
| 33 | pullRequestListFilters: { |
| 34 | environmentId: current.pullRequestListFilters?.environmentId ?? null, |
| 35 | controls: withEnvironmentControls( |
| 36 | current.pullRequestListFilters?.controls, |
| 37 | environment ?? "", |
| 38 | controls, |
| 39 | ), |
| 40 | }, |
| 41 | }), |
| 42 | }); |
| 43 | }; |
| 44 | const applyView = async (view: PullRequestSavedView) => { |
| 45 | const result = await savePreferences({ |
| 46 | transform: (current) => pullRequestSavedViewPreferences(current, view), |
| 47 | }); |
| 48 | if (result._tag === "Failure") throw squashAtomCommandFailure(result); |
| 49 | }; |
| 50 | return { environmentId, controlsFor, setEnvironmentId, setControls, applyView } as const; |
| 51 | } |
| 52 | |
| 53 | function withEnvironmentControls( |
| 54 | saved: Readonly<Record<string, PullRequestListControls>> | undefined, |
| 55 | environment: string, |
| 56 | controls: PullRequestListControls, |
| 57 | ): Record<string, PullRequestListControls> { |
| 58 | return { ...saved, [environment]: controls }; |
| 59 | } |