Skip to content

Commit

Permalink
Add loading info when image is being generated (#539)
Browse files Browse the repository at this point in the history
* show loading when generating image

* add info type

* fix typo

* make id optional
  • Loading branch information
mingming-ma authored Mar 30, 2024
1 parent 7c7f441 commit 2fa48f0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/hooks/use-alert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from "react";
import { useToast } from "@chakra-ui/react";

type AlertArguments = {
export type AlertArguments = {
// Use `id` if you want to avoid duplicate alerts showing
id?: string;
title: string;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/commands/ImageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ import { ChatCraftCommand } from "../ChatCraftCommand";
import { ChatCraftChat } from "../ChatCraftChat";
import { ChatCraftHumanMessage } from "../ChatCraftMessage";
import { generateImage, isGenerateImageSupported } from "../../lib/ai";
import { utilizeAlert } from "../../lib/utils";

export class ImageCommand extends ChatCraftCommand {
constructor() {
super("image");
}

async execute(chat: ChatCraftChat, user: User | undefined, args?: string[]) {
const { loading, closeLoading } = await utilizeAlert();

if (!(await isGenerateImageSupported())) {
throw new Error("Failed to generate image, no image generation models available");
}
if (!(args && args[0])) {
throw new Error("must include a prompt");
}

const prompt = args.join(" ");
let imageUrls: string[] = [];
const text = `(DALL·E 3 result of the prompt: ${prompt})`;

const alertId = loading({
title: `Generating image, please wait.`,
});

try {
imageUrls = await generateImage(prompt);
} catch (error: any) {
console.error(`Failed to generate image: ${error.message}`);
throw new Error(`Failed to generate image: ${error.message}`);
}

closeLoading(alertId);
return chat.addMessage(new ChatCraftHumanMessage({ user, text, imageUrls }));
}
}
48 changes: 48 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,51 @@ export const updateImageUrls = (
}
});
};

/**
* Only meant to be used outside components or hooks
* where useAlert cannot be used.
*/
import type { AlertArguments } from "../hooks/use-alert";
export const utilizeAlert = async () => {
const { createStandaloneToast } = await import("@chakra-ui/react");
const { toast } = createStandaloneToast();

const info = ({ id, title, message }: AlertArguments) => {
toast({
id,
title,
description: message,
colorScheme: "blue",
status: "info",
position: "top",
isClosable: true,
duration: 3000,
});
};

const loading = ({ id, title, message }: AlertArguments) => {
const fallbackId = new Date().toISOString();
toast({
id: id ?? fallbackId,
title,
description: message,
colorScheme: "blue",
status: "loading",
position: "top",
isClosable: true,
duration: null,
});
return id ?? fallbackId;
};

const closeLoading = (id: string) => {
toast.close(id);
};

return {
info,
loading,
closeLoading,
};
};

0 comments on commit 2fa48f0

Please sign in to comment.