File size: 994 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
import { api, getApiUrl } from './client'

export type OpenTargetKind = 'ide' | 'file_manager'

export type OpenTarget = {
  id: string
  kind: OpenTargetKind
  label: string
  icon: string
  iconUrl?: string
  platform: string
}

export type OpenTargetList = {
  platform: string
  targets: OpenTarget[]
  primaryTargetId: string | null
  cachedAt: number
  ttlMs: number
}

export type OpenTargetOpenResponse = {
  ok: true
  targetId: string
  path: string
}

function normalizeOpenTargetList(result: OpenTargetList): OpenTargetList {
  return {
    ...result,
    targets: result.targets.map((target) => ({
      ...target,
      iconUrl: target.iconUrl ? getApiUrl(target.iconUrl) : undefined,
    })),
  }
}

export const openTargetsApi = {
  async list() {
    return normalizeOpenTargetList(await api.get<OpenTargetList>('/api/open-targets'))
  },
  open(targetId: string, path: string) {
    return api.post<OpenTargetOpenResponse>('/api/open-targets/open', { targetId, path })
  },
}