Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge origin/main into fix/loom-csv-import-resilience

Richie McIlroy905a14380b58
diff --cc apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
index 607c0bb9d,085577b42..a07460f5b
--- a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
+++ b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
@@@ -117,140 -135,8 +137,91 @@@ function buildCsvImportResult
};
}
+function buildRowFailure(
+ row: MappedRow,
+ error: string,
+): LoomCsvImportRowResult {
+ return {
+ rowNumber: row.rowNumber,
+ userEmail: row.userEmail,
+ spaceName: row.spaceName || undefined,
+ success: false,
+ error,
+ };
+}
+
+function withNotAttemptedRows(
+ rows: MappedRow[],
+ results: LoomCsvImportRowResult[],
+): LoomCsvImportRowResult[] {
+ const attempted = new Set(results.map((row) => row.rowNumber));
+ const missing = rows.filter((row) => !attempted.has(row.rowNumber));
+ if (missing.length === 0) return results;
+
+ return [
+ ...results,
+ ...missing.map((row) =>
+ buildRowFailure(row, LOOM_CSV_NOT_ATTEMPTED_MESSAGE),
+ ),
+ ].sort((a, b) => a.rowNumber - b.rowNumber);
+}
+
+function isRateLimitError(message: string | undefined) {
+ if (!message) return false;
+ const normalized = message.toLowerCase();
+ return normalized.includes("rate limit") || normalized.includes("too many");
+}
+
+function rateLimitedBatchRows(
+ batch: MappedRow[],
+ results: LoomCsvImportRowResult[],
+) {
+ const limited = new Set(
+ results
+ .filter((row) => !row.success && isRateLimitError(row.error))
+ .map((row) => row.rowNumber),
+ );
+ return batch.filter((row) => limited.has(row.rowNumber));
+}
+
+function importErrorMessage(error: unknown) {
+ return error instanceof Error && error.message
+ ? error.message
+ : "The import request failed for this batch.";
+}
+
+function escapeCsvValue(value: string) {
+ if (/[",\n\r]/.test(value)) {
+ return `"${value.replace(/"/g, '""')}"`;
+ }
+ return value;
+}
+
+function buildResultsCsv(
+ results: LoomCsvImportRowResult[],
+ sourceRows: MappedRow[],
+) {
+ const rowsByNumber = new Map(sourceRows.map((row) => [row.rowNumber, row]));
+ const lines = [
+ ["row", "loom_video_url", "user_email", "space_name", "status", "error"],
+ ...results.map((row) => [
+ String(row.rowNumber),
+ rowsByNumber.get(row.rowNumber)?.loomUrl ?? "",
+ row.userEmail,
+ row.spaceName ?? "",
+ row.success
+ ? "started"
+ : row.error === LOOM_CSV_NOT_ATTEMPTED_MESSAGE
+ ? "not_attempted"
+ : "failed",
+ row.error ?? "",
+ ]),
+ ];
+ return `${lines.map((line) => line.map(escapeCsvValue).join(",")).join("\n")}\n`;
+}
+
- function parseCsvRecords(text: string) {
- const records: string[][] = [];
- let field = "";
- let row: string[] = [];
- let inQuotes = false;
- const input = text.replace(/^\uFEFF/, "");
-
- for (let index = 0; index < input.length; index += 1) {
- const char = input.charAt(index);
- const next = input.charAt(index + 1);
-
- if (char === '"') {
- if (inQuotes && next === '"') {
- field += '"';
- index += 1;
- } else {
- inQuotes = !inQuotes;
- }
- continue;
- }
-
- if (char === "," && !inQuotes) {
- row.push(field.trim());
- field = "";
- continue;
- }
-
- if ((char === "\n" || char === "\r") && !inQuotes) {
- if (char === "\r" && next === "\n") index += 1;
- row.push(field.trim());
- if (row.some((cell) => cell.length > 0)) records.push(row);
- row = [];
- field = "";
- continue;
- }
-
- field += char;
- }
-
- if (inQuotes) throw new Error("CSV has an unclosed quoted field.");
-
- if (field.length > 0 || row.length > 0) {
- row.push(field.trim());
- if (row.some((cell) => cell.length > 0)) records.push(row);
- }
-
- return records;
- }
-
function parseCsv(text: string, fileName: string): CsvData {
- const records = parseCsvRecords(text);
+ const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((header) => header.trim()) ?? [];
const rows = records
.slice(1)