Add workspace client adapters
This commit is contained in:
+71
-11
@@ -75,6 +75,18 @@ export type HostedSession = {
|
||||
user: HostedUser | null
|
||||
}
|
||||
|
||||
export type WorkspaceClient = {
|
||||
loadSnapshot(): Promise<Partial<WorkspaceSnapshot> | null>
|
||||
saveSnapshot(snapshot: WorkspaceSnapshot): Promise<void>
|
||||
}
|
||||
|
||||
export type AuthClient = {
|
||||
enabled: boolean
|
||||
loadSession(): Promise<HostedSession>
|
||||
login(username: string, password: string): Promise<HostedSession>
|
||||
logout(): Promise<void>
|
||||
}
|
||||
|
||||
export function hostedWorkspaceEnabled() {
|
||||
return remoteWorkspaceEnabled
|
||||
}
|
||||
@@ -159,9 +171,7 @@ export function selectActiveSessionId(sessions: StudySession[], requestedId?: st
|
||||
}
|
||||
|
||||
export async function loadWorkspaceSnapshot(): Promise<WorkspaceSnapshot> {
|
||||
const remoteSnapshot = remoteWorkspaceEnabled ? await loadRemoteWorkspaceSnapshot() : null
|
||||
const localSnapshot = remoteSnapshot ? null : loadLocalWorkspaceSnapshot()
|
||||
const source = remoteSnapshot ?? localSnapshot
|
||||
const source = await getWorkspaceClient().loadSnapshot()
|
||||
const sessions = normalizeSessions(source?.sessions)
|
||||
const activeSessionId = selectActiveSessionId(sessions, source?.activeSessionId)
|
||||
|
||||
@@ -173,9 +183,7 @@ export async function loadWorkspaceSnapshot(): Promise<WorkspaceSnapshot> {
|
||||
}
|
||||
|
||||
export function saveWorkspaceSnapshot(snapshot: WorkspaceSnapshot): Promise<void> {
|
||||
saveLocalWorkspaceSnapshot(snapshot)
|
||||
if (!remoteWorkspaceEnabled) return Promise.resolve()
|
||||
return saveRemoteWorkspaceSnapshot(snapshot)
|
||||
return getWorkspaceClient().saveSnapshot(snapshot)
|
||||
}
|
||||
|
||||
function loadLocalWorkspaceSnapshot(): Partial<WorkspaceSnapshot> {
|
||||
@@ -234,8 +242,49 @@ async function saveRemoteWorkspaceSnapshot(snapshot: WorkspaceSnapshot) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadHostedSession(): Promise<HostedSession> {
|
||||
if (!remoteWorkspaceEnabled) return { authenticated: false, user: null }
|
||||
const localWorkspaceClient: WorkspaceClient = {
|
||||
loadSnapshot: async () => loadLocalWorkspaceSnapshot(),
|
||||
saveSnapshot: async (snapshot) => {
|
||||
saveLocalWorkspaceSnapshot(snapshot)
|
||||
},
|
||||
}
|
||||
|
||||
const hostedWorkspaceClient: WorkspaceClient = {
|
||||
loadSnapshot: async () => {
|
||||
const remoteSnapshot = await loadRemoteWorkspaceSnapshot()
|
||||
return remoteSnapshot ?? loadLocalWorkspaceSnapshot()
|
||||
},
|
||||
saveSnapshot: async (snapshot) => {
|
||||
saveLocalWorkspaceSnapshot(snapshot)
|
||||
await saveRemoteWorkspaceSnapshot(snapshot)
|
||||
},
|
||||
}
|
||||
|
||||
const inactiveAuthClient: AuthClient = {
|
||||
enabled: false,
|
||||
loadSession: async () => ({ authenticated: false, user: null }),
|
||||
login: async () => {
|
||||
throw new Error('Hosted login is not enabled in this workspace mode.')
|
||||
},
|
||||
logout: async () => undefined,
|
||||
}
|
||||
|
||||
const hostedAuthClient: AuthClient = {
|
||||
enabled: true,
|
||||
loadSession: loadRemoteHostedSession,
|
||||
login: loginRemoteHostedWorkspace,
|
||||
logout: logoutRemoteHostedWorkspace,
|
||||
}
|
||||
|
||||
export function getWorkspaceClient(): WorkspaceClient {
|
||||
return remoteWorkspaceEnabled ? hostedWorkspaceClient : localWorkspaceClient
|
||||
}
|
||||
|
||||
export function getAuthClient(): AuthClient {
|
||||
return remoteWorkspaceEnabled ? hostedAuthClient : inactiveAuthClient
|
||||
}
|
||||
|
||||
async function loadRemoteHostedSession(): Promise<HostedSession> {
|
||||
const response = await fetch(`${remoteWorkspaceApiBase}/session`, {
|
||||
credentials: 'include',
|
||||
})
|
||||
@@ -245,7 +294,7 @@ export async function loadHostedSession(): Promise<HostedSession> {
|
||||
return response.json() as Promise<HostedSession>
|
||||
}
|
||||
|
||||
export async function loginHostedWorkspace(username: string, password: string): Promise<HostedSession> {
|
||||
async function loginRemoteHostedWorkspace(username: string, password: string): Promise<HostedSession> {
|
||||
const response = await fetch(`${remoteWorkspaceApiBase}/login`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
@@ -260,8 +309,7 @@ export async function loginHostedWorkspace(username: string, password: string):
|
||||
return response.json() as Promise<HostedSession>
|
||||
}
|
||||
|
||||
export async function logoutHostedWorkspace(): Promise<void> {
|
||||
if (!remoteWorkspaceEnabled) return
|
||||
async function logoutRemoteHostedWorkspace(): Promise<void> {
|
||||
const response = await fetch(`${remoteWorkspaceApiBase}/logout`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
@@ -270,3 +318,15 @@ export async function logoutHostedWorkspace(): Promise<void> {
|
||||
throw new Error(`Unable to log out: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function loadHostedSession(): Promise<HostedSession> {
|
||||
return getAuthClient().loadSession()
|
||||
}
|
||||
|
||||
export function loginHostedWorkspace(username: string, password: string): Promise<HostedSession> {
|
||||
return getAuthClient().login(username, password)
|
||||
}
|
||||
|
||||
export function logoutHostedWorkspace(): Promise<void> {
|
||||
return getAuthClient().logout()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user