File size: 5,891 Bytes
df033d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import { Eye, Image as ImageIcon, Leaf, Moon, Package, Pill, Sun } from 'lucide-react'
import { useEffect, useState } from 'react'
import About from './pages/About'
import Caption from './pages/Caption'
import FoodLabel from './pages/FoodLabel'
import Home from './pages/Home'
import Plant from './pages/Plant'
import Prescription from './pages/Prescription'
// Hash routes, so a page can be linked and survives a reload (no router needed).
const ROUTES = {
'#/caption': 'caption',
'#/prescription': 'prescription',
'#/product-label': 'label',
'#/plant': 'plant',
'#/about': 'about',
}
const pageFromHash = () => ROUTES[window.location.hash] ?? 'home'
const TOOLS = [
{ page: 'caption', href: '#/caption', label: 'Caption a photo', short: 'Caption', icon: ImageIcon },
{ page: 'prescription', href: '#/prescription', label: 'Read a prescription', short: 'Prescription', icon: Pill },
{ page: 'label', href: '#/product-label', label: 'Check a food label', short: 'Food label', icon: Package },
{ page: 'plant', href: '#/plant', label: 'Identify a plant', short: 'Plant', icon: Leaf },
]
function useTheme() {
const [theme, setTheme] = useState(() => (document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light'))
useEffect(() => {
if (theme === 'dark') document.documentElement.dataset.theme = 'dark'
else delete document.documentElement.dataset.theme
try {
localStorage.setItem('theme', theme)
} catch {
// storage blocked: the choice lasts for this visit only
}
}, [theme])
return [theme, () => setTheme((t) => (t === 'dark' ? 'light' : 'dark'))]
}
function Header({ page, theme, onToggleTheme }) {
return (
<header className="border-b border-hairline bg-card">
<div className="mx-auto flex max-w-6xl flex-wrap items-center gap-x-6 px-4 sm:px-6">
<a href="#/" className="flex min-h-14 items-center gap-2 text-lg font-semibold text-ink no-underline">
<span className="grid size-7 place-items-center rounded-[7px] bg-accent text-on-accent">
<Eye className="size-[18px]" strokeWidth={2} aria-hidden="true" />
</span>
Vision AI
</a>
<nav
aria-label="Tools"
className="order-3 -mx-4 flex w-[calc(100%+2rem)] border-t border-hairline sm:order-none sm:mx-0 sm:w-auto sm:border-t-0"
>
{TOOLS.map(({ page: p, href, label, short, icon: Icon }) => {
const active = p === page
return (
<a
key={p}
href={href}
aria-current={active ? 'page' : undefined}
className={`flex min-h-12 flex-1 items-center justify-center gap-2 border-b-2 px-3 text-sm font-medium transition-colors duration-150 sm:min-h-14 sm:flex-none ${
active ? 'border-accent text-ink' : 'border-transparent text-ink-2 hover:text-ink'
}`}
>
<Icon className="size-[18px]" strokeWidth={1.75} aria-hidden="true" />
<span className="sm:hidden">{short}</span>
<span className="hidden sm:inline">{label}</span>
</a>
)
})}
</nav>
<div className="ml-auto flex items-center gap-1">
<a
href="#/about"
aria-current={page === 'about' ? 'page' : undefined}
className={`rounded-control px-2 py-2 text-sm ${page === 'about' ? 'text-ink' : 'text-ink-2 hover:text-ink'}`}
>
About
</a>
<button
type="button"
onClick={onToggleTheme}
aria-label={theme === 'dark' ? 'Switch to light theme' : 'Switch to dark theme'}
className="grid size-11 place-items-center rounded-control text-ink-2 transition-colors duration-150 hover:bg-raised hover:text-ink"
>
{theme === 'dark' ? (
<Sun className="size-[18px]" strokeWidth={1.75} aria-hidden="true" />
) : (
<Moon className="size-[18px]" strokeWidth={1.75} aria-hidden="true" />
)}
</button>
</div>
</div>
</header>
)
}
function Footer() {
return (
<footer className="mx-auto mt-8 flex w-full max-w-6xl flex-wrap gap-x-6 gap-y-1 border-t border-hairline px-4 py-5 text-xs text-ink-2 sm:px-6">
<span>Vision AI is a student project, not a medical device</span>
<a href="#/about" className="text-accent underline underline-offset-4">
About and credits
</a>
<span>Additive names from Open Food Facts (ODbL)</span>
</footer>
)
}
export default function App() {
const [page, setPage] = useState(pageFromHash)
const [theme, toggleTheme] = useTheme()
useEffect(() => {
const onHash = () => {
setPage(pageFromHash())
window.scrollTo(0, 0)
}
window.addEventListener('hashchange', onHash)
return () => window.removeEventListener('hashchange', onHash)
}, [])
return (
<div className="flex min-h-dvh flex-col">
{/* A button, not href="#main": the hash is the router */}
<button
type="button"
onClick={() => document.getElementById('main')?.focus()}
className="sr-only focus:not-sr-only focus:absolute focus:left-4 focus:top-2 focus:z-10 focus:rounded-control focus:bg-card focus:px-3 focus:py-2"
>
Skip to content
</button>
<Header page={page} theme={theme} onToggleTheme={toggleTheme} />
<main id="main" tabIndex={-1} className="mx-auto w-full max-w-6xl flex-1 px-4 py-6 sm:px-6 sm:py-8">
{page === 'home' && <Home />}
{page === 'caption' && <Caption />}
{page === 'prescription' && <Prescription />}
{page === 'label' && <FoodLabel />}
{page === 'plant' && <Plant />}
{page === 'about' && <About />}
</main>
<Footer />
</div>
)
}
|