headplane/app/routes/dns/components/domains.tsx

238 lines
5.7 KiB
TypeScript
Raw Normal View History

/* eslint-disable unicorn/no-keyword-prefix */
2025-01-28 16:06:41 -05:00
import { DndContext, DragOverlay, closestCorners } from '@dnd-kit/core';
2024-03-28 19:40:06 -04:00
import {
restrictToParentElement,
2024-12-31 10:30:14 +05:30
restrictToVerticalAxis,
} from '@dnd-kit/modifiers';
import {
SortableContext,
2025-01-28 16:06:41 -05:00
arrayMove,
useSortable,
2024-12-31 10:30:14 +05:30
verticalListSortingStrategy,
} from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
2025-02-04 11:42:57 -05:00
import { GripVertical, Lock } from 'lucide-react';
2024-12-31 10:30:14 +05:30
import { useEffect, useState } from 'react';
2025-01-28 16:06:41 -05:00
import { type FetcherWithComponents, useFetcher } from 'react-router';
2025-02-04 11:42:57 -05:00
import Button from '~/components/Button';
import Input from '~/components/Input';
2024-12-31 10:30:14 +05:30
import Spinner from '~/components/Spinner';
import TableList from '~/components/TableList';
2025-01-28 17:46:16 -05:00
import cn from '~/utils/cn';
type Properties = {
readonly baseDomain?: string;
readonly searchDomains: string[];
2025-02-04 17:21:03 -05:00
readonly disabled?: boolean; // TODO: isDisabled
2024-12-31 10:30:14 +05:30
};
2024-12-31 10:30:14 +05:30
export default function Domains({
baseDomain,
searchDomains,
disabled,
}: Properties) {
const [activeId, setActiveId] = useState<number | string | null>(null);
const [localDomains, setLocalDomains] = useState(searchDomains);
const [newDomain, setNewDomain] = useState('');
const fetcher = useFetcher();
2024-03-28 19:40:06 -04:00
useEffect(() => {
2024-12-31 10:30:14 +05:30
setLocalDomains(searchDomains);
}, [searchDomains]);
return (
2024-12-31 10:30:14 +05:30
<div className="flex flex-col w-2/3">
<h1 className="text-2xl font-medium mb-4">Search Domains</h1>
2025-02-04 17:21:03 -05:00
<p className="mb-4">
2024-12-31 10:30:14 +05:30
Set custom DNS search domains for your Tailnet. When using Magic DNS,
your tailnet domain is used as the first search domain.
</p>
2024-03-28 19:40:06 -04:00
<DndContext
modifiers={[restrictToVerticalAxis, restrictToParentElement]}
collisionDetection={closestCorners}
2024-12-31 10:30:14 +05:30
onDragStart={(event) => {
setActiveId(event.active.id);
2024-03-28 19:40:06 -04:00
}}
2024-12-31 10:30:14 +05:30
onDragEnd={(event) => {
setActiveId(null);
const { active, over } = event;
2024-03-28 19:40:06 -04:00
if (!over) {
2024-12-31 10:30:14 +05:30
return;
2024-03-28 19:40:06 -04:00
}
2024-12-31 10:30:14 +05:30
const activeItem = localDomains[(active.id as number) - 1];
const overItem = localDomains[(over.id as number) - 1];
2024-03-28 19:40:06 -04:00
if (!activeItem || !overItem) {
2024-12-31 10:30:14 +05:30
return;
2024-03-28 19:40:06 -04:00
}
2024-12-31 10:30:14 +05:30
const oldIndex = localDomains.indexOf(activeItem);
const newIndex = localDomains.indexOf(overItem);
2024-03-28 19:40:06 -04:00
if (oldIndex !== newIndex) {
2024-12-31 10:30:14 +05:30
setLocalDomains(arrayMove(localDomains, oldIndex, newIndex));
2024-03-28 19:40:06 -04:00
}
}}
>
<TableList>
2024-03-28 19:40:06 -04:00
{baseDomain ? (
2024-12-31 10:30:14 +05:30
<TableList.Item key="magic-dns-sd">
2025-02-04 17:21:03 -05:00
<div
className={cn(
'flex items-center gap-4',
disabled ? 'flex-row-reverse justify-between w-full' : '',
)}
>
2025-02-04 11:42:57 -05:00
<Lock className="p-0.5" />
<p className="font-mono text-sm py-0.5">{baseDomain}</p>
</div>
</TableList.Item>
2024-03-28 19:40:06 -04:00
) : undefined}
<SortableContext
items={localDomains}
strategy={verticalListSortingStrategy}
>
{localDomains.map((sd, index) => (
<Domain
2025-01-28 16:06:41 -05:00
key={sd}
domain={sd}
id={index + 1}
localDomains={localDomains}
disabled={disabled}
2024-03-30 04:12:37 -04:00
fetcher={fetcher}
/>
))}
2024-03-28 19:40:06 -04:00
<DragOverlay adjustScale>
2024-12-31 10:30:14 +05:30
{activeId ? (
<Domain
isDrag
domain={localDomains[(activeId as number) - 1]}
localDomains={localDomains}
id={(activeId as number) - 1}
disabled={disabled}
fetcher={fetcher}
/>
) : undefined}
2024-03-28 19:40:06 -04:00
</DragOverlay>
</SortableContext>
{disabled ? undefined : (
2024-12-31 10:30:14 +05:30
<TableList.Item key="add-sd">
<Input
2024-12-31 10:30:14 +05:30
type="text"
2025-02-04 11:42:57 -05:00
className={cn(
'border-none font-mono p-0',
'rounded-none focus:ring-0 w-full',
)}
2024-12-31 10:30:14 +05:30
placeholder="Search Domain"
2025-02-04 11:42:57 -05:00
onChange={setNewDomain}
label="Search Domain"
labelHidden
/>
2024-03-30 04:12:37 -04:00
{fetcher.state === 'idle' ? (
<Button
className={cn(
2025-02-04 11:42:57 -05:00
'px-2 py-1 rounded-md',
'text-blue-500 dark:text-blue-400',
)}
isDisabled={newDomain.length === 0}
onPress={() => {
2024-12-31 10:30:14 +05:30
fetcher.submit(
{
'dns.search_domains': [...localDomains, newDomain],
},
{
method: 'PATCH',
encType: 'application/json',
},
);
2024-12-31 10:30:14 +05:30
setNewDomain('');
2024-03-30 04:12:37 -04:00
}}
>
Add
</Button>
) : (
2024-12-31 10:30:14 +05:30
<Spinner className="w-3 h-3 mr-0" />
2024-03-30 04:12:37 -04:00
)}
</TableList.Item>
)}
</TableList>
2024-03-28 19:40:06 -04:00
</DndContext>
</div>
2024-12-31 10:30:14 +05:30
);
}
type DomainProperties = {
readonly domain: string;
readonly id: number;
readonly isDrag?: boolean;
readonly localDomains: string[];
2025-02-04 11:42:57 -05:00
readonly disabled?: boolean; // TODO: isDisabled
2024-03-30 04:12:37 -04:00
readonly fetcher: FetcherWithComponents<unknown>;
2024-12-31 10:30:14 +05:30
};
2024-12-31 10:30:14 +05:30
function Domain({
domain,
id,
localDomains,
isDrag,
disabled,
fetcher,
}: DomainProperties) {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
2024-12-31 10:30:14 +05:30
isDragging,
} = useSortable({ id });
return (
2025-02-04 11:42:57 -05:00
<TableList.Item
ref={setNodeRef}
className={cn(
2025-02-04 11:42:57 -05:00
isDragging ? 'opacity-50' : '',
isDrag ? 'ring bg-white dark:bg-headplane-900' : '',
)}
style={{
transform: CSS.Transform.toString(transform),
2024-12-31 10:30:14 +05:30
transition,
}}
>
2024-12-31 10:30:14 +05:30
<p className="font-mono text-sm flex items-center gap-4">
{disabled ? undefined : (
2025-02-04 11:42:57 -05:00
<GripVertical {...attributes} {...listeners} className="p-0.5" />
)}
{domain}
</p>
{isDrag ? undefined : (
2024-03-29 15:58:22 -04:00
<Button
className={cn(
2025-02-04 11:42:57 -05:00
'px-2 py-1 rounded-md',
'text-red-500 dark:text-red-400',
)}
isDisabled={disabled}
onPress={() => {
2024-12-31 10:30:14 +05:30
fetcher.submit(
{
'dns.search_domains': localDomains.filter(
(_, index) => index !== id - 1,
),
},
{
method: 'PATCH',
encType: 'application/json',
},
);
}}
>
Remove
2024-03-29 15:58:22 -04:00
</Button>
)}
2025-02-04 11:42:57 -05:00
</TableList.Item>
2024-12-31 10:30:14 +05:30
);
}