File size: 2,885 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * Mode Storage
 * Safe localStorage wrapper for mode persistence
 * Handles localStorage failures gracefully
 */

export type DocDoeMode = "tuition" | "study-chat" | "first-hour" | "learn-anything" | "exam-coach" | "home";

const STORAGE_KEYS = {
  MODE: "docdoe_selected_mode",
  LAST_QUERY: "docdoe_last_query",
  LAST_EXAM_DATE: "docdoe_last_exam_date",
  LAST_DURATION: "docdoe_last_duration",
} as const;

/**
 * Safely get an item from localStorage
 * Returns null if localStorage is unavailable or item doesn't exist
 */
export function getStorageItem<T>(key: string): T | null {
  try {
    if (typeof window === "undefined") {
      return null;
    }
    const item = window.localStorage.getItem(key);
    return item ? JSON.parse(item) : null;
  } catch {
    // Silently fail if localStorage is not available
    return null;
  }
}

/**
 * Safely set an item in localStorage
 * Returns false if localStorage is unavailable
 */
export function setStorageItem<T>(key: string, value: T): boolean {
  try {
    if (typeof window === "undefined") {
      return false;
    }
    window.localStorage.setItem(key, JSON.stringify(value));
    return true;
  } catch {
    // Silently fail if localStorage is not available
    return false;
  }
}

/**
 * Get the current selected mode
 */
export function getMode(): DocDoeMode | null {
  return getStorageItem<DocDoeMode>(STORAGE_KEYS.MODE);
}

/**
 * Set the current selected mode
 */
export function setMode(mode: DocDoeMode): boolean {
  return setStorageItem(STORAGE_KEYS.MODE, mode);
}

/**
 * Get the last query the user submitted
 */
export function getLastQuery(): string | null {
  return getStorageItem<string>(STORAGE_KEYS.LAST_QUERY);
}

/**
 * Set the last query the user submitted
 */
export function setLastQuery(query: string): boolean {
  return setStorageItem(STORAGE_KEYS.LAST_QUERY, query);
}

/**
 * Get the last exam date the user entered
 */
export function getLastExamDate(): string | null {
  return getStorageItem<string>(STORAGE_KEYS.LAST_EXAM_DATE);
}

/**
 * Set the last exam date the user entered
 */
export function setLastExamDate(date: string): boolean {
  return setStorageItem(STORAGE_KEYS.LAST_EXAM_DATE, date);
}

/**
 * Get the last duration the user selected
 */
export function getLastDuration(): number | null {
  return getStorageItem<number>(STORAGE_KEYS.LAST_DURATION);
}

/**
 * Set the last duration the user selected (in minutes)
 */
export function setLastDuration(duration: number): boolean {
  return setStorageItem(STORAGE_KEYS.LAST_DURATION, duration);
}

/**
 * Clear all mode-related storage
 */
export function clearModeStorage(): boolean {
  try {
    if (typeof window === "undefined") {
      return false;
    }
    Object.values(STORAGE_KEYS).forEach((key) => {
      window.localStorage.removeItem(key);
    });
    return true;
  } catch {
    return false;
  }
}