Skip to content

Commit

Permalink
Add useCopilot hook for managing copilot data
Browse files Browse the repository at this point in the history
  • Loading branch information
faltawy committed Jan 8, 2024
1 parent 4e5c5e8 commit c664e32
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dashboard/hooks/useCopilot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CopilotType, getCopilot, updateCopilot } from "@/data/copilot";
import { useParams } from "next/navigation";
import { useAsyncFn } from "react-use";
import useSWR from "swr";

export function useCopilot() {
const { copilot_id } = useParams();
const copilotData = useSWR(copilot_id, getCopilot);

async function updateCopilotAsync(copilot: Partial<CopilotType>) {
if (!copilot_id) throw new Error("Copilot id is required");
const copilotId = copilot_id as string;
return await updateCopilot(copilotId, copilot);
}
const [state,] = useAsyncFn(updateCopilotAsync);
return [copilotData, state] as const;
}

0 comments on commit c664e32

Please sign in to comment.