11import type { EnvironmentId , SidebarProjectGroupingMode } from "@t3tools/contracts" ;
22import { DEFAULT_SIDEBAR_PROJECT_SORT_ORDER } from "@t3tools/contracts" ;
3- import { useAtomSet } from "@effect/atom-react" ;
4- import {
5- createContext ,
6- createElement ,
7- useContext ,
8- useEffect ,
9- useRef ,
10- useState ,
11- type PropsWithChildren ,
12- type Dispatch ,
13- type SetStateAction ,
14- } from "react" ;
3+ import { RegistryContext , useAtomSet , useAtomValue } from "@effect/atom-react" ;
4+ import { Atom } from "effect/reactivity" ;
5+ import { createContext , createElement , useContext , useState , type PropsWithChildren } from "react" ;
156
167import type { ThreadListFilters } from "../../persistence/mobile-preferences" ;
178import { updateMobilePreferencesAtom } from "../../state/preferences" ;
@@ -40,7 +31,7 @@ function defaultHomeListOptions(filters?: ThreadListFilters): HomeListOptions {
4031
4132interface HomeListOptionsContextValue {
4233 readonly options : HomeListOptions ;
43- readonly setOptions : Dispatch < SetStateAction < HomeListOptions > > ;
34+ readonly updateOptions : ( patch : Partial < HomeListOptions > ) => void ;
4435 readonly projectGroupingMode : SidebarProjectGroupingMode ;
4536}
4637
@@ -54,28 +45,39 @@ export function HomeListOptionsProvider({
5445 readonly projectGroupingMode : SidebarProjectGroupingMode ;
5546 readonly savedFilters ?: ThreadListFilters | undefined ;
5647} > ) {
57- const [ options , setOptions ] = useState ( ( ) => defaultHomeListOptions ( savedFilters ) ) ;
48+ const registry = useContext ( RegistryContext ) ;
49+ const [ optionsAtom ] = useState ( ( ) => Atom . make ( defaultHomeListOptions ( savedFilters ) ) ) ;
50+ const options = useAtomValue ( optionsAtom ) ;
5851 const savePreferences = useAtomSet ( updateMobilePreferencesAtom ) ;
59- const saved = useRef ( savedFilters ) ;
60- const { selectedEnvironmentId : environmentId , selectedProjectKey : projectKey } = options ;
61- useEffect ( ( ) => {
52+ // Only the scope persists; the sort order is per session. Updates read the
53+ // registry, so several in one event each build on the last and save it.
54+ const updateOptions = ( patch : Partial < HomeListOptions > ) => {
55+ const current = registry . get ( optionsAtom ) ;
56+ const next = { ...current , ...patch } ;
57+ registry . set ( optionsAtom , next ) ;
6258 if (
63- ( saved . current ?. environmentId ?? null ) === environmentId &&
64- ( saved . current ?. projectKey ?? null ) === projectKey
65- )
66- return ;
67- saved . current = { environmentId, projectKey } ;
68- savePreferences ( { threadListFilters : saved . current } ) ;
69- } , [ environmentId , projectKey , savePreferences ] ) ;
70- const value = { options, setOptions, projectGroupingMode } ;
59+ next . selectedEnvironmentId !== current . selectedEnvironmentId ||
60+ next . selectedProjectKey !== current . selectedProjectKey
61+ ) {
62+ savePreferences ( {
63+ threadListFilters : {
64+ environmentId : next . selectedEnvironmentId ,
65+ projectKey : next . selectedProjectKey ,
66+ } ,
67+ } ) ;
68+ }
69+ } ;
70+ const value = { options, updateOptions, projectGroupingMode } ;
7171 return createElement ( HomeListOptionsContext , { value } , children ) ;
7272}
7373
7474export function useHomeListOptions ( availableEnvironmentIds : ReadonlySet < EnvironmentId > ) {
7575 const shared = useContext ( HomeListOptionsContext ) ;
7676 const [ localOptions , setLocalOptions ] = useState < HomeListOptions > ( defaultHomeListOptions ) ;
7777 const options = shared ?. options ?? localOptions ;
78- const setOptions = shared ?. setOptions ?? setLocalOptions ;
78+ const updateOptions =
79+ shared ?. updateOptions ??
80+ ( ( patch : Partial < HomeListOptions > ) => setLocalOptions ( ( current ) => ( { ...current , ...patch } ) ) ) ;
7981 const selectedEnvironmentId =
8082 options . selectedEnvironmentId !== null &&
8183 availableEnvironmentIds . has ( options . selectedEnvironmentId )
@@ -91,13 +93,13 @@ export function useHomeListOptions(availableEnvironmentIds: ReadonlySet<Environm
9193 } ;
9294
9395 const setSelectedEnvironmentId = ( value : EnvironmentId | null ) => {
94- setOptions ( ( current ) => ( { ... current , selectedEnvironmentId : value } ) ) ;
96+ updateOptions ( { selectedEnvironmentId : value } ) ;
9597 } ;
9698 const setSelectedProjectKey = ( value : string | null ) => {
97- setOptions ( ( current ) => ( { ... current , selectedProjectKey : value } ) ) ;
99+ updateOptions ( { selectedProjectKey : value } ) ;
98100 } ;
99101 const setProjectSortOrder = ( value : HomeProjectSortOrder ) => {
100- setOptions ( ( current ) => ( { ... current , projectSortOrder : value } ) ) ;
102+ updateOptions ( { projectSortOrder : value } ) ;
101103 } ;
102104 return {
103105 options : resolvedOptions ,
0 commit comments