-
Notifications
You must be signed in to change notification settings - Fork 56
Add filtered CSV exports for Jobs and Documents #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6556f06
feat(ui): add filtered exports for admin tables
hedhoud 9028fde
fix(ui): sanitize CSV formula cells
hedhoud 0f81900
fix(ui): guard CSV control-prefixed formulas
hedhoud 4c3c26d
fix(ui): polish admin filter controls
hedhoud 8cbd32a
fix(ui): avoid all-partitions sentinel collision
hedhoud 5cad00f
Merge develop into fix/546-admin-ui-filters-export
hedhoud d85e49c
Merge develop into fix/546-admin-ui-filters-export
hedhoud 270f3a9
Merge branch 'develop' into fix/546-admin-ui-filters-export
hedhoud 4e70ea2
Merge branch 'develop' into fix/546-admin-ui-filters-export
hedhoud 25907b1
Merge remote-tracking branch 'origin/develop' into pr684-review-fixes…
hedhoud 61b236e
fix(admin): harden filtered CSV exports
hedhoud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { downloadCsv, toCsv, toExcelCsv } from "./csv"; | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| vi.unstubAllGlobals(); | ||
| document.body.replaceChildren(); | ||
| }); | ||
|
|
||
| describe("toCsv", () => { | ||
| it("sanitizes formula-leading cells", () => { | ||
| const csv = toCsv( | ||
| [{ header: "filename", value: (row: { filename: string }) => row.filename }], | ||
| [ | ||
| { filename: "=cmd|' /C calc'!A0" }, | ||
| { filename: "+sum(1,2)" }, | ||
| { filename: "-10" }, | ||
| { filename: "@admin" }, | ||
| { filename: "\t=cmd" }, | ||
| { filename: "\r=cmd" }, | ||
| { filename: "\n=cmd" }, | ||
| { filename: "safe.pdf" }, | ||
| ], | ||
| ); | ||
|
|
||
| expect(csv).toBe([ | ||
| "filename", | ||
| "'=cmd|' /C calc'!A0", | ||
| "\"'+sum(1,2)\"", | ||
| "'-10", | ||
| "'@admin", | ||
| "'\t=cmd", | ||
| "\"'\r=cmd\"", | ||
| "\"'\n=cmd\"", | ||
| "safe.pdf", | ||
| ].join("\r\n")); | ||
| }); | ||
|
|
||
| it("adds Excel encoding and delimiter hints to downloads", () => { | ||
| const csv = toExcelCsv( | ||
| [{ header: "filename", value: (row: { filename: string }) => row.filename }], | ||
| [{ filename: "Rapport_priv\u00e9.pdf" }], | ||
| ); | ||
|
|
||
| expect(csv).toBe("\uFEFFsep=,\r\nfilename\r\nRapport_priv\u00e9.pdf"); | ||
| }); | ||
|
|
||
| it("keeps the object URL alive until the browser handles the click", () => { | ||
| vi.useFakeTimers(); | ||
| const createObjectURL = vi.fn(() => "blob:csv"); | ||
| const revokeObjectURL = vi.fn(); | ||
| vi.stubGlobal("URL", { createObjectURL, revokeObjectURL }); | ||
| const click = vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(() => {}); | ||
|
|
||
| downloadCsv("report.csv", [{ header: "name", value: (row: { name: string }) => row.name }], [ | ||
| { name: "report" }, | ||
| ]); | ||
|
|
||
| expect(document.querySelector('a[download="report.csv"]')).not.toBeNull(); | ||
| expect(click).toHaveBeenCalledOnce(); | ||
| expect(revokeObjectURL).not.toHaveBeenCalled(); | ||
|
|
||
| vi.runAllTimers(); | ||
|
|
||
| expect(document.querySelector('a[download="report.csv"]')).toBeNull(); | ||
| expect(revokeObjectURL).toHaveBeenCalledWith("blob:csv"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| type CsvColumn<T> = { | ||
| header: string; | ||
| value: (row: T) => unknown; | ||
| }; | ||
|
|
||
| const FORMULA_PREFIX = /^[=+\-@\t\r\n]/; | ||
| const CSV_DELIMITER = ","; | ||
| const CSV_LINE_BREAK = "\r\n"; | ||
| const UTF8_BOM = "\uFEFF"; | ||
|
|
||
| function csvCell(value: unknown): string { | ||
| const raw = value == null ? "" : String(value); | ||
| const text = FORMULA_PREFIX.test(raw) ? `'${raw}` : raw; | ||
| return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text; | ||
| } | ||
|
|
||
| export function toCsv<T>(columns: CsvColumn<T>[], rows: T[]): string { | ||
| return [ | ||
| columns.map((column) => csvCell(column.header)).join(CSV_DELIMITER), | ||
| ...rows.map((row) => columns.map((column) => csvCell(column.value(row))).join(CSV_DELIMITER)), | ||
| ].join(CSV_LINE_BREAK); | ||
| } | ||
|
|
||
| export function toExcelCsv<T>(columns: CsvColumn<T>[], rows: T[]): string { | ||
| return `${UTF8_BOM}sep=${CSV_DELIMITER}${CSV_LINE_BREAK}${toCsv(columns, rows)}`; | ||
| } | ||
|
|
||
| export function downloadCsv<T>(filename: string, columns: CsvColumn<T>[], rows: T[]) { | ||
| const blob = new Blob([toExcelCsv(columns, rows)], { type: "text/csv;charset=utf-8" }); | ||
| const url = URL.createObjectURL(blob); | ||
| const link = document.createElement("a"); | ||
| link.href = url; | ||
| link.download = filename; | ||
| link.style.display = "none"; | ||
|
|
||
| try { | ||
| document.body.appendChild(link); | ||
| link.click(); | ||
| } finally { | ||
| window.setTimeout(() => { | ||
| link.remove(); | ||
| URL.revokeObjectURL(url); | ||
| }, 0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.