Refactor WhisperButton and ChatBOX components to use Button component for consistency; improve layout and structure

This commit is contained in:
ecwu
2024-12-20 19:52:55 +08:00
parent 145b333ce4
commit de231e215e
2 changed files with 125 additions and 127 deletions

View File

@@ -2,6 +2,7 @@ import { createRef } from "preact";
import { ChatStore } from "@/types/chatstore"; import { ChatStore } from "@/types/chatstore";
import { StateUpdater, useEffect, useState, Dispatch } from "preact/hooks"; import { StateUpdater, useEffect, useState, Dispatch } from "preact/hooks";
import { Button } from "@/components/ui/button";
const WhisperButton = (props: { const WhisperButton = (props: {
chatStore: ChatStore; chatStore: ChatStore;
@@ -12,10 +13,9 @@ const WhisperButton = (props: {
const mediaRef = createRef(); const mediaRef = createRef();
const [isRecording, setIsRecording] = useState("Mic"); const [isRecording, setIsRecording] = useState("Mic");
return ( return (
<button <Button
className={`btn disabled:line-through disabled:btn-neutral disabled:text-white m-1 p-1 ${ variant={isRecording === "Recording" ? "destructive" : "default"}
isRecording === "Recording" ? "btn-error" : "btn-success" className={`m-1 p-1 ${isRecording !== "Mic" ? "animate-pulse" : ""}`}
} ${isRecording !== "Mic" ? "animate-pulse" : ""}`}
disabled={isRecording === "Transcribing"} disabled={isRecording === "Transcribing"}
ref={mediaRef} ref={mediaRef}
onClick={async () => { onClick={async () => {
@@ -38,7 +38,7 @@ const WhisperButton = (props: {
} else { } else {
return content.map((c) => c?.text).join(" "); return content.map((c) => c?.text).join(" ");
} }
}), })
) )
.concat([inputMsg]) .concat([inputMsg])
.join(" "); .join(" ");
@@ -52,7 +52,7 @@ const WhisperButton = (props: {
await navigator.mediaDevices.getUserMedia({ await navigator.mediaDevices.getUserMedia({
audio: true, audio: true,
}), }),
{ audioBitsPerSecond: 64 * 1000 }, { audioBitsPerSecond: 64 * 1000 }
); );
// mount mediaRecorder to ref // mount mediaRecorder to ref
@@ -125,7 +125,7 @@ const WhisperButton = (props: {
}} }}
> >
{isRecording} {isRecording}
</button> </Button>
); );
}; };

View File

@@ -470,8 +470,7 @@ export default function ChatBOX(props: {
setShow={setShowSearch} setShow={setShowSearch}
/> />
)} )}
<ChatMessageList>
<div className="grow overflow-scroll">
{!chatStore.apiKey && ( {!chatStore.apiKey && (
<p className="bg-base-200 p-6 rounded my-3 text-left"> <p className="bg-base-200 p-6 rounded my-3 text-left">
{Tr("Please click above to set")} (OpenAI) API KEY {Tr("Please click above to set")} (OpenAI) API KEY
@@ -538,7 +537,7 @@ export default function ChatBOX(props: {
setChatStore={setChatStore} setChatStore={setChatStore}
/> />
)} )}
<ChatMessageList>
{chatStore.history.filter((msg) => !msg.example).length == 0 && ( {chatStore.history.filter((msg) => !msg.example).length == 0 && (
<div className="bg-base-200 break-all p-3 my-3 text-left"> <div className="bg-base-200 break-all p-3 my-3 text-left">
<h2> <h2>
@@ -663,7 +662,6 @@ export default function ChatBOX(props: {
)} )}
<div ref={messagesEndRef}></div> <div ref={messagesEndRef}></div>
</ChatMessageList> </ChatMessageList>
</div>
{images.length > 0 && ( {images.length > 0 && (
<div className="flex flex-wrap"> <div className="flex flex-wrap">
{images.map((image, index) => ( {images.map((image, index) => (
@@ -727,14 +725,14 @@ export default function ChatBOX(props: {
</Button> </Button>
{chatStore.whisper_api && chatStore.whisper_key && ( {chatStore.whisper_api && chatStore.whisper_key && (
<Button variant="ghost" size="icon"> <>
<WhisperButton <WhisperButton
chatStore={chatStore} chatStore={chatStore}
inputMsg={inputMsg} inputMsg={inputMsg}
setInputMsg={setInputMsg} setInputMsg={setInputMsg}
/> />
<span className="sr-only">Use Microphone</span> <span className="sr-only">Use Microphone</span>
</Button> </>
)} )}
<Button <Button