Skip to content

Commit

Permalink
revert: undo changes to prompt component
Browse files Browse the repository at this point in the history
  • Loading branch information
agrattan0820 committed Jul 26, 2024
1 parent 356641c commit 0a3c25a
Showing 1 changed file with 48 additions and 62 deletions.
110 changes: 48 additions & 62 deletions apps/client/src/components/game/prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import toast from "react-hot-toast";
import { FiHelpCircle, FiX } from "react-icons/fi";
import useSound from "use-sound";
import type { Session } from "next-auth";
import { captureException } from "@sentry/nextjs";

import Button, { SecondaryButton } from "@ai/components/button";
import Ellipsis from "@ai/components/ellipsis";
Expand Down Expand Up @@ -110,54 +109,45 @@ const Prompt = ({
const outOfRegenerations = remainingImageGenerations === 0;

const onPromptSubmit = async (e: FormEvent<PromptFormType>) => {
try {
e.preventDefault();
setLoading(true);
e.preventDefault();
setLoading(true);

const formPrompt = e.currentTarget.elements.prompt.value;
setImagePrompt(formPrompt);
const formPrompt = e.currentTarget.elements.prompt.value;
setImagePrompt(formPrompt);

console.time("Execution Time");
console.time("Execution Time");

const images = await generateSDXLImages(formPrompt);
const images = await generateSDXLImages(formPrompt);

console.timeEnd("Execution Time");
console.timeEnd("Execution Time");

if (!images || !Array.isArray(images) || images.length !== 2) {
throw new Error(
"Images wasn't defined or the incorrect amount of images were returned",
);
}

if (!userId) {
throw new Error("User id not defined when creating generations");
}

const generations = await createGenerations({
userId,
gameId,
questionId: currQuestion.id,
images: images?.map((image) => {
return {
text: formPrompt,
imageUrl: image,
};
}),
});
if (images && Array.isArray(images) && images.length === 2) {
if (userId) {
const generations = await createGenerations({
userId,
gameId,
questionId: currQuestion.id,
images: images?.map((image) => {
return {
text: formPrompt,
imageUrl: image,
};
}),
});

setImageOption1(generations[0]);
setImageOption2(generations[1]);
setImageOption1(generations[0]);
setImageOption2(generations[1]);

setCurrQuestionNumGenerations(
currQuestionNumGenerations + generations.length,
);
} catch (error) {
setCurrQuestionNumGenerations(
currQuestionNumGenerations + generations.length,
);
}
} else {
console.error("Images were unable to be generated");
toast.error("I'm afraid I don't know how to process such a request.");
captureException(error);
} finally {
setLoading(false);
}

setLoading(false);
};

const resetImage = () => {
Expand All @@ -171,38 +161,34 @@ const Prompt = ({
};

const onImageSubmit = async () => {
try {
setLoading(true);

if (!session || !imageOption1 || !imageOption2) {
throw new Error(
"User was not defined when trying to submit a generation",
);
}
setLoading(true);

if (session && imageOption1 && imageOption2) {
socket.emit("generationSubmitted", {
generationId: selectedImage === 1 ? imageOption1?.id : imageOption2?.id,
gameId: gameId,
round: currRound,
});

if (stage === "FIRST") {
setCurrQuestionNumGenerations(0);
setStage("SECOND");
setImagePrompt("");
resetImage();
} else {
if (soundEnabled) {
play();
}
send({ type: "SUBMIT" });
}
} catch (error) {
} else {
console.error("User was not defined when trying to submit a generation");
toast.error("Oops, we can't submit your generation.");
captureException(error);
} finally {
setLoading(false);
return;
}

if (stage === "FIRST") {
setCurrQuestionNumGenerations(0);
setStage("SECOND");
setImagePrompt("");
resetImage();
} else {
if (soundEnabled) {
play();
}
send({ type: "SUBMIT" });
}

setLoading(false);
};

useEffect(() => {
Expand Down

0 comments on commit 0a3c25a

Please sign in to comment.