Skip to content

Commit

Permalink
Minor refactor: removed inline chat type definitions, created central…
Browse files Browse the repository at this point in the history
… chat type definitions at types/chat.ts (#25)

* Add @types/react-syntax-highlighter for improved TypeScript support

* Refactor: Centralize type definitions and update imports

- Created types/chat.ts with shared type definitions
- Updated ChatArea/index.tsx to use imported types
- Updated MessageContent.tsx to use new ParsedResponse and MessageContentState types
- Removed inline type definitions from components

* refactor(ChatArea): Remove inline types, import from types folder
  • Loading branch information
arjunkmrm authored Oct 7, 2024
1 parent 8d2ea6d commit 2f7b4ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
11 changes: 6 additions & 5 deletions ui/components/ChatArea/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneLight, oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
import 'katex/dist/katex.min.css';
import { useTheme } from 'next-themes';
import { MessageContentState, ParsedResponse } from '@/types/chat';

// Import only the languages you need
import js from 'react-syntax-highlighter/dist/esm/languages/prism/javascript';
Expand All @@ -28,22 +29,22 @@ interface MessageContentProps {

const MessageContent: React.FC<MessageContentProps> = ({ content, role }) => {
const { theme } = useTheme();
const [state, setState] = useState({
const [state, setState] = useState<MessageContentState>({
thinking: true,
parsed: {},
parsed: { response: '' },
error: false
});

useEffect(() => {
if (role !== "assistant" || !content) {
setState({ thinking: false, parsed: {}, error: false });
setState({ thinking: false, parsed: { response: '' }, error: false });
return;
}

const timer = setTimeout(() => setState(s => ({ ...s, thinking: false, error: true })), 30000);

try {
const result = JSON.parse(content);
const result = JSON.parse(content) as ParsedResponse;
console.log("🔍 Parsed Result:", result);

if (result.response && result.response.length > 0 && result.response !== "...") {
Expand All @@ -52,7 +53,7 @@ const MessageContent: React.FC<MessageContentProps> = ({ content, role }) => {
}
} catch (error) {
console.error("Error parsing JSON:", error);
setState({ thinking: false, parsed: {}, error: true });
setState({ thinking: false, parsed: { response: '' }, error: true });
}

return () => clearTimeout(timer);
Expand Down
20 changes: 1 addition & 19 deletions ui/components/ChatArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,7 @@ import Artifacts from '../Artifacts';
import EditModal from './EditModal';
import ArtifactSidebar from '../Artifacts/Sidebar';
import { motion, AnimatePresence } from 'framer-motion';

interface Message {
id: string;
role: string;
content: string;
}

type Model = {
id: string;
name: string;
};

interface ArtifactContent {
id: string;
type: 'code' | 'html' | 'text' | 'log';
content: string;
language?: string;
name: string;
}
import { Message, Model, ArtifactContent } from "@/types/chat";

function ChatArea() {
const [messages, setMessages] = useState<Message[]>([]);
Expand Down
28 changes: 28 additions & 0 deletions ui/types/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface Message {
id: string;
role: string;
content: string;
}

export type Model = {
id: string;
name: string;
};

export interface ArtifactContent {
id: string;
type: 'code' | 'html' | 'text' | 'log';
content: string;
language?: string;
name: string;
}

export interface ParsedResponse {
response: string;
}

export interface MessageContentState {
thinking: boolean;
parsed: ParsedResponse;
error: boolean;
}

0 comments on commit 2f7b4ad

Please sign in to comment.