Because https://app.openpipe.ai/experiments/B1EtN6oHeXMele2 is a cooler URL than https://app.openpipe.ai/experiments/3692942c-6f1b-4bef-83b1-c11f00a3fbdd
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useCallback } from "react";
|
|
import { api } from "~/utils/api";
|
|
import { useExperiment, useHandledAsyncCallback } from "~/utils/hooks";
|
|
import { signIn, useSession } from "next-auth/react";
|
|
import { useRouter } from "next/router";
|
|
import { useAppStore } from "~/state/store";
|
|
|
|
export const useOnForkButtonPressed = () => {
|
|
const router = useRouter();
|
|
|
|
const user = useSession().data;
|
|
const experiment = useExperiment();
|
|
const selectedProjectId = useAppStore((state) => state.selectedProjectId);
|
|
|
|
const forkMutation = api.experiments.fork.useMutation();
|
|
|
|
const [onFork, isForking] = useHandledAsyncCallback(async () => {
|
|
if (!experiment.data?.id || !selectedProjectId) return;
|
|
const newExperiment = await forkMutation.mutateAsync({
|
|
id: experiment.data.id,
|
|
projectId: selectedProjectId,
|
|
});
|
|
await router.push({
|
|
pathname: "/experiments/[experimentSlug]",
|
|
query: { experimentSlug: newExperiment.slug },
|
|
});
|
|
}, [forkMutation, experiment.data?.id, router]);
|
|
|
|
const onForkButtonPressed = useCallback(() => {
|
|
if (user === null) {
|
|
signIn("github").catch(console.error);
|
|
} else {
|
|
onFork();
|
|
}
|
|
}, [onFork, user]);
|
|
|
|
return { onForkButtonPressed, isForking };
|
|
};
|