fix(site/src): correct useMemo dependencies (#30567)
Prepares for enabling oxlint's `exhaustive-deps` rule (4/6). Stacked on #30566. Fixes two `useMemo` calls outside React Compiler directories, where the reports reflect real behavior: - `CreateWorkspacePage`: `presets` falls back to a module-level `readonly` empty array instead of a new `[]`. While presets load, the view's `[presets]` effects no longer rerun on every parent render. `presets` is typed `readonly` through `CreateWorkspacePageView`, so the shared fallback can't be mutated. Every use only reads it, and loaded query data is already shared across consumers. - `DeploymentBannerView`: `lastAggregated` listed `timeUntilRefresh` as a dep only to recompute on each countdown tick. Each tick already re-renders, so the value now comes from a small `formatLastAggregated` helper with no memo. Adds a `DeploymentBannerView` test with fake timers that the relative "last aggregated" text advances as the countdown ticks. <details> <summary>Stack</summary> 1. #30564 Formik setters 2. #30565 `useEffectEvent` in AgentsPage 3. #30566 TemplateBuilder fallback arrays 4. **This PR**: `useMemo` dependencies 5. #30568 Missing effect dependencies 6. #30569 Enable `exhaustive-deps` </details> _Generated by Coder Agents on behalf of @jeremyruppel._
This commit is contained in:
1 parent
dfeea82325
commit
8797ca50e4
4 files changed
+46
-12
No files matched your search
@@ -41,6 +41,33 @@ describe("groupSessionApps", () => {
|
||||
});
|
||||
|
||||
describe("DeploymentBannerView", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("updates the last aggregated time as the refresh countdown ticks", () => {
|
||||
vi.useFakeTimers();
|
||||
const now = new Date("2023-03-06T19:13:35.000Z");
|
||||
vi.setSystemTime(now);
|
||||
const stats = {
|
||||
...MockDeploymentStats,
|
||||
collected_at: new Date(now.getTime() - 40_000).toISOString(),
|
||||
next_update_at: new Date(now.getTime() + 60_000).toISOString(),
|
||||
};
|
||||
render(<DeploymentBannerView stats={stats} fetchStats={vi.fn()} />);
|
||||
|
||||
expect(
|
||||
screen.getByRole("button", { name: "a few seconds ago" }),
|
||||
).toBeInTheDocument();
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(10_000);
|
||||
});
|
||||
|
||||
expect(
|
||||
screen.getByRole("button", { name: "a minute ago" }),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
it("exposes stat tooltips to keyboard and screen reader users", async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<DeploymentBannerView stats={MockDeploymentStats} />);
|
||||
|
||||
@@ -89,16 +89,7 @@ export const DeploymentBannerView: React.FC<DeploymentBannerViewProps> = ({
|
||||
};
|
||||
}, [fetchStats, stats]);
|
||||
|
||||
const lastAggregated = useMemo(() => {
|
||||
if (!stats) {
|
||||
return;
|
||||
}
|
||||
if (!fetchStats) {
|
||||
// Storybook!
|
||||
return "just now";
|
||||
}
|
||||
return dayjs().to(dayjs(stats.collected_at));
|
||||
}, [timeUntilRefresh, stats, fetchStats]);
|
||||
const lastAggregated = formatLastAggregated(stats, Boolean(fetchStats));
|
||||
|
||||
const healthErrors = health ? getHealthErrors(health) : [];
|
||||
const displayLatency = stats?.workspaces.connection_latency_ms.P50 || -1;
|
||||
@@ -522,6 +513,19 @@ const HealthIssue: React.FC<React.PropsWithChildren> = ({ children }) => {
|
||||
);
|
||||
};
|
||||
|
||||
const formatLastAggregated = (
|
||||
stats: DeploymentStats | undefined,
|
||||
isLive: boolean,
|
||||
): string | undefined => {
|
||||
if (!stats) {
|
||||
return undefined;
|
||||
}
|
||||
if (!isLive) {
|
||||
return "just now";
|
||||
}
|
||||
return dayjs().to(dayjs(stats.collected_at));
|
||||
};
|
||||
|
||||
const getHealthErrors = (health: HealthcheckReport) => {
|
||||
const warnings: string[] = [];
|
||||
const sections = [
|
||||
|
||||
@@ -21,6 +21,7 @@ import type {
|
||||
DynamicParametersRequest,
|
||||
DynamicParametersResponse,
|
||||
MinimalUser,
|
||||
Preset,
|
||||
Workspace,
|
||||
} from "#/api/typesGenerated";
|
||||
import { ErrorAlert } from "#/components/Alert/ErrorAlert";
|
||||
@@ -42,6 +43,8 @@ import {
|
||||
const createWorkspaceModes = ["form", "auto", "duplicate"] as const;
|
||||
export type CreateWorkspaceMode = (typeof createWorkspaceModes)[number];
|
||||
|
||||
const emptyPresets: readonly Preset[] = [];
|
||||
|
||||
const CreateWorkspacePage: React.FC = () => {
|
||||
const { organization: organizationName = "default", template: templateName } =
|
||||
useParams() as { organization?: string; template: string };
|
||||
@@ -112,7 +115,7 @@ const CreateWorkspacePage: React.FC = () => {
|
||||
|
||||
const effectivePresetName = mode === "duplicate" ? undefined : presetName;
|
||||
|
||||
const presets = templateVersionPresetsQuery.data ?? [];
|
||||
const presets = templateVersionPresetsQuery.data ?? emptyPresets;
|
||||
|
||||
const urlPresetResult = useMemo(() => {
|
||||
if (!effectivePresetName) return { preset: undefined, error: undefined };
|
||||
|
||||
@@ -62,7 +62,7 @@ type CreateWorkspacePageViewProps = {
|
||||
mode: CreateWorkspaceMode;
|
||||
parameters: PreviewParameter[];
|
||||
permissions: CreateWorkspacePermissions;
|
||||
presets: TypesGen.Preset[];
|
||||
presets: readonly TypesGen.Preset[];
|
||||
template: TypesGen.Template;
|
||||
urlPreset?: TypesGen.Preset;
|
||||
urlPresetError?: string;
|
||||
|
||||
Reference in new issue
Block a user