|
|
|
@ -4,6 +4,23 @@ import { useNostr } from '@/providers/NostrProvider' |
|
|
|
import { TMailboxRelay, TMailboxRelayScope } from '@/types' |
|
|
|
import { TMailboxRelay, TMailboxRelayScope } from '@/types' |
|
|
|
import { useEffect, useState } from 'react' |
|
|
|
import { useEffect, useState } from 'react' |
|
|
|
import { useTranslation } from 'react-i18next' |
|
|
|
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 MailboxRelay from './MailboxRelay' |
|
|
|
import NewMailboxRelayInput from './NewMailboxRelayInput' |
|
|
|
import NewMailboxRelayInput from './NewMailboxRelayInput' |
|
|
|
import RelayCountWarning from './RelayCountWarning' |
|
|
|
import RelayCountWarning from './RelayCountWarning' |
|
|
|
@ -15,6 +32,37 @@ export default function MailboxSetting() { |
|
|
|
const [relays, setRelays] = useState<TMailboxRelay[]>([]) |
|
|
|
const [relays, setRelays] = useState<TMailboxRelay[]>([]) |
|
|
|
const [hasChange, setHasChange] = useState(false) |
|
|
|
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(() => { |
|
|
|
useEffect(() => { |
|
|
|
if (!relayList) return |
|
|
|
if (!relayList) return |
|
|
|
|
|
|
|
|
|
|
|
@ -68,16 +116,25 @@ export default function MailboxSetting() { |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<RelayCountWarning relays={relays} /> |
|
|
|
<RelayCountWarning relays={relays} /> |
|
|
|
<SaveButton mailboxRelays={relays} hasChange={hasChange} setHasChange={setHasChange} /> |
|
|
|
<SaveButton mailboxRelays={relays} hasChange={hasChange} setHasChange={setHasChange} /> |
|
|
|
<div className="space-y-2"> |
|
|
|
<DndContext |
|
|
|
{relays.map((relay) => ( |
|
|
|
sensors={sensors} |
|
|
|
<MailboxRelay |
|
|
|
collisionDetection={closestCenter} |
|
|
|
key={relay.url} |
|
|
|
onDragEnd={handleDragEnd} |
|
|
|
mailboxRelay={relay} |
|
|
|
modifiers={[restrictToVerticalAxis, restrictToParentElement]} |
|
|
|
changeMailboxRelayScope={changeMailboxRelayScope} |
|
|
|
> |
|
|
|
removeMailboxRelay={removeMailboxRelay} |
|
|
|
<SortableContext items={relays.map((r) => r.url)} strategy={verticalListSortingStrategy}> |
|
|
|
/> |
|
|
|
<div className="space-y-2"> |
|
|
|
))} |
|
|
|
{relays.map((relay) => ( |
|
|
|
</div> |
|
|
|
<MailboxRelay |
|
|
|
|
|
|
|
key={relay.url} |
|
|
|
|
|
|
|
mailboxRelay={relay} |
|
|
|
|
|
|
|
changeMailboxRelayScope={changeMailboxRelayScope} |
|
|
|
|
|
|
|
removeMailboxRelay={removeMailboxRelay} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
))} |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
</SortableContext> |
|
|
|
|
|
|
|
</DndContext> |
|
|
|
<NewMailboxRelayInput saveNewMailboxRelay={saveNewMailboxRelay} /> |
|
|
|
<NewMailboxRelayInput saveNewMailboxRelay={saveNewMailboxRelay} /> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
) |
|
|
|
) |
|
|
|
|