import { createForm } from '@felte/solid' import { validator } from '@felte/validator-zod' import { useI18n } from '@solid-primitives/i18n' import { IconX } from '@tabler/icons-solidjs' import type { DragEventHandler, Draggable, Droppable, } from '@thisbeyond/solid-dnd' import { DragDropProvider, DragDropSensors, DragOverlay, SortableProvider, closestCenter, createSortable, useDragDropContext, } from '@thisbeyond/solid-dnd' import { uniq } from 'lodash' import { Component, For, Index, Show, createSignal } from 'solid-js' import { z } from 'zod' import { Button, ConfigTitle } from '~/components' import { CONNECTIONS_TABLE_ACCESSOR_KEY, CONNECTIONS_TABLE_INITIAL_COLUMN_ORDER, CONNECTIONS_TABLE_INITIAL_COLUMN_VISIBILITY, MODAL, TAILWINDCSS_SIZE, } from '~/constants' import { allConnections, clientSourceIPTags, connectionsTableSize, setClientSourceIPTags, setConnectionsTableSize, } from '~/signals' import { ConnectionsTableColumnOrder, ConnectionsTableColumnVisibility, } from '~/types' const TagClientSourceIPWithNameForm: Component = () => { const schema = z.object({ tagName: z.string().nonempty(), sourceIP: z.string().nonempty(), }) const [t] = useI18n() const { form } = createForm>({ extend: validator({ schema }), onSubmit: ({ tagName, sourceIP }) => setClientSourceIPTags((tags) => { if ( tags.some( (tag) => tag.tagName === tagName || tag.sourceIP === sourceIP, ) ) { return tags } return [...tags, { tagName, sourceIP }] }), }) return (
) } export const ConnectionsSettingsModal = (props: { order: ConnectionsTableColumnOrder visible: ConnectionsTableColumnVisibility onOrderChange: (value: ConnectionsTableColumnOrder) => void onVisibleChange: (value: ConnectionsTableColumnVisibility) => void }) => { const [t] = useI18n() const [activeKey, setActiveKey] = createSignal(null) const onDragStart = ({ draggable }: { draggable: Draggable }) => setActiveKey(draggable.id as CONNECTIONS_TABLE_ACCESSOR_KEY) const onDragEnd = ({ draggable, droppable, }: { draggable: Draggable droppable: Droppable }) => { if (draggable && droppable) { const currentItems = props.order const fromIndex = currentItems.indexOf( draggable.id as CONNECTIONS_TABLE_ACCESSOR_KEY, ) const toIndex = currentItems.indexOf( droppable.id as CONNECTIONS_TABLE_ACCESSOR_KEY, ) if (fromIndex !== toIndex) { const updatedItems = currentItems.slice() updatedItems.splice(toIndex, 0, ...updatedItems.splice(fromIndex, 1)) props.onOrderChange(updatedItems) } } } const FormRow: Component<{ key: CONNECTIONS_TABLE_ACCESSOR_KEY }> = ({ key }) => { const sortable = createSortable(key) const [state] = useDragDropContext()! return (
{t(key)} { props.onVisibleChange({ ...props.visible, [key]: e.target.checked, }) }} />
) } return ( ) }