Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): keep genuine leading apostrophes when importing Loom CSVs

Richie McIlroy0af94c9437f7
diff --git a/apps/web/__tests__/unit/loom-csv.test.ts b/apps/web/__tests__/unit/loom-csv.test.ts
index 7435b47c6..0c50abfc8 100644
--- a/apps/web/__tests__/unit/loom-csv.test.ts
+++ b/apps/web/__tests__/unit/loom-csv.test.ts
@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";
import {
formatLoomCsvCell,
+ isLoomCsvResultsFile,
+ LOOM_CSV_RESULTS_HEADERS,
parseConciergeLoomCsv,
parseLoomCsvRecords,
readLoomCsvCell,
@@ -61,16 +63,43 @@ describe("Loom CSV results cells", () => {
});
describe("Loom CSV results round trip", () => {
- it("restores neutralized values when a results CSV is imported again", () => {
- const values = ["@Sales", "[email protected]", "-Ops, EU", "=1"];
+ it("restores every value when a results CSV is imported again", () => {
+ const values = [
+ "@Sales",
+ "-Ops, EU",
+ "=1",
+ "'@Sales",
+ "''=1",
+ "'Sales",
+ "O'Brien",
+ "Sales",
+ ];
const [cells] = parseLoomCsvRecords(
`${values.map(formatLoomCsvCell).join(",")}\n`,
);
expect(cells?.map(readLoomCsvCell)).toEqual(values);
});
- it("leaves other leading apostrophes alone", () => {
+ it("leaves apostrophes alone when no formula character follows", () => {
expect(readLoomCsvCell("'Sales")).toBe("'Sales");
expect(readLoomCsvCell("O'Brien")).toBe("O'Brien");
});
+
+ it("recognizes the results CSV layout regardless of column order or case", () => {
+ expect(isLoomCsvResultsFile([...LOOM_CSV_RESULTS_HEADERS])).toBe(true);
+ expect(
+ isLoomCsvResultsFile([
+ "Status",
+ "Error",
+ "Row",
+ "User_Email",
+ "Space_Name",
+ "Loom_Video_URL",
+ ]),
+ ).toBe(true);
+ expect(
+ isLoomCsvResultsFile(["loom_video_url", "user_email", "space_name"]),
+ ).toBe(false);
+ });
});
diff --git a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
index cc719df2e..fcf63b506 100644
--- a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
+++ b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
@@ -58,6 +58,8 @@ import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { UpgradeModal } from "@/components/UpgradeModal";
import {
formatLoomCsvCell,
+ isLoomCsvResultsFile,
+ LOOM_CSV_RESULTS_HEADERS,
parseLoomCsvRecords,
readLoomCsvCell,
} from "@/lib/loom-csv";
@@ -197,7 +199,7 @@ function buildResultsCsv(
) {
const rowsByNumber = new Map(sourceRows.map((row) => [row.rowNumber, row]));
const lines = [
- ["row", "loom_video_url", "user_email", "space_name", "status", "error"],
+ LOOM_CSV_RESULTS_HEADERS,
...results.map((row) => [
String(row.rowNumber),
rowsByNumber.get(row.rowNumber)?.loomUrl ?? "",
@@ -217,10 +219,12 @@ function buildResultsCsv(
function parseCsv(text: string, fileName: string): CsvData {
const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((header) => header.trim()) ?? [];
- const rows = records
+ const dataRows = records
.slice(1)
- .filter((row) => row.some((cell) => cell.trim().length > 0))
- .map((row) => row.map(readLoomCsvCell));
+ .filter((row) => row.some((cell) => cell.trim().length > 0));
+ const rows = isLoomCsvResultsFile(headers)
+ ? dataRows.map((row) => row.map(readLoomCsvCell))
+ : dataRows;
if (headers.length === 0) {
throw new Error("No CSV headers found.");
diff --git a/apps/web/lib/loom-csv.ts b/apps/web/lib/loom-csv.ts
index 407c93456..99cbb2441 100644
--- a/apps/web/lib/loom-csv.ts
+++ b/apps/web/lib/loom-csv.ts
@@ -50,19 +50,33 @@ export function parseLoomCsvRecords(text: string) {
return records;
}
-const SPREADSHEET_FORMULA_PREFIX = /^[=+\-@\t\r]/;
-const NEUTRALIZED_FORMULA_PREFIX = /^'(?=[=+\-@\t\r])/;
+export const LOOM_CSV_RESULTS_HEADERS = [
+ "row",
+ "loom_video_url",
+ "user_email",
+ "space_name",
+ "status",
+ "error",
+];
+
+const GUARDED_FORMULA_PREFIX = /^'*[=+\-@\t\r]/;
+const FORMULA_GUARD = /^'(?='*[=+\-@\t\r])/;
export function formatLoomCsvCell(value: string) {
- const literal = SPREADSHEET_FORMULA_PREFIX.test(value) ? `'${value}` : value;
+ const literal = GUARDED_FORMULA_PREFIX.test(value) ? `'${value}` : value;
if (/[",\n\r]/.test(literal)) {
return `"${literal.replace(/"/g, '""')}"`;
}
return literal;
}
+export function isLoomCsvResultsFile(headers: string[]) {
+ const normalized = new Set(headers.map((header) => header.toLowerCase()));
+ return LOOM_CSV_RESULTS_HEADERS.every((header) => normalized.has(header));
+}
+
export function readLoomCsvCell(value: string) {
- return value.replace(NEUTRALIZED_FORMULA_PREFIX, "");
+ return value.replace(FORMULA_GUARD, "");
}
export function parseConciergeLoomCsv(text: string): LoomCsvImportRow[] {