Skip to content

Commit

Permalink
Rename hooks useAssistant -> useAssistantList
Browse files Browse the repository at this point in the history
  • Loading branch information
fjsj committed Jun 14, 2024
1 parent dcae388 commit b0421be
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 80 deletions.
32 changes: 8 additions & 24 deletions example/assets/js/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,17 @@ import Markdown from "react-markdown";
import {
ThreadMessagesSchemaOut,
ThreadSchema,
useAssistant,
useMessage,
useThread,
useAssistantList,
useMessageList,
useThreadList,
} from "django-ai-assistant-client";

function ChatMessage({
threadId,
message,
deleteMessage,
}: {
threadId: string;
message: ThreadMessagesSchemaOut;
deleteMessage: ({
threadId,
messageId,
}: {
threadId: string;
messageId: string;
}) => Promise<void>;
deleteMessage: ({ messageId }: { messageId: string }) => Promise<void>;
}) {
const isUserMessage = message.type === "human";

Expand All @@ -54,7 +46,7 @@ function ChatMessage({
color="red"
size="sm"
onClick={async () => {
await deleteMessage({ threadId, messageId: message.id });
await deleteMessage({ messageId: message.id });
}}
aria-label="Delete message"
>
Expand Down Expand Up @@ -97,17 +89,14 @@ function ChatMessage({
}

function ChatMessageList({
threadId,
messages,
deleteMessage,
}: {
threadId: string;
messages: ThreadMessagesSchemaOut[];
deleteMessage: ({
threadId,
messageId,
}: {
threadId: string;
messageId: string;
}) => Promise<void>;
}) {
Expand All @@ -120,7 +109,6 @@ function ChatMessageList({
{messages.map((message, index) => (
<ChatMessage
key={index}
threadId={threadId}
message={message}
deleteMessage={deleteMessage}
/>
Expand All @@ -133,7 +121,7 @@ export function Chat({ assistantId }: { assistantId: string }) {
const [activeThread, setActiveThread] = useState<ThreadSchema | null>(null);
const [inputValue, setInputValue] = useState<string>("");

const { fetchThreads, threads, createThread, deleteThread } = useThread();
const { fetchThreads, threads, createThread, deleteThread } = useThreadList();
const {
fetchMessages,
messages,
Expand All @@ -142,7 +130,7 @@ export function Chat({ assistantId }: { assistantId: string }) {
loadingCreateMessage,
deleteMessage,
loadingDeleteMessage,
} = useMessage();
} = useMessageList({ threadId: activeThread?.id });

const loadingMessages =
loadingFetchMessages || loadingCreateMessage || loadingDeleteMessage;
Expand Down Expand Up @@ -175,17 +163,14 @@ export function Chat({ assistantId }: { assistantId: string }) {
if (!assistantId) return;
if (!activeThread) return;

fetchMessages({
threadId: activeThread.id,
});
fetchMessages();
scrollToBottom();
}, [assistantId, activeThread?.id, fetchMessages]);

async function handleCreateMessage() {
if (!activeThread) return;

await createMessage({
threadId: activeThread.id,
assistantId,
messageTextValue: inputValue,
});
Expand Down Expand Up @@ -223,7 +208,6 @@ export function Chat({ assistantId }: { assistantId: string }) {
/>
{isThreadSelected ? (
<ChatMessageList
threadId={activeThread.id}
messages={messages || []}
deleteMessage={deleteMessage}
/>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./useAssistant";
export * from "./useMessage";
export * from "./useThread";
export * from "./useAssistantList";
export * from "./useMessageList";
export * from "./useThreadList";
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from "../client";

/**
* React hook to manage the Assistant resource.
* React hook to manage the list of Assistants.
*/
export function useAssistant() {
export function useAssistantList() {
const [assistants, setAssistants] = useState<AssistantSchema[] | null>(null);
const [loadingFetchAssistants, setLoadingFetchAssistants] =
useState<boolean>(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import {
ThreadMessagesSchemaOut,
} from "../client";


function hasNullThreadId(threadId: string | null): threadId is null {
if (threadId == null) {
console.warn("threadId is null or undefined. Ignoring fetch operation.");
return true;
}
return false;
}


/**
* React hook to manage the Message resource.
* React hook to manage the list, create, and delete of Messages.
*
* @param threadId The ID of the thread for which to manage messages.
*/
export function useMessage() {
export function useMessageList({ threadId }: { threadId: string | null }) {
const [messages, setMessages] = useState<ThreadMessagesSchemaOut[] | null>(
null
);
Expand All @@ -23,16 +35,14 @@ export function useMessage() {

/**
* Fetches a list of messages.
* Does nothing if `threadId` of `useMessageList` hook is null.
*
* @param threadId The ID of the thread for which to fetch messages.
* @returns A promise that resolves with the fetched list of messages.
* @returns - A promise that resolves with the fetched list of messages.
*/
const fetchMessages = useCallback(
async ({
threadId,
}: {
threadId: string;
}): Promise<ThreadMessagesSchemaOut[]> => {
async (): Promise<ThreadMessagesSchemaOut[] | null> => {
if (hasNullThreadId(threadId)) return null;

try {
setLoadingFetchMessages(true);
const fetchedMessages = await djangoAiAssistantViewsListThreadMessages({
Expand All @@ -44,26 +54,26 @@ export function useMessage() {
setLoadingFetchMessages(false);
}
},
[]
[threadId]
);

/**
* Creates a new message in a thread.
* Does nothing if `threadId` of `useMessageList` hook is null.
*
* @param threadId The ID of the thread in which to create the message.
* @param assistantId The ID of the assistant.
* @param messageTextValue The content of the message.
*/
const createMessage = useCallback(
async ({
threadId,
assistantId,
messageTextValue,
}: {
threadId: string;
assistantId: string;
messageTextValue: string;
}): Promise<void> => {
if (hasNullThreadId(threadId)) return;

try {
setLoadingCreateMessage(true);
// successful response is 201, None
Expand All @@ -74,40 +84,41 @@ export function useMessage() {
assistant_id: assistantId,
},
});
await fetchMessages({ threadId });
await fetchMessages();
} finally {
setLoadingCreateMessage(false);
}
},
[fetchMessages]
[fetchMessages, threadId]
);

/**
* Deletes a message in a thread.
* Does nothing if `threadId` of `useMessageList` hook is null.
*
* @param threadId The ID of the thread in which to delete the message.
* @param messageId The ID of the message to delete.
*/
const deleteMessage = useCallback(
async ({
threadId,
messageId,
}: {
threadId: string;
messageId: string;
}): Promise<void> => {
if (hasNullThreadId(threadId)) return;

try {
setLoadingDeleteMessage(true);
await djangoAiAssistantViewsDeleteThreadMessage({
threadId,
messageId,
});
await fetchMessages({ threadId });
await fetchMessages();
} finally {
setLoadingDeleteMessage(false);
}
},
[fetchMessages]
[fetchMessages, threadId]
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
} from "../client";

/**
* React hook to manage the Thread resource.
* React hook to manage the list, create, and delete of Threads.
*/
export function useThread() {
export function useThreadList() {
const [threads, setThreads] = useState<ThreadSchema[] | null>(null);
const [loadingFetchThreads, setLoadingFetchThreads] =
useState<boolean>(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, renderHook } from "@testing-library/react";
import { useAssistant } from "../src/hooks";
import { useAssistantList } from "../src/hooks";
import { djangoAiAssistantViewsListAssistants } from "../src/client";

jest.mock("../src/client", () => ({
Expand All @@ -8,13 +8,13 @@ jest.mock("../src/client", () => ({
.mockImplementation(() => Promise.resolve()),
}));

describe("useAssistant", () => {
describe("useAssistantList", () => {
beforeEach(() => {
jest.clearAllMocks();
});

it("should initialize with no assistants and loading false", () => {
const { result } = renderHook(() => useAssistant());
const { result } = renderHook(() => useAssistantList());

expect(result.current.assistants).toBeNull();
expect(result.current.loadingFetchAssistants).toBe(false);
Expand All @@ -30,7 +30,7 @@ describe("useAssistant", () => {
mockAssistants
);

const { result } = renderHook(() => useAssistant());
const { result } = renderHook(() => useAssistantList());

expect(result.current.assistants).toBeNull();
expect(result.current.loadingFetchAssistants).toBe(false);
Expand All @@ -48,7 +48,7 @@ describe("useAssistant", () => {
new Error("Failed to fetch")
);

const { result } = renderHook(() => useAssistant());
const { result } = renderHook(() => useAssistantList());

expect(result.current.assistants).toBeNull();
expect(result.current.loadingFetchAssistants).toBe(false);
Expand Down
Loading

0 comments on commit b0421be

Please sign in to comment.