seahaven-ap/src/mocks/session.ts
Adam Moussa 532c0f7f85
feat(frontend): add invoice detail 3-pane shell (AP-6)
Prove invoice coding and collaboration layout on mocks with a fixture PDF viewer, editable header/lines, Comments/Activity feeds, and role-aware action stubs via mock session switching.
2026-08-10 18:10:41 -04:00

72 lines
1.7 KiB
TypeScript

import { getMockUser, type MockUser } from "@/mocks/users";
export const DEFAULT_MOCK_USER_ID = "user-1";
const SESSION_STORAGE_KEY = "seahaven-ap.mockUserId";
let currentUserId = DEFAULT_MOCK_USER_ID;
function readStoredUserId(): string | null {
if (typeof sessionStorage === "undefined") {
return null;
}
try {
return sessionStorage.getItem(SESSION_STORAGE_KEY);
} catch {
return null;
}
}
function writeStoredUserId(userId: string): void {
if (typeof sessionStorage === "undefined") {
return;
}
try {
sessionStorage.setItem(SESSION_STORAGE_KEY, userId);
} catch {
// Ignore quota / private-mode failures in POC.
}
}
export function getMockSessionUserId(): string {
const stored = readStoredUserId();
if (stored && getMockUser(stored)) {
currentUserId = stored;
return stored;
}
return currentUserId;
}
export function getMockSessionUser(): MockUser {
const userId = getMockSessionUserId();
const user = getMockUser(userId);
if (user) {
return user;
}
const fallback = getMockUser(DEFAULT_MOCK_USER_ID);
if (!fallback) {
throw new Error("Default mock user is missing from the seed store");
}
return fallback;
}
export function setMockSessionUserId(userId: string): MockUser {
const user = getMockUser(userId);
if (!user) {
throw new Error(`Mock user not found: ${userId}`);
}
currentUserId = userId;
writeStoredUserId(userId);
return { ...user };
}
export function resetMockSession(): void {
currentUserId = DEFAULT_MOCK_USER_ID;
if (typeof sessionStorage === "undefined") {
return;
}
try {
sessionStorage.removeItem(SESSION_STORAGE_KEY);
} catch {
// Ignore storage failures in POC.
}
}