All files / src/apps/user/pages/swarms EditSwarm.tsx

64.28% Statements 9/14
60% Branches 6/10
40% Functions 2/5
64.28% Lines 9/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                            1x 6x 6x 6x 6x 6x     3x     6x 6x                                                                
import { useResourceId } from "@/components/detail";
import SubPage from "@/components/page/SubPage/SubPage";
import type { Swarm } from "@/objects/Swarm";
import { APP_ROUTES } from "@/routes/const";
import { swarmHref } from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
import React from "react";
import { useNavigate } from "react-router-dom";
import SwarmForm from "./SwarmForm";
 
/** Change a swarm. Its runs keep what they ran: each stores the version. */
const EditSwarm: React.FC = () => {
  const swarmId = useResourceId("swarmId");
  const { getApiHandler } = useApiHandler();
  const queryClient = useQueryClient();
  const navigate = useNavigate();
  const { data: swarm, isLoading } = useQuery<Swarm | null>({
    queryKey: [QUERY_KEYS.GET.SWARM_BY_ID, swarmId],
    queryFn: () =>
      swarmId ? getApiHandler().swarm_router.getById(swarmId) : Promise.resolve(null),
    enabled: !!swarmId,
  });
  const back = swarmId ? swarmHref(swarmId) : APP_ROUTES.USER.INFERENCE.swarms;
  return (
    <SubPage
      title={swarm ? `Edit ${swarm.name}` : "Edit Swarm"}
      description="Runs already sent keep the version they ran."
      back={back}
    >
      {isLoading ? (
        <div className="flex justify-center py-16">
          <Loader2 className="size-5 animate-spin text-gray-400" />
        </div>
      ) : !swarm ? (
        <p className="text-sm text-gray-500 dark:text-gray-400">This swarm was not found.</p>
      ) : (
        <SwarmForm
          initial={swarm}
          submitLabel="Save"
          onSubmit={(body) => getApiHandler().swarm_router.update(swarm.id, body)}
          onDone={(saved) => {
            void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SWARMS] });
            void queryClient.invalidateQueries({
              queryKey: [QUERY_KEYS.GET.SWARM_BY_ID, saved.id],
            });
            navigate(swarmHref(saved.id));
          }}
          onCancel={() => navigate(back)}
        />
      )}
    </SubPage>
  );
};
 
export default EditSwarm;