Skip to content

Commit

Permalink
Add dusa-specific backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed May 16, 2024
1 parent ccba34b commit 881fb3f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sketchzone",
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"main": "lib/main.js",
"types": "lib/main.d.ts",
Expand Down
36 changes: 33 additions & 3 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,40 @@ export async function initializeStorage(
// This will be fixed by either adding default entries or by adding the document from the hashAction.
const tabs: TabsObject = { displayedSketchIndex: 0, sketches: [] };

// TODO: Check for legacy localStorage data and add that to the database if need be
// This while if-statement can be deleted after JUNE 2024
// It grabs all the entries for the Dusa editor to facilitate migrating dusa.rocks to sketchzone
if (localStorage.getItem('dusa-sessions')) {
const programs: { [uuid: string]: { title: string; key: IDBValidKey } } = {};
for (let i = 0; i < localStorage.length; i++) {
const prefix = 'dusa-session-';
const key = localStorage.key(i);
if (key?.startsWith(prefix)) {
const uuid = key.slice(prefix.length);
const program = localStorage.getItem(key)!;
programs[uuid] = {
title: extractTitleFromDoc(program),
key: await addSketch(sketchStore, {
document: program,
createdAt: now,
updatedAt: now,
}),
};
}
}

tabs.sketches = localStorage
.getItem('dusa-sessions')!
.split(',')
.map((uuid): undefined | { title: string; key: IDBValidKey } => programs[uuid])
.filter((elem): elem is { title: string; key: IDBValidKey } => elem !== undefined);
}

if (
hashAction === null &&
tabs.sketches.length === 0 // this check is redundant when the localStorage logic above is removed
) {
// If there's no hashAction, add the default entries to the store and the tabs

// If there's no hashAction, add the default entries to the store and the tabs
if (hashAction === null) {
tabs.sketches = await Promise.all(
defaultEntries.map(async (entry) => ({
title: extractTitleFromDoc(entry),
Expand Down

0 comments on commit 881fb3f

Please sign in to comment.