22import React , { useState , useCallback } from 'react' ;
33import type { Node } from 'react' ;
44import { Keyboard } from 'react-native' ;
5+ import Clipboard from '@react-native-clipboard/clipboard' ;
56
67import type { RouteProp } from '../react-navigation' ;
78import type { AppNavigationProp } from '../nav/AppNavigator' ;
@@ -15,6 +16,7 @@ import ZulipButton from '../common/ZulipButton';
1516import { tryParseUrl } from '../utils/url' ;
1617import * as api from '../api' ;
1718import { navigateToAuth } from '../actions' ;
19+ import { useClipboardHasURL } from '../@react-native-clipboard/clipboard' ;
1820
1921type Props = $ReadOnly < { |
2022 navigation : AppNavigationProp < 'realm-input' > ,
@@ -77,6 +79,24 @@ export default function RealmInputScreen(props: Props): Node {
7779 button : { marginTop : 8 } ,
7880 } ;
7981
82+ const tryCopiedUrl = useCallback ( async ( ) => {
83+ // The copied string might not be a valid realm URL:
84+ // - It might not be a URL because useClipboardHasURL is subject to
85+ // races (and Clipboard.getString is itself async).
86+ // - It might not be a valid Zulip realm that the client can connect to.
87+ //
88+ // So…
89+ const url = await Clipboard . getString ( ) ;
90+
91+ // …let the user see what string is being tried and edit it if it fails…
92+ setRealmInputValue ( url ) ;
93+
94+ // …and run it through our usual validation.
95+ await tryRealm ( url ) ;
96+ } , [ tryRealm ] ) ;
97+
98+ const clipboardHasURL = useClipboardHasURL ( ) ;
99+
80100 return (
81101 < Screen
82102 title = "Welcome"
@@ -107,6 +127,19 @@ export default function RealmInputScreen(props: Props): Node {
107127 onPress = { handleInputSubmit }
108128 disabled = { urlFromInputValue ( realmInputValue ) === undefined }
109129 />
130+ { clipboardHasURL === true && (
131+ // Recognize when the user has copied a URL, and let them use it
132+ // without making them enter it into the input.
133+ //
134+ // TODO(?): Instead, use a FAB that persists while
135+ // clipboardHasURL !== true && !progress
136+ < ZulipButton
137+ style = { styles . button }
138+ text = "Use copied URL"
139+ progress = { progress }
140+ onPress = { tryCopiedUrl }
141+ />
142+ ) }
110143 </ Screen >
111144 ) ;
112145}
0 commit comments