File size: 937 Bytes
2cd1b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export interface DeckMeta {
  id: string
  title: string
  createdAt: number
}

export async function listMyDecks(): Promise<DeckMeta[]> {
  const r = await fetch('/api/decks/mine')
  if (!r.ok) return []
  const j = (await r.json()) as { decks?: DeckMeta[] }
  return j.decks ?? []
}

export async function createDeck(title: string): Promise<DeckMeta> {
  const r = await fetch('/api/decks', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title }),
  })
  if (!r.ok) {
    let msg = 'Could not create presentation'
    try {
      msg = (await r.json()).error || msg
    } catch {
      /* ignore */
    }
    throw new Error(msg)
  }
  return (await r.json()) as DeckMeta
}

/** Deck id from the URL (/d/<id>), or null for the home page. */
export function currentDeckId(): string | null {
  const m = location.pathname.match(/^\/d\/([A-Za-z0-9_-]+)$/)
  return m ? m[1] : null
}