tooot/src/components/Instance/index.tsx

346 lines
11 KiB
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import Button from '@components/Button'
import Icon from '@components/Icon'
2022-12-04 00:35:13 +01:00
import browserPackage from '@helpers/browserPackage'
2022-12-10 01:59:26 +01:00
import { redirectUri, useAppsMutation } from '@utils/queryHooks/apps'
2021-01-11 21:36:57 +01:00
import { useInstanceQuery } from '@utils/queryHooks/instance'
2022-12-10 01:59:26 +01:00
import { checkInstanceFeature, getInstances } from '@utils/slices/instancesSlice'
2021-01-07 19:13:09 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-10 01:59:26 +01:00
import * as AuthSession from 'expo-auth-session'
2021-01-26 12:17:25 +01:00
import * as WebBrowser from 'expo-web-browser'
2021-01-07 19:13:09 +01:00
import { debounce } from 'lodash'
2022-12-10 01:59:26 +01:00
import React, { RefObject, useCallback, useState } from 'react'
2022-06-10 19:41:51 +02:00
import { Trans, useTranslation } from 'react-i18next'
2022-11-29 23:44:11 +01:00
import { Alert, Image, KeyboardAvoidingView, Platform, TextInput, View } from 'react-native'
2021-04-09 21:43:12 +02:00
import { ScrollView } from 'react-native-gesture-handler'
2021-02-09 01:16:12 +01:00
import { useSelector } from 'react-redux'
2021-03-06 21:01:38 +01:00
import { Placeholder } from 'rn-placeholder'
2022-12-18 00:00:58 +01:00
import validUrl from 'valid-url'
2022-12-10 01:59:26 +01:00
import InstanceInfo from './Info'
import CustomText from '../Text'
import { useNavigation } from '@react-navigation/native'
import { TabMeStackNavigationProp } from '@utils/navigation/navigators'
import queryClient from '@helpers/queryClient'
import { useAppDispatch } from '@root/store'
import addInstance from '@utils/slices/instances/add'
2021-01-07 19:13:09 +01:00
export interface Props {
2021-04-09 21:43:12 +02:00
scrollViewRef?: RefObject<ScrollView>
2021-01-07 19:13:09 +01:00
disableHeaderImage?: boolean
goBack?: boolean
}
const ComponentInstance: React.FC<Props> = ({
2021-04-09 21:43:12 +02:00
scrollViewRef,
2021-01-07 19:13:09 +01:00
disableHeaderImage,
goBack = false
}) => {
2021-02-09 01:16:12 +01:00
const { t } = useTranslation('componentInstance')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2022-12-10 01:59:26 +01:00
const navigation = useNavigation<TabMeStackNavigationProp<'Tab-Me-Root' | 'Tab-Me-Switch'>>()
2021-01-20 12:35:24 +01:00
2022-12-10 01:59:26 +01:00
const [domain, setDomain] = useState<string>('')
2022-12-18 00:00:58 +01:00
const [errorCode, setErrorCode] = useState<number | null>(null)
const whitelisted: boolean =
!!domain.length &&
!!errorCode &&
!!validUrl.isHttpsUri(`https://${domain}`) &&
errorCode === 401
2021-01-07 19:13:09 +01:00
2022-12-10 01:59:26 +01:00
const dispatch = useAppDispatch()
const instances = useSelector(getInstances, () => true)
2021-01-11 21:36:57 +01:00
const instanceQuery = useInstanceQuery({
2021-02-20 19:12:44 +01:00
domain,
2022-12-18 00:00:58 +01:00
options: {
enabled: !!domain,
retry: false,
onError: err => {
if (err.status) {
setErrorCode(err.status)
}
}
}
2021-01-07 19:13:09 +01:00
})
2022-12-10 01:59:26 +01:00
const deprecateAuthFollow = useSelector(checkInstanceFeature('deprecate_auth_follow'))
const appsMutation = useAppsMutation({
retry: false,
onSuccess: async (data, variables) => {
const clientId = data.client_id
const clientSecret = data.client_secret
const discovery = { authorizationEndpoint: `https://${domain}/oauth/authorize` }
const request = new AuthSession.AuthRequest({
clientId,
clientSecret,
scopes: deprecateAuthFollow
? ['read', 'write', 'push']
: ['read', 'write', 'follow', 'push'],
redirectUri
})
await request.makeAuthUrlAsync(discovery)
const promptResult = await request.promptAsync(discovery)
if (promptResult?.type === 'success') {
const { accessToken } = await AuthSession.exchangeCodeAsync(
{
clientId,
clientSecret,
scopes: ['read', 'write', 'follow', 'push'],
redirectUri,
code: promptResult.params.code,
extraParams: { grant_type: 'authorization_code' }
},
{ tokenEndpoint: `https://${variables.domain}/oauth/token` }
)
queryClient.clear()
dispatch(
addInstance({
domain,
token: accessToken,
instance: instanceQuery.data!,
appData: { clientId, clientSecret }
})
)
goBack && navigation.goBack()
}
}
})
2021-01-07 19:13:09 +01:00
const processUpdate = useCallback(() => {
2021-02-20 19:12:44 +01:00
if (domain) {
2022-11-29 23:44:11 +01:00
if (instances && instances.filter(instance => instance.url === domain).length) {
2021-02-20 19:12:44 +01:00
Alert.alert(t('update.alert.title'), t('update.alert.message'), [
{
text: t('common:buttons.cancel'),
2021-02-20 19:12:44 +01:00
style: 'cancel'
},
{
text: t('common:buttons.continue'),
2022-12-10 01:59:26 +01:00
onPress: () => appsMutation.mutate({ domain })
2021-02-20 19:12:44 +01:00
}
])
2021-02-09 01:16:12 +01:00
} else {
2022-12-10 01:59:26 +01:00
appsMutation.mutate({ domain })
2021-01-07 19:13:09 +01:00
}
}
2021-02-20 19:12:44 +01:00
}, [domain])
2021-01-07 19:13:09 +01:00
return (
2021-03-06 21:01:38 +01:00
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
>
2021-01-07 19:13:09 +01:00
{!disableHeaderImage ? (
<View style={{ flexDirection: 'row' }}>
2021-01-07 19:13:09 +01:00
<Image
2021-03-28 23:31:10 +02:00
source={require('assets/images/welcome.png')}
style={{ resizeMode: 'contain', flex: 1, aspectRatio: 16 / 9 }}
2021-01-07 19:13:09 +01:00
/>
</View>
) : null}
<View
style={{
marginTop: StyleConstants.Spacing.L,
marginHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
>
<View
style={{
flexDirection: 'row',
marginHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
>
<TextInput
2021-04-09 21:43:12 +02:00
accessible={false}
accessibilityRole='none'
style={{
borderBottomWidth: 1,
...StyleConstants.FontStyle.M,
color: colors.primaryDefault,
2022-12-18 00:00:58 +01:00
borderBottomColor: instanceQuery.isError
? whitelisted
? colors.yellow
: colors.red
: colors.border,
2022-12-11 14:08:27 +01:00
...(Platform.OS === 'android' && { paddingRight: 0 })
}}
editable={false}
2021-05-10 23:45:54 +02:00
defaultValue='https://'
/>
2021-01-07 19:13:09 +01:00
<TextInput
style={{
flex: 1,
borderBottomWidth: 1,
...StyleConstants.FontStyle.M,
marginRight: StyleConstants.Spacing.M,
color: colors.primaryDefault,
2022-12-18 00:00:58 +01:00
borderBottomColor: instanceQuery.isError
? whitelisted
? colors.yellow
: colors.red
: colors.border,
2022-12-11 14:08:27 +01:00
...(Platform.OS === 'android' && { paddingLeft: 0 })
}}
2022-12-18 00:00:58 +01:00
onChangeText={debounce(
text => {
setDomain(text.replace(/^http(s)?\:\/\//i, ''))
setErrorCode(null)
},
1000,
{
trailing: true
}
)}
2021-01-07 19:13:09 +01:00
autoCapitalize='none'
clearButtonMode='never'
keyboardType='url'
textContentType='URL'
2022-08-07 01:18:10 +02:00
onSubmitEditing={({ nativeEvent: { text } }) => {
if (
text === domain &&
instanceQuery.isSuccess &&
instanceQuery.data &&
instanceQuery.data.uri
) {
processUpdate()
}
}}
2021-04-09 21:43:12 +02:00
placeholder={' ' + t('server.textInput.placeholder')}
2022-02-12 14:51:01 +01:00
placeholderTextColor={colors.secondary}
2021-01-07 19:13:09 +01:00
returnKeyType='go'
keyboardAppearance={mode}
2021-04-09 21:43:12 +02:00
{...(scrollViewRef && {
onFocus: () =>
2022-11-29 23:44:11 +01:00
setTimeout(() => scrollViewRef.current?.scrollTo({ y: 0, animated: true }), 150)
2021-04-09 21:43:12 +02:00
})}
autoCorrect={false}
spellCheck={false}
2021-01-07 19:13:09 +01:00
/>
<Button
type='text'
2021-02-20 19:12:44 +01:00
content={t('server.button')}
2021-01-07 19:13:09 +01:00
onPress={processUpdate}
2022-12-18 00:00:58 +01:00
disabled={!instanceQuery.data?.uri && !whitelisted}
2022-12-10 01:59:26 +01:00
loading={instanceQuery.isFetching || appsMutation.isLoading}
2021-01-07 19:13:09 +01:00
/>
</View>
2021-01-26 12:17:25 +01:00
2021-01-07 19:13:09 +01:00
<View>
2022-12-18 00:00:58 +01:00
{whitelisted ? (
<CustomText
fontStyle='S'
style={{
color: colors.yellow,
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
paddingTop: StyleConstants.Spacing.XS
}}
>
{t('server.whitelisted')}
</CustomText>
) : (
<Placeholder>
2021-01-23 02:41:50 +01:00
<InstanceInfo
2022-12-18 00:00:58 +01:00
header={t('server.information.name')}
content={instanceQuery.data?.title || undefined}
potentialWidth={2}
2021-01-23 02:41:50 +01:00
/>
2022-12-18 00:00:58 +01:00
<View style={{ flex: 1, flexDirection: 'row' }}>
<InstanceInfo
style={{ alignItems: 'flex-start' }}
header={t('server.information.accounts')}
content={instanceQuery.data?.stats?.user_count?.toString() || undefined}
potentialWidth={4}
/>
<InstanceInfo
style={{ alignItems: 'center' }}
header={t('server.information.statuses')}
content={instanceQuery.data?.stats?.status_count?.toString() || undefined}
potentialWidth={4}
/>
<InstanceInfo
style={{ alignItems: 'flex-end' }}
header={t('server.information.domains')}
content={instanceQuery.data?.stats?.domain_count?.toString() || undefined}
potentialWidth={4}
/>
</View>
</Placeholder>
)}
<View
style={{
flexDirection: 'row',
marginHorizontal: StyleConstants.Spacing.Global.PagePadding,
2022-06-10 19:41:51 +02:00
marginTop: StyleConstants.Spacing.M
}}
>
2021-02-09 01:16:12 +01:00
<Icon
name='Lock'
size={StyleConstants.Font.Size.S}
2022-02-12 14:51:01 +01:00
color={colors.secondary}
style={{
2022-11-29 23:44:11 +01:00
marginTop: (StyleConstants.Font.LineHeight.S - StyleConstants.Font.Size.S) / 2,
marginRight: StyleConstants.Spacing.XS
}}
2021-02-09 01:16:12 +01:00
/>
2022-11-29 23:44:11 +01:00
<CustomText fontStyle='S' style={{ flex: 1, color: colors.secondary }}>
2021-02-09 01:16:12 +01:00
{t('server.disclaimer.base')}
2022-06-10 19:41:51 +02:00
</CustomText>
</View>
<View
style={{
flexDirection: 'row',
marginHorizontal: StyleConstants.Spacing.Global.PagePadding,
marginBottom: StyleConstants.Spacing.M
}}
>
<Icon
name='CheckSquare'
size={StyleConstants.Font.Size.S}
color={colors.secondary}
style={{
2022-11-29 23:44:11 +01:00
marginTop: (StyleConstants.Font.LineHeight.S - StyleConstants.Font.Size.S) / 2,
2022-06-10 19:41:51 +02:00
marginRight: StyleConstants.Spacing.XS
}}
/>
<CustomText
fontStyle='S'
style={{ flex: 1, color: colors.secondary }}
accessibilityRole='link'
>
<Trans
i18nKey='componentInstance:server.terms.base'
components={[
<CustomText
accessible
style={{ color: colors.blue }}
2022-12-04 00:35:13 +01:00
onPress={async () =>
WebBrowser.openBrowserAsync('https://tooot.app/privacy-policy', {
2022-12-16 22:00:22 +01:00
...(await browserPackage())
2022-12-04 00:35:13 +01:00
})
}
2022-06-10 19:41:51 +02:00
/>,
<CustomText
accessible
style={{ color: colors.blue }}
2022-12-04 00:35:13 +01:00
onPress={async () =>
WebBrowser.openBrowserAsync('https://tooot.app/terms-of-service', {
2022-12-16 22:00:22 +01:00
...(await browserPackage())
2022-12-04 00:35:13 +01:00
})
2022-11-29 23:44:11 +01:00
}
2022-06-10 19:41:51 +02:00
/>
]}
/>
</CustomText>
2021-02-09 01:16:12 +01:00
</View>
2021-01-07 19:13:09 +01:00
</View>
</View>
2021-03-06 21:01:38 +01:00
</KeyboardAvoidingView>
2021-01-07 19:13:09 +01:00
)
}
export default ComponentInstance