import React, { useContext, useState } from 'react' import { ScrollView, StyleSheet, View } from 'react-native' import Clipboard from '@react-native-clipboard/clipboard' import { useTranslation } from 'react-i18next' import { RelayPoolContext } from '../../Contexts/RelayPoolContext' import { Relay } from '../../Functions/DatabaseFunctions/Relays' import { defaultRelays, REGEX_SOCKET_LINK } from '../../Constants/Relay' import { List, Switch, AnimatedFAB, useTheme, Text, Button, TextInput, IconButton, Divider, Snackbar, } from 'react-native-paper' import RBSheet from 'react-native-raw-bottom-sheet' export const RelaysPage: React.FC = () => { const defaultRelayInput = React.useMemo(() => 'wss://', []) const { activeRelayItem, desactiveRelayItem, addRelayItem, removeRelayItem, relays } = useContext(RelayPoolContext) const { t } = useTranslation('common') const theme = useTheme() const bottomSheetAddRef = React.useRef(null) const bottomSheetEditRef = React.useRef(null) const [selectedRelay, setSelectedRelay] = useState() const [addRelayInput, setAddRelayInput] = useState(defaultRelayInput) const [showNotification, setShowNotification] = useState() const addRelay: (url: string) => void = (url) => { addRelayItem({ url, active: true, }).then(() => { setShowNotification('add') }) } const removeRelay: (url: string) => void = (url) => { removeRelayItem({ url, }).then(() => { setShowNotification('remove') }) } const activeRelay: (relay: Relay) => void = (relay) => { activeRelayItem(relay).then(() => { setShowNotification('active') }) } const desactiveRelay: (relay: Relay) => void = (relay) => { desactiveRelayItem(relay).then(() => { setShowNotification('desactive') }) } const onPressAddRelay: () => void = () => { if (REGEX_SOCKET_LINK.test(addRelayInput)) { bottomSheetAddRef.current?.close() addRelay(addRelayInput) setAddRelayInput(defaultRelayInput) } else { bottomSheetAddRef.current?.close() setShowNotification('badFormat') } } const rbSheetCustomStyles = React.useMemo(() => { return { container: { backgroundColor: theme.colors.background, paddingTop: 16, paddingRight: 16, paddingBottom: 32, paddingLeft: 16, borderTopRightRadius: 28, borderTopLeftRadius: 28, height: 'auto', }, } }, []) const myRelays = relays.filter((relay) => !defaultRelays.includes(relay.url)) return ( {myRelays.length > 0 && ( <> {t('relaysPage.myList')} {myRelays.length > 0 && myRelays.map((relay, index) => { return ( ( relay.active ? desactiveRelay(relay) : activeRelay(relay) } /> )} onPress={() => { setSelectedRelay(relay) bottomSheetEditRef.current?.open() }} /> ) })} )} {t('relaysPage.recommended')} {defaultRelays.map((url, index) => { const relay = { url, active: relays.find((relay) => relay.url === url && relay.active) !== undefined, } return ( ( (relay.active ? desactiveRelay(relay) : activeRelay(relay))} /> )} onPress={() => { setSelectedRelay(relay) bottomSheetEditRef.current?.open() }} /> ) })} bottomSheetAddRef.current?.open()} animateFrom='right' iconMode='static' extended={false} /> {showNotification && ( setShowNotification(undefined)} onDismiss={() => setShowNotification(undefined)} > {t(`relaysPage.notifications.${showNotification}`)} )} { if (selectedRelay) removeRelay(selectedRelay.url) bottomSheetEditRef.current?.close() }} /> {t('relaysPage.removeRelay')} { if (selectedRelay) Clipboard.setString(selectedRelay.url) }} /> {t('relaysPage.copyRelay')} {selectedRelay?.url.split('wss://')[1]?.split('/')[0]} ) } const styles = StyleSheet.create({ title: { paddingLeft: 16, }, bottomDrawerButton: { paddingBottom: 16, }, container: { padding: 0, paddingBottom: 32, }, list: { padding: 0, paddingBottom: 45, }, snackbar: { margin: 16, bottom: 70, }, fab: { bottom: 16, right: 16, position: 'absolute', }, addRelay: { alignContent: 'center', justifyContent: 'space-between', }, relayActions: { flexDirection: 'row', }, actionButton: { justifyContent: 'center', alignItems: 'center', width: 80, }, divider: { marginBottom: 26, marginTop: 26, }, }) export default RelaysPage