|
| 1 | +import * as C from '@/constants' |
| 2 | +import * as Kb from '@/common-adapters' |
| 3 | +import * as Tabs from '@/constants/tabs' |
| 4 | +import * as React from 'react' |
| 5 | +import {Linking} from 'react-native' |
| 6 | + |
| 7 | +type InitialStateState = 'init' | 'loading' | 'loaded' |
| 8 | + |
| 9 | +const argArrayGood = (arr: Array<string>, len: number) => { |
| 10 | + return arr.length === len && arr.every(p => !!p.length) |
| 11 | +} |
| 12 | +const isValidLink = (link: string) => { |
| 13 | + const urlPrefix = 'https://keybase.io/' |
| 14 | + if (link.startsWith(urlPrefix)) { |
| 15 | + if (link.substring(urlPrefix.length).split('/').length === 1) { |
| 16 | + return true |
| 17 | + } |
| 18 | + } |
| 19 | + const prefix = 'keybase://' |
| 20 | + if (!link.startsWith(prefix)) { |
| 21 | + return false |
| 22 | + } |
| 23 | + const path = link.substring(prefix.length) |
| 24 | + const [root, ...parts] = path.split('/') |
| 25 | + |
| 26 | + switch (root) { |
| 27 | + case 'profile': |
| 28 | + switch (parts[0]) { |
| 29 | + case 'new-proof': |
| 30 | + return argArrayGood(parts, 2) || argArrayGood(parts, 3) |
| 31 | + case 'show': |
| 32 | + return argArrayGood(parts, 2) |
| 33 | + default: |
| 34 | + } |
| 35 | + return false |
| 36 | + case 'private': |
| 37 | + return true |
| 38 | + case 'public': |
| 39 | + return true |
| 40 | + case 'team': |
| 41 | + return true |
| 42 | + case 'convid': |
| 43 | + return argArrayGood(parts, 1) |
| 44 | + case 'chat': |
| 45 | + return argArrayGood(parts, 1) || argArrayGood(parts, 2) |
| 46 | + case 'team-page': |
| 47 | + return argArrayGood(parts, 3) |
| 48 | + case 'incoming-share': |
| 49 | + return true |
| 50 | + case 'team-invite-link': |
| 51 | + return argArrayGood(parts, 1) |
| 52 | + case 'settingsPushPrompt': |
| 53 | + return true |
| 54 | + default: |
| 55 | + return false |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export const useInitialState = (loggedInLoaded: boolean) => { |
| 60 | + const config = C.useConfigState( |
| 61 | + C.useShallow(s => { |
| 62 | + const {androidShare, loggedIn, startup} = s |
| 63 | + return {androidShare, loggedIn, startup} |
| 64 | + }) |
| 65 | + ) |
| 66 | + const {androidShare, loggedIn, startup} = config |
| 67 | + const {tab: startupTab, followUser: startupFollowUser, loaded: startupLoaded} = startup |
| 68 | + let {conversation: startupConversation} = startup |
| 69 | + |
| 70 | + if (!C.Chat.isValidConversationIDKey(startupConversation)) { |
| 71 | + startupConversation = '' |
| 72 | + } |
| 73 | + |
| 74 | + const showMonster = C.usePushState(s => { |
| 75 | + const {hasPermissions, justSignedUp, showPushPrompt} = s |
| 76 | + return loggedIn && !justSignedUp && showPushPrompt && !hasPermissions |
| 77 | + }) |
| 78 | + |
| 79 | + const [initialState, setInitialState] = React.useState<undefined | object>(undefined) |
| 80 | + const [initialStateState, setInitialStateState] = React.useState<InitialStateState>('init') |
| 81 | + |
| 82 | + React.useEffect(() => { |
| 83 | + if (!startupLoaded) return |
| 84 | + |
| 85 | + if (!loggedInLoaded) { |
| 86 | + return |
| 87 | + } |
| 88 | + if (initialStateState !== 'init') { |
| 89 | + return |
| 90 | + } |
| 91 | + setInitialStateState('loading') |
| 92 | + const loadInitialURL = async () => { |
| 93 | + let url = await Linking.getInitialURL() |
| 94 | + |
| 95 | + // don't try and resume or follow links if we're signed out |
| 96 | + if (!loggedIn) { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + if (!url && showMonster) { |
| 101 | + url = 'keybase://settingsPushPrompt' |
| 102 | + } |
| 103 | + if (!url && androidShare) { |
| 104 | + url = `keybase://incoming-share` |
| 105 | + } |
| 106 | + |
| 107 | + if (url && isValidLink(url)) { |
| 108 | + setTimeout(() => url && C.useDeepLinksState.getState().dispatch.handleAppLink(url), 1) |
| 109 | + } else if (startupFollowUser && !startupConversation) { |
| 110 | + url = `keybase://profile/show/${startupFollowUser}` |
| 111 | + |
| 112 | + if (isValidLink(url)) { |
| 113 | + const initialTabState = { |
| 114 | + state: { |
| 115 | + index: 1, |
| 116 | + routes: [{name: 'peopleRoot'}, {name: 'profile', params: {username: startupFollowUser}}], |
| 117 | + }, |
| 118 | + } |
| 119 | + setInitialState({ |
| 120 | + index: 0, |
| 121 | + routes: [ |
| 122 | + { |
| 123 | + name: 'loggedIn', |
| 124 | + state: { |
| 125 | + index: 0, |
| 126 | + routeNames: [Tabs.peopleTab], |
| 127 | + routes: [{name: Tabs.peopleTab, ...initialTabState}], |
| 128 | + }, |
| 129 | + }, |
| 130 | + ], |
| 131 | + }) |
| 132 | + } |
| 133 | + } else if (startupTab || startupConversation) { |
| 134 | + try { |
| 135 | + const tab = startupConversation ? Tabs.chatTab : startupTab |
| 136 | + C.Chat.useState_.getState().dispatch.unboxRows([startupConversation]) |
| 137 | + C.Chat.getConvoState_(startupConversation).dispatch.loadMoreMessages({ |
| 138 | + reason: 'savedLastState', |
| 139 | + }) |
| 140 | + |
| 141 | + const initialTabState = startupConversation |
| 142 | + ? { |
| 143 | + state: { |
| 144 | + index: 1, |
| 145 | + routes: [ |
| 146 | + {name: 'chatRoot'}, |
| 147 | + {name: 'chatConversation', params: {conversationIDKey: startupConversation}}, |
| 148 | + ], |
| 149 | + }, |
| 150 | + } |
| 151 | + : {} |
| 152 | + |
| 153 | + setInitialState({ |
| 154 | + index: 0, |
| 155 | + routes: [ |
| 156 | + { |
| 157 | + name: 'loggedIn', |
| 158 | + state: { |
| 159 | + index: 0, |
| 160 | + routeNames: [tab], |
| 161 | + routes: [{name: tab, ...initialTabState}], |
| 162 | + }, |
| 163 | + }, |
| 164 | + ], |
| 165 | + }) |
| 166 | + } catch {} |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + const f = async () => { |
| 171 | + await loadInitialURL() |
| 172 | + setInitialStateState('loaded') |
| 173 | + } |
| 174 | + |
| 175 | + C.ignorePromise(f()) |
| 176 | + }, [ |
| 177 | + androidShare, |
| 178 | + initialState, |
| 179 | + initialStateState, |
| 180 | + loggedIn, |
| 181 | + loggedInLoaded, |
| 182 | + showMonster, |
| 183 | + startupConversation, |
| 184 | + startupFollowUser, |
| 185 | + startupLoaded, |
| 186 | + startupTab, |
| 187 | + ]) |
| 188 | + |
| 189 | + return {initialState, initialStateState} |
| 190 | +} |
| 191 | + |
| 192 | +// on android we rerender everything on dark mode changes |
| 193 | +export const useRootKey = () => { |
| 194 | + const [rootKey, setRootKey] = React.useState('') |
| 195 | + const isDarkMode = Kb.Styles.useIsDarkMode() |
| 196 | + React.useEffect(() => { |
| 197 | + if (!C.isAndroid) return |
| 198 | + setRootKey(isDarkMode ? 'android-dark' : 'android-light') |
| 199 | + }, [isDarkMode]) |
| 200 | + |
| 201 | + return rootKey |
| 202 | +} |
0 commit comments