Complete shared app core phase
This commit is contained in:
+21
-7
@@ -1,6 +1,8 @@
|
||||
export const timelineLimit = 200
|
||||
export const defaultTranslationId = 'kjv-eng-kjv2006'
|
||||
export const workspaceSchemaVersion = 1
|
||||
|
||||
const workspaceStorageKey = 'libre-study.workspace.v1'
|
||||
const sessionStorageKey = 'libre-study.sessions.v1'
|
||||
const activeSessionStorageKey = 'libre-study.active-session.v1'
|
||||
const studySettingsStorageKey = 'libre-study.study-settings.v1'
|
||||
@@ -60,11 +62,14 @@ export type StudySettings = {
|
||||
}
|
||||
|
||||
export type WorkspaceSnapshot = {
|
||||
schemaVersion: number
|
||||
sessions: StudySession[]
|
||||
activeSessionId: string
|
||||
studySettings: StudySettings
|
||||
}
|
||||
|
||||
export type PersistedWorkspaceSnapshot = Partial<WorkspaceSnapshot> | null | undefined
|
||||
|
||||
export type HostedUser = {
|
||||
id: number
|
||||
username: string
|
||||
@@ -76,7 +81,7 @@ export type HostedSession = {
|
||||
}
|
||||
|
||||
export type WorkspaceClient = {
|
||||
loadSnapshot(): Promise<Partial<WorkspaceSnapshot> | null>
|
||||
loadSnapshot(): Promise<PersistedWorkspaceSnapshot>
|
||||
saveSnapshot(snapshot: WorkspaceSnapshot): Promise<void>
|
||||
}
|
||||
|
||||
@@ -170,23 +175,30 @@ export function selectActiveSessionId(sessions: StudySession[], requestedId?: st
|
||||
return sessions.some((session) => session.id === requestedId) ? requestedId ?? sessions[0].id : sessions[0].id
|
||||
}
|
||||
|
||||
export async function loadWorkspaceSnapshot(): Promise<WorkspaceSnapshot> {
|
||||
const source = await getWorkspaceClient().loadSnapshot()
|
||||
export function normalizeWorkspaceSnapshot(source?: PersistedWorkspaceSnapshot): WorkspaceSnapshot {
|
||||
const sessions = normalizeSessions(source?.sessions)
|
||||
const activeSessionId = selectActiveSessionId(sessions, source?.activeSessionId)
|
||||
|
||||
return {
|
||||
schemaVersion: workspaceSchemaVersion,
|
||||
sessions,
|
||||
activeSessionId,
|
||||
studySettings: normalizeStudySettings(source?.studySettings),
|
||||
}
|
||||
}
|
||||
|
||||
export function saveWorkspaceSnapshot(snapshot: WorkspaceSnapshot): Promise<void> {
|
||||
return getWorkspaceClient().saveSnapshot(snapshot)
|
||||
export async function loadWorkspaceSnapshot(): Promise<WorkspaceSnapshot> {
|
||||
return normalizeWorkspaceSnapshot(await getWorkspaceClient().loadSnapshot())
|
||||
}
|
||||
|
||||
function loadLocalWorkspaceSnapshot(): Partial<WorkspaceSnapshot> {
|
||||
export function saveWorkspaceSnapshot(snapshot: PersistedWorkspaceSnapshot): Promise<void> {
|
||||
return getWorkspaceClient().saveSnapshot(normalizeWorkspaceSnapshot(snapshot))
|
||||
}
|
||||
|
||||
function loadLocalWorkspaceSnapshot(): PersistedWorkspaceSnapshot {
|
||||
const currentSnapshot = readJson<WorkspaceSnapshot>(workspaceStorageKey)
|
||||
if (currentSnapshot) return currentSnapshot
|
||||
|
||||
return {
|
||||
sessions: readJson<StudySession[]>(sessionStorageKey) ?? undefined,
|
||||
activeSessionId: localStorage.getItem(activeSessionStorageKey) ?? undefined,
|
||||
@@ -195,6 +207,8 @@ function loadLocalWorkspaceSnapshot(): Partial<WorkspaceSnapshot> {
|
||||
}
|
||||
|
||||
function saveLocalWorkspaceSnapshot(snapshot: WorkspaceSnapshot) {
|
||||
localStorage.setItem(workspaceStorageKey, JSON.stringify(snapshot))
|
||||
// Keep legacy keys current until every adapter reads the versioned snapshot.
|
||||
localStorage.setItem(sessionStorageKey, JSON.stringify(snapshot.sessions))
|
||||
localStorage.setItem(activeSessionStorageKey, snapshot.activeSessionId)
|
||||
localStorage.setItem(studySettingsStorageKey, JSON.stringify(snapshot.studySettings))
|
||||
@@ -212,7 +226,7 @@ function readJson<T>(key: string): T | null {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRemoteWorkspaceSnapshot(): Promise<Partial<WorkspaceSnapshot> | null> {
|
||||
async function loadRemoteWorkspaceSnapshot(): Promise<PersistedWorkspaceSnapshot> {
|
||||
const response = await fetch(`${remoteWorkspaceApiBase}/workspace`, {
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user