Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291import {useEffect, useMemo, useState} from 'react'import {View} from 'react-native'import Animated, { FadeIn, FadeOut, LayoutAnimationConfig, LinearTransition, SlideInRight, SlideOutLeft,} from 'react-native-reanimated'import {type ComAtprotoServerCreateAppPassword} from '@atproto/api'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Trans} from '@lingui/react/macro'import {useMutation} from '@tanstack/react-query'
import { type GatekeeperConfig, useAppPasswordCreateMutation,} from '#/state/queries/app-passwords'import {atoms as a, native, useTheme} from '#/alf'import {Admonition} from '#/components/Admonition'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import * as Dialog from '#/components/Dialog'import * as TextInput from '#/components/forms/TextField'import * as Toggle from '#/components/forms/Toggle'import {ChevronRight_Stroke2_Corner0_Rounded as ChevronRight} from '#/components/icons/Chevron'import {SquareBehindSquare4_Stroke2_Corner0_Rounded as CopyIcon} from '#/components/icons/SquareBehindSquare4'import {Text} from '#/components/Typography'import {IS_WEB} from '#/env'import {CopyButton} from './CopyButton'
export function AddAppPasswordDialog({ control, passwords, gatekeeper,}: { control: Dialog.DialogControlProps passwords: string[] gatekeeper?: GatekeeperConfig}) { return ( <Dialog.Outer control={control} nativeOptions={{fullHeight: true}}> <Dialog.Handle /> <CreateDialogInner passwords={passwords} gatekeeper={gatekeeper} /> </Dialog.Outer> )}
function CreateDialogInner({ passwords, gatekeeper,}: { passwords: string[] gatekeeper?: GatekeeperConfig}) { const control = Dialog.useDialogContext() const t = useTheme() const {_} = useLingui() const autogeneratedName = useRandomName() const [name, setName] = useState('') const [privileged, setPrivileged] = useState(false) const { mutateAsync: actuallyCreateAppPassword, error: apiError, data, } = useAppPasswordCreateMutation(gatekeeper)
const regexFailError = useMemo( () => new DisplayableError( _( msg`App password names can only contain letters, numbers, spaces, dashes, and underscores`, ), ), [_], )
const { mutate: createAppPassword, error: validationError, isPending, } = useMutation< ComAtprotoServerCreateAppPassword.AppPassword, Error | DisplayableError >({ mutationFn: async () => { const chosenName = name.trim() || autogeneratedName if (chosenName.length < 4) { throw new DisplayableError( _(msg`App password names must be at least 4 characters long`), ) } if (passwords.find(p => p === chosenName)) { throw new DisplayableError(_(msg`App password name must be unique`)) } return await actuallyCreateAppPassword({name: chosenName, privileged}) }, })
const [hasBeenCopied, setHasBeenCopied] = useState(false) useEffect(() => { if (hasBeenCopied) { const timeout = setTimeout(() => setHasBeenCopied(false), 100) return () => clearTimeout(timeout) } }, [hasBeenCopied])
const error = validationError || (!name.match(/^[a-zA-Z0-9-_ ]*$/) && regexFailError)
return ( <Dialog.ScrollableInner label={_(msg`Add app password`)}> <View style={[native(a.pt_md)]}> <LayoutAnimationConfig skipEntering skipExiting> {!data ? ( <Animated.View style={[a.gap_lg]} exiting={native(SlideOutLeft)} key={0}> <Text style={[a.text_2xl, a.font_semi_bold]}> <Trans>Add App Password</Trans> </Text> <Text style={[a.text_md, a.leading_snug]}> <Trans> Please enter a unique name for this app password or use our randomly generated one. </Trans> </Text> <View> <TextInput.Root isInvalid={!!error}> <Dialog.Input label={_(msg`App Password`)} placeholder={autogeneratedName} onChangeText={setName} returnKeyType="done" onSubmitEditing={() => createAppPassword()} blurOnSubmit autoCorrect={false} autoComplete="off" autoCapitalize="none" autoFocus /> </TextInput.Root> </View> {error instanceof DisplayableError && ( <Animated.View entering={FadeIn} exiting={FadeOut}> <Admonition type="error">{error.message}</Admonition> </Animated.View> )} <Animated.View style={[a.gap_lg]} layout={native(LinearTransition)}> <Toggle.Item name="privileged" type="checkbox" label={_(msg`Allow access to your direct messages`)} value={privileged} onChange={setPrivileged} style={[a.flex_1]}> <Toggle.Checkbox /> <Toggle.LabelText style={[a.font_normal, a.text_md, a.leading_snug]}> <Trans>Allow access to your direct messages</Trans> </Toggle.LabelText> </Toggle.Item> <Button label={_(msg`Next`)} size="large" variant="solid" color="primary" style={[a.flex_1]} onPress={() => createAppPassword()} disabled={isPending}> <ButtonText> <Trans>Next</Trans> </ButtonText> <ButtonIcon icon={ChevronRight} /> </Button> {!!apiError || (error && !(error instanceof DisplayableError) && ( <Animated.View entering={FadeIn} exiting={FadeOut}> <Admonition type="error"> <Trans> Failed to create app password. Please try again. </Trans> </Admonition> </Animated.View> ))} </Animated.View> </Animated.View> ) : ( <Animated.View style={[a.gap_lg]} entering={IS_WEB ? FadeIn.delay(200) : SlideInRight} key={1}> <Text style={[a.text_2xl, a.font_semi_bold]}> <Trans>Here is your app password!</Trans> </Text> <Text style={[a.text_md, a.leading_snug]}> <Trans> Use this to sign in to the other app along with your handle. </Trans> </Text> <CopyButton value={data.password} label={_(msg`Copy App Password`)} size="large" color="secondary"> <ButtonText>{data.password}</ButtonText> <ButtonIcon icon={CopyIcon} /> </CopyButton> <Text style={[ a.text_md, a.leading_snug, t.atoms.text_contrast_medium, ]}> <Trans> For security reasons, you won't be able to view this again. If you lose this app password, you'll need to generate a new one. </Trans> </Text> <Button label={_(msg`Done`)} size="large" variant="outline" color="primary" style={[a.flex_1]} onPress={() => control.close()}> <ButtonText> <Trans>Done</Trans> </ButtonText> </Button> </Animated.View> )} </LayoutAnimationConfig> </View> <Dialog.Close /> </Dialog.ScrollableInner> )}
class DisplayableError extends Error { constructor(message: string) { super(message) this.name = 'DisplayableError' }}
function useRandomName() { return useState( () => shadesOfBlue[Math.floor(Math.random() * shadesOfBlue.length)], )[0]}
const shadesOfBlue: string[] = [ 'AliceBlue', 'Aqua', 'Aquamarine', 'Azure', 'BabyBlue', 'Blue', 'BlueViolet', 'CadetBlue', 'CornflowerBlue', 'Cyan', 'DarkBlue', 'DarkCyan', 'DarkSlateBlue', 'DeepSkyBlue', 'DodgerBlue', 'ElectricBlue', 'LightBlue', 'LightCyan', 'LightSkyBlue', 'LightSteelBlue', 'MediumAquaMarine', 'MediumBlue', 'MediumSlateBlue', 'MidnightBlue', 'Navy', 'PowderBlue', 'RoyalBlue', 'SkyBlue', 'SlateBlue', 'SteelBlue', 'Teal', 'Turquoise',]