Skip to content
Merged
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
53 changes: 6 additions & 47 deletions components/TasksPage/TasksList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Tables } from "@/types/database.types";
import { Task } from "@/lib/tasks/getTasks";
import TaskCard from "@/components/VercelChat/tools/tasks/TaskCard";
import TaskSkeleton from "./TaskSkeleton";
import TaskDetailsDialog from "@/components/VercelChat/dialogs/tasks/TaskDetailsDialog";
import { useArtistProvider } from "@/providers/ArtistProvider";
import { useUserProvider } from "@/providers/UserProvder";
import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";

type AccountEmail = Tables<"account_emails">;

interface TasksListProps {
tasks: Task[];
Expand All @@ -18,45 +12,13 @@ interface TasksListProps {

const TasksList: React.FC<TasksListProps> = ({ tasks, isLoading, isError }) => {
const { userData } = useUserProvider();
const { selectedArtist } = useArtistProvider();

// Extract unique account IDs from tasks
const accountIds = useMemo(
() => [...new Set(tasks.map(task => task.account_id))],
[tasks]
);

// Batch fetch emails for all task owners
const { data: accountEmails = [] } = useQuery<AccountEmail[]>({
queryKey: ["task-owner-emails", accountIds],
queryFn: async () => {
if (accountIds.length === 0 || !userData) return [];
const params = new URLSearchParams();
accountIds.forEach(id => params.append("accountIds", id));
params.append("currentAccountId", userData.id);
if (selectedArtist) {
params.append("artistAccountId", selectedArtist.account_id);
}
const response = await fetch(`/api/account-emails?${params}`);
if (!response.ok) throw new Error("Failed to fetch emails");
return response.json();
},
enabled: accountIds.length > 0 && !!userData,
});

// Create lookup map for O(1) email access
const emailByAccountId = useMemo(() => {
const map = new Map<string, string>();
accountEmails.forEach(ae => {
if (ae.account_id && ae.email) {
map.set(ae.account_id, ae.email);
}
});
return map;
}, [accountEmails]);

if (isError) {
return <div className="text-sm text-red-600 dark:text-red-400">Failed to load tasks</div>;
return (
<div className="text-sm text-red-600 dark:text-red-400">
Failed to load tasks
</div>
);
}

if (isLoading || !userData) {
Expand Down Expand Up @@ -88,10 +50,7 @@ const TasksList: React.FC<TasksListProps> = ({ tasks, isLoading, isError }) => {
index !== tasks.length - 1 ? "border-b border-border " : ""
}
>
<TaskCard
task={task}
ownerEmail={emailByAccountId.get(task.account_id)}
/>
<TaskCard task={task} ownerEmail={task.owner_email ?? undefined} />
</div>
</TaskDetailsDialog>
))}
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/getTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ScheduledAction = Tables<"scheduled_actions">;
export type Task = ScheduledAction & {
recent_runs?: TaskRunItem[];
upcoming?: string[];
owner_email?: string | null;
};

export interface GetTasksParams {
Expand All @@ -27,7 +28,7 @@ export interface GetTasksResponse {
*/
export async function getTasks(
accessToken: string,
params?: GetTasksParams
params?: GetTasksParams,
): Promise<Task[]> {
try {
const url = new URL(`${getClientApiBaseUrl()}/api/tasks`);
Expand Down
Loading