-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { CourseData } from './gpx_parser'; | ||
import { base64ToString, digestSHA1, stringToBase64 } from './ab'; | ||
|
||
export type PersistedCourse = { | ||
id: string; | ||
name: string; | ||
notes: string; | ||
ts: number; // ms | ||
course: CourseData; | ||
}; | ||
|
||
export function getCourses(): PersistedCourse[] { | ||
const courses: PersistedCourse[] = []; | ||
|
||
if (typeof window === 'undefined') { | ||
return courses; | ||
} | ||
|
||
for (let i in localStorage) { | ||
if (i.startsWith('course:')) { | ||
const v = JSON.parse(localStorage[i]); | ||
|
||
courses.push({ | ||
id: i, | ||
name: v.name, | ||
notes: v.notes, | ||
ts: v.ts, | ||
course: JSON.parse(base64ToString(v.course)), | ||
}); | ||
} | ||
} | ||
|
||
return courses.sort((a, b) => b.ts - a.ts); | ||
} | ||
|
||
export async function saveCourse(name: string, notes: string, course: CourseData, ts?: number): Promise<string> { | ||
const courseStr = JSON.stringify(course); | ||
const digest = await digestSHA1(`${name}${courseStr}`); | ||
const id = `course:${digest}`; | ||
|
||
localStorage.setItem( | ||
id, | ||
JSON.stringify({ | ||
name, | ||
notes, | ||
ts: ts ?? Date.now(), | ||
course: await stringToBase64(courseStr), | ||
}) | ||
); | ||
|
||
return id; | ||
} | ||
|
||
export function deleteCourse(id: string) { | ||
if (!id.startsWith('course:')) { | ||
throw new Error('Not a course'); | ||
} | ||
|
||
localStorage.removeItem(id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c09ece5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bfree – ./
bfree.vercel.app
bfree-olliv.vercel.app
bfree-git-master-olliv.vercel.app