refactor: Reduce Prop Drilling & Improve Separation of Concerns:

This commit is contained in:
dayuan.jiang
2025-03-27 07:48:19 +00:00
parent 13ace596d2
commit 34cc437523
3 changed files with 14 additions and 14 deletions

View File

@@ -23,11 +23,11 @@ interface ChatInputProps {
status: "submitted" | "streaming" | "ready" | "error"; status: "submitted" | "streaming" | "ready" | "error";
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void; onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void; onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
setMessages: (messages: any[]) => void; onClearChat: () => void;
files?: FileList; files?: FileList;
onFileChange?: (files: FileList | undefined) => void; onFileChange?: (files: FileList | undefined) => void;
showHistory?: boolean; showHistory?: boolean;
setShowHistory?: (show: boolean) => void; onToggleHistory?: (show: boolean) => void;
} }
export function ChatInput({ export function ChatInput({
@@ -35,11 +35,11 @@ export function ChatInput({
status, status,
onSubmit, onSubmit,
onChange, onChange,
setMessages, onClearChat,
files, files,
onFileChange, onFileChange,
showHistory = false, showHistory = false,
setShowHistory = () => {}, onToggleHistory = () => {},
}: ChatInputProps) { }: ChatInputProps) {
const { loadDiagram: onDisplayChart, diagramHistory } = useDiagram(); const { loadDiagram: onDisplayChart, diagramHistory } = useDiagram();
const textareaRef = useRef<HTMLTextAreaElement>(null); const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -132,7 +132,7 @@ export function ChatInput({
// Handle clearing conversation and diagram // Handle clearing conversation and diagram
const handleClear = () => { const handleClear = () => {
setMessages([]); onClearChat();
onDisplayChart(`<mxfile host="embed.diagrams.net" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36" version="26.1.1"> onDisplayChart(`<mxfile host="embed.diagrams.net" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36" version="26.1.1">
<diagram name="Page-1" id="NsivuNt5aJDXaP8udwGv"> <diagram name="Page-1" id="NsivuNt5aJDXaP8udwGv">
<mxGraphModel dx="394" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0"> <mxGraphModel dx="394" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
@@ -221,7 +221,7 @@ export function ChatInput({
<HistoryDialog <HistoryDialog
showHistory={showHistory} showHistory={showHistory}
setShowHistory={setShowHistory} onToggleHistory={onToggleHistory}
/> />
</div> </div>
<div className="flex gap-2"> <div className="flex gap-2">
@@ -230,7 +230,7 @@ export function ChatInput({
type="button" type="button"
variant="outline" variant="outline"
size="icon" size="icon"
onClick={() => setShowHistory(true)} onClick={() => onToggleHistory(true)}
disabled={ disabled={
status === "streaming" || status === "streaming" ||
diagramHistory.length === 0 diagramHistory.length === 0

View File

@@ -116,11 +116,11 @@ export default function ChatPanel() {
status={status} status={status}
onSubmit={onFormSubmit} onSubmit={onFormSubmit}
onChange={handleInputChange} onChange={handleInputChange}
setMessages={setMessages} onClearChat={() => setMessages([])}
files={files} files={files}
onFileChange={handleFileChange} onFileChange={handleFileChange}
showHistory={showHistory} showHistory={showHistory}
setShowHistory={setShowHistory} onToggleHistory={setShowHistory}
/> />
</CardFooter> </CardFooter>
</Card> </Card>

View File

@@ -14,17 +14,17 @@ import { useDiagram } from "@/contexts/diagram-context";
interface HistoryDialogProps { interface HistoryDialogProps {
showHistory: boolean; showHistory: boolean;
setShowHistory: (show: boolean) => void; onToggleHistory: (show: boolean) => void;
} }
export function HistoryDialog({ export function HistoryDialog({
showHistory, showHistory,
setShowHistory, onToggleHistory,
}: HistoryDialogProps) { }: HistoryDialogProps) {
const { loadDiagram: onDisplayChart, diagramHistory } = useDiagram(); const { loadDiagram: onDisplayChart, diagramHistory } = useDiagram();
return ( return (
<Dialog open={showHistory} onOpenChange={setShowHistory}> <Dialog open={showHistory} onOpenChange={onToggleHistory}>
<DialogContent className="max-w-3xl max-h-[80vh] overflow-y-auto"> <DialogContent className="max-w-3xl max-h-[80vh] overflow-y-auto">
<DialogHeader> <DialogHeader>
<DialogTitle>Diagram History</DialogTitle> <DialogTitle>Diagram History</DialogTitle>
@@ -48,7 +48,7 @@ export function HistoryDialog({
className="border rounded-md p-2 cursor-pointer hover:border-primary transition-colors" className="border rounded-md p-2 cursor-pointer hover:border-primary transition-colors"
onClick={() => { onClick={() => {
onDisplayChart(item.xml); onDisplayChart(item.xml);
setShowHistory(false); onToggleHistory(false);
}} }}
> >
<div className="aspect-video bg-white rounded overflow-hidden flex items-center justify-center"> <div className="aspect-video bg-white rounded overflow-hidden flex items-center justify-center">
@@ -71,7 +71,7 @@ export function HistoryDialog({
<DialogFooter> <DialogFooter>
<Button <Button
variant="outline" variant="outline"
onClick={() => setShowHistory(false)} onClick={() => onToggleHistory(false)}
> >
Close Close
</Button> </Button>