|
|
|
@@ -0,0 +1,391 @@
|
|
|
|
|
import { useState } from 'react'
|
|
|
|
|
import { useParams, useNavigate } from 'react-router-dom'
|
|
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
import { ArrowLeft, Pencil, Trash2, AlertTriangle, Loader2, Archive } from 'lucide-react'
|
|
|
|
|
import ColorSwatch from '../components/ColorSwatch'
|
|
|
|
|
import { fetchFilamentById, fetchUsageLogs, deleteFilament } from '../services/filamentService'
|
|
|
|
|
import type { UsageLog } from '../types/filament'
|
|
|
|
|
|
|
|
|
|
export default function FilamentDetailPage() {
|
|
|
|
|
const { id } = useParams<{ id: string }>()
|
|
|
|
|
const spoolId = Number(id)
|
|
|
|
|
const navigate = useNavigate()
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
|
const [deleteConfirm, setDeleteConfirm] = useState(false)
|
|
|
|
|
const [deleting, setDeleting] = useState(false)
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: spool,
|
|
|
|
|
isLoading: spoolLoading,
|
|
|
|
|
error: spoolError,
|
|
|
|
|
refetch: refetchSpool,
|
|
|
|
|
} = useQuery({
|
|
|
|
|
queryKey: ['filament', spoolId],
|
|
|
|
|
queryFn: () => fetchFilamentById(spoolId),
|
|
|
|
|
enabled: !isNaN(spoolId),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: usageLogs,
|
|
|
|
|
isLoading: usageLoading,
|
|
|
|
|
error: usageError,
|
|
|
|
|
refetch: refetchUsage,
|
|
|
|
|
} = useQuery({
|
|
|
|
|
queryKey: ['usage-logs', spoolId],
|
|
|
|
|
queryFn: () => fetchUsageLogs(spoolId),
|
|
|
|
|
enabled: !isNaN(spoolId),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const handleDelete = async () => {
|
|
|
|
|
setDeleting(true)
|
|
|
|
|
try {
|
|
|
|
|
await deleteFilament(spoolId)
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ['filaments'] })
|
|
|
|
|
navigate('/')
|
|
|
|
|
} catch {
|
|
|
|
|
setDeleting(false)
|
|
|
|
|
setDeleteConfirm(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Loading state ──
|
|
|
|
|
if (spoolLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col items-center justify-center py-24 gap-3 text-slate-400">
|
|
|
|
|
<Loader2 size={32} className="animate-spin text-emerald-400" />
|
|
|
|
|
<span>Loading spool details…</span>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Error state ──
|
|
|
|
|
if (spoolError || !spool) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col items-center justify-center py-24 gap-4">
|
|
|
|
|
<div className="flex h-14 w-14 items-center justify-center rounded-full bg-red-900/30">
|
|
|
|
|
<AlertTriangle size={28} className="text-red-400" />
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-red-400 font-medium">Failed to load spool details.</p>
|
|
|
|
|
<div className="flex gap-3">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => refetchSpool()}
|
|
|
|
|
className="rounded-lg bg-slate-700 px-4 py-2 text-sm font-medium text-slate-200 hover:bg-slate-600 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Retry
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => navigate('/')}
|
|
|
|
|
className="rounded-lg bg-slate-800 border border-slate-700 px-4 py-2 text-sm font-medium text-slate-300 hover:bg-slate-700 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Back to Inventory
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pctRemaining = spool.initial_grams > 0
|
|
|
|
|
? (spool.remaining_grams / spool.initial_grams) * 100
|
|
|
|
|
: 0
|
|
|
|
|
const isLow = spool.remaining_grams <= (spool.low_stock_threshold_grams || 0)
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-6">
|
|
|
|
|
{/* ── Header Bar ── */}
|
|
|
|
|
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => navigate('/')}
|
|
|
|
|
className="inline-flex items-center gap-2 rounded-lg bg-slate-800 border border-slate-700 px-3 py-2 text-sm font-medium text-slate-200 hover:bg-slate-700 active:bg-slate-600 transition-colors w-fit"
|
|
|
|
|
>
|
|
|
|
|
<ArrowLeft size={16} />
|
|
|
|
|
Back to Inventory
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<button className="inline-flex items-center gap-2 rounded-lg bg-slate-700 px-4 py-2 text-sm font-semibold text-slate-200 hover:bg-slate-600 active:bg-slate-500 transition-colors">
|
|
|
|
|
<Pencil size={16} />
|
|
|
|
|
Edit
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setDeleteConfirm(true)}
|
|
|
|
|
className="inline-flex items-center gap-2 rounded-lg bg-red-900/30 border border-red-700 px-4 py-2 text-sm font-semibold text-red-300 hover:bg-red-900/50 active:bg-red-900/70 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
<Trash2 size={16} />
|
|
|
|
|
Delete
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* ── Spool Info Card ── */}
|
|
|
|
|
<div className="rounded-xl border border-slate-700 bg-slate-800 overflow-hidden">
|
|
|
|
|
{/* Card header */}
|
|
|
|
|
<div className="px-6 py-5 border-b border-slate-700 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<ColorSwatch colorHex={spool.color_hex} size={36} />
|
|
|
|
|
<div>
|
|
|
|
|
<h1 className="text-xl font-bold text-slate-100">{spool.name}</h1>
|
|
|
|
|
<p className="text-sm text-slate-400">
|
|
|
|
|
{spool.brand && <span>{spool.brand} · </span>}
|
|
|
|
|
{spool.diameter_mm}mm
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
{isLow ? (
|
|
|
|
|
<span className="inline-flex items-center gap-1.5 rounded-full bg-red-900/40 border border-red-700 px-3 py-1 text-sm font-medium text-red-300">
|
|
|
|
|
<AlertTriangle size={14} /> Low Stock
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="inline-flex items-center gap-1.5 rounded-full bg-emerald-900/30 border border-emerald-700 px-3 py-1 text-sm font-medium text-emerald-300">
|
|
|
|
|
In Stock
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Card body — 2-column grid */}
|
|
|
|
|
<div className="px-6 py-5 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-x-8 gap-y-4">
|
|
|
|
|
<DetailItem label="Material" value={spool.material_base?.name ?? '—'} />
|
|
|
|
|
<DetailItem label="Finish" value={spool.material_finish?.name ?? '—'} />
|
|
|
|
|
<DetailItem
|
|
|
|
|
label="Modifier"
|
|
|
|
|
value={spool.material_modifier?.name ?? 'None'}
|
|
|
|
|
/>
|
|
|
|
|
<DetailItem label="Color" value={spool.color_hex.toUpperCase()} />
|
|
|
|
|
<DetailItem label="Brand" value={spool.brand || '—'} />
|
|
|
|
|
<DetailItem label="Diameter" value={`${spool.diameter_mm}mm`} />
|
|
|
|
|
<DetailItem
|
|
|
|
|
label="Remaining"
|
|
|
|
|
value={
|
|
|
|
|
<span className="font-semibold tabular-nums">
|
|
|
|
|
{spool.remaining_grams.toLocaleString()} g
|
|
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<DetailItem
|
|
|
|
|
label="Initial"
|
|
|
|
|
value={`${spool.initial_grams.toLocaleString()} g`}
|
|
|
|
|
/>
|
|
|
|
|
<DetailItem
|
|
|
|
|
label="Cost"
|
|
|
|
|
value={
|
|
|
|
|
spool.cost_usd != null
|
|
|
|
|
? `$${spool.cost_usd.toFixed(2)}`
|
|
|
|
|
: '—'
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Progress Bar */}
|
|
|
|
|
<div className="px-6 pb-5 pt-1">
|
|
|
|
|
<div className="flex items-center justify-between text-sm mb-1.5">
|
|
|
|
|
<span className="text-slate-400">
|
|
|
|
|
<span className="text-slate-200 font-semibold tabular-nums">
|
|
|
|
|
{spool.remaining_grams.toLocaleString()}g
|
|
|
|
|
</span>{' '}
|
|
|
|
|
of {spool.initial_grams.toLocaleString()}g
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-slate-400 font-medium tabular-nums">
|
|
|
|
|
{pctRemaining.toFixed(1)}%
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="w-full h-3 rounded-full bg-slate-700 overflow-hidden">
|
|
|
|
|
<div
|
|
|
|
|
className={`h-full rounded-full transition-all duration-500 ${
|
|
|
|
|
pctRemaining <= 10
|
|
|
|
|
? 'bg-red-500'
|
|
|
|
|
: pctRemaining <= 25
|
|
|
|
|
? 'bg-amber-500'
|
|
|
|
|
: 'bg-emerald-500'
|
|
|
|
|
}`}
|
|
|
|
|
style={{ width: `${Math.min(100, pctRemaining)}%` }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* ── Usage History ── */}
|
|
|
|
|
<div className="rounded-xl border border-slate-700 bg-slate-800 overflow-hidden">
|
|
|
|
|
<div className="px-6 py-4 border-b border-slate-700">
|
|
|
|
|
<h2 className="text-lg font-semibold text-slate-100">Usage History</h2>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Loading sub-state */}
|
|
|
|
|
{usageLoading && (
|
|
|
|
|
<div className="px-6 py-8 text-center text-slate-400">
|
|
|
|
|
<Loader2 size={20} className="animate-spin inline mr-2" />
|
|
|
|
|
Loading usage history…
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Usage error sub-state */}
|
|
|
|
|
{usageError && !usageLoading && (
|
|
|
|
|
<div className="px-6 py-8 text-center">
|
|
|
|
|
<p className="text-red-400 text-sm">
|
|
|
|
|
Failed to load usage history.
|
|
|
|
|
</p>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => refetchUsage()}
|
|
|
|
|
className="mt-2 text-sm text-emerald-400 hover:text-emerald-300 underline"
|
|
|
|
|
>
|
|
|
|
|
Retry
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Empty state */}
|
|
|
|
|
{!usageLoading && !usageError && usageLogs && usageLogs.length === 0 && (
|
|
|
|
|
<div className="px-6 py-10 text-center">
|
|
|
|
|
<Archive size={32} className="mx-auto text-slate-600 mb-2" />
|
|
|
|
|
<p className="text-slate-400 text-sm">No usage recorded yet.</p>
|
|
|
|
|
<p className="text-slate-500 text-xs mt-1">
|
|
|
|
|
Usage data will appear here when this spool is used in print jobs.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Table — desktop */}
|
|
|
|
|
{!usageLoading && !usageError && usageLogs && usageLogs.length > 0 && (
|
|
|
|
|
<>
|
|
|
|
|
<div className="hidden md:block overflow-x-auto">
|
|
|
|
|
<table className="w-full text-sm">
|
|
|
|
|
<thead className="bg-slate-800/50 text-slate-300">
|
|
|
|
|
<tr>
|
|
|
|
|
<th className="px-6 py-3 text-left font-semibold">Date</th>
|
|
|
|
|
<th className="px-6 py-3 text-left font-semibold">Print Job</th>
|
|
|
|
|
<th className="px-6 py-3 text-right font-semibold">mm Extruded</th>
|
|
|
|
|
<th className="px-6 py-3 text-right font-semibold">Grams Used</th>
|
|
|
|
|
<th className="px-6 py-3 text-right font-semibold">Cost</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody className="divide-y divide-slate-700">
|
|
|
|
|
{usageLogs.map((log: UsageLog) => (
|
|
|
|
|
<tr
|
|
|
|
|
key={log.id}
|
|
|
|
|
className="hover:bg-slate-700/30 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
<td className="px-6 py-3 text-slate-200 tabular-nums">
|
|
|
|
|
{new Date(log.logged_at).toLocaleDateString('en-US', {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
})}
|
|
|
|
|
</td>
|
|
|
|
|
<td className="px-6 py-3 text-slate-300">
|
|
|
|
|
{log.print_job_name || `Job #${log.print_job_id ?? log.id}`}
|
|
|
|
|
</td>
|
|
|
|
|
<td className="px-6 py-3 text-right text-slate-200 tabular-nums">
|
|
|
|
|
{log.mm_extruded.toLocaleString()}
|
|
|
|
|
</td>
|
|
|
|
|
<td className="px-6 py-3 text-right text-slate-200 tabular-nums">
|
|
|
|
|
{log.grams_used.toFixed(2)} g
|
|
|
|
|
</td>
|
|
|
|
|
<td className="px-6 py-3 text-right text-slate-200 tabular-nums">
|
|
|
|
|
{log.cost_usd != null ? `$${log.cost_usd.toFixed(3)}` : '—'}
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Mobile cards */}
|
|
|
|
|
<div className="md:hidden divide-y divide-slate-700">
|
|
|
|
|
{usageLogs.map((log: UsageLog) => (
|
|
|
|
|
<div key={log.id} className="px-4 py-4 space-y-2">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<span className="text-sm font-medium text-slate-200">
|
|
|
|
|
{log.print_job_name || `Job #${log.print_job_id ?? log.id}`}
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-xs text-slate-400 tabular-nums">
|
|
|
|
|
{new Date(log.logged_at).toLocaleDateString('en-US', {
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center justify-between text-sm">
|
|
|
|
|
<span className="text-slate-400">
|
|
|
|
|
Extruded:{' '}
|
|
|
|
|
<span className="text-slate-300 tabular-nums">
|
|
|
|
|
{log.mm_extruded.toLocaleString()} mm
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-slate-400">
|
|
|
|
|
Used:{' '}
|
|
|
|
|
<span className="text-slate-300 tabular-nums">
|
|
|
|
|
{log.grams_used.toFixed(2)} g
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-slate-400">
|
|
|
|
|
{log.cost_usd != null ? (
|
|
|
|
|
<>
|
|
|
|
|
Cost:{' '}
|
|
|
|
|
<span className="text-slate-300 tabular-nums">
|
|
|
|
|
${log.cost_usd.toFixed(3)}
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
'—'
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* ── Delete Confirmation Modal ── */}
|
|
|
|
|
{deleteConfirm && (
|
|
|
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
|
|
|
|
<div className="w-full max-w-sm rounded-xl bg-slate-800 border border-slate-700 p-6 shadow-2xl space-y-4">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-red-900/30 flex-shrink-0">
|
|
|
|
|
<AlertTriangle size={20} className="text-red-400" />
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="text-lg font-semibold text-slate-100">
|
|
|
|
|
Delete “{spool.name}”?
|
|
|
|
|
</h3>
|
|
|
|
|
<p className="text-sm text-slate-400">
|
|
|
|
|
This permanently removes the spool and all usage history. This cannot be undone.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex justify-end gap-3">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setDeleteConfirm(false)}
|
|
|
|
|
disabled={deleting}
|
|
|
|
|
className="rounded-lg bg-slate-700 px-4 py-2 text-sm font-medium text-slate-200 hover:bg-slate-600 transition-colors disabled:opacity-50"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleDelete}
|
|
|
|
|
disabled={deleting}
|
|
|
|
|
className="inline-flex items-center gap-2 rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-500 transition-colors disabled:opacity-50"
|
|
|
|
|
>
|
|
|
|
|
{deleting && <Loader2 size={16} className="animate-spin" />}
|
|
|
|
|
{deleting ? 'Deleting…' : 'Delete'}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Small detail row for the info card grid */
|
|
|
|
|
function DetailItem({ label, value }: { label: string; value: React.ReactNode }) {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<dt className="text-xs font-medium uppercase tracking-wider text-slate-500 mb-0.5">
|
|
|
|
|
{label}
|
|
|
|
|
</dt>
|
|
|
|
|
<dd className="text-sm text-slate-200">{value}</dd>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|