File size: 1,692 Bytes
d0878a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Persist starter greeting + Getting Started chips per user/goal. */

const SUGGESTIONS_PREFIX = 'csa.starterSuggestions.v1:';
const GREETING_PREFIX = 'csa.starterGreeting.v1:';

function storage() {
  try {
    return window.sessionStorage;
  } catch {
    return null;
  }
}

function localStore() {
  try {
    return window.localStorage;
  } catch {
    return null;
  }
}

function readJson(store, key) {
  if (!store) return null;
  try {
    const raw = store.getItem(key);
    if (!raw) return null;
    return JSON.parse(raw);
  } catch {
    return null;
  }
}

function writeJson(store, key, value) {
  if (!store) return;
  try {
    store.setItem(key, JSON.stringify(value));
  } catch {
    /* quota / private mode */
  }
}

export function loadStarterSuggestions(cacheKey) {
  if (!cacheKey) return null;
  const key = SUGGESTIONS_PREFIX + cacheKey;
  return readJson(storage(), key) || readJson(localStore(), key);
}

export function saveStarterSuggestions(cacheKey, payload) {
  if (!cacheKey) return;
  const key = SUGGESTIONS_PREFIX + cacheKey;
  const body = { ...payload, ts: Date.now() };
  writeJson(storage(), key, body);
  writeJson(localStore(), key, body);
}

export function loadStarterGreeting(cacheKey) {
  if (!cacheKey) return null;
  const key = GREETING_PREFIX + cacheKey;
  return readJson(storage(), key) || readJson(localStore(), key);
}

export function saveStarterGreeting(cacheKey, payload) {
  if (!cacheKey) return;
  const key = GREETING_PREFIX + cacheKey;
  const body = { ...payload, ts: Date.now() };
  writeJson(storage(), key, body);
  writeJson(localStore(), key, body);
}