Skip to content

Commit

Permalink
refactor: serialize and de-serialize timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Jan 8, 2025
1 parent 8fb7b79 commit 83a42a1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions frontend/src/lib/stores/recent_quibs.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ interface IRecentQuib extends Quib {

const stored_recent_quibs = browser ? localStorage.getItem('recent_posts_store') : null;
const parsed_stored_recent_quibs: IRecentQuib[] = stored_recent_quibs
? JSON.parse(stored_recent_quibs)
? // convert string to Date object
(JSON.parse(stored_recent_quibs) as IRecentQuib[]).map((q) => ({
...q,
timestamp: new Date(q.timestamp)
}))
: [];

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

function sync_to_localstorage() {
if (browser) {
localStorage.setItem('recent_posts_store', JSON.stringify(recent_quibs_state));
// convert Date object to string
localStorage.setItem(
'recent_posts_store',
JSON.stringify(
recent_quibs_state.map((q) => ({ ...q, timestamp: q.timestamp.toISOString() }))
)
);
}
}

Expand Down

0 comments on commit 83a42a1

Please sign in to comment.