nostros/frontend/Pages/RelaysPage/index.tsx

229 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-01-14 14:52:10 +00:00
import React, { useContext, useState } from 'react'
2023-01-13 16:36:05 +00:00
import { Clipboard, FlatList, ListRenderItem, StyleSheet, View } from 'react-native'
2022-11-05 18:04:17 +00:00
import { useTranslation } from 'react-i18next'
import { RelayPoolContext } from '../../Contexts/RelayPoolContext'
2023-01-14 14:52:10 +00:00
import { Relay } from '../../Functions/DatabaseFunctions/Relays'
2023-01-13 11:51:30 +00:00
import { defaultRelays, REGEX_SOCKET_LINK } from '../../Constants/Relay'
2023-01-13 16:36:05 +00:00
import {
List,
Switch,
AnimatedFAB,
useTheme,
Text,
Button,
TextInput,
IconButton,
Divider,
2023-01-17 13:21:32 +00:00
Snackbar,
2023-01-13 16:36:05 +00:00
} from 'react-native-paper'
import RBSheet from 'react-native-raw-bottom-sheet'
2023-01-18 18:25:13 +00:00
import { useFocusEffect } from '@react-navigation/native'
2022-11-05 18:04:17 +00:00
export const RelaysPage: React.FC = () => {
2023-01-13 16:36:05 +00:00
const defaultRelayInput = React.useMemo(() => 'wss://', [])
2023-01-14 14:52:10 +00:00
const { addRelayItem, removeRelayItem, relays } = useContext(RelayPoolContext)
2022-11-05 18:04:17 +00:00
const { t } = useTranslation('common')
2023-01-13 16:36:05 +00:00
const theme = useTheme()
const bottomSheetAddRef = React.useRef<RBSheet>(null)
const bottomSheetEditRef = React.useRef<RBSheet>(null)
const [selectedRelay, setSelectedRelay] = useState<Relay>()
const [addRelayInput, setAddRelayInput] = useState<string>(defaultRelayInput)
2023-01-16 20:06:12 +00:00
const [showNotification, setShowNotification] = useState<string>()
2022-11-05 18:04:17 +00:00
2023-01-14 14:52:10 +00:00
const addRelay: (url: string) => void = (url) => {
addRelayItem({
url,
}).then(() => {
setShowNotification('add')
})
2022-11-05 18:04:17 +00:00
}
2023-01-14 14:52:10 +00:00
const removeRelay: (url: string) => void = (url) => {
removeRelayItem({
url,
}).then(() => {
setShowNotification('remove')
})
2022-11-05 18:04:17 +00:00
}
2023-01-02 21:17:16 +00:00
const onPressAddRelay: () => void = () => {
2023-01-13 16:42:12 +00:00
if (REGEX_SOCKET_LINK.test(addRelayInput)) {
2023-01-13 16:36:05 +00:00
bottomSheetAddRef.current?.close()
2023-01-14 20:35:35 +00:00
2023-01-13 16:36:05 +00:00
setAddRelayInput(defaultRelayInput)
2023-01-02 21:17:16 +00:00
} else {
2023-01-13 16:36:05 +00:00
bottomSheetAddRef.current?.close()
setShowNotification('badFormat')
2023-01-02 21:17:16 +00:00
}
}
2022-11-05 18:04:17 +00:00
const defaultList: () => Relay[] = () => {
return defaultRelays
.filter((url) => !relays?.find((item) => item.url === url))
.map((url) => {
return {
url,
}
})
}
2023-01-13 16:36:05 +00:00
const relayToggle: (relay: Relay) => JSX.Element = (relay) => {
const active = relays?.some((item) => item.url === relay.url)
const onValueChange: () => void = () => {
2023-01-14 14:52:10 +00:00
active ? removeRelay(relay.url) : addRelay(relay.url)
2023-01-13 16:36:05 +00:00
}
return <Switch value={active} onValueChange={onValueChange} />
}
const renderItem: ListRenderItem<Relay> = ({ index, item }) => (
<List.Item
key={index}
2023-01-13 16:42:12 +00:00
title={item.url.split('wss://')[1]?.split('/')[0]}
2023-01-13 16:36:05 +00:00
right={() => relayToggle(item)}
onPress={() => {
setSelectedRelay(item)
bottomSheetEditRef.current?.open()
}}
/>
)
2022-11-05 18:04:17 +00:00
2023-01-14 20:35:35 +00:00
const rbSheetCustomStyles = React.useMemo(() => {
return {
container: {
...styles.rbsheetContainer,
backgroundColor: theme.colors.background,
},
draggableIcon: styles.rbsheetDraggableIcon,
}
}, [])
2022-11-05 18:04:17 +00:00
return (
2023-01-13 16:36:05 +00:00
<View style={styles.container}>
<FlatList style={styles.list} data={[...relays, ...defaultList()]} renderItem={renderItem} />
<AnimatedFAB
style={styles.fab}
2023-01-16 09:03:01 +00:00
icon='plus'
label='Add'
2023-01-13 16:36:05 +00:00
onPress={() => bottomSheetAddRef.current?.open()}
2023-01-16 09:03:01 +00:00
animateFrom='right'
iconMode='static'
2023-01-13 16:36:05 +00:00
extended={false}
/>
2023-01-16 20:06:12 +00:00
{showNotification && (
2023-01-17 13:21:32 +00:00
<Snackbar
style={styles.snackbar}
visible={showNotification !== undefined}
duration={Snackbar.DURATION_SHORT}
onIconPress={() => setShowNotification(undefined)}
onDismiss={() => setShowNotification(undefined)}
2023-01-16 20:06:12 +00:00
>
{t(`relaysPage.notifications.${showNotification}`)}
2023-01-17 13:21:32 +00:00
</Snackbar>
2023-01-16 20:06:12 +00:00
)}
2023-01-16 12:09:18 +00:00
<RBSheet
ref={bottomSheetAddRef}
closeOnDragDown={true}
height={260}
customStyles={rbSheetCustomStyles}
>
2023-01-13 16:36:05 +00:00
<View>
<TextInput
mode='outlined'
label={t('relaysPage.labelAdd') ?? ''}
onChangeText={setAddRelayInput}
value={addRelayInput}
/>
<Button mode='contained' onPress={onPressAddRelay}>
{t('relaysPage.add')}
</Button>
<Button
mode='outlined'
onPress={() => {
bottomSheetAddRef.current?.close()
setAddRelayInput(defaultRelayInput)
}}
>
{t('relaysPage.cancel')}
</Button>
</View>
</RBSheet>
<RBSheet
ref={bottomSheetEditRef}
closeOnDragDown={true}
height={260}
2023-01-14 20:35:35 +00:00
customStyles={rbSheetCustomStyles}
2023-01-13 16:36:05 +00:00
>
<View>
<View style={styles.relayActions}>
<View style={styles.actionButton}>
<IconButton
icon='trash-can-outline'
size={28}
onPress={() => {
2023-01-14 14:52:10 +00:00
if (selectedRelay) removeRelay(selectedRelay.url)
2023-01-13 16:36:05 +00:00
bottomSheetEditRef.current?.close()
}}
/>
<Text>{t('relaysPage.removeRelay')}</Text>
</View>
<View style={styles.actionButton}>
<IconButton
icon='content-copy'
size={28}
onPress={() => {
if (selectedRelay) Clipboard.setString(selectedRelay.url)
}}
/>
<Text>{t('relaysPage.copyRelay')}</Text>
</View>
</View>
2023-01-14 20:35:35 +00:00
<Divider style={styles.divider} />
2023-01-13 16:42:12 +00:00
<Text variant='titleLarge'>{selectedRelay?.url.split('wss://')[1]?.split('/')[0]}</Text>
2023-01-13 16:36:05 +00:00
</View>
</RBSheet>
</View>
2022-11-05 18:04:17 +00:00
)
}
2023-01-13 16:36:05 +00:00
const styles = StyleSheet.create({
container: {
padding: 0,
},
list: {
padding: 0,
},
snackbar: {
margin: 16,
bottom: 70,
},
fab: {
bottom: 16,
right: 16,
position: 'absolute',
},
rbsheetDraggableIcon: {
backgroundColor: '#000',
},
rbsheetContainer: {
padding: 16,
borderTopRightRadius: 28,
borderTopLeftRadius: 28,
},
relayActions: {
flexDirection: 'row',
},
actionButton: {
justifyContent: 'center',
alignItems: 'center',
2023-01-14 20:35:35 +00:00
width: 80,
2023-01-13 16:36:05 +00:00
},
divider: {
marginBottom: 26,
2023-01-14 20:35:35 +00:00
marginTop: 26,
},
2023-01-13 16:36:05 +00:00
})
2022-11-05 18:04:17 +00:00
export default RelaysPage