Bin
2025-12-17 611bfe34c3c96199eaaf6cf9e41a75892e44e879
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const STORAGE_KEY_PREFIX = "ls:visited-projects";
const MAX_PROJECTS = 10;
 
function getStorageKey(userId: number): string {
  return `${STORAGE_KEY_PREFIX}:${userId}`;
}
 
/**
 * Get the list of recently visited project IDs from localStorage for a specific user.
 * Returns an array of project IDs sorted by most recently visited first.
 */
export function getVisitedProjectIds(userId?: number): number[] {
  if (!userId) return [];
 
  try {
    const stored = localStorage.getItem(getStorageKey(userId));
    if (!stored) return [];
 
    const parsed = JSON.parse(stored);
    if (!Array.isArray(parsed)) return [];
 
    return parsed.filter((id): id is number => typeof id === "number");
  } catch {
    return [];
  }
}
 
/**
 * Add a project ID to the visited projects list for a specific user.
 * If the project already exists, it moves to the front.
 * The list is capped at MAX_PROJECTS (10).
 */
export function addVisitedProject(projectId: number, userId?: number): void {
  if (!userId) return;
 
  try {
    const current = getVisitedProjectIds(userId);
    const filtered = current.filter((id) => id !== projectId);
    const updated = [projectId, ...filtered].slice(0, MAX_PROJECTS);
 
    localStorage.setItem(getStorageKey(userId), JSON.stringify(updated));
  } catch {
    // Silently fail if localStorage is unavailable
  }
}