forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import * as Updates from 'expo-updates'
2import {msg, Trans} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4import {useMutation, useQuery} from '@tanstack/react-query'
5
6import * as Toast from '#/view/com/util/Toast'
7import {Button, ButtonIcon, ButtonText} from '#/components/Button'
8import {ArrowRotateCounterClockwise_Stroke2_Corner0_Rounded as RetryIcon} from '#/components/icons/ArrowRotate'
9import {Shapes_Stroke2_Corner0_Rounded as ShapesIcon} from '#/components/icons/Shapes'
10import {Loader} from '#/components/Loader'
11import * as SettingsList from '../components/SettingsList'
12
13export function OTAInfo() {
14 const {_} = useLingui()
15 const {
16 data: isAvailable,
17 isPending: isPendingInfo,
18 isFetching: isFetchingInfo,
19 isError: isErrorInfo,
20 refetch,
21 } = useQuery({
22 queryKey: ['ota-info'],
23 queryFn: async () => {
24 const status = await Updates.checkForUpdateAsync()
25 return status.isAvailable
26 },
27 })
28
29 const {mutate: fetchAndLaunchUpdate, isPending: isPendingUpdate} =
30 useMutation({
31 mutationFn: async () => {
32 await Updates.fetchUpdateAsync()
33 await Updates.reloadAsync()
34 },
35 onError: error =>
36 Toast.show(`Failed to update: ${error.message}`, 'xmark'),
37 })
38
39 if (!Updates.isEnabled || __DEV__) {
40 return null
41 }
42
43 return (
44 <SettingsList.Item>
45 <SettingsList.ItemIcon icon={ShapesIcon} />
46 <SettingsList.ItemText>
47 {isAvailable ? (
48 <Trans>OTA status: Available!</Trans>
49 ) : isErrorInfo ? (
50 <Trans>OTA status: Error fetching update</Trans>
51 ) : isPendingInfo ? (
52 <Trans>OTA status: ...</Trans>
53 ) : (
54 <Trans>OTA status: None available</Trans>
55 )}
56 </SettingsList.ItemText>
57 <Button
58 label={isAvailable ? _(msg`Update`) : _(msg`Fetch update`)}
59 disabled={isFetchingInfo || isPendingUpdate}
60 variant="solid"
61 size="small"
62 color={isAvailable ? 'primary' : 'secondary_inverted'}
63 onPress={() => {
64 if (isFetchingInfo || isPendingUpdate) return
65
66 if (isAvailable) {
67 fetchAndLaunchUpdate()
68 } else {
69 refetch()
70 }
71 }}>
72 {isAvailable ? (
73 <ButtonText>
74 <Trans>Update</Trans>
75 </ButtonText>
76 ) : (
77 <ButtonIcon icon={isFetchingInfo ? Loader : RetryIcon} />
78 )}
79 </Button>
80 </SettingsList.Item>
81 )
82}