import React, { useEffect, useState } from 'react'; import { Link, useLocation } from '@tanstack/react-router'; type ViewedItem = { type: 'customer' | 'project'; id: string; name: string; viewedAt: number; }; export default function RecentlyViewed() { const location = useLocation(); const [items, setItems] = useState([]); useEffect(() => { // 1. Track current route const path = location.pathname; const customerMatch = path.match(/\/customers\/([^/]+)/); const projectMatch = path.match(/\/projects\/([^/]+)/); if (customerMatch || projectMatch) { const type = customerMatch ? 'customer' : 'project'; const id = customerMatch ? customerMatch[1] : projectMatch![1]; // In a real app, we'd fetch the name from a store/cache. // For the widget, we'll use a placeholder or try to extract from state if available. // Here we use a generic label since we don't have the full entity object in the URL. const name = `View ${id}`; const stored = localStorage.getItem('recently_viewed'); const parsed: ViewedItem[] = stored ? JSON.parse(stored) : []; // Remove existing entry of same item to move it to top const filtered = parsed.filter(i => !(i.type === type && i.id === id)); const updated = [{ type, id, name, viewedAt: Date.now() }, ...filtered].slice(0, 5); localStorage.setItem('recently_viewed', JSON.stringify(updated)); setItems(updated); } }, [location.pathname]); // Initial load useEffect(() => { const stored = localStorage.getItem('recently_viewed'); if (stored) setItems(JSON.parse(stored)); }, []); if (items.length === 0) return null; return (

Recently Viewed

); }