fabro/apps/fabro-web/app/routes/run-children.tsx
fabro-sh-0530[bot] b196a97ac4
Introduce approved effect hooks and migrate direct useEffect calls (#425)
## Summary

Implements the React Effects Policy by creating the approved hook
surface in `hooks/effects.ts` and migrating a broad set of direct
`useEffect` calls across the codebase to either purpose-named hooks or
non-effect patterns.

### Plan Summary

- Add `hooks/effects.ts` exporting `useMountEffect`, `useInterval`,
`useTimeout`, `useDebouncedValue`, `useWindowEvent`, `useDocumentEvent`,
`useDocumentTitle`, `useMediaQuery`, `useLocationHash`, and
`useResizeObserver`
- Extract large imperative effects into purpose-named hooks:
`useTerminalSession`, `useFloatingTooltipMeasurements`,
`useAnnotatedRunGraphSvg`, `useInstallEffects`, and others
- Move install session fetch from a component effect into a SWR query
(`install-query.ts`)
- Replace `useEffect` + `useState` state-derivation patterns with
render-time computation or ref callbacks
- Replace `AskFabroLayoutProvider`/`useAskFabroLayout` context with a
prop callback

## What changed and why

**`hooks/effects.ts`** — the new approved primitive surface. All
internal `useEffect` calls here are intentional; the hooks expose the
*external system* they manage rather than leaking `useEffect` to
component code. `useMediaQuery` and `useLocationHash` use
`useSyncExternalStore` instead of effect + state.

**`useTerminalSession`** — the largest extraction. The 130-line
xterm/WebSocket/ResizeObserver setup block moves from
`terminal-view.tsx` into its own hook, which now owns the `terminalRef`,
`fitRef`, and `socketRef` that previously cluttered the component.
`TerminalConnectionError` and `ConnectionStatus` types are exported from
the hook.

**`useFloatingTooltipMeasurements`** — extracts the `useLayoutEffect` +
ResizeObserver + window resize listener out of `FloatingTooltip`. The
`FloatingTooltipSize` type moves with it so consumers don't need to
import from the component.

**`useInstallSessionQuery` + `useInstallEffects`** — the install session
fetch moves from a component effect to SWR (`install-query.ts`). The
three remaining install effects (token URL scrubbing, GitHub error URL
scrubbing, health-poll restart) move into
`hooks/use-install-effects.ts`. The root-redirect effect is replaced
with a render-time `<Navigate>` gate. The `SessionState` discriminant
now carries `token` so stale query results can be discarded without an
effect chain.

**`SelectionCheckbox`** — `useEffect` setting `input.indeterminate` is
replaced with a ref callback, which runs synchronously after the node is
attached and avoids a stale-frame flash.

**`event-debug.tsx`** — the manual `window.addEventListener("keydown",
...)` pattern is replaced with `useWindowEvent`, removing the
`react-doctor-disable` suppression comments.

**`run-waterfall.tsx`** — the local `useTickingNow` is deleted;
`RunWaterfall` now calls the shared `useTickingNow` from `lib/time` with
the new `active` parameter signature.

**`toast.test.tsx`** — `useEffect(() => onReady?.(api), ...)` in the
test helper is replaced with a direct call during render, which is valid
because `onReady` has no side effects that React cares about.

**`AskFabroSidebar`** — `setIsResizing` from the layout context is
replaced with an `onResizeActiveChange` prop, removing the
`useAskFabroLayout` call and the hidden context coupling from the
sidebar.


### Fabro Details

<details>
<summary>Ran 3 stages in 114m 5s for $95.71</summary>

| Stage | Duration | Cost | Retries |
|---|---|---|---|
| start | 0s | – | 0 |
| work | 103m 3s | $80.42 | 0 |
| audit | 10m 19s | $15.29 | 0 |
| **Total** | **114m 5s** | **$95.71** | **0** |

</details>

<details>
<summary>Ran <code>Goal.fabro</code> (4 nodes and 5 edges)</summary>

```dot
digraph Goal {
    graph [
        goal="Complete the user-provided goal",
        rankdir=LR,
        max_node_visits=30
    ]

    start [shape=Mdiamond, label="Start"]
    exit  [shape=Msquare, label="Exit"]

    work [
        label="Work",
        thread_id="goal",
        fidelity="full",
        max_visits=12,
        model="gpt-55",
        reasoning_effort="xhigh",
        prompt="@prompts/continue.md"
    ]

    audit [
        label="Completion Audit",
        thread_id="goal",
        fidelity="full",
        goal_gate=true,
        retry_target="work",
        output_schema="routing",
        output_retries=2,
        max_visits=12,
        model="gpt-55",
        reasoning_effort="xhigh",
        prompt="@prompts/audit.md"
    ]

    start -> work -> audit

    audit -> exit [label="Done", condition="outcome=succeeded"]
    audit -> work [label="Continue", condition="outcome=failed || preferred_label=Continue"]
    audit -> work [label="No clear verdict"]
}

```

</details>

⚒️ Generated with [Fabro](https://fabro.sh)

---------

Co-authored-by: Fabro <noreply@fabro.sh>
Co-authored-by: Bryan Helmkamp <bryan@brynary.com>
2026-05-27 10:37:29 -04:00

223 lines
7.8 KiB
TypeScript

import { useCallback, useMemo } from "react";
import { Navigate, useParams, useSearchParams } from "react-router";
import { ArrowPathIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
import type { ListRunsSortEnum } from "@qltysh/fabro-api-client";
import { EmptyState, ErrorState, LoadingState } from "../components/state";
import { ColumnPickerButton } from "../components/runs-list/column-picker-button";
import {
childRunsListPreferencesFromSearchParams,
childRunsListPreferencesToSearchParams,
hiddenColumnsFromSearchParams,
parseDirection,
parsePage,
parsePageSize,
parseSort,
persistChildRunsListPreferences,
resolveChildRunsListSearchParams,
} from "../components/runs-list/preferences";
import type { ChildRunsListPreferences } from "../components/runs-list/preferences";
import { RunsListView } from "../components/runs-list/runs-list-view";
import { serializeHiddenColumns } from "../components/runs-list/toggleable-column";
import type { ToggleableColumn } from "../components/runs-list/toggleable-column";
import { SECONDARY_BUTTON_CLASS } from "../components/ui";
import { ApiError } from "../lib/api-client";
import { formatRelativeTime } from "../lib/format";
import { useRun, useRunsPage } from "../lib/queries";
import { useTickingNow } from "../lib/time";
import { useDataUpdatedAt } from "../hooks/use-data-updated-at";
export const handle = { wide: true, hideSteerBar: true };
export default function RunChildren() {
const { id } = useParams();
const runQuery = useRun(id);
const [urlSearchParams, setSearchParams] = useSearchParams();
const searchParams = useMemo(
() => resolveChildRunsListSearchParams(urlSearchParams),
[urlSearchParams],
);
const hydratedSearch =
searchParams === urlSearchParams ? null : `?${searchParams.toString()}`;
const query = searchParams.get("search") ?? "";
const sort = parseSort(searchParams.get("sort"));
const direction = parseDirection(searchParams.get("direction"));
const page = parsePage(searchParams.get("page"));
const pageSize = parsePageSize(searchParams.get("size"));
const hiddenColumns = useMemo(
() => hiddenColumnsFromSearchParams(searchParams),
[searchParams],
);
const updatePreferences = useCallback(
(updater: (prev: ChildRunsListPreferences) => ChildRunsListPreferences) => {
setSearchParams(
(prevParams) => {
const next = updater(childRunsListPreferencesFromSearchParams(prevParams));
persistChildRunsListPreferences(next);
return childRunsListPreferencesToSearchParams(next);
},
{ replace: true },
);
},
[setSearchParams],
);
const setQuery = (value: string) =>
updatePreferences((prev) => ({ ...prev, search: value }));
const setPage = useCallback(
(next: number) => updatePreferences((prev) => ({ ...prev, page: next })),
[updatePreferences],
);
const setPageSize = useCallback(
(next: number) => updatePreferences((prev) => ({ ...prev, size: next, page: 1 })),
[updatePreferences],
);
const setHiddenColumns = useCallback(
(next: Set<ToggleableColumn>) =>
updatePreferences((prev) => ({ ...prev, hide: serializeHiddenColumns(next) ?? "" })),
[updatePreferences],
);
const handleSortClick = useCallback(
(key: ListRunsSortEnum) =>
updatePreferences((prev) =>
prev.sort === key
? { ...prev, direction: prev.direction === "asc" ? "desc" : "asc", page: 1 }
: { ...prev, sort: key, direction: "desc", page: 1 },
),
[updatePreferences],
);
const childRunsQuery = useRunsPage(
{
parentId: id,
includeArchived: false,
sort,
direction,
limit: pageSize,
offset: (page - 1) * pageSize,
},
id != null,
);
const now = useTickingNow(true, 15_000);
const updatedAt = useDataUpdatedAt(childRunsQuery.data);
const handleRefresh = useCallback(() => {
void childRunsQuery.mutate();
void runQuery.mutate();
}, [childRunsQuery, runQuery]);
const searchHydration = hydratedSearch
? <Navigate to={{ search: hydratedSearch }} replace />
: null;
if (childRunsQuery.isLoading && !childRunsQuery.data) {
return (
<>
{searchHydration}
<LoadingState label="Loading child runs…" />
</>
);
}
const apiError =
childRunsQuery.error instanceof ApiError ? childRunsQuery.error : null;
if (apiError && !childRunsQuery.data) {
return (
<>
{searchHydration}
<ErrorState
title="Couldn't load child runs"
description={`Server returned ${apiError.status}.`}
onRetry={handleRefresh}
/>
</>
);
}
const lowerQuery = query.toLowerCase();
return (
<>
{searchHydration}
<div className="space-y-4">
<div className="flex flex-wrap items-center gap-2">
<div className="relative w-64">
<MagnifyingGlassIcon className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-fg-muted" />
<input
type="text"
name="search"
aria-label="Search child runs"
placeholder="Search child runs…"
value={query}
onChange={(e) => setQuery(e.target.value)}
className="w-full rounded-md border border-line bg-panel/80 py-2 pl-9 pr-3 text-sm text-fg-2 placeholder-fg-muted outline-none transition-colors focus:border-focus focus:ring-0"
/>
</div>
<div className="ml-auto flex items-center gap-3">
{updatedAt != null ? (
<span className="font-mono text-xs text-fg-muted">
Updated{" "}
{formatRelativeTime(new Date(updatedAt).toISOString(), now)}
</span>
) : null}
<button
type="button"
onClick={handleRefresh}
disabled={childRunsQuery.isValidating}
aria-label={
childRunsQuery.isValidating
? "Refreshing child runs"
: "Refresh child runs"
}
title="Refresh"
className="inline-flex size-9 items-center justify-center rounded-md border border-line bg-panel/80 text-fg-3 transition-colors hover:bg-panel hover:text-fg disabled:cursor-default disabled:opacity-60 disabled:hover:bg-panel/80 disabled:hover:text-fg-3"
>
<ArrowPathIcon
className={`size-4 ${childRunsQuery.isValidating ? "animate-spin [animation-duration:450ms]" : ""}`}
aria-hidden="true"
/>
</button>
<ColumnPickerButton hidden={hiddenColumns} onChange={setHiddenColumns} />
</div>
</div>
<RunsListView
data={childRunsQuery.data ?? undefined}
isLoading={childRunsQuery.data == null && childRunsQuery.isLoading}
emptyState={
<EmptyState
title="No child runs"
description="When you launch another run with this run as its parent, it will appear here."
action={
<a
href="https://docs.fabro.sh/execution/child-runs"
target="_blank"
rel="noopener noreferrer"
className={SECONDARY_BUTTON_CLASS}
>
Learn about child runs
</a>
}
/>
}
sort={sort}
direction={direction}
page={page}
pageSize={pageSize}
hiddenColumns={hiddenColumns}
onSortClick={handleSortClick}
onPageChange={setPage}
onPageSizeChange={setPageSize}
query={lowerQuery}
repoFilter="all"
workflowFilter="all"
createdCutoffMs={null}
/>
</div>
</>
);
}