Skip to content

Commit

Permalink
Merge pull request #2 from OpenAI-Next/dev-sd-test
Browse files Browse the repository at this point in the history
using indexdb store image data
  • Loading branch information
lloydzhou authored Jul 16, 2024
2 parents dd10301 + 5267ad4 commit 4b84fb3
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 151 deletions.
1 change: 0 additions & 1 deletion app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { AuthPage } from "./auth";
import { getClientConfig } from "../config/client";
import { type ClientApi, getClientApi } from "../client/api";
import { useAccessStore } from "../store";
import { initDB } from "react-indexed-db-hook";

export function Loading(props: { noLogo?: boolean }) {
return (
Expand Down
10 changes: 3 additions & 7 deletions app/components/sd-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import { Select, showToast } from "@/app/components/ui-lib";
import { IconButton } from "@/app/components/button";
import locales from "@/app/locales";
import { nanoid } from "nanoid";
import { useIndexedDB } from "react-indexed-db-hook";
import { StoreKey } from "@/app/constant";
import { SdDbInit, sendSdTask, useSdStore } from "@/app/store/sd";

SdDbInit();
import { useSdStore } from "@/app/store/sd";

const sdCommonParams = (model: string, data: any) => {
return [
Expand Down Expand Up @@ -286,8 +283,7 @@ export function SdPanel() {
setCurrentModel(model);
setParams(getModelParamBasicData(model.params({}), params));
};
const sdListDb = useIndexedDB(StoreKey.SdList);
const { execCountInc } = useSdStore();
const sdStore = useSdStore();
const handleSubmit = () => {
const columns = currentModel.params(params);
const reqParams: any = {};
Expand All @@ -309,7 +305,7 @@ export function SdPanel() {
created_at: new Date().toLocaleString(),
img_data: "",
};
sendSdTask(data, sdListDb, execCountInc, () => {
sdStore.sendTask(data, () => {
setParams(getModelParamBasicData(columns, params, true));
});
};
Expand Down
61 changes: 21 additions & 40 deletions app/components/sd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styles from "@/app/components/sd.module.scss";
import { IconButton } from "@/app/components/button";
import ReturnIcon from "@/app/icons/return.svg";
import Locale from "@/app/locales";
import { Path, StoreKey } from "@/app/constant";
import { Path } from "@/app/constant";
import React, { useEffect, useMemo, useRef, useState } from "react";
import {
copyToClipboard,
Expand All @@ -20,8 +20,7 @@ import DeleteIcon from "@/app/icons/clear.svg";
import CopyIcon from "@/app/icons/copy.svg";
import PromptIcon from "@/app/icons/prompt.svg";
import ResetIcon from "@/app/icons/reload.svg";
import { useIndexedDB } from "react-indexed-db-hook";
import { sendSdTask, useSdStore } from "@/app/store/sd";
import { useSdStore } from "@/app/store/sd";
import locales from "@/app/locales";
import LoadingIcon from "../icons/three-dots.svg";
import ErrorIcon from "../icons/delete.svg";
Expand All @@ -31,17 +30,7 @@ import {
showImageModal,
showModal,
} from "@/app/components/ui-lib";

function getBase64ImgUrl(base64Data: string, contentType: string) {
const byteCharacters = atob(base64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: contentType });
return URL.createObjectURL(blob);
}
import { removeImage } from "@/app/utils/chat";

function getSdTaskStatus(item: any) {
let s: string;
Expand Down Expand Up @@ -100,15 +89,12 @@ export function Sd() {
const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
const config = useAppConfig();
const scrollRef = useRef<HTMLDivElement>(null);
const sdListDb = useIndexedDB(StoreKey.SdList);
const [sdImages, setSdImages] = useState([]);
const { execCount, execCountInc } = useSdStore();
const sdStore = useSdStore();
const [sdImages, setSdImages] = useState(sdStore.draw);

useEffect(() => {
sdListDb.getAll().then((data) => {
setSdImages(((data as never[]) || []).reverse());
});
}, [execCount]);
setSdImages(sdStore.draw);
}, [sdStore.currentId]);

return (
<div className={chatStyles.chat} key={"1"}>
Expand Down Expand Up @@ -161,20 +147,20 @@ export function Sd() {
{item.status === "success" ? (
<img
className={styles["img"]}
src={`data:image/png;base64,${item.img_data}`}
alt={`${item.id}`}
onClick={(e) => {
src={item.img_data}
alt={item.id}
onClick={(e) =>
showImageModal(
getBase64ImgUrl(item.img_data, "image/png"),
item.img_data,
true,
isMobileScreen
? { width: "100%", height: "fit-content" }
: { maxWidth: "100%", maxHeight: "100%" },
isMobileScreen
? { width: "100%", height: "fit-content" }
: { width: "100%", height: "100%" },
);
}}
)
}
/>
) : item.status === "error" ? (
<div className={styles["pre-img"]}>
Expand Down Expand Up @@ -258,26 +244,21 @@ export function Sd() {
created_at: new Date().toLocaleString(),
img_data: "",
};
sendSdTask(reqData, sdListDb, execCountInc);
sdStore.sendTask(reqData);
}}
/>
<ChatAction
text={Locale.Sd.Actions.Delete}
icon={<DeleteIcon />}
onClick={async () => {
if (await showConfirm(Locale.Sd.Danger.Delete)) {
sdListDb.deleteRecord(item.id).then(
() => {
setSdImages(
sdImages.filter(
(i: any) => i.id !== item.id,
),
);
},
(error) => {
console.error(error);
},
);
// remove img_data + remove item in list
removeImage(item.img_data).finally(() => {
sdStore.draw = sdImages.filter(
(i: any) => i.id !== item.id,
);
sdStore.getNextId();
});
}
}}
/>
Expand Down
5 changes: 3 additions & 2 deletions app/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { stabilityRequestCall } from "@/app/store/sd";

export const OWNER = "Yidadaa";
export const REPO = "ChatGPT-Next-Web";
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
Expand All @@ -25,6 +23,8 @@ export const BYTEDANCE_BASE_URL = "https://ark.cn-beijing.volces.com";

export const ALIBABA_BASE_URL = "https://dashscope.aliyuncs.com/api/";

export const UPLOAD_URL = "/api/cache/upload";

export enum Path {
Home = "/",
Chat = "/chat",
Expand Down Expand Up @@ -57,6 +57,7 @@ export enum FileName {
}

export enum StoreKey {
File = "chat-next-web-file",
Chat = "chat-next-web-store",
Access = "access-control",
Config = "app-config",
Expand Down
1 change: 0 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Analytics } from "@vercel/analytics/react";
import { Home } from "./components/home";

import { getServerSideConfig } from "./config/server";
import { SdDbInit } from "@/app/store/sd";

const serverConfig = getServerSideConfig();

Expand Down
Loading

0 comments on commit 4b84fb3

Please sign in to comment.