Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update select-file.tsx

Kiran Kc053090eca9e
diff --git a/apps/web/ui/modals/import-csv-modal/select-file.tsx b/apps/web/ui/modals/import-csv-modal/select-file.tsx
index a37dcfb32f..f4e7db757f 100644
--- a/apps/web/ui/modals/import-csv-modal/select-file.tsx
+++ b/apps/web/ui/modals/import-csv-modal/select-file.tsx
@@ -5,12 +5,15 @@ import { useEffect, useState } from "react";
import { Controller } from "react-hook-form";
import { useCsvContext } from ".";
+const MAX_ROWS_LIMIT = 50000;
+
export function SelectFile() {
const { watch, control, fileColumns, setFileColumns, setFirstRows } =
useCsvContext();
const file = watch("file");
const [error, setError] = useState<string | null>(null);
+ const [isCountingRows, setIsCountingRows] = useState<boolean>(false);
useEffect(() => {
if (!file) {
@@ -18,35 +21,48 @@ export function SelectFile() {
return;
}
- readLines(file, 4)
- .then((lines) => {
- const { data, meta } = Papa.parse(lines, {
- worker: false,
- skipEmptyLines: true,
- header: true,
- });
-
- if (!data || data.length < 2) {
- setError("CSV file must have at least 2 rows.");
+ setIsCountingRows(true);
+ countCsvRows(file)
+ .then((rowCount) => {
+ if (rowCount > MAX_ROWS_LIMIT) {
+ setError("CSV file exceeds the maximum limit of 50,000 rows.");
setFileColumns(null);
setFirstRows(null);
return;
}
- if (!meta || !meta.fields || meta.fields.length <= 1) {
- setError("Failed to retrieve CSV column data.");
- setFileColumns(null);
- setFirstRows(null);
- return;
- }
+ return readLines(file, 4).then((lines) => {
+ const { data, meta } = Papa.parse(lines, {
+ worker: false,
+ skipEmptyLines: true,
+ header: true,
+ });
- setFileColumns(meta.fields);
- setFirstRows(data);
+ if (!data || data.length < 2) {
+ setError("CSV file must have at least 2 rows.");
+ setFileColumns(null);
+ setFirstRows(null);
+ return;
+ }
+
+ if (!meta || !meta.fields || meta.fields.length <= 1) {
+ setError("Failed to retrieve CSV column data.");
+ setFileColumns(null);
+ setFirstRows(null);
+ return;
+ }
+
+ setFileColumns(meta.fields);
+ setFirstRows(data);
+ });
})
.catch(() => {
setError("Failed to read CSV file.");
setFileColumns(null);
setFirstRows(null);
+ })
+ .finally(() => {
+ setIsCountingRows(false);
});
}, [file]);
@@ -81,6 +97,11 @@ export function SelectFile() {
) : file ? (
<div className="flex items-center justify-center">
<LoadingSpinner />
+ {isCountingRows && (
+ <p className="ml-2 text-sm text-neutral-600">
+ Validating file size...
+ </p>
+ )}
</div>
) : null}
</div>
@@ -100,6 +121,26 @@ const listColumns = (columns: string[]) => {
return allTruncated.join(", ");
};
+const countCsvRows = async (file: File): Promise<number> => {
+ return new Promise((resolve, reject) => {
+ let rowCount = 0;
+
+ Papa.parse(file, {
+ worker: true,
+ skipEmptyLines: true,
+ step: () => {
+ rowCount++;
+ },
+ complete: () => {
+ resolve(rowCount);
+ },
+ error: (error) => {
+ reject(error);
+ },
+ });
+ });
+};
+
const readLines = async (file: File, count = 4): Promise<string> => {
const reader = file.stream().getReader();
const decoder = new TextDecoder("utf-8");