|
| 1 | +// This device's project filter (RSI-Software/t3code-hyprws#1351), kept as a |
| 2 | +// device-local preference. The single-select chooser's picks write it, and the |
| 3 | +// Home list's and the sidebar's selected project follow it. |
| 4 | +import { useAtomSet, useAtomValue } from "@effect/atom-react"; |
| 5 | +import { |
| 6 | + ALL_PROJECTS_FILTER, |
| 7 | + projectFilterFromKey, |
| 8 | + projectFilterScopeKey, |
| 9 | + reconcileProjectFilter, |
| 10 | +} from "@t3tools/client-runtime/state/project-filter"; |
| 11 | +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 12 | +import { AsyncResult } from "effect/unstable/reactivity"; |
| 13 | +import { useEffect, useMemo, useRef } from "react"; |
| 14 | + |
| 15 | +import { appAtomRegistry } from "../../state/atom-registry"; |
| 16 | +import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; |
| 17 | +import { useWorkspaceState } from "../../state/workspace"; |
| 18 | +import type { ResolvedHomeListOptions } from "./home-list-options"; |
| 19 | +import { buildHomeProjectScopes } from "./homeThreadList"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Records a chooser pick: a project replaces the filter, `null` clears it. A |
| 23 | + * cleared filter of several entries leaves the selected key `null` as it was, |
| 24 | + * so the pick itself, not a key change, has to reach the preference. |
| 25 | + */ |
| 26 | +export function pickMobileProjectFilter(projectKey: string | null): void { |
| 27 | + appAtomRegistry.set(updateMobilePreferencesAtom, { |
| 28 | + projectFilter: projectFilterFromKey(projectKey), |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Replaces upstream's `onProjectChange` when spread after it. Optional in type |
| 34 | + * only, so the spread may follow the prop it replaces. |
| 35 | + */ |
| 36 | +export const mobileProjectFilterPick: { |
| 37 | + readonly onProjectChange?: (projectKey: string | null) => void; |
| 38 | +} = { onProjectChange: pickMobileProjectFilter }; |
| 39 | + |
| 40 | +/** |
| 41 | + * Keeps `selectedProjectKey` on the stored filter. The filter follows the device's project groups; an entry whose |
| 42 | + * environment is not connected yet stays stored, and shows again once back. |
| 43 | + */ |
| 44 | +export function useMobileProjectFilter( |
| 45 | + projects: ReadonlyArray<EnvironmentProject>, |
| 46 | + options: Pick<ResolvedHomeListOptions, "projectGroupingMode" | "selectedEnvironmentId">, |
| 47 | + selectedProjectKey: string | null, |
| 48 | + setSelectedProjectKey: (key: string | null) => void, |
| 49 | +) { |
| 50 | + const preferences = useAtomValue(mobilePreferencesAtom); |
| 51 | + const savePreferences = useAtomSet(updateMobilePreferencesAtom); |
| 52 | + const loaded = AsyncResult.isSuccess(preferences); |
| 53 | + const stored = (loaded && preferences.value.projectFilter) || ALL_PROJECTS_FILTER; |
| 54 | + const { environments, state } = useWorkspaceState(); |
| 55 | + const settled = |
| 56 | + !state.isLoadingConnections && |
| 57 | + state.hasLoadedShellSnapshot && |
| 58 | + !state.hasPendingShellSnapshot && |
| 59 | + environments.every((environment) => { |
| 60 | + return !environment.isEnabled || environment.connectionState === "connected"; |
| 61 | + }); |
| 62 | + const { projectGroupingMode, selectedEnvironmentId } = options; |
| 63 | + const groups = useMemo( |
| 64 | + () => |
| 65 | + buildHomeProjectScopes({ projects, environmentId: null, projectGroupingMode }).map( |
| 66 | + (scope) => ({ key: scope.key, members: scope.projectRefs }), |
| 67 | + ), |
| 68 | + [projectGroupingMode, projects], |
| 69 | + ); |
| 70 | + const filter = useMemo( |
| 71 | + () => reconcileProjectFilter(stored, groups, settled), |
| 72 | + [groups, settled, stored], |
| 73 | + ); |
| 74 | + // oxlint-disable-next-line t3code/react-effect-budget -- writes the reconciled filter back to the persisted device preferences store |
| 75 | + useEffect(() => { |
| 76 | + if (loaded && filter !== stored) savePreferences({ projectFilter: filter }); |
| 77 | + }, [filter, loaded, savePreferences, stored]); |
| 78 | + |
| 79 | + // The chooser can only show a key its environment filter lists. |
| 80 | + const scopeKey = projectFilterScopeKey(filter); |
| 81 | + const exposedKey = useMemo(() => { |
| 82 | + if (scopeKey === null) return null; |
| 83 | + const visible = buildHomeProjectScopes({ |
| 84 | + projects, |
| 85 | + environmentId: selectedEnvironmentId, |
| 86 | + projectGroupingMode, |
| 87 | + }); |
| 88 | + return visible.some((scope) => scope.key === scopeKey) ? scopeKey : null; |
| 89 | + }, [projectGroupingMode, projects, scopeKey, selectedEnvironmentId]); |
| 90 | + |
| 91 | + const seenExposed = useRef<string | null>(null); |
| 92 | + // oxlint-disable-next-line t3code/react-effect-budget -- pushes filter changes into the caller-owned upstream selected-project state |
| 93 | + useEffect(() => { |
| 94 | + if (!loaded || exposedKey === seenExposed.current) return; |
| 95 | + seenExposed.current = exposedKey; |
| 96 | + if (exposedKey !== selectedProjectKey) setSelectedProjectKey(exposedKey); |
| 97 | + }, [exposedKey, loaded, selectedProjectKey, setSelectedProjectKey]); |
| 98 | +} |
0 commit comments