import {useCallback, useImperativeHandle, useRef, useState} from 'react' import {View} from 'react-native' import {useWindowDimensions} from 'react-native' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {BSKY_SERVICE} from '#/lib/constants' import {logEvent} from '#/lib/statsig/statsig' import * as persisted from '#/state/persisted' import {useSession} from '#/state/session' import {atoms as a, useBreakpoints, useTheme} from '#/alf' import {Admonition} from '#/components/Admonition' import {Button, ButtonText} from '#/components/Button' import * as Dialog from '#/components/Dialog' import * as TextField from '#/components/forms/TextField' import * as ToggleButton from '#/components/forms/ToggleButton' import {Globe_Stroke2_Corner0_Rounded as Globe} from '#/components/icons/Globe' import {InlineLinkText} from '#/components/Link' import {P, Text} from '#/components/Typography' export function ServerInputDialog({ control, onSelect, }: { control: Dialog.DialogOuterProps['control'] onSelect: (url: string) => void }) { const {height} = useWindowDimensions() const formRef = useRef(null) // persist these options between dialog open/close const [fixedOption, setFixedOption] = useState(BSKY_SERVICE) const [previousCustomAddress, setPreviousCustomAddress] = useState('') const onClose = useCallback(() => { const result = formRef.current?.getFormState() if (result) { onSelect(result) if (result !== BSKY_SERVICE) { setPreviousCustomAddress(result) } } logEvent('signin:hostingProviderPressed', { hostingProviderDidChange: fixedOption !== BSKY_SERVICE, }) }, [onSelect, fixedOption]) return ( ) } type DialogInnerRef = {getFormState: () => string | null} function DialogInner({ formRef, fixedOption, setFixedOption, initialCustomAddress, }: { formRef: React.Ref fixedOption: string setFixedOption: (opt: string) => void initialCustomAddress: string }) { const control = Dialog.useDialogContext() const {_} = useLingui() const t = useTheme() const {accounts} = useSession() const {gtMobile} = useBreakpoints() const [customAddress, setCustomAddress] = useState(initialCustomAddress) const [pdsAddressHistory, setPdsAddressHistory] = useState( persisted.get('pdsAddressHistory') || [], ) useImperativeHandle( formRef, () => ({ getFormState: () => { let url if (fixedOption === 'custom') { url = customAddress.trim().toLowerCase() if (!url) { return null } } else { url = fixedOption } if (!url.startsWith('http://') && !url.startsWith('https://')) { if (url === 'localhost' || url.startsWith('localhost:')) { url = `http://${url}` } else { url = `https://${url}` } } if (fixedOption === 'custom') { if (!pdsAddressHistory.includes(url)) { const newHistory = [url, ...pdsAddressHistory.slice(0, 4)] setPdsAddressHistory(newHistory) persisted.write('pdsAddressHistory', newHistory) } } return url }, }), [customAddress, fixedOption, pdsAddressHistory], ) const isFirstTimeUser = accounts.length === 0 return ( Choose your account provider setFixedOption(values[0])}> {_(msg`Bluesky`)} {_(msg`Custom`)} {fixedOption === BSKY_SERVICE && isFirstTimeUser && ( Bluesky is an open network where you can choose your own provider. If you're new here, we recommend sticking with the default Bluesky Social option. )} {fixedOption === 'custom' && ( Server address {pdsAddressHistory.length > 0 && ( {pdsAddressHistory.map(uri => ( ))} )} )}

{isFirstTimeUser ? ( If you're a developer, you can host your own server. ) : ( Bluesky is an open network where you can choose your hosting provider. If you're a developer, you can host your own server. )}{' '} Learn more.

) }