-
Notifications
You must be signed in to change notification settings - Fork 12
/
useFetchShortcut.jsx
63 lines (55 loc) · 1.79 KB
/
useFetchShortcut.jsx
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useState, useEffect } from 'react'
import { Q } from '../queries/dsl'
import CozyClient from '../CozyClient'
const DEFAULT_CACHE_TIMEOUT_QUERIES = 10 * 60 * 1000 // 10 minutes
const useFetchShortcut = (client, id) => {
const [shortcutInfos, setShortcutInfos] = useState(null)
const [shortcutImg, setShortcutImg] = useState(null)
const [fetchStatus, setFetchStatus] = useState('idle')
useEffect(() => {
const fetchData = async () => {
setFetchStatus('loading')
try {
const shortcutInfosResult = await client.fetchQueryAndGetFromState({
definition: Q('io.cozy.files.shortcuts').getById(id),
options: {
as: `io.cozy.files.shortcuts/${id}`,
fetchPolicy: CozyClient.fetchPolicies.olderThan(
DEFAULT_CACHE_TIMEOUT_QUERIES
),
singleDocData: true
}
})
const targetApp =
shortcutInfosResult?.data?.attributes?.metadata?.target?.app
if (targetApp) {
const targetAppIconUrl = await client.getStackClient().getIconURL({
type: 'app',
slug: targetApp,
priority: 'stack'
})
setShortcutImg(targetAppIconUrl)
} else {
const shortcutRemoteUrl = new URL(
shortcutInfosResult.data.attributes.url
)
const imgUrl = `${client.getStackClient().uri}/bitwarden/icons/${
shortcutRemoteUrl.host
}/icon.png`
setShortcutImg(imgUrl)
}
setShortcutInfos({ data: shortcutInfosResult.data })
setFetchStatus('loaded')
} catch (e) {
setFetchStatus('failed')
}
}
fetchData()
}, [client, id])
return {
shortcutInfos,
shortcutImg,
fetchStatus
}
}
export default useFetchShortcut