2
2
import React , { useState , useCallback } from 'react' ;
3
3
import type { Node } from 'react' ;
4
4
import { Keyboard } from 'react-native' ;
5
+ import Clipboard from '@react-native-clipboard/clipboard' ;
5
6
6
7
import type { RouteProp } from '../react-navigation' ;
7
8
import type { AppNavigationProp } from '../nav/AppNavigator' ;
@@ -15,6 +16,7 @@ import ZulipButton from '../common/ZulipButton';
15
16
import { tryParseUrl } from '../utils/url' ;
16
17
import * as api from '../api' ;
17
18
import { navigateToAuth } from '../actions' ;
19
+ import { useClipboardHasURL } from '../@react-native-clipboard/clipboard' ;
18
20
19
21
type Props = $ReadOnly < { |
20
22
navigation : AppNavigationProp < 'realm-input' > ,
@@ -77,6 +79,24 @@ export default function RealmInputScreen(props: Props): Node {
77
79
button : { marginTop : 8 } ,
78
80
} ;
79
81
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
+
80
100
return (
81
101
< Screen
82
102
title = "Welcome"
@@ -107,6 +127,19 @@ export default function RealmInputScreen(props: Props): Node {
107
127
onPress = { handleInputSubmit }
108
128
disabled = { urlFromInputValue ( realmInputValue ) === undefined }
109
129
/>
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
+ ) }
110
143
</ Screen >
111
144
) ;
112
145
}
0 commit comments