-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathHeader.tsx
More file actions
139 lines (129 loc) · 4.17 KB
/
Header.tsx
File metadata and controls
139 lines (129 loc) · 4.17 KB
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
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { LuCog, LuMenu, LuSquarePen } from 'react-icons/lu';
import { useNavigate } from 'react-router';
import { Button } from '../components/ui/button';
import { useAppContext } from '../context/app';
import { useChatContext } from '../context/chat';
import { useInferenceContext } from '../context/inference';
import { Dropdown } from './common';
import { Label } from './ui/label';
export default function Header() {
const navigate = useNavigate();
const { t } = useTranslation();
const {
config,
config: { model },
saveConfig,
showSettings,
} = useAppContext();
const { models } = useInferenceContext();
const { viewingChat } = useChatContext();
const currConv = useMemo(() => viewingChat?.conv ?? null, [viewingChat]);
const title = useMemo(
() =>
showSettings
? t('header.title.settings')
: currConv
? currConv.name
: t('header.title.noChat'),
[t, currConv, showSettings]
);
const selectedModel = useMemo(() => {
const selectedModel = models.find((m) => m.id === model);
return selectedModel ? selectedModel.name : <s>{model}</s>;
}, [models, model]);
return (
<header className="flex flex-col gap-2 justify-center max-md:pb-2 md:py-2 sticky top-0 z-10">
<section className="flex flex-row items-center xl:hidden">
{/* open sidebar button */}
<Label variant="btn-ghost" size="icon" htmlFor="toggle-drawer">
<LuMenu className="lucide h-5 w-5" />
</Label>
{/* spacer */}
<Label
variant="fake-btn"
className="grow font-medium truncate px-4"
aria-label={title}
role="button"
onClick={() => {
if (showSettings) return;
if (currConv) navigate(`/chat/${currConv.id}`);
else navigate('/');
}}
>
{title}
</Label>
{/* new conversation button */}
<Button
variant="ghost"
size="icon-rounded"
onClick={() => navigate('/')}
title={t('header.buttons.newConv')}
aria-label={t('header.ariaLabels.newConv')}
>
<LuSquarePen className="lucide w-5 h-5" />
</Button>
</section>
{showSettings && (
<section className="flex items-center max-xl:hidden">
<Label
className="font-medium truncate text-center px-4"
aria-label={title}
>
{title}
</Label>
</section>
)}
{!showSettings && (
<section className="flex flex-row items-center">
{/* model information */}
<Dropdown
className="ml-2 px-1 xl:px-4 py-0"
entity="Model"
options={models.map((model) => ({
value: model.id,
label: model.name,
}))}
filterable={true}
hideChevron={models.length < 2}
align="start"
currentValue={
<span className="max-w-64 sm:max-w-80 truncate text-nowrap font-semibold">
{selectedModel}
</span>
}
renderOption={(option) => (
<span className="max-w-64 sm:max-w-80 truncate text-nowrap">
{option.label}
</span>
)}
isSelected={(option) => model === option.value}
onSelect={(option) =>
saveConfig({
...config,
model: option.value,
})
}
/>
{/* spacer */}
<div className="grow"></div>
{/* action buttons (top right) */}
<div className="flex items-center">
<Button
variant="ghost"
size="icon-rounded"
className="max-xl:hidden"
title={t('header.buttons.settings')}
aria-label={t('header.ariaLabels.settings')}
onClick={() => navigate('/settings')}
>
{/* settings button */}
<LuCog className="lucide w-5 h-5" />
</Button>
</div>
</section>
)}
</header>
);
}