mirror of
https://github.com/willchen96/mike.git
synced 2026-07-24 23:41:04 +02:00
Merge pull request #238 from amal66/olp-pr/directory-fetch-storm
[Fix 01] Batch the directory modal's project fetch (N+1 getProject storm)
This commit is contained in:
commit
eda088e143
3 changed files with 58 additions and 17 deletions
|
|
@ -1,11 +1,7 @@
|
|||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
getLibrary,
|
||||
getProject,
|
||||
listProjects,
|
||||
} from "@/app/lib/mikeApi";
|
||||
import { getLibrary, listProjects } from "@/app/lib/mikeApi";
|
||||
import type { Document, LibraryFolder, Project } from "./types";
|
||||
|
||||
export type DirectoryTab = "files" | "templates" | "projects";
|
||||
|
|
@ -45,17 +41,14 @@ async function loadTemplates() {
|
|||
}
|
||||
|
||||
async function loadProjects() {
|
||||
const projects = await listProjects();
|
||||
const fullProjects = await Promise.all(
|
||||
projects.map((project) => getProject(project.id)),
|
||||
);
|
||||
const projectCounts = new Map(
|
||||
projects.map((project) => [project.id, project.document_count ?? 0]),
|
||||
);
|
||||
return fullProjects.map((project) => ({
|
||||
// One batched request. Fanning out getProject(id) per project caused an
|
||||
// N+1 burst on every directory-modal open that could overwhelm the
|
||||
// Supabase gateway once an account had accumulated projects.
|
||||
const projects = await listProjects({ includeDocuments: true });
|
||||
return projects.map((project) => ({
|
||||
...project,
|
||||
document_count:
|
||||
project.documents?.length ?? projectCounts.get(project.id) ?? 0,
|
||||
project.documents?.length ?? project.document_count ?? 0,
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -163,8 +163,11 @@ async function toApiError(response: Response, path: string) {
|
|||
// Projects
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export async function listProjects(): Promise<Project[]> {
|
||||
return apiRequest<Project[]>("/projects");
|
||||
export async function listProjects(options?: {
|
||||
includeDocuments?: boolean;
|
||||
}): Promise<Project[]> {
|
||||
const query = options?.includeDocuments ? "?include=documents" : "";
|
||||
return apiRequest<Project[]>(`/projects${query}`);
|
||||
}
|
||||
|
||||
export async function createProject(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue