First commit
This commit is contained in:
42
frontend/app/utils/playmat.ts
Normal file
42
frontend/app/utils/playmat.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { authHeaders } from './auth'
|
||||
|
||||
export type PlaymatChoice =
|
||||
| { kind: 'card'; oracleId: string }
|
||||
| { kind: 'upload'; data: string }
|
||||
|
||||
export type PlaymatSelection = PlaymatChoice | { kind: 'none' }
|
||||
|
||||
export async function loadPlaymatChoice(apiBase: string): Promise<PlaymatChoice | undefined> {
|
||||
const { Authorization } = authHeaders()
|
||||
if (!Authorization) return undefined
|
||||
try {
|
||||
const data = await $fetch<{ playmat: PlaymatChoice | null }>(`${apiBase}/me/preferences`, {
|
||||
headers: { Authorization },
|
||||
})
|
||||
const saved = data.playmat
|
||||
if (saved?.kind === 'card' && typeof saved.oracleId === 'string') {
|
||||
return { kind: 'card', oracleId: saved.oracleId }
|
||||
}
|
||||
if (saved?.kind === 'upload' && typeof saved.data === 'string') {
|
||||
return { kind: 'upload', data: saved.data }
|
||||
}
|
||||
} catch {
|
||||
// An unavailable account should not block the page.
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** False when the account cannot store the image; the caller should say so. */
|
||||
export async function savePlaymatChoice(apiBase: string, choice: PlaymatChoice | undefined): Promise<boolean> {
|
||||
if (!authHeaders().Authorization) return false
|
||||
try {
|
||||
await $fetch(`${apiBase}/me/preferences`, {
|
||||
method: 'PUT',
|
||||
headers: authHeaders(),
|
||||
body: { playmat: choice ?? null },
|
||||
})
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user