GeezerVison is showing github.com — refactor(mobile): workspace tabs, home filters and connection status … · pingdotgg/t3code@79f6cd3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/home/WorkspaceConnectionTitle.tsx +17-22Lines changed: 17 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { NativeStackNavigationOptions } from "@react-navigation/native-stack"; | 2 | | -import { createContext, use, useEffect, useMemo, useState, type ReactNode } from "react"; | | 2 | +import { createContext, use, useEffect, useState, type ReactNode } from "react"; | 3 | 3 | import { Animated, Platform, Pressable, View } from "react-native"; | 4 | 4 |
| 5 | 5 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; | @@ -11,6 +11,7 @@ import { | 11 | 11 | CompactBrandTitle, | 12 | 12 | getCompactBrandHeaderOptions, | 13 | 13 | } from "../../components/CompactBrandTitle"; | | 14 | +import { useDeadlinePassed } from "../../lib/clock"; | 14 | 15 | import { useWorkspaceState } from "../../state/workspace"; | 15 | 16 | import { | 16 | 17 | workspaceConnectionStatusPresentation, | @@ -37,29 +38,24 @@ export function WorkspaceConnectionStatusProvider(props: { readonly children: Re | 37 | 38 | const { state } = useWorkspaceState(); | 38 | 39 | const presentation = workspaceConnectionStatusPresentation(state); | 39 | 40 | const hasStatus = presentation !== null; | 40 | | - const [phase, setPhase] = useState<"waiting" | "fading" | "shown">("waiting"); | 41 | | - | 42 | | - useEffect(() => { | 43 | | - if (!hasStatus) return; | 44 | | - const timers = [ | 45 | | - setTimeout(() => setPhase("fading"), STATUS_SHOW_DELAY_MS), | 46 | | - setTimeout(() => setPhase("shown"), STATUS_SHOW_DELAY_MS + FADE_IN_MS), | 47 | | - ]; | 48 | | - return () => { | 49 | | - for (const timer of timers) clearTimeout(timer); | 50 | | - setPhase("waiting"); | 51 | | - }; | 52 | | - }, [hasStatus]); | | 41 | + // When the current status run began; a gap in the status restarts the delay. | | 42 | + const [statusSince, setStatusSince] = useState(() => (hasStatus ? Date.now() : null)); | | 43 | + if (hasStatus !== (statusSince !== null)) { | | 44 | + // oxlint-disable-next-line react/purity -- Reads the clock only in the render-phase adjustment that runs when the status appears. | | 45 | + setStatusSince(hasStatus ? Date.now() : null); | | 46 | + } | | 47 | + const since = hasStatus ? statusSince : null; | | 48 | + const fadeStarted = useDeadlinePassed(since === null ? null : since + STATUS_SHOW_DELAY_MS); | | 49 | + const fadeEnded = useDeadlinePassed( | | 50 | + since === null ? null : since + STATUS_SHOW_DELAY_MS + FADE_IN_MS, | | 51 | + ); | 53 | 52 |
| 54 | 53 | const label = presentation?.label; | 55 | 54 | const showsProgress = presentation?.showsProgress ?? false; | 56 | | - const status = useMemo( | 57 | | - () => | 58 | | - label === undefined || phase === "waiting" | 59 | | - ? null | 60 | | - : { label, showsProgress, fadeIn: phase === "fading" }, | 61 | | - [label, showsProgress, phase], | 62 | | - ); | | 55 | + const status = | | 56 | + label === undefined || since === null || !fadeStarted | | 57 | + ? null | | 58 | + : { label, showsProgress, fadeIn: !fadeEnded }; | 63 | 59 | return <ConnectionStatusContext value={status}>{props.children}</ConnectionStatusContext>; | 64 | 60 | } | 65 | 61 |
| @@ -72,7 +68,6 @@ function StatusFadeIn(props: { | 72 | 68 | }) { | 73 | 69 | const { animate } = props; | 74 | 70 | const [opacity] = useState(() => new Animated.Value(0)); | 75 | | - | 76 | 71 | useEffect(() => { | 77 | 72 | if (!animate) return; | 78 | 73 | const animation = Animated.timing(opacity, { | |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { EnvironmentId } from "@t3tools/contracts"; | | 2 | +import { act, useLayoutEffect } from "react"; | | 3 | +import { createRoot } from "react-dom/client"; | | 4 | +import { expect, it, vi } from "vite-plus/test"; | | 5 | + | | 6 | +const harness = vi.hoisted(() => ({ saved: [] as unknown[] })); | | 7 | + | | 8 | +vi.mock("../../state/preferences", () => ({ updateMobilePreferencesAtom: {} })); | | 9 | +vi.mock("@effect/atom-react", async (importOriginal) => ({ | | 10 | + ...(await importOriginal<typeof import("@effect/atom-react")>()), | | 11 | + useAtomSet: () => (value: unknown) => harness.saved.push(value), | | 12 | +})); | | 13 | + | | 14 | +import { HomeListOptionsProvider, useHomeListOptions } from "./home-list-options"; | | 15 | + | | 16 | +const ENVIRONMENT = "env-1" as EnvironmentId; | | 17 | +const AVAILABLE = new Set([ENVIRONMENT]); | | 18 | + | | 19 | +it("saves the scope from every update made in one event", async () => { | | 20 | + harness.saved = []; | | 21 | + let controls: ReturnType<typeof useHomeListOptions> | undefined; | | 22 | + function Probe() { | | 23 | + const result = useHomeListOptions(AVAILABLE); | | 24 | + useLayoutEffect(() => { | | 25 | + controls = result; | | 26 | + }); | | 27 | + return null; | | 28 | + } | | 29 | + | | 30 | + const root = createRoot(document.createElement("div")); | | 31 | + await act(async () => | | 32 | + root.render( | | 33 | + <HomeListOptionsProvider projectGroupingMode="repository"> | | 34 | + <Probe /> | | 35 | + </HomeListOptionsProvider>, | | 36 | + ), | | 37 | + ); | | 38 | + act(() => { | | 39 | + controls!.setSelectedEnvironmentId(ENVIRONMENT); | | 40 | + controls!.setSelectedProjectKey("env-1:project-1"); | | 41 | + }); | | 42 | + | | 43 | + expect(harness.saved.at(-1)).toEqual({ | | 44 | + threadListFilters: { environmentId: ENVIRONMENT, projectKey: "env-1:project-1" }, | | 45 | + }); | | 46 | + expect(controls!.options.selectedEnvironmentId).toBe(ENVIRONMENT); | | 47 | + expect(controls!.options.selectedProjectKey).toBe("env-1:project-1"); | | 48 | + act(() => root.unmount()); | | 49 | +}); |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/home/home-list-options.ts +31-29Lines changed: 31 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { EnvironmentId, SidebarProjectGroupingMode } from "@t3tools/contracts"; | 2 | 2 | import { 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"; | 15 | 6 |
| 16 | 7 | import type { ThreadListFilters } from "../../persistence/mobile-preferences"; | 17 | 8 | import { updateMobilePreferencesAtom } from "../../state/preferences"; | @@ -40,7 +31,7 @@ function defaultHomeListOptions(filters?: ThreadListFilters): HomeListOptions { | 40 | 31 |
| 41 | 32 | interface HomeListOptionsContextValue { | 42 | 33 | readonly options: HomeListOptions; | 43 | | - readonly setOptions: Dispatch<SetStateAction<HomeListOptions>>; | | 34 | + readonly updateOptions: (patch: Partial<HomeListOptions>) => void; | 44 | 35 | readonly projectGroupingMode: SidebarProjectGroupingMode; | 45 | 36 | } | 46 | 37 |
| @@ -54,28 +45,39 @@ export function HomeListOptionsProvider({ | 54 | 45 | readonly projectGroupingMode: SidebarProjectGroupingMode; | 55 | 46 | readonly savedFilters?: ThreadListFilters | undefined; | 56 | 47 | }>) { | 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); | 58 | 51 | 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); | 62 | 58 | 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 }; | 71 | 71 | return createElement(HomeListOptionsContext, { value }, children); | 72 | 72 | } | 73 | 73 |
| 74 | 74 | export function useHomeListOptions(availableEnvironmentIds: ReadonlySet<EnvironmentId>) { | 75 | 75 | const shared = useContext(HomeListOptionsContext); | 76 | 76 | const [localOptions, setLocalOptions] = useState<HomeListOptions>(defaultHomeListOptions); | 77 | 77 | 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 }))); | 79 | 81 | const selectedEnvironmentId = | 80 | 82 | options.selectedEnvironmentId !== null && | 81 | 83 | availableEnvironmentIds.has(options.selectedEnvironmentId) | @@ -91,13 +93,13 @@ export function useHomeListOptions(availableEnvironmentIds: ReadonlySet<Environm | 91 | 93 | }; | 92 | 94 |
| 93 | 95 | const setSelectedEnvironmentId = (value: EnvironmentId | null) => { | 94 | | - setOptions((current) => ({ ...current, selectedEnvironmentId: value })); | | 96 | + updateOptions({ selectedEnvironmentId: value }); | 95 | 97 | }; | 96 | 98 | const setSelectedProjectKey = (value: string | null) => { | 97 | | - setOptions((current) => ({ ...current, selectedProjectKey: value })); | | 99 | + updateOptions({ selectedProjectKey: value }); | 98 | 100 | }; | 99 | 101 | const setProjectSortOrder = (value: HomeProjectSortOrder) => { | 100 | | - setOptions((current) => ({ ...current, projectSortOrder: value })); | | 102 | + updateOptions({ projectSortOrder: value }); | 101 | 103 | }; | 102 | 104 | return { | 103 | 105 | options: resolvedOptions, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/workspace/AndroidWorkspaceNavigation.android.tsx +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { useKeyboardState } from "react-native-keyboard-controller"; | 13 | 13 |
| 14 | 14 | import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing"; | 15 | 15 | import { useToolbarLayout } from "../../components/ui/useToolbarLayout"; | | 16 | +import { rootNavigationRef } from "../../lib/rootNavigation"; | 16 | 17 | import { useWorkspaceConnectionState } from "../../state/workspace"; | 17 | 18 | import { androidWorkspaceNavigationColors } from "./android-workspace-navigation-colors"; | 18 | 19 | import { | @@ -116,11 +117,12 @@ export function AndroidWorkspaceNavigation(props: { | 116 | 117 | const tab = event.nativeEvent.tab; | 117 | 118 | if (tab !== "settings") workspace.dispatch({ type: "select", tab }); | 118 | 119 | // Reset the root stack even when a nested settings navigator has focus. | 119 | | - if (root) { | | 120 | + // The provider renders inside the root stack's layout, so the container's root state is | | 121 | + // that stack's current state. | | 122 | + const rootState = rootNavigationRef.getRootState(); | | 123 | + if (root && rootState) { | 120 | 124 | navigation.dispatch({ | 121 | | - ...CommonActions.reset( | 122 | | - androidWorkspaceTabNavigationState(root.getState(), tab, rail), | 123 | | - ), | | 125 | + ...CommonActions.reset(androidWorkspaceTabNavigationState(rootState, tab, rail)), | 124 | 126 | target: root.key, | 125 | 127 | }); | 126 | 128 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/workspace/AndroidWorkspaceTabsProvider.tsx +3-20Lines changed: 3 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { NavigationState } from "@react-navigation/native"; | 2 | | -import { | 3 | | - createContext, | 4 | | - use, | 5 | | - useCallback, | 6 | | - useLayoutEffect, | 7 | | - useMemo, | 8 | | - useRef, | 9 | | - type ReactNode, | 10 | | -} from "react"; | | 2 | +import { createContext, use, type ReactNode } from "react"; | 11 | 3 | import { Platform } from "react-native"; | 12 | 4 | import { androidWorkspaceSettingsOpen } from "./android-workspace-tabs"; | 13 | 5 | import { WorkspaceTabsProvider } from "./WorkspaceTabsContext"; | 14 | 6 |
| 15 | 7 | interface AndroidWorkspaceRoot { | 16 | 8 | /** | 17 | 9 | * The rail's `useNavigation()` dispatches to the focused navigator, which may be nested Settings. | 18 | | - * Use the root stack's state and key so a tab press resets the root. | | 10 | + * Target the root stack's key (and read `rootNavigationRef`'s state) so a tab press resets the root. | 19 | 11 | */ | 20 | 12 | readonly key: string; | 21 | | - readonly getState: () => NavigationState; | 22 | 13 | /** Settings is derived from navigation rather than kept in the tab reducer. */ | 23 | 14 | readonly settingsOpen: boolean; | 24 | 15 | readonly settingsRoute: NavigationState["routes"][number] | undefined; | @@ -36,21 +27,13 @@ export function AndroidWorkspaceTabsProvider(props: { | 36 | 27 | readonly state: NavigationState; | 37 | 28 | readonly children: ReactNode; | 38 | 29 | }) { | 39 | | - const stateRef = useRef(props.state); | 40 | | - useLayoutEffect(() => { | 41 | | - stateRef.current = props.state; | 42 | | - }, [props.state]); | 43 | | - const getState = useCallback(() => stateRef.current, []); | 44 | 30 | const key = props.state.key; | 45 | 31 | const settingsOpen = Platform.OS === "android" && androidWorkspaceSettingsOpen(props.state); | 46 | 32 | const settingsIndex = props.state.routes.findIndex((route) => route.name === "Settings"); | 47 | 33 | const settingsRoute = settingsIndex < 0 ? undefined : props.state.routes[settingsIndex]; | 48 | 34 | const settingsPage = | 49 | 35 | settingsIndex < 0 ? null : (props.state.routes[settingsIndex + 1]?.name ?? null); | 50 | | - const root = useMemo( | 51 | | - () => ({ key, getState, settingsOpen, settingsRoute, settingsPage }), | 52 | | - [key, getState, settingsOpen, settingsRoute, settingsPage], | 53 | | - ); | | 36 | + const root = { key, settingsOpen, settingsRoute, settingsPage }; | 54 | 37 | return Platform.OS === "android" ? ( | 55 | 38 | <WorkspaceTabsProvider> | 56 | 39 | <AndroidWorkspaceRootContext value={root}>{props.children}</AndroidWorkspaceRootContext> | |
|