forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useCallback, useState} from 'react'
2import {View} from 'react-native'
3import {msg, Trans} from '@lingui/macro'
4import {useLingui} from '@lingui/react'
5
6import {saveBytesToDisk} from '#/lib/media/manip'
7import {logger} from '#/logger'
8import {useAgent} from '#/state/session'
9import {atoms as a, useTheme, web} from '#/alf'
10import {Button, ButtonIcon, ButtonText} from '#/components/Button'
11import * as Dialog from '#/components/Dialog'
12import {Download_Stroke2_Corner0_Rounded as DownloadIcon} from '#/components/icons/Download'
13import {InlineLinkText} from '#/components/Link'
14import {Loader} from '#/components/Loader'
15import * as Toast from '#/components/Toast'
16import {Text} from '#/components/Typography'
17
18export function ExportCarDialog({
19 control,
20}: {
21 control: Dialog.DialogControlProps
22}) {
23 const {_} = useLingui()
24 const t = useTheme()
25 const agent = useAgent()
26 const [loading, setLoading] = useState(false)
27
28 const download = useCallback(async () => {
29 if (!agent.session) {
30 return // shouldnt ever happen
31 }
32 try {
33 setLoading(true)
34 const did = agent.session.did
35 const downloadRes = await agent.com.atproto.sync.getRepo({did})
36 const saveRes = await saveBytesToDisk(
37 'repo.car',
38 downloadRes.data,
39 downloadRes.headers['content-type'] || 'application/vnd.ipld.car',
40 )
41
42 if (saveRes) {
43 Toast.show(_(msg`File saved successfully!`))
44 }
45 } catch (e) {
46 logger.error('Error occurred while downloading CAR file', {message: e})
47 Toast.show(_(msg`Error occurred while saving file`), {type: 'error'})
48 } finally {
49 setLoading(false)
50 control.close()
51 }
52 }, [_, control, agent])
53
54 return (
55 <Dialog.Outer control={control} nativeOptions={{preventExpansion: true}}>
56 <Dialog.Handle />
57 <Dialog.ScrollableInner
58 accessibilityDescribedBy="dialog-description"
59 accessibilityLabelledBy="dialog-title"
60 style={web({maxWidth: 500})}>
61 <View style={[a.relative, a.gap_lg, a.w_full]}>
62 <Text nativeID="dialog-title" style={[a.text_2xl, a.font_bold]}>
63 <Trans>Export My Data</Trans>
64 </Text>
65 <Text
66 nativeID="dialog-description"
67 style={[a.text_sm, a.leading_snug, t.atoms.text_contrast_high]}>
68 <Trans>
69 Your account repository, containing all public data records, can
70 be downloaded as a "CAR" file. This file does not include media
71 embeds, such as images, or your private data, which must be
72 fetched separately.
73 </Trans>
74 </Text>
75
76 <Button
77 color="primary"
78 size="large"
79 label={_(msg`Download CAR file`)}
80 disabled={loading}
81 onPress={download}>
82 <ButtonIcon icon={DownloadIcon} />
83 <ButtonText>
84 <Trans>Download CAR file</Trans>
85 </ButtonText>
86 {loading && <ButtonIcon icon={Loader} />}
87 </Button>
88
89 <Text
90 style={[
91 t.atoms.text_contrast_medium,
92 a.text_sm,
93 a.leading_snug,
94 a.flex_1,
95 ]}>
96 <Trans>
97 This feature is in beta. You can read more about repository
98 exports in{' '}
99 <InlineLinkText
100 label={_(msg`View blogpost for more details`)}
101 to="https://docs.bsky.app/blog/repo-export"
102 style={[a.text_sm]}>
103 this blogpost
104 </InlineLinkText>
105 .
106 </Trans>
107 </Text>
108 </View>
109 <Dialog.Close />
110 </Dialog.ScrollableInner>
111 </Dialog.Outer>
112 )
113}