80 lines
2.4 KiB
TypeScript
Executable File
80 lines
2.4 KiB
TypeScript
Executable File
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import posthog from "posthog-js";
|
|
import { useRef, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { checkEnkaStatus, submitArtifact } from "@/lib/api";
|
|
import { shared } from "@/lib/comms";
|
|
import type { ActionSubmitContextData } from "@/components/action-submit-provider";
|
|
import { ActionSubmitContext } from "@/components/action-submit-provider";
|
|
|
|
export function ArtifactFormWrapper({
|
|
edit,
|
|
enka,
|
|
...props
|
|
}: React.ComponentProps<"form"> & {
|
|
edit?: { sub: string; token: string };
|
|
enka: boolean;
|
|
}) {
|
|
const router = useRouter();
|
|
const [, setDialog] = shared.state("warning");
|
|
const [, setWarningUid] = shared.state("warning.uid");
|
|
const [warningSrc, setWarningSrc] = shared.state("warning.src");
|
|
const [dataHold, setDataHold] = useState<FormData>();
|
|
const callback = useRef<() => void>(() => {});
|
|
const context: ActionSubmitContextData = {
|
|
listenForComplete(complete) {
|
|
callback.current = complete;
|
|
},
|
|
};
|
|
|
|
// Disable warning for 60 seconds
|
|
shared.signal(
|
|
"warningSolved",
|
|
() => dataHold && warningSrc === "submit" && submit(dataHold, false),
|
|
);
|
|
|
|
async function submit(data: FormData, doCheck = true) {
|
|
if (enka && doCheck) {
|
|
const check = await checkEnkaStatus(
|
|
(data.get("uid") ?? "").toString(),
|
|
(data.get("character") ?? "").toString(),
|
|
);
|
|
console.log(data.get("uid"), data.get("character"), check);
|
|
setWarningUid(data.get("character")?.toString());
|
|
setWarningSrc("submit");
|
|
setDataHold(data);
|
|
if (check) return setDialog(check);
|
|
}
|
|
posthog.capture("artifact_submitted", {
|
|
character: data.get("character")?.toString(),
|
|
is_edit: !!edit,
|
|
});
|
|
submitArtifact(data, edit)
|
|
.then((q) => {
|
|
if (typeof q === "string") return toast.error(q);
|
|
try {
|
|
// biome-ignore lint/suspicious/noTsIgnore: error appears on build but not visible to biome
|
|
// @ts-ignore
|
|
cookieStore.set("sid", q.id);
|
|
} catch {}
|
|
router.refresh();
|
|
})
|
|
.catch((e) => toast.error(`${e.message || e}`));
|
|
}
|
|
return (
|
|
<ActionSubmitContext.Provider value={context}>
|
|
<form
|
|
{...props}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
submit(new FormData(e.target as HTMLFormElement)).finally(
|
|
callback.current,
|
|
);
|
|
}}
|
|
/>
|
|
</ActionSubmitContext.Provider>
|
|
);
|
|
}
|