Skip to content

Commit

Permalink
final bits
Browse files Browse the repository at this point in the history
  • Loading branch information
amcdnl committed Jul 23, 2024
1 parent 41fa621 commit a08d96c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
56 changes: 33 additions & 23 deletions src/Sessions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, PropsWithChildren, ReactNode, useEffect, useMemo, useState } from 'react';
import { FC, PropsWithChildren, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import { useHotkeys } from 'reakeys';
import { cn, useComponentTheme } from 'reablocks';
import { ResponseTransformer, Session } from './types';
Expand Down Expand Up @@ -109,7 +109,8 @@ export const Sessions: FC<SessionsProps> = ({
newSessionContent = '',
allowedFiles,
newSessionText = 'New Session',
inputDefaultValue
inputDefaultValue,
children
}) => {
// TODO: Make this hook more dynamic
// const theme: ChatTheme = useComponentTheme('chat', customTheme);
Expand All @@ -120,20 +121,20 @@ export const Sessions: FC<SessionsProps> = ({
setInternalActiveSessionID(activeSessionId);
}, [activeSessionId]);

function handleSelectSession(sessionId: string) {
const handleSelectSession = useCallback((sessionId: string) => {
setInternalActiveSessionID(sessionId);
onSelectSession?.(sessionId);
}
}, [onSelectSession]);

function handleDeleteSession(sessionId: string) {
const handleDeleteSession = useCallback((sessionId: string) => {
setInternalActiveSessionID(undefined);
onDeleteSession?.(sessionId);
}
}, [onDeleteSession]);

function handleCreateNewSession() {
const handleCreateNewSession = useCallback(() => {
setInternalActiveSessionID(undefined);
onNewSession?.();
}
}, [onNewSession]);

useHotkeys([
{
Expand All @@ -147,10 +148,25 @@ export const Sessions: FC<SessionsProps> = ({
}
]);

const activeSession = useMemo(() =>
sessions.find(session => session.id === internalActiveSessionID),
[sessions, internalActiveSessionID]);

const contextValue = useMemo(() => ({
sessions,
activeSessionId
}), [sessions, activeSessionId]);
activeSession,
activeSessionId: internalActiveSessionID,
selectSession: handleSelectSession,
deleteSession: handleDeleteSession,
createSession: handleCreateNewSession
}), [
sessions,
activeSession,
internalActiveSessionID,
handleSelectSession,
handleDeleteSession,
handleCreateNewSession
]);

return (
<SessionsContext.Provider value={contextValue}>
Expand All @@ -169,19 +185,13 @@ export const Sessions: FC<SessionsProps> = ({
onCreateNewSession={handleCreateNewSession}
/>
<div className="flex-1 h-full flex flex-col">
{internalActiveSessionID ? (
<>
{sessions
.filter(session => session.id === internalActiveSessionID)
.map(session => (
<SessionMessages
key={session.id}
session={session}
responseTransformers={responseTransformers}
theme={theme}
/>
))}
</>
{activeSession ? (
<SessionMessages
key={activeSession.id}
session={activeSession}
responseTransformers={responseTransformers}
theme={theme}
/>
) : (
<div className={cn(theme.empty)}>
{newSessionContent}
Expand Down
4 changes: 4 additions & 0 deletions src/SessionsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Session } from './types';
export interface SessionContextProps {
sessions: Session[];
activeSessionId: string | null;
activeSession?: Session | null;
selectSession?: (sessionId: string) => void;
deleteSession?: (sessionId: string) => void;
createSession?: () => void;
}

export const SessionsContext = createContext<SessionContextProps>({
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface Conversation {
question: string;
response?: string;
sources?: ConversationSource[];
user?: User;
files: string[]; // TODO
user?: User; // TODO
}

export interface Session {
Expand Down

0 comments on commit a08d96c

Please sign in to comment.