Fix manual company switch route sync
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -22,6 +22,7 @@ import { useTheme } from "../context/ThemeContext";
|
|||||||
import { useKeyboardShortcuts } from "../hooks/useKeyboardShortcuts";
|
import { useKeyboardShortcuts } from "../hooks/useKeyboardShortcuts";
|
||||||
import { useCompanyPageMemory } from "../hooks/useCompanyPageMemory";
|
import { useCompanyPageMemory } from "../hooks/useCompanyPageMemory";
|
||||||
import { healthApi } from "../api/health";
|
import { healthApi } from "../api/health";
|
||||||
|
import { shouldSyncCompanySelectionFromRoute } from "../lib/company-selection";
|
||||||
import { queryKeys } from "../lib/queryKeys";
|
import { queryKeys } from "../lib/queryKeys";
|
||||||
import { cn } from "../lib/utils";
|
import { cn } from "../lib/utils";
|
||||||
import { NotFoundPage } from "../pages/NotFound";
|
import { NotFoundPage } from "../pages/NotFound";
|
||||||
@@ -36,6 +37,7 @@ export function Layout() {
|
|||||||
loading: companiesLoading,
|
loading: companiesLoading,
|
||||||
selectedCompany,
|
selectedCompany,
|
||||||
selectedCompanyId,
|
selectedCompanyId,
|
||||||
|
selectionSource,
|
||||||
setSelectedCompanyId,
|
setSelectedCompanyId,
|
||||||
} = useCompany();
|
} = useCompany();
|
||||||
const { theme, toggleTheme } = useTheme();
|
const { theme, toggleTheme } = useTheme();
|
||||||
@@ -88,7 +90,13 @@ export function Layout() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedCompanyId !== matchedCompany.id) {
|
if (
|
||||||
|
shouldSyncCompanySelectionFromRoute({
|
||||||
|
selectionSource,
|
||||||
|
selectedCompanyId,
|
||||||
|
routeCompanyId: matchedCompany.id,
|
||||||
|
})
|
||||||
|
) {
|
||||||
setSelectedCompanyId(matchedCompany.id, { source: "route_sync" });
|
setSelectedCompanyId(matchedCompany.id, { source: "route_sync" });
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
@@ -99,6 +107,7 @@ export function Layout() {
|
|||||||
location.pathname,
|
location.pathname,
|
||||||
location.search,
|
location.search,
|
||||||
navigate,
|
navigate,
|
||||||
|
selectionSource,
|
||||||
selectedCompanyId,
|
selectedCompanyId,
|
||||||
setSelectedCompanyId,
|
setSelectedCompanyId,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import type { Company } from "@paperclipai/shared";
|
|||||||
import { companiesApi } from "../api/companies";
|
import { companiesApi } from "../api/companies";
|
||||||
import { ApiError } from "../api/client";
|
import { ApiError } from "../api/client";
|
||||||
import { queryKeys } from "../lib/queryKeys";
|
import { queryKeys } from "../lib/queryKeys";
|
||||||
|
import type { CompanySelectionSource } from "../lib/company-selection";
|
||||||
type CompanySelectionSource = "manual" | "route_sync" | "bootstrap";
|
|
||||||
type CompanySelectionOptions = { source?: CompanySelectionSource };
|
type CompanySelectionOptions = { source?: CompanySelectionSource };
|
||||||
|
|
||||||
interface CompanyContextValue {
|
interface CompanyContextValue {
|
||||||
|
|||||||
34
ui/src/lib/company-selection.test.ts
Normal file
34
ui/src/lib/company-selection.test.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { shouldSyncCompanySelectionFromRoute } from "./company-selection";
|
||||||
|
|
||||||
|
describe("shouldSyncCompanySelectionFromRoute", () => {
|
||||||
|
it("does not resync when selection already matches the route", () => {
|
||||||
|
expect(
|
||||||
|
shouldSyncCompanySelectionFromRoute({
|
||||||
|
selectionSource: "route_sync",
|
||||||
|
selectedCompanyId: "pap",
|
||||||
|
routeCompanyId: "pap",
|
||||||
|
}),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("defers route sync while a manual company switch is in flight", () => {
|
||||||
|
expect(
|
||||||
|
shouldSyncCompanySelectionFromRoute({
|
||||||
|
selectionSource: "manual",
|
||||||
|
selectedCompanyId: "pap",
|
||||||
|
routeCompanyId: "ret",
|
||||||
|
}),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("syncs back to the route company for non-manual mismatches", () => {
|
||||||
|
expect(
|
||||||
|
shouldSyncCompanySelectionFromRoute({
|
||||||
|
selectionSource: "route_sync",
|
||||||
|
selectedCompanyId: "pap",
|
||||||
|
routeCompanyId: "ret",
|
||||||
|
}),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
18
ui/src/lib/company-selection.ts
Normal file
18
ui/src/lib/company-selection.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
export type CompanySelectionSource = "manual" | "route_sync" | "bootstrap";
|
||||||
|
|
||||||
|
export function shouldSyncCompanySelectionFromRoute(params: {
|
||||||
|
selectionSource: CompanySelectionSource;
|
||||||
|
selectedCompanyId: string | null;
|
||||||
|
routeCompanyId: string;
|
||||||
|
}): boolean {
|
||||||
|
const { selectionSource, selectedCompanyId, routeCompanyId } = params;
|
||||||
|
|
||||||
|
if (selectedCompanyId === routeCompanyId) return false;
|
||||||
|
|
||||||
|
// Let manual company switches finish their remembered-path navigation first.
|
||||||
|
if (selectionSource === "manual" && selectedCompanyId) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user