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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
<script lang="ts">
import type { Route } from '../lib/router';
import { go } from '../lib/router';
import { session } from '../lib/session.svelte';
import { postLogout } from '../lib/client';
import { projectsStore, projectOf, projectInvisible } from '../lib/listsStore.svelte';
import { Button } from '$lib/components/ui/button';
import * as Tabs from '$lib/components/ui/tabs';
import { Separator } from '$lib/components/ui/separator';
interface Props {
route: Route;
children?: import('svelte').Snippet;
}
let { route, children }: Props = $props();
const topLeft: { name: string; label: string }[] = [
{ name: 'projects', label: '我的项目' },
{ name: 'datasets', label: '数据集' },
{ name: 'runs', label: '实验记录' },
{ name: 'usage', label: 'AI 用量' }
];
const bottom: { name: string; label: string }[] =
session.me?.role === 'admin'
? [
{ name: 'account', label: '账户与安全' },
{ name: 'admin', label: '管理员' }
]
: [{ name: 'account', label: '账户与安全' }];
async function logout() {
await postLogout().catch(() => undefined);
session.set(null);
// drop cached projects/failure marks: the next session on this browser
// must not see (or be marked invisible by) the previous user's projects
projectsStore.clear();
go('/login');
}
const isProject = $derived(route.name.startsWith('project-'));
const projectTab = $derived(isProject ? route.name.replace('project-', '') : '');
const projectNav: [string, string][] = [
['data', '数据'],
['strategy', '策略'],
['backtest', '回测'],
['results', '结果']
];
const activeProject = $derived(isProject ? projectOf(route.params.id) : null);
const projectHidden =
$derived(isProject && !activeProject && projectInvisible(route.params.id));
// Deep link / first navigation into a project tab: the sidebar store may not
// contain the project yet (ProjectsPage was never visited). Fetch it once so
// the 当前项目 name resolves without waiting for the tab's own lifecycle.
$effect(() => {
const id = isProject ? route.params.id : null;
if (id && !activeProject) void projectsStore.ensure(id).catch(() => undefined);
});
// ---- 移动端导航(≤767px 底部 Tab Bar,shadcn Tabs 组件实现)----
import { onDestroy } from 'svelte';
let phoneMode = $state(false);
let mqListener: ((e: MediaQueryListEvent) => void) | null = null;
$effect(() => {
if (typeof window === 'undefined' || !window.matchMedia) {
phoneMode = false;
return;
}
const mq = window.matchMedia('(max-width: 767px)');
phoneMode = !!mq.matches;
const fn: (e: MediaQueryListEvent) => void = (e) => (phoneMode = e.matches);
if (mq.addEventListener) mq.addEventListener('change', fn);
else mq.addListener?.(fn as never);
mqListener = fn;
return () => {
if (mq.removeEventListener) mq.removeEventListener('change', mqListener as never);
else mq.removeListener?.(mqListener as never);
mqListener = null;
};
});
const mobile = $derived(phoneMode);
const tabs: { name: string; label: string; icon: 'project' | 'dataset' | 'run' | 'user' }[] = [
{ name: 'projects', label: '我的项目', icon: 'project' },
{ name: 'datasets', label: '数据集', icon: 'dataset' },
{ name: 'runs', label: '实验记录', icon: 'run' },
{ name: 'account', label: '我的', icon: 'user' }
];
const activeTab = $derived(
isProject || route.name === 'admin' ? 'projects' : route.name === 'account' ? 'account' : route.name
);
const navLinkBase = 'block rounded-md py-1.5 px-2 text-sm no-underline hover:text-foreground';
</script>
<div class="grid min-h-screen grid-cols-1 md:grid-cols-[208px_minmax(0,1fr)]">
{#if mobile}
<header
class="sticky top-0 z-30 flex items-center gap-2 min-h-10 px-2.5 border-b border-border bg-sidebar/90 backdrop-blur font-sm text-[0.85rem]"
data-testid="mobile-topbar"
>
<span class="font-bold text-foreground whitespace-nowrap">策研 <em class="not-italic font-medium text-[0.62rem] text-muted-foreground block">Strategy Lab</em></span>
{#if isProject}
<span class="flex-1 min-w-0 truncate text-center font-semibold text-foreground" aria-label="当前项目">{activeProject?.name ?? '项目'} · {projectNav.find(([t]) => t === projectTab)?.[1] ?? ''}</span>
{/if}
<Button variant="ghost" size="icon-sm" onclick={() => go('/account')} aria-label="账户与安全">
<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><circle cx="12" cy="8" r="4" fill="none" stroke="currentColor" stroke-width="2"/><path d="M4 21c0-4 4-6 8-6s8 2 8 6" fill="none" stroke="currentColor" stroke-width="2"/></svg>
</Button>
</header>
{/if}
<aside
class="bg-sidebar border-b md:border-b-0 md:border-r border-border md:sticky md:top-0 md:overflow-y-auto md:h-screen max-md:hidden flex flex-row md:flex-col flex-wrap md:flex-nowrap items-center md:items-stretch gap-1 md:gap-2 px-2 md:px-3 py-2 md:py-3"
aria-label="全局导航"
>
<a href="#/projects" class="flex items-center gap-1.5 py-1 px-1 pb-1 border-b border-border mb-1 no-underline w-full text-foreground">
<span class="size-5 rounded-lg bg-primary shrink-0" aria-hidden="true"></span>
<span class="font-bold text-[0.98rem] tracking-tight">策研 <span class="text-muted-foreground/70 font-medium text-[0.68rem] block">Strategy Lab</span></span>
</a>
{#if isProject}
<div class="flex flex-wrap md:block gap-0.5 w-full min-w-0">
<div class="hidden md:block text-[0.68rem] uppercase tracking-widest text-muted-foreground px-1 pt-1 pb-0.5 font-bold">当前项目</div>
<div class="font-semibold px-1.5 pt-0.5 pb-1 text-[0.9rem] break-all text-foreground md:max-md:w-full">{activeProject?.name ?? (projectHidden ? '不可见项目' : '加载中…')}</div>
{#each projectNav as [t, label] (t)}
<a
class={`${navLinkBase} text-[0.85rem] ${projectTab === t ? 'bg-primary text-primary-foreground font-semibold' : 'text-muted-foreground hover:bg-secondary'}`}
href={`#/projects/${route.params.id}/${t}`}
aria-current={projectTab === t ? 'page' : undefined}
>{label}</a>
{/each}
<Separator class="hidden md:block mx-2 my-2" />
</div>
{/if}
<div class="hidden md:block text-[0.68rem] uppercase tracking-widest text-muted-foreground px-1 pt-1 pb-0.5 font-bold">工作台</div>
<div class="flex flex-wrap md:flex-col gap-0.5">
{#each topLeft as item (item.name)}
<a class={`${navLinkBase} ${route.name === item.name ? 'bg-primary text-primary-foreground font-semibold' : 'text-muted-foreground hover:bg-secondary'}`} href={`#/${item.name}`} aria-current={route.name === item.name ? 'page' : undefined}>
{item.label}
</a>
{/each}
</div>
<div class="flex-1 md:min-h-4"></div>
<div class="hidden md:block text-[0.68rem] uppercase tracking-widest text-muted-foreground px-1 pt-1 pb-0.5 font-bold">账户</div>
<div class="flex flex-wrap md:flex-col gap-0.5">
{#each bottom as item (item.name)}
<a class={`${navLinkBase} ${route.name === item.name ? 'bg-primary text-primary-foreground font-semibold' : 'text-muted-foreground hover:bg-secondary'}`} href={`#/${item.name}`} aria-current={route.name === item.name ? 'page' : undefined}>
{item.label}
</a>
{/each}
</div>
{#if session.me}
<div class="flex items-center justify-between gap-1 p-1 bg-secondary rounded-md text-[0.84rem] w-full">
<span class="truncate text-muted-foreground min-w-0">{session.me.name}{session.me.role === 'admin' ? ' · 管理员' : ''}</span>
<Button variant="ghost" size="sm" onclick={() => logout()}>退出</Button>
</div>
{/if}
</aside>
<main class="p-4 md:px-8 md:pt-6 max-w-[1360px] w-full min-w-0 pb-16 md:pb-12" aria-label="主内容">
{@render children?.()}
</main>
</div>
{#if mobile}
<Tabs.Root
value={activeTab}
onValueChange={(v) => go(`/${v}`)}
data-testid="mobile-tabbar"
class="fixed left-0 right-0 bottom-0 z-40 flex items-stretch justify-around min-h-14 pb-[env(safe-area-inset-bottom,0)] bg-card/95 border-t border-border backdrop-blur"
aria-label="底部导航"
>
<Tabs.List variant="line" class="w-full max-w-none justify-around p-0 rounded-none h-auto">
{#each tabs as t (t.name)}
<Tabs.Trigger
value={t.name}
data-tab={t.name}
class="flex-1 min-h-14 flex-col gap-0.5 rounded-none text-[0.66rem] data-active:font-semibold"
aria-current={activeTab === t.name ? 'page' : undefined}
>
{#if t.icon === 'project'}
<svg viewBox="0 0 24 24" class="size-5" aria-hidden="true"><rect x="3" y="4" width="18" height="16" rx="2" fill="none" stroke="currentColor" stroke-width="2"/><path d="M3 10h18" stroke="currentColor" stroke-width="2"/></svg>
{:else if t.icon === 'dataset'}
<svg viewBox="0 0 24 24" class="size-5" aria-hidden="true"><ellipse cx="12" cy="6" rx="8" ry="3" fill="none" stroke="currentColor" stroke-width="2"/><path d="M4 6v12c0 1.7 3.6 3 8 3s8-1.3 8-3V6" fill="none" stroke="currentColor" stroke-width="2"/></svg>
{:else if t.icon === 'run'}
<svg viewBox="0 0 24 24" class="size-5" aria-hidden="true"><path d="M4 20v-8" stroke="currentColor" stroke-width="2"/><path d="M9 20V4" stroke="currentColor" stroke-width="2"/><path d="M14 20v-6" stroke="currentColor" stroke-width="2"/><path d="M19 20V9" stroke="currentColor" stroke-width="2"/></svg>
{:else}
<svg viewBox="0 0 24 24" class="size-5" aria-hidden="true"><circle cx="12" cy="8" r="4" fill="none" stroke="currentColor" stroke-width="2"/><path d="M4 21c0-4 4-6 8-6s8 2 8 6" fill="none" stroke="currentColor" stroke-width="2"/></svg>
{/if}
<span>{t.label}</span>
</Tabs.Trigger>
{/each}
</Tabs.List>
</Tabs.Root>
{/if}
|