Skip to content

Commit 83a42a1

Browse files
committed
refactor: serialize and de-serialize timestamp
1 parent 8fb7b79 commit 83a42a1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

frontend/src/lib/stores/recent_quibs.svelte.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@ interface IRecentQuib extends Quib {
1919

2020
const stored_recent_quibs = browser ? localStorage.getItem('recent_posts_store') : null;
2121
const parsed_stored_recent_quibs: IRecentQuib[] = stored_recent_quibs
22-
? JSON.parse(stored_recent_quibs)
22+
? // convert string to Date object
23+
(JSON.parse(stored_recent_quibs) as IRecentQuib[]).map((q) => ({
24+
...q,
25+
timestamp: new Date(q.timestamp)
26+
}))
2327
: [];
2428

2529
let recent_quibs_state = $state<IRecentQuib[]>(parsed_stored_recent_quibs);
2630

2731
function sync_to_localstorage() {
2832
if (browser) {
29-
localStorage.setItem('recent_posts_store', JSON.stringify(recent_quibs_state));
33+
// convert Date object to string
34+
localStorage.setItem(
35+
'recent_posts_store',
36+
JSON.stringify(
37+
recent_quibs_state.map((q) => ({ ...q, timestamp: q.timestamp.toISOString() }))
38+
)
39+
);
3040
}
3141
}
3242

0 commit comments

Comments
 (0)