Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link shortening to starter pack link sharing #4581

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/components/StarterPack/QrCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ViewShot from 'react-native-view-shot'
import {AppBskyGraphDefs, AppBskyGraphStarterpack} from '@atproto/api'
import {Trans} from '@lingui/macro'

import {makeStarterPackLink} from 'lib/routes/links'
import {isWeb} from 'platform/detection'
import {Logo} from 'view/icons/Logo'
import {Logotype} from 'view/icons/Logotype'
Expand All @@ -16,10 +15,11 @@ import {Text} from '#/components/Typography'

interface Props {
starterPack: AppBskyGraphDefs.StarterPackView
link: string
}

export const QrCode = React.forwardRef<ViewShot, Props>(function QrCode(
{starterPack},
{starterPack, link},
ref,
) {
const {record} = starterPack
Expand Down Expand Up @@ -56,7 +56,7 @@ export const QrCode = React.forwardRef<ViewShot, Props>(function QrCode(
<Trans>Join the conversation</Trans>
</Text>
<View style={[a.rounded_sm, a.overflow_hidden]}>
<QrCodeInner url={makeStarterPackLink(starterPack)} />
<QrCodeInner link={link} />
</View>

<View style={[a.flex_row, a.align_center, {gap: 5}]}>
Expand All @@ -79,12 +79,12 @@ export const QrCode = React.forwardRef<ViewShot, Props>(function QrCode(
)
})

export function QrCodeInner({url}: {url: string}) {
export function QrCodeInner({link}: {link: string}) {
const t = useTheme()

return (
<QRCode
data={url}
data={link}
style={[
a.rounded_sm,
{height: 225, width: 225, backgroundColor: '#f3f3f3'},
Expand Down
85 changes: 58 additions & 27 deletions src/components/StarterPack/QrCodeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import ViewShot from 'react-native-view-shot'
import * as FS from 'expo-file-system'
import {requestMediaLibraryPermissionsAsync} from 'expo-image-picker'
import * as Sharing from 'expo-sharing'
import {AppBskyGraphDefs, AppBskyGraphStarterpack} from '@atproto/api'
import {AppBskyGraphDefs, AppBskyGraphStarterpack, AtUri} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {nanoid} from 'nanoid/non-secure'

import {logger} from '#/logger'
import {saveImageToMediaLibrary} from 'lib/media/manip'
import {makeStarterPackLink} from 'lib/routes/links'
import {logEvent} from 'lib/statsig/statsig'
import {isNative, isWeb} from 'platform/detection'
import {useShortenLink} from 'state/queries/shorten-link'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
Expand All @@ -24,15 +26,32 @@ import {QrCode} from '#/components/StarterPack/QrCode'
export function QrCodeDialog({
control,
starterPack,
isOpen,
setIsOpen,
}: {
control: DialogControlProps
starterPack: AppBskyGraphDefs.StarterPackView
isOpen: boolean
setIsOpen: (isOpen: boolean) => void
}) {
const {_} = useLingui()
const [isProcessing, setIsProcessing] = React.useState(false)
const [link, setLink] = React.useState<string>()
const shortenLink = useShortenLink()

const ref = React.useRef<ViewShot>(null)

React.useEffect(() => {
if (!isOpen || link) return
;(async () => {
const rkey = new AtUri(starterPack.uri).rkey
const res = await shortenLink(
makeStarterPackLink(starterPack.creator.did, rkey),
)
setLink(res.url)
})()
}, [isOpen, link, shortenLink, starterPack.creator.did, starterPack.uri])

const getCanvas = (base64: string): Promise<HTMLCanvasElement> => {
return new Promise(resolve => {
const image = new Image()
Expand Down Expand Up @@ -149,39 +168,51 @@ export function QrCodeDialog({
}

return (
<Dialog.Outer control={control}>
<Dialog.Outer
control={control}
onClose={() => {
setIsOpen(false)
}}>
<Dialog.Handle />
<Dialog.ScrollableInner
label={_(msg`Create a QR code for a starter pack`)}>
<View style={[a.flex_1, a.align_center, a.gap_5xl]}>
<QrCode starterPack={starterPack} ref={ref} />
{isProcessing ? (
<View>
{!link ? (
<View style={[a.align_center, a.p_xl]}>
<Loader size="xl" />
</View>
) : (
<View style={[a.w_full, a.gap_md]}>
<Button
label={_(msg`Copy QR code`)}
variant="solid"
color="primary"
size="medium"
onPress={isWeb ? onCopyPress : onSharePress}>
<ButtonText>
{isWeb ? <Trans>Copy</Trans> : <Trans>Share</Trans>}
</ButtonText>
</Button>
<Button
label={_(msg`Save QR code`)}
variant="solid"
color="secondary"
size="medium"
onPress={onSavePress}>
<ButtonText>
<Trans>Save</Trans>
</ButtonText>
</Button>
</View>
<>
<QrCode starterPack={starterPack} link={link} ref={ref} />
{isProcessing ? (
<View>
<Loader size="xl" />
</View>
) : (
<View style={[a.w_full, a.gap_md]}>
<Button
label={_(msg`Copy QR code`)}
variant="solid"
color="primary"
size="medium"
onPress={isWeb ? onCopyPress : onSharePress}>
<ButtonText>
{isWeb ? <Trans>Copy</Trans> : <Trans>Share</Trans>}
</ButtonText>
</Button>
<Button
label={_(msg`Save QR code`)}
variant="solid"
color="secondary"
size="medium"
onPress={onSavePress}>
<ButtonText>
<Trans>Save</Trans>
</ButtonText>
</Button>
</View>
)}
</>
)}
</View>
</Dialog.ScrollableInner>
Expand Down
Loading
Loading