From 5ca3b96f8066ecbe6354d19db6da3a300b71a370 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Sun, 26 Jan 2025 11:45:29 -0800 Subject: [PATCH 1/2] avoid portal perf pitfall --- src/components/forms/HostingProvider.tsx | 4 +- src/view/com/auth/server-input/index.tsx | 343 ++++++++++++----------- 2 files changed, 189 insertions(+), 158 deletions(-) diff --git a/src/components/forms/HostingProvider.tsx b/src/components/forms/HostingProvider.tsx index 4732434b01..221a759d56 100644 --- a/src/components/forms/HostingProvider.tsx +++ b/src/components/forms/HostingProvider.tsx @@ -71,10 +71,10 @@ export function HostingProvider({ a.flex_row, a.align_center, a.rounded_sm, - a.px_md, + a.pl_md, a.pr_sm, a.gap_xs, - {paddingVertical: isAndroid ? 14 : 9}, + {paddingVertical: isAndroid ? 14 : 8}, ]} onPress={onPressSelectService}> {({hovered, pressed}) => { diff --git a/src/view/com/auth/server-input/index.tsx b/src/view/com/auth/server-input/index.tsx index f9a5c84bf8..49574a2781 100644 --- a/src/view/com/auth/server-input/index.tsx +++ b/src/view/com/auth/server-input/index.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import {useCallback, useImperativeHandle, useRef, useState} from 'react' import {View} from 'react-native' import {useWindowDimensions} from 'react-native' import {msg, Trans} from '@lingui/macro' @@ -24,177 +24,208 @@ export function ServerInputDialog({ 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) + } + } + }, [onSelect]) + + 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 {height} = useWindowDimensions() + const {accounts} = useSession() const {gtMobile} = useBreakpoints() - const [pdsAddressHistory, setPdsAddressHistory] = React.useState( + const [customAddress, setCustomAddress] = useState(initialCustomAddress) + const [pdsAddressHistory, setPdsAddressHistory] = useState( persisted.get('pdsAddressHistory') || [], ) - const [fixedOption, setFixedOption] = React.useState([BSKY_SERVICE]) - const [customAddress, setCustomAddress] = React.useState('') - const {accounts} = useSession() - - const isFirstTimeUser = accounts.length === 0 - const onClose = React.useCallback(() => { - let url - if (fixedOption[0] === 'custom') { - url = customAddress.trim().toLowerCase() - if (!url) { - return - } - } else { - url = fixedOption[0] - } - if (!url.startsWith('http://') && !url.startsWith('https://')) { - if (url === 'localhost' || url.startsWith('localhost:')) { - url = `http://${url}` + useImperativeHandle(formRef, () => ({ + getFormState: () => { + let url + if (fixedOption === 'custom') { + url = customAddress.trim().toLowerCase() + if (!url) { + return null + } } else { - url = `https://${url}` + url = fixedOption + } + if (!url.startsWith('http://') && !url.startsWith('https://')) { + if (url === 'localhost' || url.startsWith('localhost:')) { + url = `http://${url}` + } else { + url = `https://${url}` + } } - } - if (fixedOption[0] === 'custom') { - if (!pdsAddressHistory.includes(url)) { - const newHistory = [url, ...pdsAddressHistory.slice(0, 4)] - setPdsAddressHistory(newHistory) - persisted.write('pdsAddressHistory', newHistory) + if (fixedOption === 'custom') { + if (!pdsAddressHistory.includes(url)) { + const newHistory = [url, ...pdsAddressHistory.slice(0, 4)] + setPdsAddressHistory(newHistory) + persisted.write('pdsAddressHistory', newHistory) + } } - } - onSelect(url) - }, [ - fixedOption, - customAddress, - onSelect, - pdsAddressHistory, - setPdsAddressHistory, - ]) + return url + }, + })) + + const isFirstTimeUser = accounts.length === 0 return ( - - - - - - Choose your account provider - - - - - {_(msg`Bluesky`)} - - - - - {_(msg`Custom`)} - - - - - {fixedOption[0] === BSKY_SERVICE && isFirstTimeUser && ( - + + + + 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 ? ( - 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. + If you're a developer, you can host your own server. - - )} - - {fixedOption[0] === '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. - -

-
+ ) : ( + + 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. + +

+
- - - + + -
-
+ + ) } From c7a473b77c5a0aa7f6621849e54a1e3c08bcb03b Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 3 Feb 2025 22:41:45 +0000 Subject: [PATCH 2/2] add dep array --- src/view/com/auth/server-input/index.tsx | 54 +++++++++++++----------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/view/com/auth/server-input/index.tsx b/src/view/com/auth/server-input/index.tsx index 49574a2781..7c0bda45be 100644 --- a/src/view/com/auth/server-input/index.tsx +++ b/src/view/com/auth/server-input/index.tsx @@ -80,36 +80,40 @@ function DialogInner({ 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}` + useImperativeHandle( + formRef, + () => ({ + getFormState: () => { + let url + if (fixedOption === 'custom') { + url = customAddress.trim().toLowerCase() + if (!url) { + return null + } } else { - url = `https://${url}` + 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) + if (fixedOption === 'custom') { + if (!pdsAddressHistory.includes(url)) { + const newHistory = [url, ...pdsAddressHistory.slice(0, 4)] + setPdsAddressHistory(newHistory) + persisted.write('pdsAddressHistory', newHistory) + } } - } - return url - }, - })) + return url + }, + }), + [customAddress, fixedOption, pdsAddressHistory], + ) const isFirstTimeUser = accounts.length === 0