🎉 feat: initialization

This commit is contained in:
web
2025-11-26 19:56:16 -08:00
commit a801849fb2
553 changed files with 213088 additions and 0 deletions
+275
View File
@@ -0,0 +1,275 @@
"use client";
import { Badge } from "@workspace/ui/components/badge";
import { Button } from "@workspace/ui/components/button";
import { Switch } from "@workspace/ui/components/switch";
import { ConfirmButton } from "@workspace/ui/composed/confirm-button";
import {
ProTable,
type ProTableActions,
} from "@workspace/ui/composed/pro-table/pro-table";
import {
createNode,
deleteNode,
filterNodeList,
resetSortWithNode,
toggleNodeStatus,
updateNode,
} from "@workspace/ui/services/admin/server";
import { useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { useNode } from "@/stores/node";
import { useServer } from "@/stores/server";
import NodeForm from "./node-form";
export default function Nodes() {
const { t } = useTranslation("nodes");
const ref = useRef<ProTableActions>(null);
const [loading, setLoading] = useState(false);
// Use our zustand store for server data
const { getServerName, getServerAddress, getProtocolPort } = useServer();
const { fetchNodes, fetchTags } = useNode();
return (
<ProTable<API.Node, { search: string }>
action={ref}
actions={{
render: (row) => [
<NodeForm
initialValues={row}
key="edit"
loading={loading}
onSubmit={async (values) => {
setLoading(true);
try {
const body: API.UpdateNodeRequest = {
...row,
...values,
} as any;
await updateNode(body);
toast.success(t("updated", "Updated"));
ref.current?.refresh();
fetchNodes();
fetchTags();
setLoading(false);
return true;
} catch {
setLoading(false);
return false;
}
}}
title={t("drawerEditTitle", "Edit Node")}
trigger={t("edit", "Edit")}
/>,
<ConfirmButton
cancelText={t("cancel", "Cancel")}
confirmText={t("confirm", "Confirm")}
description={t(
"confirmDeleteDesc",
"This action cannot be undone."
)}
key="delete"
onConfirm={async () => {
await deleteNode({ id: row.id } as any);
toast.success(t("deleted", "Deleted"));
ref.current?.refresh();
fetchNodes();
fetchTags();
}}
title={t("confirmDeleteTitle", "Delete this node?")}
trigger={
<Button variant="destructive">{t("delete", "Delete")}</Button>
}
/>,
<Button
key="copy"
onClick={async () => {
const {
id: _id,
sort: _sort,
enabled: _enabled,
updated_at: _updated_at,
created_at: _created_at,
...rest
} = row as any;
await createNode({
...rest,
enabled: false,
});
toast.success(t("copied", "Copied"));
ref.current?.refresh();
fetchNodes();
fetchTags();
}}
variant="outline"
>
{t("copy", "Copy")}
</Button>,
],
batchRender(rows) {
return [
<ConfirmButton
cancelText={t("cancel", "Cancel")}
confirmText={t("confirm", "Confirm")}
description={t(
"confirmDeleteDesc",
"This action cannot be undone."
)}
key="delete"
onConfirm={async () => {
await Promise.all(
rows.map((r) => deleteNode({ id: r.id } as any))
);
toast.success(t("deleted", "Deleted"));
ref.current?.refresh();
fetchNodes();
fetchTags();
}}
title={t("confirmDeleteTitle", "Delete this node?")}
trigger={
<Button variant="destructive">{t("delete", "Delete")}</Button>
}
/>,
];
},
}}
columns={[
{
id: "enabled",
header: t("enabled", "Enabled"),
cell: ({ row }) => (
<Switch
checked={row.original.enabled}
onCheckedChange={async (v) => {
await toggleNodeStatus({ id: row.original.id, enable: v });
toast.success(
v ? t("enabled_on", "Enabled") : t("enabled_off", "Disabled")
);
ref.current?.refresh();
fetchNodes();
fetchTags();
}}
/>
),
},
{ accessorKey: "name", header: t("name", "Name") },
{
id: "address_port",
header: `${t("address", "Address")}:${t("port", "Port")}`,
cell: ({ row }) =>
`${row.original.address || "—"}:${row.original.port || "—"}`,
},
{
id: "server_id",
header: t("server", "Server"),
cell: ({ row }) =>
`${getServerName(row.original.server_id)}:${getServerAddress(row.original.server_id)}`,
},
{
id: "protocol",
header: ` ${t("protocol", "Protocol")}:${t("port", "Port")}`,
cell: ({ row }) =>
`${row.original.protocol}:${getProtocolPort(row.original.server_id, row.original.protocol)}`,
},
{
accessorKey: "tags",
header: t("tags", "Tags"),
cell: ({ row }) => (
<div className="flex flex-wrap gap-1">
{(row.original.tags || []).length === 0
? "—"
: row.original.tags.map((tg) => (
<Badge key={tg} variant="outline">
{tg}
</Badge>
))}
</div>
),
},
]}
header={{
title: t("pageTitle", "Nodes"),
toolbar: (
<NodeForm
loading={loading}
onSubmit={async (values) => {
setLoading(true);
try {
const body: API.CreateNodeRequest = {
name: values.name,
server_id: Number(values.server_id!),
protocol: values.protocol,
address: values.address,
port: Number(values.port!),
tags: values.tags || [],
enabled: false,
};
await createNode(body);
toast.success(t("created", "Created"));
ref.current?.refresh();
fetchNodes();
fetchTags();
setLoading(false);
return true;
} catch {
setLoading(false);
return false;
}
}}
title={t("drawerCreateTitle", "Create Node")}
trigger={t("create", "Create")}
/>
),
}}
onSort={async (source, target, items) => {
const sourceIndex = items.findIndex(
(item) => String(item.id) === source
);
const targetIndex = items.findIndex(
(item) => String(item.id) === target
);
const originalSorts = items.map((item) => item.sort);
const [movedItem] = items.splice(sourceIndex, 1);
items.splice(targetIndex, 0, movedItem!);
const updatedItems = items.map((item, index) => {
const originalSort = originalSorts[index];
const newSort = originalSort !== undefined ? originalSort : item.sort;
return { ...item, sort: newSort };
});
const changedItems = updatedItems.filter(
(item, index) => item.sort !== items[index]?.sort
);
if (changedItems.length > 0) {
resetSortWithNode({
sort: changedItems.map((item) => ({
id: item.id,
sort: item.sort,
})) as API.SortItem[],
});
toast.success(t("sorted_success", "Sorted successfully"));
}
return updatedItems;
}}
params={[{ key: "search" }]}
request={async (pagination, filter) => {
const { data } = await filterNodeList({
page: pagination.page,
size: pagination.size,
search: filter?.search || undefined,
});
const list = (data?.data?.list || []) as API.Node[];
const total = Number(data?.data?.total || list.length);
return { list, total };
}}
/>
);
}
+416
View File
@@ -0,0 +1,416 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { Button } from "@workspace/ui/components/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@workspace/ui/components/form";
import { ScrollArea } from "@workspace/ui/components/scroll-area";
import {
Sheet,
SheetContent,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@workspace/ui/components/sheet";
import { Combobox } from "@workspace/ui/composed/combobox";
import { EnhancedInput } from "@workspace/ui/composed/enhanced-input";
import TagInput from "@workspace/ui/composed/tag-input";
import type { TFunction } from "i18next";
import { useEffect, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { z } from "zod";
import { useNode } from "@/stores/node";
import { useServer } from "@/stores/server";
export type ProtocolName =
| "shadowsocks"
| "vmess"
| "vless"
| "trojan"
| "hysteria"
| "tuic"
| "anytls"
| "naive"
| "http"
| "socks"
| "mieru";
const buildSchema = (t: TFunction) =>
z.object({
name: z
.string()
.trim()
.min(1, t("errors.nameRequired", "Please enter a name")),
server_id: z
.number({ message: t("errors.serverRequired", "Please select a server") })
.int()
.gt(0, t("errors.serverRequired", "Please select a server"))
.optional(),
protocol: z
.string()
.min(1, t("errors.protocolRequired", "Please select a protocol")),
address: z
.string()
.trim()
.min(1, t("errors.serverAddrRequired", "Please enter an entry address")),
port: z
.number({
message: t("errors.portRange", "Port must be between 1 and 65535"),
})
.int()
.min(1, t("errors.portRange", "Port must be between 1 and 65535"))
.max(65_535, t("errors.portRange", "Port must be between 1 and 65535")),
tags: z.array(z.string()),
});
export type NodeFormValues = z.infer<ReturnType<typeof buildSchema>>;
export default function NodeForm(props: {
trigger: string;
title: string;
loading?: boolean;
initialValues?: Partial<NodeFormValues>;
onSubmit: (values: NodeFormValues) => Promise<boolean> | boolean;
}) {
const { trigger, title, loading, initialValues, onSubmit } = props;
const { t } = useTranslation("nodes");
const Scheme = useMemo(() => buildSchema(t), [t]);
const [open, setOpen] = useState(false);
const [autoFilledFields, setAutoFilledFields] = useState<Set<string>>(
new Set()
);
const addAutoFilledField = (fieldName: string) => {
setAutoFilledFields((prev) => new Set(prev).add(fieldName));
};
const removeAutoFilledField = (fieldName: string) => {
setAutoFilledFields((prev) => {
const newSet = new Set(prev);
newSet.delete(fieldName);
return newSet;
});
};
const form = useForm<NodeFormValues>({
resolver: zodResolver(Scheme),
defaultValues: {
name: "",
server_id: undefined,
protocol: "",
address: "",
port: 0,
tags: [],
...initialValues,
},
});
const serverId = form.watch("server_id");
const { servers, getAvailableProtocols } = useServer();
const { tags } = useNode();
const existingTags: string[] = tags || [];
const availableProtocols = getAvailableProtocols(serverId);
useEffect(() => {
if (initialValues) {
form.reset({
name: "",
server_id: undefined,
protocol: "",
address: "",
port: 0,
tags: [],
...initialValues,
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialValues]);
function handleServerChange(nextId?: number | null) {
const id = nextId ?? undefined;
form.setValue("server_id", id);
if (!id) {
setAutoFilledFields(new Set());
return;
}
const selectedServer = servers.find((s) => s.id === id);
if (!selectedServer) return;
const currentValues = form.getValues();
const fieldsToFill: string[] = [];
if (!currentValues.name || autoFilledFields.has("name")) {
form.setValue("name", selectedServer.name as string, {
shouldDirty: false,
});
fieldsToFill.push("name");
}
if (!currentValues.address || autoFilledFields.has("address")) {
form.setValue("address", selectedServer.address as string, {
shouldDirty: false,
});
fieldsToFill.push("address");
}
const protocols = getAvailableProtocols(id);
const firstProtocol = protocols[0];
if (
firstProtocol &&
(!currentValues.protocol || autoFilledFields.has("protocol"))
) {
form.setValue("protocol", firstProtocol.protocol, { shouldDirty: false });
fieldsToFill.push("protocol");
if (
!currentValues.port ||
currentValues.port === 0 ||
autoFilledFields.has("port")
) {
const port = firstProtocol.port || 0;
form.setValue("port", port, { shouldDirty: false });
fieldsToFill.push("port");
}
}
setAutoFilledFields(new Set(fieldsToFill));
}
const handleManualFieldChange = (
fieldName: keyof NodeFormValues,
value: any
) => {
form.setValue(fieldName, value);
removeAutoFilledField(fieldName);
};
function handleProtocolChange(nextProto?: ProtocolName | null) {
const protocol = (nextProto || "") as ProtocolName | "";
form.setValue("protocol", protocol);
if (!(protocol && serverId)) {
removeAutoFilledField("protocol");
return;
}
const currentValues = form.getValues();
const isPortAutoFilled = autoFilledFields.has("port");
removeAutoFilledField("protocol");
if (!currentValues.port || currentValues.port === 0 || isPortAutoFilled) {
const protocolData = availableProtocols.find(
(p) => p.protocol === protocol
);
if (protocolData) {
const port = protocolData.port || 0;
form.setValue("port", port, { shouldDirty: false });
addAutoFilledField("port");
}
}
}
async function handleSubmit(values: NodeFormValues) {
const result = await onSubmit(values);
if (result) {
setOpen(false);
setAutoFilledFields(new Set());
}
}
return (
<Sheet onOpenChange={setOpen} open={open}>
<SheetTrigger asChild>
<Button
onClick={() => {
form.reset();
setAutoFilledFields(new Set());
}}
>
{trigger}
</Button>
</SheetTrigger>
<SheetContent className="w-[560px] max-w-full">
<SheetHeader>
<SheetTitle>{title}</SheetTitle>
</SheetHeader>
<ScrollArea className="h-[calc(100dvh-48px-36px-36px-env(safe-area-inset-top))] px-6 pt-4">
<Form {...form}>
<form className="grid grid-cols-1 gap-4">
<FormField
control={form.control}
name="server_id"
render={({ field }) => (
<FormItem>
<FormLabel>{t("server", "Server")}</FormLabel>
<FormControl>
<Combobox<number, false>
onChange={(v) => handleServerChange(v)}
options={servers.map((s) => ({
value: s.id,
label: `${s.name} (${(s.address as any) || ""})`,
}))}
placeholder={t("select_server", "Select server…")}
value={field.value}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="protocol"
render={({ field }) => (
<FormItem>
<FormLabel>{t("protocol", "Protocol")}</FormLabel>
<FormControl>
<Combobox<string, false>
onChange={(v) =>
handleProtocolChange((v as ProtocolName) || null)
}
options={availableProtocols.map((p) => ({
value: p.protocol,
label: `${p.protocol}${p.port ? ` (${p.port})` : ""}`,
}))}
placeholder={t("select_protocol", "Select protocol…")}
value={field.value}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>{t("name", "Name")}</FormLabel>
<FormControl>
<EnhancedInput
{...field}
onValueChange={(v) =>
handleManualFieldChange("name", v as string)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="address"
render={({ field }) => (
<FormItem>
<FormLabel>{t("address", "Address")}</FormLabel>
<FormControl>
<EnhancedInput
{...field}
onValueChange={(v) =>
handleManualFieldChange("address", v as string)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="port"
render={({ field }) => (
<FormItem>
<FormLabel>{t("port", "Port")}</FormLabel>
<FormControl>
<EnhancedInput
{...field}
max={65_535}
min={1}
onValueChange={(v) =>
handleManualFieldChange("port", Number(v))
}
placeholder="1-65535"
type="number"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="tags"
render={({ field }) => (
<FormItem>
<FormLabel>{t("tags", "Tags")}</FormLabel>
<FormControl>
<TagInput
onChange={(v) => form.setValue(field.name, v)}
options={existingTags}
placeholder={t(
"tags_placeholder",
"Use Enter or comma (,) to add multiple tags"
)}
value={field.value || []}
/>
</FormControl>
<FormDescription>
{t(
"tags_description",
"Permission grouping tag (incl. plan binding and delivery policies)."
)}
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</ScrollArea>
<SheetFooter className="flex-row justify-end gap-2 pt-3">
<Button
disabled={loading}
onClick={() => setOpen(false)}
variant="outline"
>
{t("cancel", "Cancel")}
</Button>
<Button
disabled={loading}
onClick={form.handleSubmit(handleSubmit, (errors) => {
const key = Object.keys(errors)[0] as keyof typeof errors;
if (key) toast.error(String(errors[key]?.message));
return false;
})}
>
{t("confirm", "Confirm")}
</Button>
</SheetFooter>
</SheetContent>
</Sheet>
);
}