Skip to content

Commit

Permalink
feat: update event signal picker
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 18, 2024
1 parent fb0e6d5 commit 6f3a8cb
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 172 deletions.
15 changes: 8 additions & 7 deletions app/src/components/FileProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const MaxFileSize = 1024 * 1024 * 25; // 25MB File Size Limit
const MaxPromptSize = 5000; // 5000 Prompt Size Limit (to avoid token overflow)

type FileProviderProps = {
value: FileArray;
onChange?: (value: FileArray) => void;
files: FileArray;
dispatch: (action: Record<string, any>) => void;
};

function FileProvider({ value, onChange }: FileProviderProps) {
function FileProvider({ files, dispatch }: FileProviderProps) {
const { t } = useTranslation();
const { toast } = useToast();
const model = useSelector(selectModel);
Expand Down Expand Up @@ -127,19 +127,20 @@ function FileProvider({ value, onChange }: FileProviderProps) {
description: t("file.max-length-prompt"),
});
}
onChange?.([...value, file]);

dispatch({ type: "add", payload: file });
}

function removeFile(index: number) {
onChange?.(value.filter((_, i) => i !== index));
dispatch({ type: "remove", payload: index });
}

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<ChatAction
text={t("file.upload")}
className={cn(value.length > 0 && "active")}
className={cn(files.length > 0 && "active")}
>
<Plus className={`h-4 w-4`} />
</ChatAction>
Expand All @@ -153,7 +154,7 @@ function FileProvider({ value, onChange }: FileProviderProps) {
<AlertCircle className="h-4 w-4" />
<AlertTitle>{t("file.type")}</AlertTitle>
</Alert>
<FileList value={value} removeFile={removeFile} />
<FileList value={files} removeFile={removeFile} />
<FileInput
loading={loading}
id={"file"}
Expand Down
4 changes: 3 additions & 1 deletion app/src/components/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message } from "@/api/types.tsx";
import { Message, UserRole } from "@/api/types.tsx";
import Markdown from "@/components/Markdown.tsx";
import {
CalendarCheck2,
Expand Down Expand Up @@ -83,6 +83,8 @@ type MessageQuotaProps = {
function MessageQuota({ message }: MessageQuotaProps) {
const [detail, setDetail] = useState(false);

if (message.role === UserRole) return null;

return (
message.quota &&
message.quota !== 0 && (
Expand Down
21 changes: 17 additions & 4 deletions app/src/components/home/ChatWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next";
import { useEffect, useMemo, useRef, useState } from "react";
import { useEffect, useMemo, useReducer, useRef, useState } from "react";
import FileAction from "@/components/FileProvider.tsx";
import { useSelector } from "react-redux";
import { selectAuthenticated, selectInit } from "@/store/auth.ts";
Expand Down Expand Up @@ -46,12 +46,25 @@ function Interface(props: InterfaceProps) {
return messages.length > 0 ? <ChatInterface {...props} /> : <ChatSpace />;
}

function fileReducer(state: FileArray, action: Record<string, any>): FileArray {
switch (action.type) {
case "add":
return [...state, action.payload];
case "remove":
return state.filter((file) => file !== action.payload);
case "clear":
return [];
default:
return state;
}
}

function ChatWrapper() {
const { t } = useTranslation();
const { toast } = useToast();
const { send: sendAction } = useMessageActions();
const process = listenMessageEvent();
const [files, setFiles] = useState<FileArray>([]);
const [files, fileDispatch] = useReducer(fileReducer, []);
const [input, setInput] = useState("");
const [visible, setVisibility] = useState(false);
const init = useSelector(selectInit);
Expand All @@ -72,7 +85,7 @@ function ChatWrapper() {
const [instance, setInstance] = useState<HTMLElement | null>(null);

function clearFile() {
setFiles([]);
fileDispatch({ type: "clear" });
}

async function processSend(
Expand Down Expand Up @@ -162,7 +175,7 @@ function ChatWrapper() {
target={instance}
/>
<WebAction visible={!visible} />
<FileAction value={files} onChange={setFiles} />
<FileAction files={files} dispatch={fileDispatch} />
<EditorAction value={input} onChange={setInput} />
<MaskAction />
<MarketAction />
Expand Down
18 changes: 3 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.1
github.com/lukasjarosch/go-docx v0.4.7
github.com/mattn/go-sqlite3 v1.14.22
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkoukk/tiktoken-go v0.1.6
github.com/russross/blackfriday/v2 v2.1.0
Expand All @@ -28,58 +29,45 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bep/godartsass v1.2.0 // indirect
github.com/bep/godartsass/v2 v2.0.0 // indirect
github.com/bep/golibsass v1.1.1 // indirect
github.com/bincooo/requests v0.0.0-20230720064210-7eae5d6c9d1e // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/cloudwego/hertz/cmd/hz v0.7.0 // indirect
github.com/cosmtrek/air v1.51.0 // indirect
github.com/creack/pty v1.1.21 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gohugoio/hugo v0.123.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/refraction-networking/utls v1.3.2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tdewolff/parse/v2 v2.7.12 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/wangluozhe/fhttp v0.0.0-20230512135433-5c2ebfb4868a // indirect
Expand Down
Loading

0 comments on commit 6f3a8cb

Please sign in to comment.