Skip to content

Commit cdc233e

Browse files
authored
Merge branch 'main' into plat479-1a-apikey-scope-aliases
2 parents d9753b5 + c029869 commit cdc233e

17 files changed

Lines changed: 103 additions & 181 deletions

File tree

‎.claude/docs/FRONTEND_PATTERNS.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function actually exercises the interaction. Jest/RTL tests are for pure logic
3030
- When a component depends on the current time or date, accept it as a prop or
3131
via context instead of reading `new Date()` or `Date.now()` internally, so
3232
stories render deterministically without mocking globals.
33+
- `renderHook` suites for stateful UI hooks are interaction tests, not pure
34+
logic. Cover that behavior through the story of the component that uses the
35+
hook.
3336

3437
**Incorrect (interaction test in Jest/RTL):**
3538

@@ -89,6 +92,8 @@ const config: ChatModel = parseConfig(data);
8992
of existing ones.
9093
- Use existing wrapped primitives (Combobox, dialogs, tables) instead of
9194
hand-assembling the underlying pieces they already wrap.
95+
- Do not introduce a new React hook when an existing hook, a plain function,
96+
or component state can express the logic.
9297
- Delete dead code and unreachable branches instead of carrying them along.
9398
- Keep the PR scoped to one change. Move unrelated cleanups, renames, and
9499
drive-by refactors to separate PRs.
@@ -209,6 +214,9 @@ Decide where logic goes before reaching for `useEffect`:
209214
is readable on its own. Share the entity fixture, not a pre-wired query
210215
object.
211216
- Query keys in mocks follow FE7: import the constant.
217+
- Never replace browser globals with `Object.defineProperty` in tests or
218+
stories. Use `vi.stubGlobal` in unit tests and `spyOn` from
219+
`storybook/test` in stories.
212220

213221
## FE10: Tests assert observable behavior
214222

‎.claude/skills/frontend-review/SKILL.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ before they see the PR.
3838
user-visible behavior? Then a changed or added `.stories.tsx` must exist,
3939
and its `play` function must perform the new interaction (open the menu,
4040
submit the form), not merely render. Interaction tests added to `.test.tsx`
41-
files are a FAIL unless they cover pure logic.
41+
files are a FAIL unless they cover pure logic; `renderHook` suites for
42+
stateful UI hooks count as interaction tests and belong in the consuming
43+
component's story.
4244
- **FE2 (types)**: Search the diff for `any`, `as unknown as`, non-null
4345
assertions in any form (`x!.y`, `items[0]!`, `fn()!`, `value! as T`), and
4446
new `as` casts. Check that API data uses types from `api/typesGenerated.ts`.
4547
- **FE3 (reuse/scope)**: For each new component, hook, or helper, search
4648
`site/src/components/` and sibling folders for an existing equivalent.
4749
Flag near-duplicates, hand-assembled versions of wrapped primitives, dead
48-
branches, and unrelated changes bundled into the diff.
50+
branches, and unrelated changes bundled into the diff. Flag new React hooks
51+
that an existing hook, a plain function, or component state could replace;
52+
several new single-use hooks in one diff is a FAIL.
4953
- **FE4 (comments)**: Read every comment line the diff adds or edits. Flag
5054
any comment that restates the identifier, assertion, or control flow.
5155
Verify surviving comments are factually correct.
@@ -68,7 +72,10 @@ before they see the PR.
6872
reads.
6973
- **FE9 (fixtures)**: Flag inline entity literals that duplicate or deviate
7074
from `Mock*` fixtures in `site/src/testHelpers/`, and shared pre-wired
71-
query objects instead of per-story inline `{ key, data }` wiring.
75+
query objects instead of per-story inline `{ key, data }` wiring. Flag any
76+
`Object.defineProperty` replacement of a browser global in tests or
77+
stories: unit tests stub with `vi.stubGlobal`, stories mock existing
78+
globals with `spyOn` from `storybook/test`.
7279
- **FE10 (test queries)**: Flag `querySelector`, class-name substring
7380
matches, geometry assertions, `behavior: "smooth"` dependence, and
7481
locale-less `toLocaleString()` in changed tests and stories.

