Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for openAI API key #86

Merged
merged 1 commit into from Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/client/apps/story/index.tsx
Expand Up @@ -31,6 +31,7 @@ export function Story() {

const [story, setStory] = useState("");
const [generating, setGenerating] = useState(false);
const [hasOpenAiApiKey, setHasOpenAiApiKey] = useState(false);

const saveStory = useCallback(
async (story_: string) => {
Expand Down Expand Up @@ -73,6 +74,8 @@ export function Story() {
});
}, [setImages]);

const isDisabled = selectedImages.length === 0 || !hasOpenAiApiKey;

useSDK<unknown, { done: boolean; story: string }>(APP_ID, {
async onMessage(message) {
console.log(message);
Expand Down Expand Up @@ -104,6 +107,16 @@ export function Story() {
};
}, [loadImages, setImages]);

useEffect(() => {
window.ipc.send("hasOpenAiApiKey");
const unsubscribe = window.ipc.on("openAiApiKey", (hasKey: boolean) => {
setHasOpenAiApiKey(hasKey);
});
return () => {
unsubscribe();
};
}, []);

if (story) {
return (
<CustomScrollbars>
Expand Down Expand Up @@ -154,6 +167,8 @@ export function Story() {
</Grid>
<Grid xs={1} sx={{ display: "flex" }}>
<StoryForm
disabled={isDisabled}
hasOpenAiApiKey={hasOpenAiApiKey}
onSubmit={() => {
setGenerating(true);
}}
Expand Down
27 changes: 25 additions & 2 deletions src/client/apps/story/story-form.tsx
@@ -1,5 +1,7 @@
import { CustomScrollbars } from "@captn/joy/custom-scrollbars";
import { useSDK } from "@captn/react/use-sdk";
import WarningIcon from "@mui/icons-material/Warning";
import Alert from "@mui/joy/Alert";
import Box from "@mui/joy/Box";
import Button from "@mui/joy/Button";
import FormControl from "@mui/joy/FormControl";
Expand All @@ -19,7 +21,15 @@ import { APP_ID } from "./constants";

import type { FormInput } from "#/types/story";

export function StoryForm({ onSubmit }: { onSubmit?(): void }) {
export function StoryForm({
hasOpenAiApiKey,
disabled,
onSubmit,
}: {
onSubmit?(): void;
disabled?: boolean;
hasOpenAiApiKey?: boolean;
}) {
const {
t,
i18n: { language: locale },
Expand Down Expand Up @@ -57,6 +67,19 @@ export function StoryForm({ onSubmit }: { onSubmit?(): void }) {
>
<Box sx={{ flex: 1, position: "relative" }}>
<CustomScrollbars>
{!hasOpenAiApiKey && (
<Alert
color="warning"
variant="soft"
startDecorator={<WarningIcon />}
slotProps={{ startDecorator: { sx: { alignSelf: "flexStart" } } }}
sx={{ mb: 2 }}
>
<Typography>
{t("common:pages.dataset.enterKeyToUseGPTVision")}
</Typography>
</Alert>
)}
<Typography sx={{ mb: 2 }}>{t("texts:storyFormIntroduction")}</Typography>
<FormControl required>
<FormLabel>{t("labels:formLabel.length")}</FormLabel>
Expand Down Expand Up @@ -153,7 +176,7 @@ export function StoryForm({ onSubmit }: { onSubmit?(): void }) {
</CustomScrollbars>
</Box>
<Box>
<Button fullWidth type="submit" color="primary" variant="solid">
<Button fullWidth disabled={disabled} type="submit" color="primary" variant="solid">
{t("labels:submitButtonText")}
</Button>
</Box>
Expand Down
3 changes: 3 additions & 0 deletions src/electron/background.ts
Expand Up @@ -53,6 +53,9 @@ import "@/ipc/testing";
// Import the vector store
import "@/ipc/vector-store";

// Import keys
import "@/ipc/keys";

let unsubscribe: (() => Promise<void>) | undefined;
// Initialize the application by calling the main function.
// Upon completion, log to the console indicating the application has started.
Expand Down
7 changes: 7 additions & 0 deletions src/electron/future/ipc/keys.ts
@@ -0,0 +1,7 @@
import { ipcMain } from "electron";

import { keyStore } from "@/stores";

ipcMain.on("hasOpenAiApiKey", event => {
event.sender.send("openAiApiKey", Boolean(keyStore.get("openAiApiKey")));
});