Below md: the sidebar is replaced by a fixed bottom bar carrying the same destinations plus the theme toggle. NAV moved to navItems.jsx so the two can't drift apart. Perspective is now lazy-loaded. It was ~90% of the bundle and only Pivot uses it, so the initial download drops from 4.9 MB gzipped to 173 kB and the rest arrives only when a pivot is opened. Desktop benefits as much as phones do. Bridge's balance table becomes stacked cards under sm: with the same subtotals, source tabs scroll rather than wrap, and page gutters tighten on small screens. Rules, Mappings, and Records are deliberately untouched — they are wide data tables and belong on a desktop. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G2HFeU5neCKagTnmA6o9Tu
389 lines
17 KiB
JavaScript
389 lines
17 KiB
JavaScript
import { useState, useRef } from 'react'
|
||
import { useNavigate } from 'react-router-dom'
|
||
import { api } from '../api'
|
||
import SampleTable from '../components/SampleTable.jsx'
|
||
|
||
const FIELD_TYPES = ['text', 'numeric', 'date']
|
||
|
||
// Ticked into the view by default when a bank feed sample contains them
|
||
const FEED_DEFAULT_VIEW = ['date', 'description', 'payee', 'amount']
|
||
|
||
export default function SourceList({ sources, setSources, setSource }) {
|
||
const navigate = useNavigate()
|
||
const [creating, setCreating] = useState(false)
|
||
const [form, setForm] = useState({ name: '', constraint_fields: '', fields: [], schema: [], importSample: true })
|
||
const [createError, setCreateError] = useState('')
|
||
const [createLoading, setCreateLoading] = useState(false)
|
||
const [csvFileName, setCsvFileName] = useState('')
|
||
const [bridgeAccounts, setBridgeAccounts] = useState(null)
|
||
const [bridgeLoading, setBridgeLoading] = useState(false)
|
||
const [bridgeError, setBridgeError] = useState('')
|
||
const [sampleInfo, setSampleInfo] = useState(null)
|
||
const fileRef = useRef()
|
||
|
||
async function loadBridgeAccounts() {
|
||
setBridgeLoading(true)
|
||
setBridgeError('')
|
||
try {
|
||
const res = await api.getSimpleFinAccounts()
|
||
setBridgeAccounts(res.accounts || [])
|
||
if (res.errors?.length) setBridgeError(res.errors.join('; '))
|
||
} catch (err) {
|
||
setBridgeError(err.message)
|
||
} finally {
|
||
setBridgeLoading(false)
|
||
}
|
||
}
|
||
|
||
// Writes only config — constraint_fields and global_picklist are left NULL so
|
||
// update_source keeps whatever is already stored.
|
||
async function handleSelectFeedAccount(accountId) {
|
||
if (!accountId) {
|
||
// Clearing the feed only resets fields we populated, not a loaded CSV
|
||
setForm(f => csvFileName ? { ...f, simplefin_account_id: '' } : {
|
||
...f, simplefin_account_id: '', fields: [], schema: [], constraint_fields: '', sampleRows: [],
|
||
})
|
||
setSampleInfo(null)
|
||
return
|
||
}
|
||
|
||
setForm(f => ({ ...f, simplefin_account_id: accountId }))
|
||
setBridgeLoading(true)
|
||
setBridgeError('')
|
||
try {
|
||
const res = await api.getSimpleFinSample(accountId)
|
||
const names = res.fields.map(f => f.name)
|
||
setSampleInfo({ fetched: res.fetched, fields: res.fields.length })
|
||
if (res.errors?.length) setBridgeError(res.errors.join('; '))
|
||
setForm(f => ({
|
||
...f,
|
||
fields: res.fields,
|
||
sampleRows: res.sampleRows || [],
|
||
schema: FEED_DEFAULT_VIEW.filter(n => names.includes(n)).map((name, i) => ({
|
||
name, type: res.fields.find(sf => sf.name === name).type, seq: i + 1,
|
||
})),
|
||
// Only default the constraint if the sample actually has an id
|
||
constraint_fields: f.constraint_fields || (names.includes('id') ? 'id' : ''),
|
||
}))
|
||
} catch (err) {
|
||
setBridgeError(err.message)
|
||
} finally {
|
||
setBridgeLoading(false)
|
||
}
|
||
}
|
||
|
||
async function handleSuggest(e) {
|
||
const file = e.target.files[0]
|
||
if (!file) return
|
||
setCsvFileName(file.name)
|
||
try {
|
||
const suggestion = await api.suggestSource(file)
|
||
setForm(f => ({
|
||
...f,
|
||
fields: suggestion.fields,
|
||
constraint_fields: '',
|
||
schema: suggestion.fields.map(f => ({ name: f.name, type: f.type, seq: suggestion.fields.indexOf(f) + 1 })),
|
||
sampleRows: suggestion.sampleRows || []
|
||
}))
|
||
} catch (err) {
|
||
setCreateError(err.message)
|
||
}
|
||
}
|
||
|
||
async function handleCreate(e) {
|
||
e.preventDefault()
|
||
setCreateError('')
|
||
const constraintArr = form.constraint_fields.split(',').map(s => s.trim()).filter(Boolean)
|
||
if (!form.name || constraintArr.length === 0) {
|
||
setCreateError('Name and at least one constraint field required')
|
||
return
|
||
}
|
||
setCreateLoading(true)
|
||
try {
|
||
const config = form.schema.length > 0 ? { fields: form.schema } : {}
|
||
if (form.simplefin_account_id) {
|
||
config.simplefin = { account_id: form.simplefin_account_id }
|
||
}
|
||
await api.createSource({ name: form.name, constraint_fields: constraintArr, config, global_picklist: form.global_picklist !== false })
|
||
if (form.schema.length > 0) {
|
||
await api.generateView(form.name)
|
||
}
|
||
if (form.importSample && fileRef.current?.files[0]) {
|
||
await api.importCSV(form.name, fileRef.current.files[0])
|
||
}
|
||
const updated = await api.getSources()
|
||
setSources(updated)
|
||
setSource(form.name)
|
||
setForm({ name: '', constraint_fields: '', fields: [], schema: [], importSample: true, simplefin_account_id: '' })
|
||
setCreating(false)
|
||
} catch (err) {
|
||
setCreateError(err.message)
|
||
} finally {
|
||
setCreateLoading(false)
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div className="p-4 sm:p-6 max-w-5xl">
|
||
<div className="flex items-center justify-between mb-6">
|
||
<h1 className="text-xl font-semibold text-ink">Sources</h1>
|
||
{!creating && (
|
||
<button
|
||
onClick={() => { setCreating(true); setCreateError('') }}
|
||
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700"
|
||
>
|
||
New source
|
||
</button>
|
||
)}
|
||
</div>
|
||
|
||
{!creating && sources.length === 0 && (
|
||
<p className="text-sm text-muted">No sources yet. Create one to get started.</p>
|
||
)}
|
||
|
||
{!creating && sources.length > 0 && (
|
||
<div className="bg-surface border border-line rounded divide-y divide-line-soft">
|
||
{sources.map(s => (
|
||
<button
|
||
key={s.name}
|
||
onClick={() => { setSource(s.name); navigate(`/sources/${encodeURIComponent(s.name)}`) }}
|
||
className="w-full text-left px-4 py-3 hover:bg-raised flex items-center gap-3"
|
||
>
|
||
<span className="text-sm font-medium text-ink flex-1">{s.name}</span>
|
||
{s.config?.simplefin?.account_id && (
|
||
<span className="text-xs bg-accent-soft text-accent border border-accent-line rounded px-1.5 py-0.5">
|
||
bank feed
|
||
</span>
|
||
)}
|
||
<span className="text-xs text-muted">
|
||
{(s.constraint_fields || []).join(', ') || 'no constraint'}
|
||
</span>
|
||
<span className="text-muted">›</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{creating && (
|
||
<div className="bg-surface border border-line rounded p-4">
|
||
<h2 className="text-sm font-semibold text-ink-soft mb-3">New source</h2>
|
||
|
||
<div className="mb-4">
|
||
<input type="file" accept=".csv" ref={fileRef} onChange={handleSuggest} className="hidden" />
|
||
<button
|
||
type="button"
|
||
onClick={() => fileRef.current?.click()}
|
||
className="text-sm border border-line rounded px-3 py-1.5 text-ink-soft hover:bg-raised hover:border-line"
|
||
>
|
||
{csvFileName || 'Choose CSV…'}
|
||
</button>
|
||
</div>
|
||
|
||
<form onSubmit={handleCreate} className="space-y-3">
|
||
<div>
|
||
<label className="text-xs text-muted block mb-1">Source name</label>
|
||
<input
|
||
className="w-full border border-line rounded px-3 py-1.5 text-sm focus:outline-none focus:border-accent"
|
||
value={form.name}
|
||
onChange={e => setForm(f => ({ ...f, name: e.target.value }))}
|
||
placeholder="e.g. chase, dcard"
|
||
/>
|
||
</div>
|
||
|
||
{/* Bank feed — optional; picking an account defaults the constraint
|
||
field to the transaction id, which is what dedupe needs */}
|
||
<div>
|
||
<label className="text-xs text-muted block mb-1">Bank feed (optional)</label>
|
||
<div className="flex items-center gap-3 flex-wrap">
|
||
{bridgeAccounts === null ? (
|
||
<button
|
||
type="button"
|
||
onClick={loadBridgeAccounts}
|
||
disabled={bridgeLoading}
|
||
className="text-sm border border-line rounded px-3 py-1.5 text-ink-soft hover:bg-raised hover:border-line disabled:opacity-50"
|
||
>
|
||
{bridgeLoading ? 'Loading…' : 'Link SimpleFIN account…'}
|
||
</button>
|
||
) : (
|
||
<select
|
||
value={form.simplefin_account_id || ''}
|
||
onChange={e => handleSelectFeedAccount(e.target.value)}
|
||
className="text-sm border border-line rounded px-3 py-1.5 bg-surface text-ink-soft"
|
||
>
|
||
<option value="">No bank feed — CSV import</option>
|
||
{bridgeAccounts.map(a => (
|
||
<option key={a.id} value={a.id}>
|
||
{a.name}{a.organization ? ` — ${a.organization}` : ''}{a.balance ? ` (${a.balance})` : ''}
|
||
</option>
|
||
))}
|
||
</select>
|
||
)}
|
||
</div>
|
||
{bridgeError && <p className="text-xs text-warn mt-1">{bridgeError}</p>}
|
||
|
||
{form.simplefin_account_id && sampleInfo && (
|
||
<div className="mt-2 bg-accent-soft border border-accent-line rounded p-3 text-xs text-ink-soft space-y-1">
|
||
<p>
|
||
Read {sampleInfo.fetched} transaction{sampleInfo.fetched === 1 ? '' : 's'} from this
|
||
account and found {sampleInfo.fields} field{sampleInfo.fields === 1 ? '' : 's'}.
|
||
The table below lists what this account actually returns — fields it never sends
|
||
won’t appear.
|
||
</p>
|
||
{form.constraint_fields === 'id' && (
|
||
<p>
|
||
<span className="font-mono text-ink-soft">id</span> is checked as the constraint
|
||
field because it is SimpleFIN’s own transaction identifier. Syncs pull an
|
||
overlapping window of days, so the same transaction arrives more than once —
|
||
matching on <span className="font-mono text-ink-soft">id</span> skips the repeats
|
||
while still keeping genuinely separate charges that share a date, amount, and
|
||
description.
|
||
</p>
|
||
)}
|
||
{sampleInfo.fetched === 0 && (
|
||
<p className="text-warn">
|
||
No transactions came back, so there was nothing to infer fields from. Sync first,
|
||
then set the fields up here.
|
||
</p>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{form.fields.length > 0 && (
|
||
<div className="pt-2 border-t border-line-soft space-y-2">
|
||
<table className="w-full text-xs">
|
||
<thead>
|
||
<tr className="text-left text-muted border-b border-line-soft">
|
||
<th className="pb-1 font-medium">Key</th>
|
||
<th className="pb-1 font-medium">Type</th>
|
||
<th className="pb-1 font-medium text-center">Constraint</th>
|
||
<th className="pb-1 font-medium text-center">In view</th>
|
||
<th className="pb-1 font-medium text-center">Seq</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{form.fields.map(f => {
|
||
const schemaEntry = form.schema.find(s => s.name === f.name)
|
||
const inView = !!schemaEntry
|
||
const currentType = schemaEntry?.type || f.type
|
||
return (
|
||
<tr key={f.name} className="border-t border-line-soft">
|
||
<td className="py-1 font-mono text-ink-soft">{f.name}</td>
|
||
<td className="py-1">
|
||
{inView && (
|
||
<select
|
||
className="border border-line rounded px-1 py-0.5 text-xs focus:outline-none focus:border-accent"
|
||
value={currentType}
|
||
onChange={e => setForm(ff => ({
|
||
...ff,
|
||
schema: ff.schema.map(s => s.name === f.name ? { ...s, type: e.target.value } : s)
|
||
}))}
|
||
>
|
||
{FIELD_TYPES.map(t => <option key={t} value={t}>{t}</option>)}
|
||
</select>
|
||
)}
|
||
</td>
|
||
<td className="py-1 text-center">
|
||
<input
|
||
type="checkbox"
|
||
checked={form.constraint_fields.split(',').map(s => s.trim()).includes(f.name)}
|
||
onChange={e => {
|
||
const current = form.constraint_fields.split(',').map(s => s.trim()).filter(Boolean)
|
||
const next = e.target.checked
|
||
? [...current, f.name]
|
||
: current.filter(n => n !== f.name)
|
||
setForm(ff => ({ ...ff, constraint_fields: next.join(', ') }))
|
||
}}
|
||
/>
|
||
</td>
|
||
<td className="py-1 text-center">
|
||
<input
|
||
type="checkbox"
|
||
checked={inView}
|
||
onChange={e => {
|
||
if (e.target.checked) {
|
||
const nextSeq = form.schema.length > 0
|
||
? Math.max(...form.schema.map(s => s.seq ?? 0)) + 1
|
||
: 1
|
||
setForm(ff => ({ ...ff, schema: [...ff.schema, { name: f.name, type: f.type, seq: nextSeq }] }))
|
||
} else {
|
||
setForm(ff => ({ ...ff, schema: ff.schema.filter(s => s.name !== f.name) }))
|
||
}
|
||
}}
|
||
/>
|
||
</td>
|
||
<td className="py-1 text-center">
|
||
{inView && (
|
||
<input
|
||
type="number"
|
||
className="w-12 border border-line rounded px-1 py-0.5 text-xs text-center focus:outline-none focus:border-accent"
|
||
value={schemaEntry.seq ?? ''}
|
||
onChange={e => setForm(ff => ({
|
||
...ff,
|
||
schema: ff.schema.map(s => s.name === f.name ? { ...s, seq: parseInt(e.target.value) || 0 } : s)
|
||
}))}
|
||
/>
|
||
)}
|
||
</td>
|
||
</tr>
|
||
)
|
||
})}
|
||
</tbody>
|
||
</table>
|
||
<SampleTable rows={form.sampleRows || []} />
|
||
</div>
|
||
)}
|
||
|
||
{form.fields.length === 0 && (
|
||
<div>
|
||
<label className="text-xs text-muted block mb-1">Constraint fields (comma-separated)</label>
|
||
<input
|
||
className="w-full border border-line rounded px-3 py-1.5 text-sm focus:outline-none focus:border-accent"
|
||
value={form.constraint_fields}
|
||
onChange={e => setForm(f => ({ ...f, constraint_fields: e.target.value }))}
|
||
placeholder="e.g. date, amount, description"
|
||
/>
|
||
</div>
|
||
)}
|
||
|
||
<div className="flex gap-4">
|
||
<label className="flex items-center gap-1.5 text-xs text-muted cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={form.global_picklist !== false}
|
||
onChange={e => setForm(f => ({ ...f, global_picklist: e.target.checked }))}
|
||
/>
|
||
Global picklist
|
||
</label>
|
||
{form.fields.length > 0 && (
|
||
<label className="flex items-center gap-1.5 text-xs text-muted cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={form.importSample !== false}
|
||
onChange={e => setForm(f => ({ ...f, importSample: e.target.checked }))}
|
||
/>
|
||
Import sample data
|
||
</label>
|
||
)}
|
||
</div>
|
||
|
||
{createError && <p className="text-xs text-danger">{createError}</p>}
|
||
|
||
<div className="flex gap-2">
|
||
<button type="submit" disabled={createLoading}
|
||
className="text-sm bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 disabled:opacity-50">
|
||
{createLoading ? 'Creating…' : 'Create'}
|
||
</button>
|
||
<button type="button"
|
||
onClick={() => { setCreating(false); setCreateError(''); setForm({ name: '', constraint_fields: '', fields: [], schema: [] }) }}
|
||
className="text-sm text-muted px-3 py-1.5 rounded hover:bg-raised">
|
||
Cancel
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|