mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
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 * as Toast from '#/view/com/util/Toast'
10import {atoms as a, useTheme} from '#/alf'
11import {Button, ButtonIcon, ButtonText} from '#/components/Button'
12import * as Dialog from '#/components/Dialog'
13import {Download_Stroke2_Corner0_Rounded as DownloadIcon} from '#/components/icons/Download'
14import {InlineLinkText} from '#/components/Link'
15import {Loader} from '#/components/Loader'
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`), 'xmark')
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 <View style={[a.relative, a.gap_lg, a.w_full]}>
61 <Text nativeID="dialog-title" style={[a.text_2xl, a.font_bold]}>
62 <Trans>Export My Data</Trans>
63 </Text>
64 <Text
65 nativeID="dialog-description"
66 style={[a.text_sm, a.leading_snug, t.atoms.text_contrast_high]}>
67 <Trans>
68 Your account repository, containing all public data records, can
69 be downloaded as a "CAR" file. This file does not include media
70 embeds, such as images, or your private data, which must be
71 fetched separately.
72 </Trans>
73 </Text>
74
75 <Button
76 color="primary"
77 size="large"
78 label={_(msg`Download CAR file`)}
79 disabled={loading}
80 onPress={download}>
81 <ButtonIcon icon={DownloadIcon} />
82 <ButtonText>
83 <Trans>Download CAR file</Trans>
84 </ButtonText>
85 {loading && <ButtonIcon icon={Loader} />}
86 </Button>
87
88 <Text
89 style={[
90 t.atoms.text_contrast_medium,
91 a.text_sm,
92 a.leading_snug,
93 a.flex_1,
94 ]}>
95 <Trans>
96 This feature is in beta. You can read more about repository
97 exports in{' '}
98 <InlineLinkText
99 label={_(msg`View blogpost for more details`)}
100 to="https://docs.bsky.app/blog/repo-export"
101 style={[a.text_sm]}>
102 this blogpost
103 </InlineLinkText>
104 .
105 </Trans>
106 </Text>
107 </View>
108 </Dialog.ScrollableInner>
109 </Dialog.Outer>
110 )
111}