feat: add validation for minimum length on various fields and update type definitions
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useParams, useRouter } from "next/navigation";
|
||||
|
||||
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
||||
import { CheckCircle2, RefreshCcw, XCircle } from "lucide-react";
|
||||
|
||||
import { ApiError } from "@/api/mutator";
|
||||
import {
|
||||
gatewaysStatusApiV1GatewaysStatusGet,
|
||||
type getGatewayApiV1GatewaysGatewayIdGetResponse,
|
||||
useGetGatewayApiV1GatewaysGatewayIdGet,
|
||||
useUpdateGatewayApiV1GatewaysGatewayIdPatch,
|
||||
} from "@/api/generated/gateways/gateways";
|
||||
import type { GatewayUpdate } from "@/api/generated/model";
|
||||
import { DashboardSidebar } from "@/components/organisms/DashboardSidebar";
|
||||
import { DashboardShell } from "@/components/templates/DashboardShell";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { getApiBaseUrl } from "@/lib/api-base";
|
||||
|
||||
const apiBase = getApiBaseUrl();
|
||||
|
||||
const DEFAULT_MAIN_SESSION_KEY = "agent:main:main";
|
||||
const DEFAULT_WORKSPACE_ROOT = "~/.openclaw";
|
||||
@@ -34,18 +39,8 @@ const validateGatewayUrl = (value: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
type Gateway = {
|
||||
id: string;
|
||||
name: string;
|
||||
url: string;
|
||||
token?: string | null;
|
||||
main_session_key: string;
|
||||
workspace_root: string;
|
||||
skyll_enabled?: boolean;
|
||||
};
|
||||
|
||||
export default function EditGatewayPage() {
|
||||
const { getToken, isSignedIn } = useAuth();
|
||||
const { isSignedIn } = useAuth();
|
||||
const router = useRouter();
|
||||
const params = useParams();
|
||||
const gatewayIdParam = params?.gatewayId;
|
||||
@@ -53,15 +48,20 @@ export default function EditGatewayPage() {
|
||||
? gatewayIdParam[0]
|
||||
: gatewayIdParam;
|
||||
|
||||
const [gateway, setGateway] = useState<Gateway | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [gatewayUrl, setGatewayUrl] = useState("");
|
||||
const [gatewayToken, setGatewayToken] = useState("");
|
||||
const [mainSessionKey, setMainSessionKey] = useState(
|
||||
DEFAULT_MAIN_SESSION_KEY
|
||||
const [name, setName] = useState<string | undefined>(undefined);
|
||||
const [gatewayUrl, setGatewayUrl] = useState<string | undefined>(undefined);
|
||||
const [gatewayToken, setGatewayToken] = useState<string | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const [mainSessionKey, setMainSessionKey] = useState<string | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const [workspaceRoot, setWorkspaceRoot] = useState<string | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const [skyllEnabled, setSkyllEnabled] = useState<boolean | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const [workspaceRoot, setWorkspaceRoot] = useState(DEFAULT_WORKSPACE_ROOT);
|
||||
const [skyllEnabled, setSkyllEnabled] = useState(false);
|
||||
|
||||
const [gatewayUrlError, setGatewayUrlError] = useState<string | null>(null);
|
||||
const [gatewayCheckStatus, setGatewayCheckStatus] = useState<
|
||||
@@ -71,48 +71,58 @@ export default function EditGatewayPage() {
|
||||
null
|
||||
);
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const gatewayQuery = useGetGatewayApiV1GatewaysGatewayIdGet<
|
||||
getGatewayApiV1GatewaysGatewayIdGetResponse,
|
||||
ApiError
|
||||
>(gatewayId ?? "", {
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn && gatewayId),
|
||||
refetchOnMount: "always",
|
||||
retry: false,
|
||||
},
|
||||
});
|
||||
|
||||
const updateMutation = useUpdateGatewayApiV1GatewaysGatewayIdPatch<ApiError>({
|
||||
mutation: {
|
||||
onSuccess: (result) => {
|
||||
if (result.status === 200) {
|
||||
router.push(`/gateways/${result.data.id}`);
|
||||
}
|
||||
},
|
||||
onError: (err) => {
|
||||
setError(err.message || "Something went wrong.");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const loadedGateway =
|
||||
gatewayQuery.data?.status === 200 ? gatewayQuery.data.data : null;
|
||||
const resolvedName = name ?? loadedGateway?.name ?? "";
|
||||
const resolvedGatewayUrl = gatewayUrl ?? loadedGateway?.url ?? "";
|
||||
const resolvedGatewayToken = gatewayToken ?? loadedGateway?.token ?? "";
|
||||
const resolvedMainSessionKey =
|
||||
mainSessionKey ??
|
||||
loadedGateway?.main_session_key ??
|
||||
DEFAULT_MAIN_SESSION_KEY;
|
||||
const resolvedWorkspaceRoot =
|
||||
workspaceRoot ?? loadedGateway?.workspace_root ?? DEFAULT_WORKSPACE_ROOT;
|
||||
const resolvedSkyllEnabled =
|
||||
skyllEnabled ?? Boolean(loadedGateway?.skyll_enabled);
|
||||
|
||||
const isLoading = gatewayQuery.isLoading || updateMutation.isPending;
|
||||
const errorMessage = error ?? gatewayQuery.error?.message ?? null;
|
||||
|
||||
const canSubmit =
|
||||
Boolean(name.trim()) &&
|
||||
Boolean(gatewayUrl.trim()) &&
|
||||
Boolean(mainSessionKey.trim()) &&
|
||||
Boolean(workspaceRoot.trim()) &&
|
||||
Boolean(resolvedName.trim()) &&
|
||||
Boolean(resolvedGatewayUrl.trim()) &&
|
||||
Boolean(resolvedMainSessionKey.trim()) &&
|
||||
Boolean(resolvedWorkspaceRoot.trim()) &&
|
||||
gatewayCheckStatus === "success";
|
||||
|
||||
useEffect(() => {
|
||||
if (!isSignedIn || !gatewayId) return;
|
||||
const loadGateway = async () => {
|
||||
try {
|
||||
const token = await getToken();
|
||||
const response = await fetch(
|
||||
`${apiBase}/api/v1/gateways/${gatewayId}`,
|
||||
{
|
||||
headers: { Authorization: token ? `Bearer ${token}` : "" },
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error("Unable to load gateway.");
|
||||
}
|
||||
const data = (await response.json()) as Gateway;
|
||||
setGateway(data);
|
||||
setName(data.name ?? "");
|
||||
setGatewayUrl(data.url ?? "");
|
||||
setGatewayToken(data.token ?? "");
|
||||
setMainSessionKey(data.main_session_key ?? DEFAULT_MAIN_SESSION_KEY);
|
||||
setWorkspaceRoot(data.workspace_root ?? DEFAULT_WORKSPACE_ROOT);
|
||||
setSkyllEnabled(Boolean(data.skyll_enabled));
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Something went wrong.");
|
||||
}
|
||||
};
|
||||
loadGateway();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [gatewayId, isSignedIn]);
|
||||
|
||||
const runGatewayCheck = async () => {
|
||||
const validationError = validateGatewayUrl(gatewayUrl);
|
||||
const validationError = validateGatewayUrl(resolvedGatewayUrl);
|
||||
setGatewayUrlError(validationError);
|
||||
if (validationError) {
|
||||
setGatewayCheckStatus("error");
|
||||
@@ -123,26 +133,25 @@ export default function EditGatewayPage() {
|
||||
setGatewayCheckStatus("checking");
|
||||
setGatewayCheckMessage(null);
|
||||
try {
|
||||
const token = await getToken();
|
||||
const params = new URLSearchParams({
|
||||
gateway_url: gatewayUrl.trim(),
|
||||
});
|
||||
if (gatewayToken.trim()) {
|
||||
params.set("gateway_token", gatewayToken.trim());
|
||||
const params: Record<string, string> = {
|
||||
gateway_url: resolvedGatewayUrl.trim(),
|
||||
};
|
||||
if (resolvedGatewayToken.trim()) {
|
||||
params.gateway_token = resolvedGatewayToken.trim();
|
||||
}
|
||||
if (mainSessionKey.trim()) {
|
||||
params.set("gateway_main_session_key", mainSessionKey.trim());
|
||||
if (resolvedMainSessionKey.trim()) {
|
||||
params.gateway_main_session_key = resolvedMainSessionKey.trim();
|
||||
}
|
||||
const response = await fetch(
|
||||
`${apiBase}/api/v1/gateways/status?${params.toString()}`,
|
||||
{
|
||||
headers: { Authorization: token ? `Bearer ${token}` : "" },
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
if (!response.ok || !data?.connected) {
|
||||
const response = await gatewaysStatusApiV1GatewaysStatusGet(params);
|
||||
if (response.status !== 200) {
|
||||
setGatewayCheckStatus("error");
|
||||
setGatewayCheckMessage(data?.error ?? "Unable to reach gateway.");
|
||||
setGatewayCheckMessage("Unable to reach gateway.");
|
||||
return;
|
||||
}
|
||||
const data = response.data;
|
||||
if (!data.connected) {
|
||||
setGatewayCheckStatus("error");
|
||||
setGatewayCheckMessage(data.error ?? "Unable to reach gateway.");
|
||||
return;
|
||||
}
|
||||
setGatewayCheckStatus("success");
|
||||
@@ -155,60 +164,42 @@ export default function EditGatewayPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (!isSignedIn || !gatewayId) return;
|
||||
|
||||
if (!name.trim()) {
|
||||
if (!resolvedName.trim()) {
|
||||
setError("Gateway name is required.");
|
||||
return;
|
||||
}
|
||||
const gatewayValidation = validateGatewayUrl(gatewayUrl);
|
||||
const gatewayValidation = validateGatewayUrl(resolvedGatewayUrl);
|
||||
setGatewayUrlError(gatewayValidation);
|
||||
if (gatewayValidation) {
|
||||
setGatewayCheckStatus("error");
|
||||
setGatewayCheckMessage(gatewayValidation);
|
||||
return;
|
||||
}
|
||||
if (!mainSessionKey.trim()) {
|
||||
if (!resolvedMainSessionKey.trim()) {
|
||||
setError("Main session key is required.");
|
||||
return;
|
||||
}
|
||||
if (!workspaceRoot.trim()) {
|
||||
if (!resolvedWorkspaceRoot.trim()) {
|
||||
setError("Workspace root is required.");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const token = await getToken();
|
||||
const response = await fetch(`${apiBase}/api/v1/gateways/${gatewayId}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: token ? `Bearer ${token}` : "",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name.trim(),
|
||||
url: gatewayUrl.trim(),
|
||||
token: gatewayToken.trim() || null,
|
||||
main_session_key: mainSessionKey.trim(),
|
||||
workspace_root: workspaceRoot.trim(),
|
||||
skyll_enabled: skyllEnabled,
|
||||
}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Unable to update gateway.");
|
||||
}
|
||||
const updated = (await response.json()) as Gateway;
|
||||
setGateway(updated);
|
||||
router.push(`/gateways/${updated.id}`);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Something went wrong.");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
const payload: GatewayUpdate = {
|
||||
name: resolvedName.trim(),
|
||||
url: resolvedGatewayUrl.trim(),
|
||||
token: resolvedGatewayToken.trim() || null,
|
||||
main_session_key: resolvedMainSessionKey.trim(),
|
||||
workspace_root: resolvedWorkspaceRoot.trim(),
|
||||
skyll_enabled: resolvedSkyllEnabled,
|
||||
};
|
||||
|
||||
updateMutation.mutate({ gatewayId, data: payload });
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -232,7 +223,9 @@ export default function EditGatewayPage() {
|
||||
<div className="border-b border-slate-200 bg-white px-8 py-6">
|
||||
<div>
|
||||
<h1 className="font-heading text-2xl font-semibold text-slate-900 tracking-tight">
|
||||
{gateway ? `Edit gateway — ${gateway.name}` : "Edit gateway"}
|
||||
{resolvedName.trim()
|
||||
? `Edit gateway — ${resolvedName.trim()}`
|
||||
: "Edit gateway"}
|
||||
</h1>
|
||||
<p className="mt-1 text-sm text-slate-500">
|
||||
Update connection settings for this OpenClaw gateway.
|
||||
@@ -251,7 +244,7 @@ export default function EditGatewayPage() {
|
||||
Gateway name <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
value={name}
|
||||
value={resolvedName}
|
||||
onChange={(event) => setName(event.target.value)}
|
||||
placeholder="Primary gateway"
|
||||
disabled={isLoading}
|
||||
@@ -273,15 +266,15 @@ export default function EditGatewayPage() {
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={skyllEnabled}
|
||||
onClick={() => setSkyllEnabled((prev) => !prev)}
|
||||
aria-checked={resolvedSkyllEnabled}
|
||||
onClick={() => setSkyllEnabled(!resolvedSkyllEnabled)}
|
||||
className={`relative inline-flex h-6 w-11 items-center rounded-full transition ${
|
||||
skyllEnabled ? "bg-blue-600" : "bg-slate-200"
|
||||
resolvedSkyllEnabled ? "bg-blue-600" : "bg-slate-200"
|
||||
}`}
|
||||
>
|
||||
<span
|
||||
className={`inline-block h-4 w-4 transform rounded-full bg-white transition ${
|
||||
skyllEnabled ? "translate-x-5" : "translate-x-1"
|
||||
resolvedSkyllEnabled ? "translate-x-5" : "translate-x-1"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
@@ -296,7 +289,7 @@ export default function EditGatewayPage() {
|
||||
</label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
value={gatewayUrl}
|
||||
value={resolvedGatewayUrl}
|
||||
onChange={(event) => {
|
||||
setGatewayUrl(event.target.value);
|
||||
setGatewayUrlError(null);
|
||||
@@ -344,7 +337,7 @@ export default function EditGatewayPage() {
|
||||
Gateway token
|
||||
</label>
|
||||
<Input
|
||||
value={gatewayToken}
|
||||
value={resolvedGatewayToken}
|
||||
onChange={(event) => {
|
||||
setGatewayToken(event.target.value);
|
||||
setGatewayCheckStatus("idle");
|
||||
@@ -363,7 +356,7 @@ export default function EditGatewayPage() {
|
||||
Main session key <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
value={mainSessionKey}
|
||||
value={resolvedMainSessionKey}
|
||||
onChange={(event) => {
|
||||
setMainSessionKey(event.target.value);
|
||||
setGatewayCheckStatus("idle");
|
||||
@@ -378,7 +371,7 @@ export default function EditGatewayPage() {
|
||||
Workspace root <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
value={workspaceRoot}
|
||||
value={resolvedWorkspaceRoot}
|
||||
onChange={(event) => setWorkspaceRoot(event.target.value)}
|
||||
placeholder={DEFAULT_WORKSPACE_ROOT}
|
||||
disabled={isLoading}
|
||||
@@ -387,7 +380,9 @@ export default function EditGatewayPage() {
|
||||
</div>
|
||||
|
||||
|
||||
{error ? <p className="text-sm text-red-500">{error}</p> : null}
|
||||
{errorMessage ? (
|
||||
<p className="text-sm text-red-500">{errorMessage}</p>
|
||||
) : null}
|
||||
|
||||
<div className="flex justify-end gap-3">
|
||||
<Button
|
||||
|
||||
@@ -3,41 +3,27 @@
|
||||
import { useMemo } from "react";
|
||||
import { useParams, useRouter } from "next/navigation";
|
||||
|
||||
import { SignInButton, SignedIn, SignedOut } from "@clerk/nextjs";
|
||||
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
||||
|
||||
import { ApiError } from "@/api/mutator";
|
||||
import {
|
||||
type gatewaysStatusApiV1GatewaysStatusGetResponse,
|
||||
type getGatewayApiV1GatewaysGatewayIdGetResponse,
|
||||
useGatewaysStatusApiV1GatewaysStatusGet,
|
||||
useGetGatewayApiV1GatewaysGatewayIdGet,
|
||||
} from "@/api/generated/gateways/gateways";
|
||||
import {
|
||||
type listAgentsApiV1AgentsGetResponse,
|
||||
useListAgentsApiV1AgentsGet,
|
||||
} from "@/api/generated/agents/agents";
|
||||
import {
|
||||
type listBoardsApiV1BoardsGetResponse,
|
||||
useListBoardsApiV1BoardsGet,
|
||||
} from "@/api/generated/boards/boards";
|
||||
import type { AgentRead } from "@/api/generated/model";
|
||||
import { DashboardSidebar } from "@/components/organisms/DashboardSidebar";
|
||||
import { DashboardShell } from "@/components/templates/DashboardShell";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useAuthedQuery } from "@/lib/api-query";
|
||||
|
||||
type Gateway = {
|
||||
id: string;
|
||||
name: string;
|
||||
url: string;
|
||||
token?: string | null;
|
||||
main_session_key: string;
|
||||
workspace_root: string;
|
||||
skyll_enabled?: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
|
||||
type Agent = {
|
||||
id: string;
|
||||
name: string;
|
||||
status: string;
|
||||
board_id?: string | null;
|
||||
last_seen_at?: string | null;
|
||||
updated_at: string;
|
||||
is_board_lead?: boolean;
|
||||
};
|
||||
|
||||
type GatewayStatus = {
|
||||
connected: boolean;
|
||||
gateway_url: string;
|
||||
sessions_count?: number;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
const formatTimestamp = (value?: string | null) => {
|
||||
if (!value) return "—";
|
||||
@@ -60,46 +46,83 @@ const maskToken = (value?: string | null) => {
|
||||
export default function GatewayDetailPage() {
|
||||
const router = useRouter();
|
||||
const params = useParams();
|
||||
const { isSignedIn } = useAuth();
|
||||
const gatewayIdParam = params?.gatewayId;
|
||||
const gatewayId = Array.isArray(gatewayIdParam)
|
||||
? gatewayIdParam[0]
|
||||
: gatewayIdParam;
|
||||
|
||||
const gatewayQuery = useAuthedQuery<Gateway>(
|
||||
["gateway", gatewayId ?? "unknown"],
|
||||
gatewayId ? `/api/v1/gateways/${gatewayId}` : null,
|
||||
{ refetchInterval: 30_000 }
|
||||
);
|
||||
const gatewayQuery = useGetGatewayApiV1GatewaysGatewayIdGet<
|
||||
getGatewayApiV1GatewaysGatewayIdGetResponse,
|
||||
ApiError
|
||||
>(gatewayId ?? "", {
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn && gatewayId),
|
||||
refetchInterval: 30_000,
|
||||
},
|
||||
});
|
||||
|
||||
const gateway = gatewayQuery.data ?? null;
|
||||
const gateway =
|
||||
gatewayQuery.data?.status === 200 ? gatewayQuery.data.data : null;
|
||||
|
||||
const agentsQuery = useAuthedQuery<Agent[]>(
|
||||
["gateway-agents", gatewayId ?? "unknown"],
|
||||
gatewayId ? `/api/v1/agents?gateway_id=${gatewayId}` : null,
|
||||
{ refetchInterval: 15_000 }
|
||||
);
|
||||
const boardsQuery = useListBoardsApiV1BoardsGet<
|
||||
listBoardsApiV1BoardsGetResponse,
|
||||
ApiError
|
||||
>({
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn),
|
||||
refetchInterval: 30_000,
|
||||
},
|
||||
});
|
||||
|
||||
const statusPath = gateway
|
||||
? (() => {
|
||||
const params = new URLSearchParams({ gateway_url: gateway.url });
|
||||
if (gateway.token) {
|
||||
params.set("gateway_token", gateway.token);
|
||||
}
|
||||
if (gateway.main_session_key) {
|
||||
params.set("gateway_main_session_key", gateway.main_session_key);
|
||||
}
|
||||
return `/api/v1/gateways/status?${params.toString()}`;
|
||||
})()
|
||||
: null;
|
||||
const agentsQuery = useListAgentsApiV1AgentsGet<
|
||||
listAgentsApiV1AgentsGetResponse,
|
||||
ApiError
|
||||
>({
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn),
|
||||
refetchInterval: 15_000,
|
||||
},
|
||||
});
|
||||
|
||||
const statusQuery = useAuthedQuery<GatewayStatus>(
|
||||
["gateway-status", gatewayId ?? "unknown"],
|
||||
statusPath,
|
||||
{ refetchInterval: 15_000, enabled: Boolean(statusPath) }
|
||||
);
|
||||
const statusParams = gateway
|
||||
? {
|
||||
gateway_url: gateway.url,
|
||||
gateway_token: gateway.token ?? undefined,
|
||||
gateway_main_session_key: gateway.main_session_key ?? undefined,
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const agents = agentsQuery.data ?? [];
|
||||
const isConnected = statusQuery.data?.connected ?? false;
|
||||
const statusQuery = useGatewaysStatusApiV1GatewaysStatusGet<
|
||||
gatewaysStatusApiV1GatewaysStatusGetResponse,
|
||||
ApiError
|
||||
>(statusParams, {
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn && statusParams),
|
||||
refetchInterval: 15_000,
|
||||
},
|
||||
});
|
||||
|
||||
const agents = useMemo(() => {
|
||||
const allAgents = agentsQuery.data?.data ?? [];
|
||||
const boards = boardsQuery.data?.status === 200 ? boardsQuery.data.data : [];
|
||||
if (!gatewayId) {
|
||||
return allAgents;
|
||||
}
|
||||
const boardIds = new Set(
|
||||
boards.filter((board) => board.gateway_id === gatewayId).map((board) => board.id),
|
||||
);
|
||||
if (boardIds.size === 0) {
|
||||
return [];
|
||||
}
|
||||
return allAgents.filter(
|
||||
(agent): agent is AgentRead => Boolean(agent.board_id && boardIds.has(agent.board_id)),
|
||||
);
|
||||
}, [agentsQuery.data, boardsQuery.data, gatewayId]);
|
||||
|
||||
const status =
|
||||
statusQuery.data?.status === 200 ? statusQuery.data.data : null;
|
||||
const isConnected = status?.connected ?? false;
|
||||
|
||||
const title = useMemo(
|
||||
() => (gateway?.name ? gateway.name : "Gateway"),
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
||||
import { CheckCircle2, RefreshCcw, XCircle } from "lucide-react";
|
||||
|
||||
import { ApiError } from "@/api/mutator";
|
||||
import {
|
||||
gatewaysStatusApiV1GatewaysStatusGet,
|
||||
useCreateGatewayApiV1GatewaysPost,
|
||||
} from "@/api/generated/gateways/gateways";
|
||||
import { DashboardSidebar } from "@/components/organisms/DashboardSidebar";
|
||||
import { DashboardShell } from "@/components/templates/DashboardShell";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { getApiBaseUrl } from "@/lib/api-base";
|
||||
|
||||
const apiBase = getApiBaseUrl();
|
||||
|
||||
const DEFAULT_MAIN_SESSION_KEY = "agent:main:main";
|
||||
const DEFAULT_WORKSPACE_ROOT = "~/.openclaw";
|
||||
@@ -35,7 +37,7 @@ const validateGatewayUrl = (value: string) => {
|
||||
};
|
||||
|
||||
export default function NewGatewayPage() {
|
||||
const { getToken, isSignedIn } = useAuth();
|
||||
const { isSignedIn } = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
const [name, setName] = useState("");
|
||||
@@ -55,9 +57,23 @@ export default function NewGatewayPage() {
|
||||
null
|
||||
);
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const createMutation = useCreateGatewayApiV1GatewaysPost<ApiError>({
|
||||
mutation: {
|
||||
onSuccess: (result) => {
|
||||
if (result.status === 200) {
|
||||
router.push(`/gateways/${result.data.id}`);
|
||||
}
|
||||
},
|
||||
onError: (err) => {
|
||||
setError(err.message || "Something went wrong.");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const isLoading = createMutation.isPending;
|
||||
|
||||
const canSubmit =
|
||||
Boolean(name.trim()) &&
|
||||
Boolean(gatewayUrl.trim()) &&
|
||||
@@ -65,11 +81,6 @@ export default function NewGatewayPage() {
|
||||
Boolean(workspaceRoot.trim()) &&
|
||||
gatewayCheckStatus === "success";
|
||||
|
||||
useEffect(() => {
|
||||
setGatewayCheckStatus("idle");
|
||||
setGatewayCheckMessage(null);
|
||||
}, [gatewayToken]);
|
||||
|
||||
const runGatewayCheck = async () => {
|
||||
const validationError = validateGatewayUrl(gatewayUrl);
|
||||
setGatewayUrlError(validationError);
|
||||
@@ -82,26 +93,26 @@ export default function NewGatewayPage() {
|
||||
setGatewayCheckStatus("checking");
|
||||
setGatewayCheckMessage(null);
|
||||
try {
|
||||
const token = await getToken();
|
||||
const params = new URLSearchParams({
|
||||
const params: Record<string, string> = {
|
||||
gateway_url: gatewayUrl.trim(),
|
||||
});
|
||||
};
|
||||
if (gatewayToken.trim()) {
|
||||
params.set("gateway_token", gatewayToken.trim());
|
||||
params.gateway_token = gatewayToken.trim();
|
||||
}
|
||||
if (mainSessionKey.trim()) {
|
||||
params.set("gateway_main_session_key", mainSessionKey.trim());
|
||||
params.gateway_main_session_key = mainSessionKey.trim();
|
||||
}
|
||||
const response = await fetch(
|
||||
`${apiBase}/api/v1/gateways/status?${params.toString()}`,
|
||||
{
|
||||
headers: { Authorization: token ? `Bearer ${token}` : "" },
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
if (!response.ok || !data?.connected) {
|
||||
|
||||
const response = await gatewaysStatusApiV1GatewaysStatusGet(params);
|
||||
if (response.status !== 200) {
|
||||
setGatewayCheckStatus("error");
|
||||
setGatewayCheckMessage(data?.error ?? "Unable to reach gateway.");
|
||||
setGatewayCheckMessage("Unable to reach gateway.");
|
||||
return;
|
||||
}
|
||||
const data = response.data;
|
||||
if (!data.connected) {
|
||||
setGatewayCheckStatus("error");
|
||||
setGatewayCheckMessage(data.error ?? "Unable to reach gateway.");
|
||||
return;
|
||||
}
|
||||
setGatewayCheckStatus("success");
|
||||
@@ -114,7 +125,7 @@ export default function NewGatewayPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (!isSignedIn) return;
|
||||
|
||||
@@ -138,35 +149,17 @@ export default function NewGatewayPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const token = await getToken();
|
||||
const response = await fetch(`${apiBase}/api/v1/gateways`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: token ? `Bearer ${token}` : "",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name.trim(),
|
||||
url: gatewayUrl.trim(),
|
||||
token: gatewayToken.trim() || null,
|
||||
main_session_key: mainSessionKey.trim(),
|
||||
workspace_root: workspaceRoot.trim(),
|
||||
skyll_enabled: skyllEnabled,
|
||||
}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Unable to create gateway.");
|
||||
}
|
||||
const created = (await response.json()) as { id: string };
|
||||
router.push(`/gateways/${created.id}`);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Something went wrong.");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
createMutation.mutate({
|
||||
data: {
|
||||
name: name.trim(),
|
||||
url: gatewayUrl.trim(),
|
||||
token: gatewayToken.trim() || null,
|
||||
main_session_key: mainSessionKey.trim(),
|
||||
workspace_root: workspaceRoot.trim(),
|
||||
skyll_enabled: skyllEnabled,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { useMemo, useState } from "react";
|
||||
import Link from "next/link";
|
||||
|
||||
import { SignInButton, SignedIn, SignedOut } from "@clerk/nextjs";
|
||||
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
||||
import {
|
||||
type ColumnDef,
|
||||
type SortingState,
|
||||
@@ -25,19 +25,15 @@ import {
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { apiRequest, useAuthedMutation, useAuthedQuery } from "@/lib/api-query";
|
||||
|
||||
type Gateway = {
|
||||
id: string;
|
||||
name: string;
|
||||
url: string;
|
||||
token?: string | null;
|
||||
main_session_key: string;
|
||||
workspace_root: string;
|
||||
skyll_enabled?: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
import { ApiError } from "@/api/mutator";
|
||||
import {
|
||||
type listGatewaysApiV1GatewaysGetResponse,
|
||||
getListGatewaysApiV1GatewaysGetQueryKey,
|
||||
useDeleteGatewayApiV1GatewaysGatewayIdDelete,
|
||||
useListGatewaysApiV1GatewaysGet,
|
||||
} from "@/api/generated/gateways/gateways";
|
||||
import type { GatewayRead } from "@/api/generated/model";
|
||||
|
||||
const truncate = (value?: string | null, max = 24) => {
|
||||
if (!value) return "—";
|
||||
@@ -58,62 +54,68 @@ const formatTimestamp = (value?: string | null) => {
|
||||
};
|
||||
|
||||
export default function GatewaysPage() {
|
||||
const { isSignedIn } = useAuth();
|
||||
const queryClient = useQueryClient();
|
||||
const [sorting, setSorting] = useState<SortingState>([
|
||||
{ id: "name", desc: false },
|
||||
]);
|
||||
const [deleteTarget, setDeleteTarget] = useState<Gateway | null>(null);
|
||||
const gatewaysQuery = useAuthedQuery<Gateway[]>(
|
||||
["gateways"],
|
||||
"/api/v1/gateways",
|
||||
{
|
||||
const [deleteTarget, setDeleteTarget] = useState<GatewayRead | null>(null);
|
||||
|
||||
const gatewaysKey = getListGatewaysApiV1GatewaysGetQueryKey();
|
||||
const gatewaysQuery = useListGatewaysApiV1GatewaysGet<
|
||||
listGatewaysApiV1GatewaysGetResponse,
|
||||
ApiError
|
||||
>({
|
||||
query: {
|
||||
enabled: Boolean(isSignedIn),
|
||||
refetchInterval: 30_000,
|
||||
refetchOnMount: "always",
|
||||
}
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const gateways = useMemo(() => gatewaysQuery.data ?? [], [gatewaysQuery.data]);
|
||||
const gateways = useMemo(() => gatewaysQuery.data?.data ?? [], [gatewaysQuery.data]);
|
||||
const sortedGateways = useMemo(() => [...gateways], [gateways]);
|
||||
|
||||
const deleteMutation = useAuthedMutation<
|
||||
void,
|
||||
Gateway,
|
||||
{ previous?: Gateway[] }
|
||||
const deleteMutation = useDeleteGatewayApiV1GatewaysGatewayIdDelete<
|
||||
ApiError,
|
||||
{ previous?: listGatewaysApiV1GatewaysGetResponse }
|
||||
>(
|
||||
async (gateway, token) =>
|
||||
apiRequest(`/api/v1/gateways/${gateway.id}`, {
|
||||
method: "DELETE",
|
||||
token,
|
||||
}),
|
||||
{
|
||||
onMutate: async (gateway) => {
|
||||
await queryClient.cancelQueries({ queryKey: ["gateways"] });
|
||||
const previous = queryClient.getQueryData<Gateway[]>(["gateways"]);
|
||||
queryClient.setQueryData<Gateway[]>(["gateways"], (old = []) =>
|
||||
old.filter((item) => item.id !== gateway.id)
|
||||
);
|
||||
return { previous };
|
||||
mutation: {
|
||||
onMutate: async ({ gatewayId }) => {
|
||||
await queryClient.cancelQueries({ queryKey: gatewaysKey });
|
||||
const previous =
|
||||
queryClient.getQueryData<listGatewaysApiV1GatewaysGetResponse>(gatewaysKey);
|
||||
if (previous) {
|
||||
queryClient.setQueryData<listGatewaysApiV1GatewaysGetResponse>(gatewaysKey, {
|
||||
...previous,
|
||||
data: previous.data.filter((gateway) => gateway.id !== gatewayId),
|
||||
});
|
||||
}
|
||||
return { previous };
|
||||
},
|
||||
onError: (_error, _gateway, context) => {
|
||||
if (context?.previous) {
|
||||
queryClient.setQueryData(gatewaysKey, context.previous);
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
setDeleteTarget(null);
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: gatewaysKey });
|
||||
},
|
||||
},
|
||||
onError: (_error, _gateway, context) => {
|
||||
if (context?.previous) {
|
||||
queryClient.setQueryData(["gateways"], context.previous);
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
setDeleteTarget(null);
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["gateways"] });
|
||||
},
|
||||
}
|
||||
},
|
||||
queryClient
|
||||
);
|
||||
|
||||
const handleDelete = () => {
|
||||
if (!deleteTarget) return;
|
||||
deleteMutation.mutate(deleteTarget);
|
||||
deleteMutation.mutate({ gatewayId: deleteTarget.id });
|
||||
};
|
||||
|
||||
const columns = useMemo<ColumnDef<Gateway>[]>(
|
||||
const columns = useMemo<ColumnDef<GatewayRead>[]>(
|
||||
() => [
|
||||
{
|
||||
accessorKey: "name",
|
||||
|
||||
Reference in New Issue
Block a user