Fixed locations import limit

X-Lovable-Edit-ID: edt-34a84493-31c9-4e28-badd-146d75f768c9
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 02:43:56 +00:00
co-authored by renee-png
+41 -13
View File
@@ -516,22 +516,50 @@ function ImportPage() {
caseByNumber: new Map(),
}), [user?.id]);
// Pre-load lookup caches from existing rows so subsequent imports can join
// Pre-load lookup caches from existing rows so subsequent imports can join.
// Always reload (and paginate past Supabase's 1000-row default limit) so caches
// pick up rows added since the page mounted (e.g., from a prior CSV import).
const PAGE = 1000;
const fetchAllPaginated = async <T,>(
table: "clients" | "cases" | "contacts" | "homeowners" | "invoices",
columns: string,
): Promise<T[]> => {
const out: T[] = [];
for (let from = 0; ; from += PAGE) {
const { data, error } = await supabase
.from(table)
.select(columns)
.range(from, from + PAGE - 1);
if (error) { console.warn(`[import] paginate ${table} failed`, error); break; }
const batch = (data ?? []) as T[];
out.push(...batch);
if (batch.length < PAGE) break;
}
return out;
};
const ensureLookupsLoaded = async () => {
if (ctx.clientByExt.size === 0) {
const { data } = await supabase.from("clients").select("id, name, external_id");
for (const r of data ?? []) {
if (r.external_id) ctx.clientByExt.set(r.external_id, r.id);
if (r.name) ctx.clientByName.set(r.name.toLowerCase(), r.id);
}
ctx.clientByExt.clear();
ctx.clientByName.clear();
const clients = await fetchAllPaginated<{ id: string; name: string | null; external_id: string | null }>(
"clients",
"id, name, external_id",
);
for (const r of clients) {
if (r.external_id) ctx.clientByExt.set(r.external_id, r.id);
if (r.name) ctx.clientByName.set(r.name.toLowerCase(), r.id);
}
if (ctx.caseByExt.size === 0) {
const { data } = await supabase.from("cases").select("id, case_number, external_id");
for (const r of data ?? []) {
if (r.external_id) ctx.caseByExt.set(r.external_id, r.id);
if (r.case_number) ctx.caseByNumber.set(r.case_number, r.id);
}
ctx.caseByExt.clear();
ctx.caseByNumber.clear();
const cases = await fetchAllPaginated<{ id: string; case_number: string | null; external_id: string | null }>(
"cases",
"id, case_number, external_id",
);
for (const r of cases) {
if (r.external_id) ctx.caseByExt.set(r.external_id, r.id);
if (r.case_number) ctx.caseByNumber.set(r.case_number, r.id);
}
console.info(`[import] lookups loaded: clients=${ctx.clientByExt.size}/${ctx.clientByName.size} cases=${ctx.caseByExt.size}/${ctx.caseByNumber.size}`);
};
const handleFile = async (cfg: ImporterConfig, file: File) => {