Skip to content

Commit 75aa9a9

Browse files
committed
Refactor: Replace DOM manipulation with direct data processing
- Add processData method to useCsvUpload hook for direct data input - Remove fragile DOM querySelector and file input manipulation - Improve reliability and testability of text input processing - Maintain same validation flow while eliminating browser dependencies
1 parent 13307e5 commit 75aa9a9

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/assets/create/distribution/token-airdrop.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ const AirdropUpload: React.FC<AirdropUploadProps> = ({
248248
noCsv,
249249
reset,
250250
removeInvalid,
251+
processData,
251252
} = useCsvUpload<AirdropAddressInput>({
252253
csvParser: (items: AirdropAddressInput[]) => {
253254
return items
@@ -262,35 +263,12 @@ const AirdropUpload: React.FC<AirdropUploadProps> = ({
262263

263264
const normalizeData = normalizeQuery.data;
264265

265-
// Handle text input - create CSV and trigger file input
266+
// Handle text input - directly process the parsed data
266267
const handleTextSubmit = () => {
267268
if (!textInput.trim()) return;
268269

269270
const parsedData = parseTextInput(textInput);
270-
271-
// Create CSV content
272-
const csvContent = `address,quantity\n${parsedData
273-
.map((item) => `${item.address},${item.quantity}`)
274-
.join("\n")}`;
275-
276-
// Create file and trigger the existing file input
277-
const blob = new Blob([csvContent], { type: "text/csv" });
278-
const file = new File([blob], "manual-input.csv", { type: "text/csv" });
279-
280-
// Get the file input and trigger change event
281-
const fileInput = document.querySelector(
282-
'input[type="file"]',
283-
) as HTMLInputElement;
284-
if (fileInput) {
285-
// Create a new FileList-like object
286-
const dataTransfer = new DataTransfer();
287-
dataTransfer.items.add(file);
288-
fileInput.files = dataTransfer.files;
289-
290-
// Trigger change event
291-
const event = new Event("change", { bubbles: true });
292-
fileInput.dispatchEvent(event);
293-
}
271+
processData(parsedData);
294272
};
295273

296274
if (!normalizeData && rawData.length > 0) {

apps/dashboard/src/hooks/useCsvUpload.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ export function useCsvUpload<
161161
// Also filteredData's type is the superset of T[]
162162
setRawData(filteredData as unknown as T[]);
163163
}, [normalizeQuery.data?.result]);
164+
165+
const processData = useCallback(
166+
(data: T[]) => {
167+
setNoCsv(false);
168+
const processedData = props.csvParser(data);
169+
if (!processedData[0]?.address) {
170+
setNoCsv(true);
171+
return;
172+
}
173+
setRawData(processedData);
174+
},
175+
[props.csvParser],
176+
);
177+
164178
return {
165179
normalizeQuery,
166180
getInputProps,
@@ -170,5 +184,6 @@ export function useCsvUpload<
170184
noCsv,
171185
reset,
172186
removeInvalid,
187+
processData,
173188
};
174189
}

0 commit comments

Comments
 (0)