From 499e53be9a4dfe98d3408f048081ecbb2ecb248e Mon Sep 17 00:00:00 2001 From: linonetwo Date: Sun, 11 Feb 2024 02:38:07 +0800 Subject: [PATCH] fix: server picker can't fill name and url --- .../MainMenu/AddNewServerModelContent.tsx | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/pages/MainMenu/AddNewServerModelContent.tsx b/src/pages/MainMenu/AddNewServerModelContent.tsx index 3dea630..f734c03 100644 --- a/src/pages/MainMenu/AddNewServerModelContent.tsx +++ b/src/pages/MainMenu/AddNewServerModelContent.tsx @@ -34,36 +34,24 @@ export function AddNewServerModelContent({ id, onClose }: WikiEditModalProps): J const [qrScannerOpen, setQrScannerOpen] = useState(false); const [addServerToWiki] = useWorkspaceStore(state => [state.addServer]); const [addServer, updateServer] = useServerStore(state => [state.add, state.update]); - const [scannedString, setScannedString] = useState(''); const [serverName, setServerName] = useState(''); - const [serverUrl, setServerUrl] = useState(); + const [serverUrlString, setServerUrlString] = useState(''); const pickerStyle = { color: theme.colors.onSurface, backgroundColor: theme.colors.surface }; const [servers, availableServersToPick] = useServerStore( state => [state.servers, Object.entries(state.servers).map(([id, server]) => ({ id, label: `${server.name} (${server.uri})` }))], ); - const [pickerSelectedServerID, setPickerSelectedServerID] = useState(''); + const [pickerSelectedServerID, setPickerSelectedServerID] = useState(availableServersToPick?.[0]?.id ?? ''); const handleFillSelectedServer = useCallback(() => { if (pickerSelectedServerID && wiki !== undefined) { const selectedServer = servers[pickerSelectedServerID]; if (selectedServer !== undefined) { - setServerUrl(new URL(selectedServer.uri)); + setServerUrlString(selectedServer.uri); + setServerName(selectedServer.name); } - setPickerSelectedServerID(''); } }, [pickerSelectedServerID, servers, wiki]); - useEffect(() => { - if (scannedString !== '') { - try { - const url = new URL(scannedString); - setServerUrl(new URL(url.origin)); - } catch (error) { - console.warn('Not a valid URL', error); - } - } - }, [scannedString]); - useEffect(() => { void (async () => { const { status } = await BarCodeScanner.requestPermissionsAsync(); @@ -76,7 +64,7 @@ export function AddNewServerModelContent({ id, onClose }: WikiEditModalProps): J if (type === BarCodeScanner.Constants.BarCodeType.qr) { try { setQrScannerOpen(false); - setScannedString(data); + setServerUrlString(data); } catch (error) { console.warn('Not a valid URL', error); } @@ -84,14 +72,15 @@ export function AddNewServerModelContent({ id, onClose }: WikiEditModalProps): J }, []); const addServerForWiki = useCallback(() => { - if (id === undefined || serverUrl?.origin === undefined) return; + if (id === undefined) return; + const serverUrl = new URL(serverUrlString); const newServer = addServer({ uri: serverUrl.origin, name: serverName }); void nativeService.getLocationWithTimeout().then(coords => { if (coords !== undefined) updateServer({ id: newServer.id, location: { coords } }); }); addServerToWiki(id, newServer.id); onClose(); - }, [addServer, addServerToWiki, id, onClose, serverName, serverUrl?.origin, updateServer]); + }, [addServer, addServerToWiki, id, onClose, serverName, serverUrlString, updateServer]); if (id === undefined || wiki === undefined) { return ( @@ -125,6 +114,8 @@ export function AddNewServerModelContent({ id, onClose }: WikiEditModalProps): J style={pickerStyle} selectedValue={pickerSelectedServerID} onValueChange={(itemValue) => { + // DEBUG: console itemValue + console.log(`itemValue`, itemValue); setPickerSelectedServerID(itemValue); }} > @@ -137,9 +128,9 @@ export function AddNewServerModelContent({ id, onClose }: WikiEditModalProps): J )} { - setScannedString(newText); + setServerUrlString(newText); }} />