import { Button } from '@/components/ui/button' import { normalizeUrl } from '@/lib/url' import { useNostr } from '@/providers/NostrProvider' import { TMailboxRelay, TMailboxRelayScope } from '@/types' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { DndContext, closestCenter, KeyboardSensor, PointerSensor, TouchSensor, useSensor, useSensors, DragEndEvent } from '@dnd-kit/core' import { arrayMove, SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy } from '@dnd-kit/sortable' import { restrictToVerticalAxis, restrictToParentElement } from '@dnd-kit/modifiers' import MailboxRelay from './MailboxRelay' import NewMailboxRelayInput from './NewMailboxRelayInput' import RelayCountWarning from './RelayCountWarning' import SaveButton from './SaveButton' export default function MailboxSetting() { const { t } = useTranslation() const { pubkey, relayList, checkLogin } = useNostr() const [relays, setRelays] = useState([]) const [hasChange, setHasChange] = useState(false) const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { distance: 8 } }), useSensor(TouchSensor, { activationConstraint: { delay: 200, tolerance: 8 } }), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }) ) function handleDragEnd(event: DragEndEvent) { const { active, over } = event if (active.id !== over?.id) { const oldIndex = relays.findIndex((relay) => relay.url === active.id) const newIndex = relays.findIndex((relay) => relay.url === over?.id) if (oldIndex !== -1 && newIndex !== -1) { setRelays((relays) => arrayMove(relays, oldIndex, newIndex)) setHasChange(true) } } } useEffect(() => { if (!relayList) return setRelays(relayList.originalRelays) }, [relayList]) if (!pubkey) { return (
) } if (!relayList) { return
{t('loading...')}
} const changeMailboxRelayScope = (url: string, scope: TMailboxRelayScope) => { setRelays((prev) => prev.map((r) => (r.url === url ? { ...r, scope } : r))) setHasChange(true) } const removeMailboxRelay = (url: string) => { setRelays((prev) => prev.filter((r) => r.url !== url)) setHasChange(true) } const saveNewMailboxRelay = (url: string) => { if (url === '') return null const normalizedUrl = normalizeUrl(url) if (!normalizedUrl) { return t('Invalid relay URL') } if (relays.some((r) => r.url === normalizedUrl)) { return t('Relay already exists') } setRelays([...relays, { url: normalizedUrl, scope: 'both' }]) setHasChange(true) return null } return (
{t('read relays description')}
{t('write relays description')}
{t('read & write relays notice')}
r.url)} strategy={verticalListSortingStrategy}>
{relays.map((relay) => ( ))}
) }