‎coderd/debug_test.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,12 @@ func TestHealthSettings(t *testing.T) {
272272
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
273273
defer cancel()
274274

275-
// given
276-
adminClient := coderdtest.New(t, nil)
275+
adminClient := coderdtest.New(t, &coderdtest.Options{
276+
// Mocked to avoid depending on DERP probe timing.
277+
HealthcheckFunc: func(context.Context, string, *healthcheck.Progress) *healthsdk.HealthcheckReport {
278+
return &healthsdk.HealthcheckReport{Time: time.Now()}
279+
},
280+
})
277281
_ = coderdtest.CreateFirstUser(t, adminClient)
278282

279283
expected := healthsdk.HealthSettings{
@@ -307,8 +311,12 @@ func TestHealthSettings(t *testing.T) {
307311
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
308312
defer cancel()
309313

310-
// given
311-
adminClient := coderdtest.New(t, nil)
314+
adminClient := coderdtest.New(t, &coderdtest.Options{
315+
// Mocked to avoid depending on DERP probe timing.
316+
HealthcheckFunc: func(context.Context, string, *healthcheck.Progress) *healthsdk.HealthcheckReport {
317+
return &healthsdk.HealthcheckReport{Time: time.Now()}
318+
},
319+
})
312320
_ = coderdtest.CreateFirstUser(t, adminClient)
313321

314322
initial := healthsdk.HealthSettings{

‎site/src/components/TableEmpty/TableEmpty.stories.tsx‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type Story = StoryObj<typeof TableEmpty>;
2525

2626
export const Example: Story = {};
2727

28-
export const WithImageAndCta: Story = {
29-
name: "With Image and CTA",
28+
export const WithCta: Story = {
29+
name: "With CTA",
3030
args: {
3131
description: "A gruff voice crackles to life on the intercom.",
3232
cta: (
@@ -35,13 +35,5 @@ export const WithImageAndCta: Story = {
3535
code="say &ldquo;Actually, it's the BBC controlling us from London&rdquo;"
3636
/>
3737
),
38-
image: (
39-
<img
40-
src="/featured/templates.webp"
41-
alt=""
42-
className="max-w-3xl h-[320px] overflow-hidden object-cover object-top"
43-
/>
44-
),
45-
style: { paddingBottom: 0 },
4638
},
4739
};

‎site/src/hooks/useIsBelowLgViewport.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎site/src/hooks/useIsBelowMdViewport.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎site/src/hooks/useMediaQuery.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useCallback, useSyncExternalStore } from "react";
2+
3+
/**
4+
* Subscribes to a CSS media query and returns whether it currently
5+
* matches, re-rendering on change. Pass a shared query constant from
6+
* `utils/mobile.ts` so breakpoints stay aligned with Tailwind
7+
* utilities.
8+
*/
9+
export const useMediaQuery = (query: string): boolean => {
10+
const subscribe = useCallback(
11+
(onStoreChange: () => void) => {
12+
const mediaQuery = window.matchMedia(query);
13+
mediaQuery.addEventListener("change", onStoreChange);
14+
return () => mediaQuery.removeEventListener("change", onStoreChange);
15+
},
16+
[query],
17+
);
18+
return useSyncExternalStore(
19+
subscribe,
20+
() => window.matchMedia(query).matches,
21+
);
22+
};

‎site/src/pages/AgentsPage/AgentChatPage.stories.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,13 @@ export const NarrowingSuppressesExpandedPanel: Story = {
19341934
expect(
19351935
canvas.queryByRole("tab", { name: "Summary" }),
19361936
).not.toBeInTheDocument();
1937+
1938+
// Widening again restores the persisted panel, still expanded.
1939+
narrowingMedia?.setMatches(belowLgViewportMediaQuery, false);
1940+
await waitFor(() => {
1941+
expect(canvas.getByRole("tab", { name: "Summary" })).toBeVisible();
1942+
});
1943+
expect(messagesRegion.checkVisibility()).toBe(false);
19371944
},
19381945
};
19391946

‎site/src/pages/AgentsPage/AgentChatPage.test.ts‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
MockWorkspaceAgent,
1919
MockWorkspaceApp,
2020
} from "#/testHelpers/entities";
21-
import { setupMatchMedia } from "#/testHelpers/matchMedia";
2221
import {
2322
buildInactiveChatQueueReconciliation,
2423
draftInputStorageKeyPrefix,
@@ -32,7 +31,6 @@ import {
3231
settlePromotedQueueHead,
3332
submitEdit,
3433
useConversationEditingState,
35-
useRightPanelNarrowSuppression,
3634
waitForPendingChatSettingsSyncs,
3735
} from "./AgentChatPage";
3836
import type { ChatMessageInputRef } from "./components/AgentChatInput";
@@ -1407,52 +1405,3 @@ describe("isChatAgentBindingUnresolved", () => {
14071405
);
14081406
});
14091407
});
1410-
1411-
describe("useRightPanelNarrowSuppression", () => {
1412-
const belowLgQuery = "(max-width: 1023px)";
1413-
1414-
const setupBelowLg = (initialBelowLg: boolean) => {
1415-
const media = setupMatchMedia({ [belowLgQuery]: initialBelowLg });
1416-
return {
1417-
setBelowLg: (value: boolean) => media.setMatches(belowLgQuery, value),
1418-
};
1419-
};
1420-
1421-
it("suppresses the panel when mounted below the lg breakpoint", () => {
1422-
setupBelowLg(true);
1423-
const { result } = renderHook(() => useRightPanelNarrowSuppression());
1424-
expect(result.current.suppressed).toBe(true);
1425-
});
1426-
1427-
it("does not suppress the panel when mounted at or above lg", () => {
1428-
setupBelowLg(false);
1429-
const { result } = renderHook(() => useRightPanelNarrowSuppression());
1430-
expect(result.current.suppressed).toBe(false);
1431-
});
1432-
1433-
it("suppresses on narrowing and clears on widening", () => {
1434-
const media = setupBelowLg(false);
1435-
const { result } = renderHook(() => useRightPanelNarrowSuppression());
1436-
1437-
act(() => media.setBelowLg(true));
1438-
expect(result.current.suppressed).toBe(true);
1439-
1440-
act(() => media.setBelowLg(false));
1441-
expect(result.current.suppressed).toBe(false);
1442-
});
1443-
1444-
it("stays cleared after an explicit clearSuppression until the next narrowing", () => {
1445-
const media = setupBelowLg(false);
1446-
const { result } = renderHook(() => useRightPanelNarrowSuppression());
1447-
1448-
act(() => media.setBelowLg(true));
1449-
expect(result.current.suppressed).toBe(true);
1450-
1451-
act(() => result.current.clearSuppression());
1452-
expect(result.current.suppressed).toBe(false);
1453-
1454-
act(() => media.setBelowLg(false));
1455-
act(() => media.setBelowLg(true));
1456-
expect(result.current.suppressed).toBe(true);
1457-
});
1458-
});

‎site/src/pages/AgentsPage/AgentChatPage.tsx‎

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ import type { ChatMessagePart } from "#/api/typesGenerated";
6565
import { useProxy } from "#/contexts/ProxyContext";
6666
import { useAuthenticated } from "#/hooks/useAuthenticated";
6767
import { useAIGatewayEnabled } from "#/hooks/useEmbeddedMetadata";
68-
import { useIsBelowLgViewport } from "#/hooks/useIsBelowLgViewport";
68+
import { useMediaQuery } from "#/hooks/useMediaQuery";
6969
import {
7070
getDefaultOrganizationName,
7171
useDashboard,
7272
} from "#/modules/dashboard/useDashboard";
73-
import { isMobileViewport } from "#/utils/mobile";
73+
import { belowLgViewportMediaQuery, isMobileViewport } from "#/utils/mobile";
7474
import { pageTitle } from "#/utils/page";
7575
import { rewriteLocalhostURL } from "#/utils/portForward";
7676
import { createReconnectingWebSocket } from "#/utils/reconnectingWebSocket";
@@ -134,29 +134,6 @@ import {
134134
/** localStorage key controlling whether the right panel is visible. */
135135
export const RIGHT_PANEL_OPEN_KEY = "agents.right-panel-open";
136136

137-
/**
138-
* Below the `lg` breakpoint, chat and the right panel are mutually
139-
* exclusive, so a panel left open on a wide window would hide chat as
140-
* soon as the window narrows. This suppresses the panel while narrow
141-
* without touching the persisted preference: widening restores the
142-
* panel, and an explicit user action (clearSuppression) overrides it.
143-
*/
144-
export function useRightPanelNarrowSuppression(): {
145-
suppressed: boolean;
146-
clearSuppression: () => void;
147-
} {
148-
const isBelowLg = useIsBelowLgViewport();
149-
const [suppressed, setSuppressed] = useState(isBelowLg);
150-
const [prevIsBelowLg, setPrevIsBelowLg] = useState(isBelowLg);
151-
// Render-time state adjustment on breakpoint crossings; see
152-
// https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
153-
if (isBelowLg !== prevIsBelowLg) {
154-
setPrevIsBelowLg(isBelowLg);
155-
setSuppressed(isBelowLg);
156-
}
157-
return { suppressed, clearSuppression: () => setSuppressed(false) };
158-
}
159-
160137
const lastModelConfigIDStorageKey = "agents.last-model-config-id";
161138

162139
const AGENT_BINDING_REPAIR_POLL_MS = 30_000;
@@ -886,15 +863,28 @@ const AgentChatPage: FC = () => {
886863
const [sidebarPanelPreference, setSidebarPanelPreference] = useState(() => {
887864
return localStorage.getItem(RIGHT_PANEL_OPEN_KEY) === "true";
888865
});
889-
const { suppressed: panelSuppressedOnNarrow, clearSuppression } =
890-
useRightPanelNarrowSuppression();
866+
// Below the lg breakpoint, chat and the right panel are mutually
867+
// exclusive, so a panel left open on a wide window would hide chat
868+
// as soon as the window narrows. Suppression hides the panel while
869+
// narrow without touching the persisted preference: widening
870+
// restores the panel, and an explicit toggle overrides it.
871+
const isBelowLg = useMediaQuery(belowLgViewportMediaQuery);
872+
const [panelSuppressedOnNarrow, setPanelSuppressedOnNarrow] =
873+
useState(isBelowLg);
874+
const [prevIsBelowLg, setPrevIsBelowLg] = useState(isBelowLg);
875+
// Render-time state adjustment on breakpoint crossings; see
876+
// https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
877+
if (isBelowLg !== prevIsBelowLg) {
878+
setPrevIsBelowLg(isBelowLg);
879+
setPanelSuppressedOnNarrow(isBelowLg);
880+
}
891881
// Canonical panel visibility: the persisted preference gated by the
892882
// narrow-viewport suppression. Only this derived value may be
893883
// rendered or handed to children; the raw preference stays local.
894884
const showSidebarPanel = sidebarPanelPreference && !panelSuppressedOnNarrow;
895885

896886
const handleSetShowSidebarPanel = (next: boolean) => {
897-
clearSuppression();
887+
setPanelSuppressedOnNarrow(false);
898888
setSidebarPanelPreference(next);
899889
localStorage.setItem(RIGHT_PANEL_OPEN_KEY, String(next));
900890
};

0 commit comments

Comments
 (0)