-
Notifications
You must be signed in to change notification settings - Fork 585
/
Copy pathuse-update-all-apps.ts
31 lines (26 loc) · 1.04 KB
/
use-update-all-apps.ts
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
import {useAllAvailableApps} from '@/providers/available-apps'
import {trpcReact} from '@/trpc/trpc'
export function useUpdateAllApps() {
const allAvailableApps = useAllAvailableApps()
const ctx = trpcReact.useContext()
const appsQ = trpcReact.apps.list.useQuery()
const updateMut = trpcReact.apps.update.useMutation({
onMutate: () => {
// Optimistic updates because otherwise it's too slow and feels like nothing is happening
ctx.apps.state.cancel()
allAvailableApps?.apps?.map((app) => {
ctx.apps.state.setData({appId: app.id}, {state: 'updating', progress: 0})
})
},
onSuccess: () => ctx.apps.list.invalidate(),
})
const updateAll = () => {
const apps = appsQ.data ?? []
// @ts-expect-error `version`
const appsWithUpdates = apps.filter((app) => allAvailableApps.appsKeyed?.[app.id]?.version !== app.version)
appsWithUpdates.map((app) => updateMut.mutate({appId: app.id}))
}
const isLoading = appsQ.isLoading || allAvailableApps.isLoading
const isUpdating = updateMut.isLoading
return {updateAll, isLoading, isUpdating}
}