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

247 lines
6.3 KiB
TypeScript
Raw Normal View History

/* eslint-disable unicorn/no-keyword-prefix */
2024-12-31 10:30:14 +05:30
import { closestCorners, DndContext, DragOverlay } 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 {
arrayMove,
SortableContext,
useSortable,
2024-12-31 10:30:14 +05:30
verticalListSortingStrategy,
} from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import { LockIcon, ThreeBarsIcon } from '@primer/octicons-react';
2024-12-31 10:31:50 +05:30
import { type FetcherWithComponents, useFetcher } from 'react-router';
2024-12-31 10:30:14 +05:30
import { useEffect, useState } from 'react';
import { Button, Input } from 'react-aria-components';
2024-12-31 10:30:14 +05:30
import Spinner from '~/components/Spinner';
import TableList from '~/components/TableList';
import { cn } from '~/utils/cn';
type Properties = {
readonly baseDomain?: string;
readonly searchDomains: string[];
// eslint-disable-next-line react/boolean-prop-naming
readonly disabled?: boolean;
2024-12-31 10:30:14 +05:30
};
2024-12-31 10:30:14 +05:30
export default function Domains({
baseDomain,
searchDomains,
disabled,
}: Properties) {
// eslint-disable-next-line unicorn/no-null, @typescript-eslint/ban-types
2024-12-31 10:30:14 +05:30
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>
<p className="text-gray-700 dark:text-gray-300 mb-2">
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) => {
2024-03-28 19:40:06 -04:00
// eslint-disable-next-line unicorn/no-null
2024-12-31 10:30:14 +05:30
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">
<p className="font-mono text-sm">{baseDomain}</p>
<LockIcon className="h-4 w-4" />
</TableList.Item>
2024-03-28 19:40:06 -04:00
) : undefined}
<SortableContext
items={localDomains}
strategy={verticalListSortingStrategy}
>
{localDomains.map((sd, index) => (
<Domain
// eslint-disable-next-line react/no-array-index-key
key={index}
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"
className="font-mono text-sm bg-transparent w-full mr-2"
placeholder="Search Domain"
value={newDomain}
2024-12-31 10:30:14 +05:30
onChange={(event) => {
setNewDomain(event.target.value);
}}
/>
2024-03-30 04:12:37 -04:00
{fetcher.state === 'idle' ? (
<Button
className={cn(
'text-sm font-semibold',
'text-blue-600 dark:text-blue-400',
'hover:text-blue-700 dark:hover:text-blue-300',
2024-12-31 10:30:14 +05:30
newDomain.length === 0 && 'opacity-50 cursor-not-allowed',
)}
isDisabled={newDomain.length === 0}
onPress={() => {
2024-12-31 10:30:14 +05:30
fetcher.submit(
{
// eslint-disable-next-line @typescript-eslint/naming-convention
'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[];
// eslint-disable-next-line react/boolean-prop-naming
readonly disabled?: boolean;
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 });
2024-03-30 04:12:37 -04:00
// TODO: Figure out why TableList.Item breaks dndkit
return (
<div
ref={setNodeRef}
className={cn(
'flex items-center justify-between px-3 py-2',
2024-03-30 04:12:37 -04:00
'border-b border-gray-200 last:border-b-0 dark:border-zinc-800',
isDragging ? 'text-gray-400' : '',
2024-12-31 10:30:14 +05:30
isDrag
? 'outline outline-1 outline-gray-500 bg-gray-200 dark:bg-zinc-800'
: '',
)}
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 : (
<ThreeBarsIcon
2024-12-31 10:30:14 +05:30
className="h-4 w-4 text-gray-400 focus:outline-none"
{...attributes}
{...listeners}
/>
)}
{domain}
</p>
{isDrag ? undefined : (
2024-03-29 15:58:22 -04:00
<Button
className={cn(
'text-sm',
'text-red-600 dark:text-red-400',
'hover:text-red-700 dark:hover:text-red-300',
2024-12-31 10:30:14 +05:30
disabled && 'opacity-50 cursor-not-allowed',
)}
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>
)}
</div>
2024-12-31 10:30:14 +05:30
);
}