diff --git a/frontend/src/components/ExportDataModal.jsx b/frontend/src/components/ExportDataModal.jsx
new file mode 100644
index 0000000..72f487b
--- /dev/null
+++ b/frontend/src/components/ExportDataModal.jsx
@@ -0,0 +1,112 @@
+import React, { useEffect, useMemo, useState } from 'react';
+import { experimentsApi } from '../api/client';
+import Button from './ui/Button';
+import Alert from './ui/Alert';
+import { listExportableParams, buildMatrix, toCSV, csvFilename } from '../lib/dataExport';
+
+const GROUP_LABELS = { metrics: 'Session metrics', daily: 'Daily record fields' };
+
+function triggerDownload(filename, text) {
+ const blob = new Blob([text], { type: 'text/csv;charset=utf-8;' });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = filename;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ setTimeout(() => URL.revokeObjectURL(url), 0);
+}
+
+export default function ExportDataModal({ experimentTitle, experimentId, dailyTemplate, animals, onClose }) {
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState(null);
+ const [statuses, setStatuses] = useState([]);
+ const [selectedId, setSelectedId] = useState(null);
+
+ useEffect(() => {
+ let alive = true;
+ setLoading(true);
+ setError(null);
+ experimentsApi.getDailyStatuses(experimentId, {})
+ .then((data) => { if (alive) setStatuses(data); })
+ .catch((err) => { if (alive) setError(err.message); })
+ .finally(() => { if (alive) setLoading(false); });
+ return () => { alive = false; };
+ }, [experimentId]);
+
+ const params = useMemo(() => listExportableParams(dailyTemplate, statuses), [dailyTemplate, statuses]);
+
+ // Derive the effective selection: the user's pick, else the first parameter.
+ const effectiveId = selectedId ?? params[0]?.id ?? null;
+ const selectedParam = params.find((p) => p.id === effectiveId) ?? null;
+
+ const matrix = useMemo(
+ () => (selectedParam ? buildMatrix(statuses, animals, selectedParam) : { columns: [], rows: [] }),
+ [statuses, animals, selectedParam],
+ );
+
+ const hasData = matrix.rows.length > 0;
+
+ // Group params for
-
+
+
+
+
{successMsg && }
@@ -405,6 +410,16 @@ export default function ExperimentDetail() {
setEditAnimal(null)} />
+ setShowExport(false)} title="Export data" size="md">
+ setShowExport(false)}
+ />
+
+
);
}
diff --git a/frontend/tests/dataExport.test.js b/frontend/tests/dataExport.test.js
new file mode 100644
index 0000000..4f01f2a
--- /dev/null
+++ b/frontend/tests/dataExport.test.js
@@ -0,0 +1,157 @@
+import { extractValue } from '../src/lib/dataExport';
+import { listExportableParams } from '../src/lib/dataExport';
+
+const status = {
+ vitals: 'ok',
+ custom_fields: { 'f-weight': 250 },
+ analysis_summary: { total: 12, success_rate: 0.83, counts: { Success: 10, Failure: 2 } },
+};
+
+describe('extractValue', () => {
+ it('reads a builtin field via statusKey', () => {
+ expect(extractValue(status, { kind: 'builtin', statusKey: 'vitals' })).toBe('ok');
+ });
+ it('reads a custom field via fieldId', () => {
+ expect(extractValue(status, { kind: 'custom', fieldId: 'f-weight' })).toBe(250);
+ });
+ it('reads session-metric total', () => {
+ expect(extractValue(status, { kind: 'total' })).toBe(12);
+ });
+ it('reads session-metric success_rate as a raw 0-1 fraction', () => {
+ expect(extractValue(status, { kind: 'success_rate' })).toBe(0.83);
+ });
+ it('reads a dynamic count category', () => {
+ expect(extractValue(status, { kind: 'category', category: 'Failure' })).toBe(2);
+ });
+ it('returns null when analysis_summary is absent', () => {
+ expect(extractValue({ custom_fields: {} }, { kind: 'total' })).toBeNull();
+ expect(extractValue({}, { kind: 'category', category: 'Success' })).toBeNull();
+ });
+ it('returns null for a missing custom field', () => {
+ expect(extractValue({ custom_fields: {} }, { kind: 'custom', fieldId: 'nope' })).toBeNull();
+ expect(extractValue({}, { kind: 'builtin', statusKey: 'notes' })).toBeNull();
+ });
+});
+
+const dailyTemplate = [
+ { fieldId: 'b-vitals', key: 'vitals', label: 'Vitals', builtin: true, active: true },
+ { fieldId: 'c-weight', key: 'weight', label: 'Weight (g)', builtin: false, active: false },
+];
+const statuses = [
+ { analysis_summary: { counts: { Success: 1, Failure: 0 } } },
+ { analysis_summary: { counts: { Success: 2, Other: 1 } } },
+ { analysis_summary: null },
+];
+
+describe('listExportableParams', () => {
+ const params = listExportableParams(dailyTemplate, statuses);
+ it('starts with the two fixed session metrics', () => {
+ expect(params.slice(0, 2)).toEqual([
+ expect.objectContaining({ id: '__total__', label: 'Total attempts', group: 'metrics', kind: 'total' }),
+ expect.objectContaining({ id: '__success_rate__', label: 'Success rate', group: 'metrics', kind: 'success_rate' }),
+ ]);
+ });
+ it('adds one metrics entry per dynamic count category, sorted', () => {
+ const cats = params.filter((p) => p.kind === 'category').map((p) => p.label);
+ expect(cats).toEqual(['Failure', 'Other', 'Success']);
+ });
+ it('includes every template field (incl. inactive) in the daily group', () => {
+ const daily = params.filter((p) => p.group === 'daily');
+ expect(daily).toEqual([
+ expect.objectContaining({ label: 'Vitals', kind: 'builtin', statusKey: 'vitals' }),
+ expect.objectContaining({ label: 'Weight (g)', kind: 'custom', fieldId: 'c-weight' }),
+ ]);
+ });
+ it('gives every param a unique id', () => {
+ const ids = params.map((p) => p.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ });
+ it('tolerates empty/missing inputs', () => {
+ expect(listExportableParams(undefined, undefined).length).toBe(2); // just the two fixed metrics
+ });
+});
+
+import { buildMatrix } from '../src/lib/dataExport';
+
+const animals = [
+ { id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002' },
+ { id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001' },
+];
+const totalParam = { kind: 'total' };
+
+describe('buildMatrix', () => {
+ it('orders columns by animal_name and rows by date ascending', () => {
+ const statuses = [
+ { animal_id: 'a1', date: '2026-07-02T00:00:00.000Z', analysis_summary: { total: 5 } },
+ { animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 9 } },
+ ];
+ const m = buildMatrix(statuses, animals, totalParam);
+ expect(m.columns.map((c) => c.header)).toEqual(['Alpha', 'Beta']);
+ expect(m.rows.map((r) => r.date)).toEqual(['2026-07-01', '2026-07-02']);
+ // Row for 07-01: Alpha blank, Beta 9
+ expect(m.rows[0].values).toEqual(['', 9]);
+ // Row for 07-02: Alpha 5, Beta blank
+ expect(m.rows[1].values).toEqual([5, '']);
+ });
+ it('includes only dates that have at least one value (0 counts as a value)', () => {
+ const statuses = [
+ { animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 0 } },
+ { animal_id: 'a1', date: '2026-07-03T00:00:00.000Z', analysis_summary: null }, // no value
+ ];
+ const m = buildMatrix(statuses, animals, totalParam);
+ expect(m.rows.map((r) => r.date)).toEqual(['2026-07-01']);
+ expect(m.rows[0].values).toEqual([0, '']);
+ });
+ it('disambiguates duplicate animal names with the id string', () => {
+ const dupAnimals = [
+ { id: 'a1', animal_name: 'Rat', animal_id_string: 'R-001' },
+ { id: 'a2', animal_name: 'Rat', animal_id_string: 'R-002' },
+ ];
+ const statuses = [{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 1 } }];
+ const m = buildMatrix(statuses, dupAnimals, totalParam);
+ expect(m.columns.map((c) => c.header)).toEqual(['Rat (R-001)', 'Rat (R-002)']);
+ });
+ it('keeps the first status when a subject has duplicates on one date', () => {
+ const statuses = [
+ { animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 7 } },
+ { animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 99 } },
+ ];
+ const m = buildMatrix(statuses, animals, totalParam);
+ expect(m.rows[0].values).toEqual([7, '']);
+ });
+ it('returns empty rows when no status has a value', () => {
+ const m = buildMatrix([{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: null }], animals, totalParam);
+ expect(m.rows).toEqual([]);
+ expect(m.columns.length).toBe(2);
+ });
+});
+
+import { toCSV, csvFilename } from '../src/lib/dataExport';
+
+describe('toCSV', () => {
+ it('writes a header row of Date + column headers, then data rows', () => {
+ const matrix = {
+ columns: [{ id: 'a1', header: 'Alpha' }, { id: 'a2', header: 'Beta' }],
+ rows: [{ date: '2026-07-01', values: [5, ''] }, { date: '2026-07-02', values: ['', 9] }],
+ };
+ expect(toCSV(matrix)).toBe('Date,Alpha,Beta\n2026-07-01,5,\n2026-07-02,,9');
+ });
+ it('escapes commas, quotes, and newlines per RFC 4180', () => {
+ const matrix = {
+ columns: [{ id: 'a1', header: 'Note, field' }],
+ rows: [{ date: '2026-07-01', values: ['he said "hi"'] }, { date: '2026-07-02', values: ['line1\nline2'] }],
+ };
+ expect(toCSV(matrix)).toBe(
+ 'Date,"Note, field"\n2026-07-01,"he said ""hi"""\n2026-07-02,"line1\nline2"',
+ );
+ });
+});
+
+describe('csvFilename', () => {
+ it('slugifies the experiment title and parameter label', () => {
+ expect(csvFilename('My Study #1', 'Success rate')).toBe('My-Study-1-Success-rate.csv');
+ });
+ it('falls back to "export" when both slugs are empty', () => {
+ expect(csvFilename('', '')).toBe('export.csv');
+ });
+});