Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/src/routes/threads/controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ function ThreadsWorkspace({
const actions = useThreadActions({
activeAppId,
activeThreadId: route.activeThreadId,
allThreads: threads.allThreads,
closeComposeDialog: route.closeComposeDialog,
markThreadReadLocal: ui.markThreadRead,
navigateToList: route.backToList,
threadsById: threads.threadsById,
togglePinnedThreadLocal: ui.togglePinnedThread,
});
const handleReadSyncError = useCallback(
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/routes/threads/model/use-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import type { ThreadListItem } from "./thread";
export function useThreadActions({
activeAppId,
activeThreadId,
allThreads,
closeComposeDialog,
markThreadReadLocal,
navigateToList,
threadsById,
togglePinnedThreadLocal,
}: {
activeAppId: string | null;
activeThreadId: string | null;
allThreads: readonly ThreadListItem[];
closeComposeDialog: () => void;
markThreadReadLocal: (input: { readAt: string; threadId: string }) => void;
navigateToList: () => void;
threadsById: ReadonlyMap<string, ThreadListItem>;
togglePinnedThreadLocal: (threadId: string) => void;
}) {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -156,7 +156,7 @@ export function useThreadActions({

const togglePinnedThread = useCallback(
async (threadId: string): Promise<void> => {
const thread = allThreads.find((candidate) => candidate.id === threadId) ?? null;
const thread = threadsById.get(threadId) ?? null;

if (thread === null) {
return;
Expand All @@ -169,12 +169,12 @@ export function useThreadActions({
setActionError(getMutationErrorMessage(error, "Failed to update pinned state."));
}
},
[allThreads, togglePinnedThreadLocal],
[threadsById, togglePinnedThreadLocal],
);

const archiveThread = useCallback(
async (threadId: string): Promise<void> => {
const thread = allThreads.find((candidate) => candidate.id === threadId) ?? null;
const thread = threadsById.get(threadId) ?? null;

try {
if (thread === null) {
Expand All @@ -190,7 +190,7 @@ export function useThreadActions({
setActionError(getMutationErrorMessage(error, "Failed to archive thread."));
}
},
[allThreads, archiveMutation],
[threadsById, archiveMutation],
);

const deleteThread = useCallback(
Expand All @@ -201,7 +201,7 @@ export function useThreadActions({
}

try {
const thread = allThreads.find((candidate) => candidate.id === threadId) ?? null;
const thread = threadsById.get(threadId) ?? null;

if (thread === null) {
throw new Error("Thread not found.");
Expand All @@ -220,7 +220,7 @@ export function useThreadActions({
setActionError(getMutationErrorMessage(error, "Failed to delete thread."));
}
},
[activeThreadId, allThreads, deleteMutation, navigateToList],
[activeThreadId, threadsById, deleteMutation, navigateToList],
);

const sendFollowUp = useCallback(
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/routes/threads/model/use-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export function useThreadQueries({
)
.toSorted(compareThreads);
}, [activeSessionsQuery.data, agentsById, archivedSessionsQuery.data, ui]);
const selectedThread = allThreads.find((thread) => thread.id === activeThreadId) ?? null;
const threadsById = useMemo(
() => new Map<string, ThreadListItem>(allThreads.map((thread) => [thread.id, thread])),
[allThreads],
);
const selectedThread = activeThreadId === null ? null : (threadsById.get(activeThreadId) ?? null);
const messagesQuery = useQuery({
enabled: selectedThread !== null,
queryFn: async () => {
Expand Down Expand Up @@ -182,5 +186,6 @@ export function useThreadQueries({
retrieveQuery,
selectedThread,
threadsBySection,
threadsById,
};
}
Loading