Preview the contacts CSV before import

React · React · beginner · greenfield

Adds the pre-import preview for the contacts CSV upload. Picking a file reads it in the browser, parses the rows (stripping the exporter's quoting, including doubled inner quotes), and renders the three-column preview table, with an empty-state message for header-only files. Re-picking the same file re-runs the preview thanks to the input reset.

Requirements

Files touched

--- src/components/ContactImportPreview.jsx
+import { useState } from 'react';
+
+function unquote(field) {
+  if (field.startsWith('"') && field.endsWith('"')) {
+    return field.slice(1, -1).replace(/""/g, '"');
+  }
+  return field;
+}
+
+export function parseContacts(text) {
+  const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
+  return lines.slice(1).map((line) => {
+    const cells = line.split(',').map(unquote);
+    return { name: cells[0], email: cells[1], company: cells[2] };
+  });
+}
+
+export default function ContactImportPreview() {
+  const [rows, setRows] = useState(null);
+
+  async function handleFile(event) {
+    const file = event.target.files[0];
+    if (!file) return;
+    const text = await file.text();
+    setRows(parseContacts(text));
+    event.target.value = '';
+  }
+
+  return (

Review this PR

React practice