File size: 6,594 Bytes
90d354b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133b124
 
90d354b
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from 'react'
import { NavLink, Outlet, useLocation, Link } from 'react-router'
import { ArrowUpRight, Bell, BookOpen, ChartNoAxesCombined, CheckCheck, ChevronDown, CircleDot, FileText, House, Menu, Plus, ScanLine, ShieldCheck, X } from 'lucide-react'
import { pendingNotices, store, useApp, LEVEL_META } from '@/lib/engine'
import { useHealth } from '@/lib/metrics'
import {isCloud} from '@/lib/cloud-client'
import { Brand, Empty } from './Design'
import {groundedApi,type ContractResult} from '@/lib/grounded'
import type {Observation} from '@/lib/domain'
const NAV=[{to:'/home',label:'家庭概览',icon:House},{to:'/watches',label:'关注事项',icon:CircleDot},{to:'/memories',label:'对象记忆',icon:BookOpen},{to:'/events',label:'事件与取证',icon:ScanLine},{to:'/review',label:'生活回顾',icon:FileText},{to:'/insights',label:'运行洞察',icon:ChartNoAxesCombined},{to:'/privacy',label:'隐私与数据',icon:ShieldCheck}]
export default function Shell(){
 const state=useApp(), health=useHealth(), location=useLocation()
 const [noticesOpen,setNoticesOpen]=useState(false),[mobileOpen,setMobileOpen]=useState(false)
 const pending=pendingNotices(state)
 const contractIds=state.items.filter(i=>i.contractId&&i.enabled).map(i=>i.contractId).join(',')
 useEffect(()=>{if(!contractIds)return;let stopped=false;const tick=()=>groundedApi<{registrationId:string;notices:ContractResult['notices'];observations:Observation[]}[]>('/api/contracts/notices?ids='+encodeURIComponent(contractIds)).then(rows=>{if(!stopped)rows.forEach(r=>{store.syncContractHistory(r.registrationId,r.observations||[]);store.applyContractNotices(r.registrationId,r.notices)})}).catch(()=>{});void tick();const timer=setInterval(()=>{void tick()},10000);return()=>{stopped=true;clearInterval(timer)}},[contractIds])
 useEffect(()=>{if(health)store.setModelStatus(health.ready?'ready':'offline')},[health])
 useEffect(()=>{setMobileOpen(false);setNoticesOpen(false);document.querySelector('.content-scroll')?.scrollTo(0,0)},[location.pathname])
 useEffect(()=>{const escape=(e:KeyboardEvent)=>{if(e.key==='Escape'){setNoticesOpen(false);setMobileOpen(false)}};window.addEventListener('keydown',escape);return()=>window.removeEventListener('keydown',escape)},[])
 const title=NAV.find(n=>location.pathname===n.to)?.label||(location.pathname==='/create'?'新建关注':'事项详情')
 return <div className="app-shell">

   <a href="#main" className="skip-link">跳到内容</a>

   {mobileOpen&&<button className="nav-scrim" aria-label="关闭菜单" onClick={()=>setMobileOpen(false)}/>}

   <aside className={'sidebar '+(mobileOpen?'is-open':'')}>

     <Brand light/>

     <div className="space-switch"><span className="space-avatar"><House size={17}/></span><span>我的家<small>个人空间</small></span><ChevronDown size={14}/></div>

     <div className="nav-label">我的空间 <span>01</span></div>

     <nav aria-label="主导航">{NAV.map((n,i)=><NavLink key={n.to} to={n.to} className={({isActive})=>(isActive?'active ':'')+(i===4?'nav-divider':'')}><n.icon size={18} strokeWidth={1.6}/><span>{n.label}</span>{i===1&&<em>{state.items.length}</em>}</NavLink>)}</nav>

     <Link to="/workspace/agent" className="sidebar-create"><Plus size={16}/>添加新的关注</Link>

     <Link to="/app" className="sidebar-create"><ArrowUpRight size={16}/>打开 App 模拟页</Link>

     <div className="sidebar-bottom">

       <div className="quiet-note"><span className="quiet-glyph">“</span><p>少一些打扰,<br/>多一分安心。</p><span>LESS NOISE. MORE LIFE.</span></div>

       <Link to="/insights" className="model-status"><i className={health?.ready?'ready':''}/><div>{health?health.ready?(isCloud()?'云端引擎已就绪':'本地引擎已连接'):(isCloud()?'云端引擎待连接':'本地引擎待连接'):'正在连接引擎'}<small>imajev · 4B</small></div><ArrowUpRight size={14}/></Link>

       <Link className="sidebar-profile" to="/privacy"><span>H</span><div>Home Pulse<small>你的家庭观察助手</small></div><ShieldCheck size={16}/></Link>

     </div>

   </aside>

   <div className="workspace">

     <header className="topbar"><div className="breadcrumb"><button className="icon-btn mobile-menu" aria-label="展开导航" onClick={()=>setMobileOpen(true)}><Menu size={20}/></button><House size={14}/><span>/</span><b>{title}</b></div><div className="topbar-right"><span className="top-date">{new Intl.DateTimeFormat('zh-CN',{timeZone:'Asia/Shanghai',month:'long',day:'numeric',weekday:'long'}).format(new Date())}</span><span className="topbar-rule"/><span className="local-badge"><ShieldCheck size={14}/>{isCloud()?'HF 云端运行':'本地运行'}</span><button className={'icon-btn notice-button '+(noticesOpen?'selected':'')} aria-label={'通知中心'+(pending?','+pending+' 条未读':'')} aria-expanded={noticesOpen} onClick={()=>setNoticesOpen(!noticesOpen)}><Bell size={19}/>{pending>0&&<i>{pending}</i>}</button><span className="avatar">H</span></div></header>

     {noticesOpen&&<><button className="notice-scrim" aria-label="关闭通知中心" onClick={()=>setNoticesOpen(false)}/><section className="notice-panel" aria-label="通知中心"><div className="section-head"><h2>重要的消息 <span className="count">{pending}</span></h2><button className="icon-btn" aria-label="关闭通知" onClick={()=>setNoticesOpen(false)}><X size={18}/></button></div>{state.notices.length>0&&<button className="text-link" onClick={()=>store.ackAll()}><CheckCheck size={14}/>全部标记已读</button>}<div className="notice-list">{!state.notices.length&&<Empty icon="check" title="现在,没有新消息" description="值得关注的变化会出现在这里。"/>}{state.notices.map(n=><article key={n.id} className={n.ack?'read':''}><div><i style={{background:LEVEL_META[n.level].color}}/><b>{n.title}</b><time>{n.time}</time></div><p>{n.body}</p><footer><Link to={'/watch/'+n.itemId} onClick={()=>store.ackNotice(n.id)}>查看证据 <ArrowUpRight size={13}/></Link>{!n.ack&&<button onClick={()=>store.ackNotice(n.id)}>知道了</button>}<button className={n.feedback==='too_many'?'feedback-active':''} onClick={()=>store.noticeFeedback(n.id,n.feedback==='too_many'?undefined:'too_many')}>减少此类提醒</button></footer></article>)}</div></section></>}

     <div className="content-scroll"><main id="main"><Outlet/></main><footer className="page-footer"><span>HOME PULSE <i/> 留意家的每一份日常</span><span>Designed for a quieter life.</span></footer></div>

   </div>
 </div>
}