The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId, SidebarProjectGroupingMode } from "@t3tools/contracts"; |
| 2 | import { DEFAULT_SIDEBAR_PROJECT_SORT_ORDER } from "@t3tools/contracts"; |
| 3 | import { RegistryContext, useAtomValue } from "@effect/atom-react"; |
| 4 | import { Atom } from "effect/reactivity"; |
| 5 | import { createContext, createElement, useContext, useState, type PropsWithChildren } from "react"; |
| 6 | |
| 7 | import type { ThreadListFilters } from "../../persistence/mobile-preferences"; |
| 8 | import { saveMobilePreferences } from "../../state/preferences"; |
| 9 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 10 | import type { HomeProjectSortOrder } from "./homeThreadList"; |
| 11 | |
| 12 | export interface HomeListOptions { |
| 13 | readonly selectedEnvironmentId: EnvironmentId | null; |
| 14 | readonly selectedProjectKey: string | null; |
| 15 | readonly projectSortOrder: HomeProjectSortOrder; |
| 16 | } |
| 17 | |
| 18 | export interface ResolvedHomeListOptions extends HomeListOptions { |
| 19 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 20 | } |
| 21 | |
| 22 | function defaultHomeListOptions(filters?: ThreadListFilters): HomeListOptions { |
| 23 | return { |
| 24 | selectedEnvironmentId: filters?.environmentId ?? null, |
| 25 | selectedProjectKey: filters?.projectKey ?? null, |
| 26 | projectSortOrder: |
| 27 | DEFAULT_SIDEBAR_PROJECT_SORT_ORDER === "manual" |
| 28 | ? "updated_at" |
| 29 | : DEFAULT_SIDEBAR_PROJECT_SORT_ORDER, |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | interface HomeListOptionsContextValue { |
| 34 | readonly options: HomeListOptions; |
| 35 | readonly updateOptions: (patch: Partial<HomeListOptions>) => void; |
| 36 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 37 | } |
| 38 | |
| 39 | const HomeListOptionsContext = createContext<HomeListOptionsContextValue | null>(null); |
| 40 | |
| 41 | export function HomeListOptionsProvider({ |
| 42 | children, |
| 43 | projectGroupingMode, |
| 44 | savedFilters, |
| 45 | }: PropsWithChildren<{ |
| 46 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 47 | readonly savedFilters?: ThreadListFilters | undefined; |
| 48 | }>) { |
| 49 | const registry = useContext(RegistryContext); |
| 50 | const [optionsAtom] = useState(() => Atom.make(defaultHomeListOptions(savedFilters))); |
| 51 | const options = useAtomValue(optionsAtom); |
| 52 | const savePreferences = useAtomCommand(saveMobilePreferences); |
| 53 | // Only the scope persists; the sort order is per session. Updates read the |
| 54 | // registry, so several in one event each build on the last and save it. |
| 55 | const updateOptions = (patch: Partial<HomeListOptions>) => { |
| 56 | const current = registry.get(optionsAtom); |
| 57 | const next = { ...current, ...patch }; |
| 58 | registry.set(optionsAtom, next); |
| 59 | if ( |
| 60 | next.selectedEnvironmentId !== current.selectedEnvironmentId || |
| 61 | next.selectedProjectKey !== current.selectedProjectKey |
| 62 | ) { |
| 63 | void savePreferences({ |
| 64 | threadListFilters: { |
| 65 | environmentId: next.selectedEnvironmentId, |
| 66 | projectKey: next.selectedProjectKey, |
| 67 | }, |
| 68 | }); |
| 69 | } |
| 70 | }; |
| 71 | const value = { options, updateOptions, projectGroupingMode }; |
| 72 | return createElement(HomeListOptionsContext, { value }, children); |
| 73 | } |
| 74 | |
| 75 | export function useHomeListOptions(availableEnvironmentIds: ReadonlySet<EnvironmentId>) { |
| 76 | const shared = useContext(HomeListOptionsContext); |
| 77 | const [localOptions, setLocalOptions] = useState<HomeListOptions>(defaultHomeListOptions); |
| 78 | const options = shared?.options ?? localOptions; |
| 79 | const updateOptions = |
| 80 | shared?.updateOptions ?? |
| 81 | ((patch: Partial<HomeListOptions>) => setLocalOptions((current) => ({ ...current, ...patch }))); |
| 82 | const selectedEnvironmentId = |
| 83 | options.selectedEnvironmentId !== null && |
| 84 | availableEnvironmentIds.has(options.selectedEnvironmentId) |
| 85 | ? options.selectedEnvironmentId |
| 86 | : null; |
| 87 | const availableOptions = |
| 88 | selectedEnvironmentId === options.selectedEnvironmentId |
| 89 | ? options |
| 90 | : { ...options, selectedEnvironmentId }; |
| 91 | const resolvedOptions: ResolvedHomeListOptions = { |
| 92 | ...availableOptions, |
| 93 | projectGroupingMode: shared?.projectGroupingMode ?? "repository", |
| 94 | }; |
| 95 | |
| 96 | const setSelectedEnvironmentId = (value: EnvironmentId | null) => { |
| 97 | updateOptions({ selectedEnvironmentId: value }); |
| 98 | }; |
| 99 | const setSelectedProjectKey = (value: string | null) => { |
| 100 | updateOptions({ selectedProjectKey: value }); |
| 101 | }; |
| 102 | const setProjectSortOrder = (value: HomeProjectSortOrder) => { |
| 103 | updateOptions({ projectSortOrder: value }); |
| 104 | }; |
| 105 | return { |
| 106 | options: resolvedOptions, |
| 107 | setSelectedEnvironmentId, |
| 108 | setSelectedProjectKey, |
| 109 | setProjectSortOrder, |
| 110 | } as const; |
| 111 | } |