mirror of
https://github.com/Sea-Haven-Industries/seahaven-ap.git
synced 2026-10-07 16:18:53 +00:00
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.
72 lines
1.7 KiB
TypeScript
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.
|
|
}
|
|
}
|