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

using CacheStorage store image data #2

Merged
merged 10 commits into from
Jul 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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