-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathuseGetMergeBox.ts
More file actions
33 lines (26 loc) · 1017 Bytes
/
useGetMergeBox.ts
File metadata and controls
33 lines (26 loc) · 1017 Bytes
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
import { useEffect } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useAtom } from 'jotai'
import { atomFamily } from 'jotai/utils'
import { GetApiClMergeBoxData } from '@gitmono/types'
import { atomWithWebStorage } from '@/utils/atomWithWebStorage'
import { legacyApiClient } from '@/utils/queryClient'
const fetchMergeBox = legacyApiClient.v1.getApiClMergeBox()
const getMergeBoxAtom = atomFamily(() => atomWithWebStorage<GetApiClMergeBoxData['data']>(`merge-box`, {}))
export const useGetMergeBox = (link: string) => {
const [mergeBoxData, setMergeBoxData] = useAtom(getMergeBoxAtom(`merge-box`))
const { data, isLoading } = useQuery({
queryKey: fetchMergeBox.requestKey(link),
queryFn: async () => {
const response = await fetchMergeBox.request(link)
return response.data ?? {}
},
refetchOnMount: 'always'
})
useEffect(() => {
if (data) {
setMergeBoxData(data)
}
}, [data, setMergeBoxData])
return { mergeBoxData, isLoading }
}