-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathindex.tsx
362 lines (328 loc) · 14.4 KB
/
index.tsx
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import React, { Fragment, useState } from 'react'
import Image from 'next/image'
import { InferenceEngine } from '@janhq/core'
import { Button, Input, Progress, ScrollArea } from '@janhq/joi'
import { useClickOutside } from '@janhq/joi'
import { useAtomValue, useSetAtom } from 'jotai'
import { SearchIcon, DownloadCloudIcon } from 'lucide-react'
import { twMerge } from 'tailwind-merge'
import LogoMark from '@/containers/Brand/Logo/Mark'
import CenterPanelContainer from '@/containers/CenterPanelContainer'
import ProgressCircle from '@/containers/Loader/ProgressCircle'
import ModelLabel from '@/containers/ModelLabel'
import { MainViewState } from '@/constants/screens'
import useDownloadModel from '@/hooks/useDownloadModel'
import { modelDownloadStateAtom } from '@/hooks/useDownloadState'
import { formatDownloadPercentage, toGibibytes } from '@/utils/converter'
import { manualRecommendationModel } from '@/utils/model'
import {
getLogoEngine,
getTitleByEngine,
isLocalEngine,
} from '@/utils/modelEngine'
import { mainViewStateAtom } from '@/helpers/atoms/App.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import {
configuredModelsAtom,
getDownloadingModelAtom,
} from '@/helpers/atoms/Model.atom'
import { selectedSettingAtom } from '@/helpers/atoms/Setting.atom'
type Props = {
isShowStarterScreen?: boolean
}
const OnDeviceStarterScreen = ({ isShowStarterScreen }: Props) => {
const [searchValue, setSearchValue] = useState('')
const [isOpen, setIsOpen] = useState(Boolean(searchValue.length))
const downloadingModels = useAtomValue(getDownloadingModelAtom)
const { downloadModel } = useDownloadModel()
const downloadStates = useAtomValue(modelDownloadStateAtom)
const setSelectedSetting = useSetAtom(selectedSettingAtom)
const engines = useAtomValue(installedEnginesAtom)
const configuredModels = useAtomValue(configuredModelsAtom)
const setMainViewState = useSetAtom(mainViewStateAtom)
const featuredModel = configuredModels.filter((x) => {
const manualRecommendModel = configuredModels.filter((x) =>
manualRecommendationModel.includes(x.id)
)
if (manualRecommendModel.length === 2) {
return (
x.id === manualRecommendationModel[0] ||
x.id === manualRecommendationModel[1]
)
} else {
return (
x.metadata?.tags?.includes('Featured') && x.metadata?.size < 5000000000
)
}
})
const remoteModel = configuredModels.filter(
(x) => !isLocalEngine(engines, x.engine)
)
const filteredModels = configuredModels.filter((model) => {
return (
isLocalEngine(engines, model.engine) &&
model.name.toLowerCase().includes(searchValue.toLowerCase())
)
})
const remoteModelEngine = remoteModel.map((x) => x.engine)
const groupByEngine = remoteModelEngine.filter(function (item, index) {
if (remoteModelEngine.indexOf(item) === index) return item
})
const itemsPerRow = 5
const getRows = (array: string[], itemsPerRow: number) => {
const rows = []
for (let i = 0; i < array.length; i += itemsPerRow) {
rows.push(array.slice(i, i + itemsPerRow))
}
return rows
}
const rows = getRows(
groupByEngine.sort((a, b) => a.localeCompare(b)),
itemsPerRow
)
const refDropdown = useClickOutside(() => setIsOpen(false))
const [visibleRows, setVisibleRows] = useState(1)
return (
<CenterPanelContainer isShowStarterScreen={isShowStarterScreen}>
<ScrollArea className="flex h-full w-full items-center">
<div className="relative mt-4 flex h-full w-full flex-col items-center justify-center">
<div className="mx-auto flex h-full w-3/4 flex-col items-center justify-center py-16 text-center">
<LogoMark
className="mx-auto mb-4 animate-wave"
width={48}
height={48}
/>
<h1 className="text-base font-medium">Select a model to start</h1>
<div className="mt-6 w-[320px] md:w-[400px]">
<Fragment>
<div className="relative" ref={refDropdown}>
<Input
value={searchValue}
onFocus={() => setIsOpen(true)}
onChange={(e) => {
setSearchValue(e.target.value)
}}
placeholder="Search..."
prefixIcon={<SearchIcon size={16} />}
/>
<div
className={twMerge(
'absolute left-0 top-10 z-20 max-h-[240px] w-full overflow-x-auto rounded-lg border border-[hsla(var(--app-border))] bg-[hsla(var(--app-bg))]',
!isOpen ? 'invisible' : 'visible'
)}
>
{!filteredModels.length ? (
<div className="p-3 text-center">
<p className="line-clamp-1 text-[hsla(var(--text-secondary))]">
No Result Found
</p>
</div>
) : (
filteredModels.map((model) => {
const isDownloading = downloadingModels.some(
(md) => md === model.id
)
return (
<div
key={model.id}
className="flex items-center justify-between gap-4 px-3 py-2 hover:bg-[hsla(var(--dropdown-menu-hover-bg))]"
>
<div className="flex items-center gap-2">
<p
className={twMerge('line-clamp-1')}
title={model.name}
>
{model.name}
</p>
<ModelLabel metadata={model.metadata} compact />
</div>
<div className="flex items-center gap-2 text-[hsla(var(--text-tertiary))]">
<span className="font-medium">
{toGibibytes(model.metadata?.size)}
</span>
{!isDownloading ? (
<DownloadCloudIcon
size={18}
className="cursor-pointer text-[hsla(var(--app-link))]"
onClick={() =>
downloadModel(
model.sources[0].url,
model.id,
model.name
)
}
/>
) : (
Object.values(downloadStates)
.filter((x) => x.modelId === model.id)
.map((item) => (
<ProgressCircle
key={item.modelId}
percentage={
formatDownloadPercentage(
item?.percent,
{
hidePercentage: true,
}
) as number
}
size={100}
/>
))
)}
</div>
</div>
)
})
)}
</div>
</div>
<div className="mt-6 flex items-center justify-between">
<h2 className="text-[hsla(var(--text-secondary))]">
On-device Models
</h2>
<p
className="cursor-pointer text-sm text-[hsla(var(--text-secondary))]"
onClick={() => {
setMainViewState(MainViewState.Hub)
}}
>
See All
</p>
</div>
{featuredModel.slice(0, 2).map((featModel) => {
const isDownloading = downloadingModels.some(
(md) => md === featModel.id
)
return (
<div
key={featModel.id}
className="my-2 flex items-start justify-between gap-2 border-b border-[hsla(var(--app-border))] pb-4 pt-1 last:border-none"
>
<div className="w-full text-left">
<h6 className="mt-1.5 font-medium">{featModel.name}</h6>
</div>
{isDownloading ? (
<div className="flex w-full flex-col items-end gap-2">
{Object.values(downloadStates)
.filter((x) => x.modelId === featModel.id)
.map((item, i) => (
<div
className="mt-1.5 flex w-full items-center gap-2"
key={i}
>
<Progress
className="w-full"
value={
formatDownloadPercentage(item?.percent, {
hidePercentage: true,
}) as number
}
/>
<div className="flex items-center justify-between gap-x-2">
<div className="flex gap-x-2">
<span className="font-medium text-[hsla(var(--primary-bg))]">
{formatDownloadPercentage(item?.percent)}
</span>
</div>
</div>
</div>
))}
<span className="text-[hsla(var(--text-secondary))]">
{toGibibytes(featModel.metadata?.size)}
</span>
</div>
) : (
<div className="flex flex-col items-end justify-end gap-2">
<Button
theme="ghost"
className="!bg-[hsla(var(--secondary-bg))]"
onClick={() =>
downloadModel(
featModel.sources[0].url,
featModel.id,
featModel.name
)
}
>
Download
</Button>
<span className="text-[hsla(var(--text-secondary))]">
{toGibibytes(featModel.metadata?.size)}
</span>
</div>
)}
</div>
)
})}
<div className="mb-2 mt-8 flex items-center justify-between">
<h2 className="text-[hsla(var(--text-secondary))]">
Cloud Models
</h2>
</div>
<div className="flex flex-col justify-center gap-6">
{rows.slice(0, visibleRows).map((row, rowIndex) => {
return (
<div
key={rowIndex}
className="my-2 flex items-center gap-4 md:gap-10"
>
{row
.filter(
(e) =>
engines?.[e as InferenceEngine]?.[0]?.type ===
'remote'
)
.map((remoteEngine) => {
const engineLogo = getLogoEngine(
remoteEngine as InferenceEngine
)
return (
<div
className="flex cursor-pointer flex-col items-center justify-center gap-4"
key={remoteEngine}
onClick={() => {
setMainViewState(MainViewState.Settings)
setSelectedSetting(
remoteEngine as InferenceEngine
)
}}
>
{engineLogo && (
<Image
width={48}
height={48}
src={engineLogo}
alt="Engine logo"
className="h-10 w-10 flex-shrink-0"
/>
)}
<p className="font-medium">
{getTitleByEngine(
remoteEngine as InferenceEngine
)}
</p>
</div>
)
})}
</div>
)
})}
</div>
{visibleRows < rows.length && (
<button
onClick={() => setVisibleRows(visibleRows + 1)}
className="mt-4 text-[hsla(var(--text-secondary))]"
>
See More
</button>
)}
</Fragment>
</div>
</div>
</div>
</ScrollArea>
</CenterPanelContainer>
)
}
export default OnDeviceStarterScreen