import React, { useRef } from "react"; import { FormattedRelativeTime } from "react-intl"; import useSWR, { useSWRConfig } from "swr"; import { WorkerData } from "../api/workersRoute.ts"; import { Counter } from "./Counter.tsx"; import { fetchApi, handleResponse } from "./apiClient.tsx"; export function WorkersPage(props: { sessionId?: string }) { const { sessionId } = props; const createWorkerModalRef = useRef(null); const session = useSWR( sessionId ? ["sessions/{sessionId}", "GET", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const user = useSWR( session.data?.userId ? ["users/{userId}", "GET", { params: { userId: String(session.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const workers = useSWR( ["workers", "GET", {}] as const, (args) => fetchApi(...args).then(handleResponse), { refreshInterval: 5000 }, ); return ( <> {user.data?.isAdmin && ( )}
{ e.preventDefault(); const data = new FormData(e.target as HTMLFormElement); const key = data.get("key") as string; const name = data.get("name") as string; const sdUrl = data.get("url") as string; const user = data.get("user") as string; const password = data.get("password") as string; console.log(key, name, user, password); workers.mutate(async () => { const worker = await fetchApi("workers", "POST", { query: { sessionId: sessionId! }, body: { type: "application/json", data: { key, name: name || null, sdUrl, sdAuth: user && password ? { user, password } : null, }, }, }).then(handleResponse); return [...(workers.data ?? []), worker]; }); createWorkerModalRef.current?.close(); }} >
); } function WorkerListItem(props: { worker: WorkerData; sessionId?: string }) { const { worker, sessionId } = props; const editWorkerModalRef = useRef(null); const deleteWorkerModalRef = useRef(null); const session = useSWR( sessionId ? ["sessions/{sessionId}", "GET", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const user = useSWR( session.data?.userId ? ["users/{userId}", "GET", { params: { userId: String(session.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const { mutate } = useSWRConfig(); return (
  • {worker.name ?? worker.key}

    {worker.isActive ?

    ✅ Active

    : ( <>

    Last seen {worker.lastOnlineTime ? ( ) : "never"}

    {worker.lastError && (

    {worker.lastError.message} ( )

    )} )}

    images per minute, {" "} steps per minute

    {user.data?.isAdmin && ( <> )}

    { e.preventDefault(); const data = new FormData(e.target as HTMLFormElement); const user = data.get("user") as string; const password = data.get("password") as string; console.log(user, password); fetchApi("workers/{workerId}", "PATCH", { params: { workerId: worker.id }, query: { sessionId: sessionId! }, body: { type: "application/json", data: { auth: user && password ? { user, password } : null, }, }, }); editWorkerModalRef.current?.close(); }} >
    { e.preventDefault(); fetchApi("workers/{workerId}", "DELETE", { params: { workerId: worker.id }, query: { sessionId: sessionId! }, }).then(handleResponse).then(() => mutate(["workers", "GET", {}])); deleteWorkerModalRef.current?.close(); }} >

    Are you sure you want to delete this worker?

  • ); }