-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPasteInviteLinkScreen.tsx
209 lines (197 loc) · 6.01 KB
/
PasteInviteLinkScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Clipboard from '@react-native-clipboard/clipboard';
import type { NativeStackScreenProps } from '@react-navigation/native-stack';
import {
BRANCH_DOMAIN,
BRANCH_KEY,
DEFAULT_INVITE_LINK_URL,
} from '@tloncorp/app/constants';
import { useBranch, useLureMetadata } from '@tloncorp/app/contexts/branch';
import {
Field,
Pressable,
ScreenHeader,
TextInput,
TlonText,
View,
YStack,
} from '@tloncorp/app/ui';
import { trackError, trackOnboardingAction } from '@tloncorp/app/utils/posthog';
import {
createInviteLinkRegex,
extractNormalizedInviteLink,
getInviteLinkMeta,
} from '@tloncorp/shared';
import { useCallback, useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { Keyboard } from 'react-native';
import type { OnboardingStackParamList } from '../../types';
const INVITE_LINK_REGEX = createInviteLinkRegex();
type Props = NativeStackScreenProps<
OnboardingStackParamList,
'PasteInviteLink'
>;
type FormData = {
inviteLink: string;
};
export const PasteInviteLinkScreen = ({ navigation }: Props) => {
const lureMeta = useLureMetadata();
const { setLure } = useBranch();
const {
control,
formState: { errors },
setValue,
watch,
trigger,
} = useForm<FormData>({
defaultValues: {
inviteLink: DEFAULT_INVITE_LINK_URL,
},
});
const [metadataError, setMetadataError] = useState<string | null>(null);
// watch for changes to the input & check for valid invite links
const inviteLinkValue = watch('inviteLink');
useEffect(() => {
async function handleInviteLinkChange() {
const extractedLink = extractNormalizedInviteLink(inviteLinkValue);
setMetadataError(null);
if (extractedLink) {
try {
const appInvite = await getInviteLinkMeta({
inviteLink: extractedLink,
});
if (appInvite) {
setLure(appInvite);
return;
} else {
throw new Error('Failed to retrieve invite metadata');
}
} catch (e) {
trackError({
message: e.message,
properties: {
inviteLink: extractedLink,
branchDomain: BRANCH_DOMAIN,
branchKey: BRANCH_KEY,
},
});
setMetadataError('Unable to load invite');
}
}
trigger('inviteLink');
}
handleInviteLinkChange();
}, [inviteLinkValue, setLure, trigger]);
// if at any point we have invite metadata, notify & allow them to proceed
// to signup
useEffect(() => {
if (lureMeta) {
trackOnboardingAction({
actionName: 'Invite Link Added',
lure: lureMeta.id,
inviteType:
lureMeta.inviteType && lureMeta.inviteType === 'user'
? 'user'
: 'group',
});
navigation.reset({
index: 0,
routes: [{ name: 'Welcome' }, { name: 'Signup' }],
});
}
}, [lureMeta, navigation]);
// handle paste button click
const onHandlePasteClick = useCallback(async () => {
const clipboardContents = await Clipboard.getString();
setValue('inviteLink', clipboardContents);
}, [setValue]);
return (
<View flex={1} backgroundColor="$secondaryBackground">
<ScreenHeader
title="Claim Invite"
showSessionStatus={false}
backAction={() => navigation.goBack()}
rightControls={
<ScreenHeader.TextButton
disabled={!lureMeta}
onPress={() => navigation.navigate('Signup')}
>
Next
</ScreenHeader.TextButton>
}
/>
<Pressable
flex={1}
pressStyle={{ opacity: 1 }}
onPress={() => Keyboard.dismiss()}
>
<YStack paddingHorizontal="$2xl" gap="$m" flex={1}>
<View padding="$xl" gap="$xl">
<TlonText.Text size="$body" color="$primaryText">
We're growing slowly. Invites let you skip the waitlist
because we know someone wants to talk to you here.
{'\n\n'}
If you used an{' '}
<TlonText.Text fontWeight="500">Invite Link</TlonText.Text> to
download the app, tap it again now or paste it below.
</TlonText.Text>
</View>
<Controller
control={control}
name="inviteLink"
rules={{
pattern: {
value: INVITE_LINK_REGEX,
message: 'Invite link not recognized.',
},
}}
render={({ field: { onChange, onBlur, value } }) => (
<Field
label="Invite Link"
error={metadataError ?? errors.inviteLink?.message}
paddingTop="$l"
>
<TextInput
placeholder="join.tlon.io/0v4.pca0n.evapv..."
onBlur={onBlur}
onChangeText={onChange}
value={value}
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
rightControls={
<TextInput.InnerButton
label="Paste"
onPress={onHandlePasteClick}
/>
}
/>
</Field>
)}
/>
<TlonText.Text
color="$secondaryText"
marginTop="$xl"
textAlign="center"
>
Don't have an invite?{` `}
<Pressable
pressStyle={{
opacity: 0.5,
}}
onPress={() => navigation.navigate('JoinWaitList', {})}
style={{ marginBottom: -3 }}
>
<TlonText.Text
color="$secondaryText"
textDecorationLine="underline"
textDecorationDistance={10}
>
Join the waitlist
</TlonText.Text>
</Pressable>
</TlonText.Text>
</YStack>
</Pressable>
</View>
);
};