-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmodel-card.tsx
More file actions
305 lines (276 loc) · 11.1 KB
/
model-card.tsx
File metadata and controls
305 lines (276 loc) · 11.1 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
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
/* eslint-disable @next/next/no-img-element */
/* eslint-disable react/display-name */
import React, { memo, useRef, useState } from 'react';
import { MdCollections } from 'react-icons/md';
import { LazyLoadComponent } from 'react-lazy-load-image-component';
import { useArchitectures } from '../../lib/hooks/use-architectures';
import { useDevicePixelRatio } from '../../lib/hooks/use-device-pixel-ratio';
import { useUpdateModel } from '../../lib/hooks/use-update-model';
import { useUsers } from '../../lib/hooks/use-users';
import { useWebApi } from '../../lib/hooks/use-web-api';
import { joinList } from '../../lib/react-util';
import { Collection, CollectionId, ImageSize, Model, ModelId, PairedThumbnail } from '../../lib/schema';
import { getTextDescription } from '../../lib/text-description';
import { asArray, assertNever, joinClasses } from '../../lib/util';
import { EditableTags } from './editable-tags';
import { Link } from './link';
import style from './model-card.module.scss';
export interface ModelCardProps {
id: ModelId;
model: Model;
lazy?: boolean;
}
export interface CollectionCardProps {
id: CollectionId;
collection: Collection;
preview: Model | undefined;
lazy?: boolean;
}
const EMPTY_SIZE: ImageSize = {
height: 0,
width: 0,
};
function getNaturalSize(image: HTMLImageElement): ImageSize {
return {
height: image.naturalHeight,
width: image.naturalWidth,
};
}
const SideBySideImage = ({ model, image }: { model: Model; image: PairedThumbnail }) => {
const [lrDimensions, setLrDimensions] = useState(image.LRSize ?? EMPTY_SIZE);
const [srDimensions, setSrDimensions] = useState(image.SRSize ?? EMPTY_SIZE);
const maxHeight = Math.max(lrDimensions.height, srDimensions.height);
const maxWidth = Math.max(lrDimensions.width, srDimensions.width);
const lrRef = useRef<HTMLImageElement>(null);
const srRef = useRef<HTMLImageElement>(null);
const dpr = useDevicePixelRatio();
// The goal of this scale is to ensure that the image is rendered as an integer scale (e.g. 100%, 200%, 300%).
// This is necessary to prevent scaling artifacts. Such artifacts are especially noticeable for 1x models.
// Here is how the scale is calculated:
// 1. `1/dpr` scales the image such that 1px in the image is 1px on the screen.
// 2. `Math.round(dpr + 0.16)` rounds the dpr to the nearest integer. Importantly, it rounds .35 up.
// This guarantees that we show at most 1.35x the original image size.
// 3. `Math.max(1, ...)` ensures that the scale is at least 1. A scale of 0 would cause the image to disappear.
const scale = (1 / dpr) * Math.max(1, Math.round(dpr + 0.16));
return (
<div className="flex h-full w-full">
<div className="relative flex h-full w-1/2 content-center overflow-hidden align-middle">
<img
alt={model.name}
className="rendering-pixelated absolute top-1/3 left-1/2 z-0 m-auto object-cover object-center"
loading="lazy"
ref={lrRef}
src={image.LR}
style={{
height: `${maxHeight}px`,
width: `${maxWidth}px`,
transform: `translate(-50%, -50%) scale(${scale})`,
}}
onLoad={(e) => {
setLrDimensions(getNaturalSize(e.target as HTMLImageElement));
}}
/>
</div>
<div className="relative flex h-full w-1/2 content-center overflow-hidden align-middle">
<img
alt={model.name}
className="rendering-pixelated absolute top-1/3 left-1/2 z-0 m-auto object-cover object-center"
loading="lazy"
ref={srRef}
src={image.SR}
style={{
height: `${maxHeight}px`,
width: `${maxWidth}px`,
transform: `translate(-50%, -50%) scale(${scale})`,
}}
onLoad={(e) => {
setSrDimensions(getNaturalSize(e.target as HTMLImageElement));
}}
/>
</div>
</div>
);
};
const getModelCardImageComponent = (model: Model | undefined) => {
const image = model?.thumbnail ?? model?.images[0];
if (!model || !image) {
return <div className="margin-auto z-0 w-full text-center">No Image</div>;
}
switch (image.type) {
case 'paired': {
return (
<SideBySideImage
image={image}
model={model}
/>
);
}
case 'standalone': {
const imageSrc = image.url;
return (
<img
alt={model.name}
className="margin-auto z-0 h-full w-full object-cover"
loading="lazy"
src={imageSrc}
/>
);
}
default:
return assertNever(image);
}
};
// eslint-disable-next-line react/display-name
const ModelCardContent = memo(({ id, model }: ModelCardProps) => {
const { userData } = useUsers();
const { archData } = useArchitectures();
const { webApi, editMode } = useWebApi();
const { updateModelProperty } = useUpdateModel(webApi, id);
const description = getTextDescription(model);
const isPaired = model.images[0]?.type === 'paired' && !editMode;
return (
<div className={style.inner}>
{/* Arch tag on image */}
<div className={style.topTags}>
<AccentTag>{archData.get(model.architecture)?.name ?? 'Unknown'}</AccentTag>
<AccentTag>{model.scale}x</AccentTag>
</div>
<Link
className={joinClasses(style.thumbnail, isPaired && style.paired, 'bg-fade-300 dark:bg-fade-700 ')}
href={`/models/${id}`}
tabIndex={-1}
>
{getModelCardImageComponent(model)}
</Link>
<div className={joinClasses(style.details, isPaired && style.paired)}>
<Link
className={`${style.name} block text-xl font-bold text-gray-800 dark:text-gray-100`}
href={`/models/${id}`}
>
{model.name}
</Link>
<div className="text-sm text-gray-600 dark:text-gray-400">
{'by '}
{joinList(
asArray(model.author).map((userId) => (
<Link
className="font-bold text-accent-600 dark:text-accent-400"
href={`/users/${userId}`}
key={userId}
>
{userData.get(userId)?.name ?? `unknown user:${userId}`}
</Link>
))
)}
</div>
{/* Description */}
<div className="mb-2 mt-1 text-sm text-gray-600 line-clamp-3 dark:text-gray-400">{description}</div>
{/* Tags */}
<div className="flex flex-row flex-wrap gap-1 text-xs">
<EditableTags
readonly={!editMode}
tags={model.tags}
onChange={(tags) => updateModelProperty('tags', tags)}
/>
</div>
</div>
</div>
);
});
// eslint-disable-next-line react/display-name
const CollectionCardContent = memo(({ id, collection, preview }: CollectionCardProps) => {
const { userData } = useUsers();
const isPaired = preview?.images[0]?.type === 'paired';
return (
<div className={style.inner}>
{/* Arch tag on image */}
<div className={style.topTags}>
<MdCollections
className="rounded-lg bg-black bg-opacity-40 p-1 text-white"
size="1.5rem"
/>
</div>
<Link
className={joinClasses(style.thumbnail, isPaired && style.paired, 'bg-fade-300 dark:bg-fade-700 ')}
href={`/collections/${id}`}
tabIndex={-1}
>
{getModelCardImageComponent(preview)}
</Link>
<div className={joinClasses(style.details, isPaired && style.paired)}>
<Link
className={`${style.name} block text-xl font-bold text-gray-800 dark:text-gray-100`}
href={`/collections/${id}`}
>
{collection.name}
</Link>
<div className="text-sm text-gray-600 dark:text-gray-400">
{'by '}
{joinList(
asArray(collection.author).map((userId) => (
<Link
className="font-bold text-accent-600 dark:text-accent-400"
href={`/users/${userId}`}
key={userId}
>
{userData.get(userId)?.name ?? `unknown user:${userId}`}
</Link>
))
)}
</div>
{/* Description */}
<div className="mb-2 mt-1 text-sm text-gray-600 line-clamp-3 dark:text-gray-400">
{collection.description}
</div>
</div>
</div>
);
});
const useMakeLazyCard = (lazy: boolean, card: JSX.Element) => {
const { editMode } = useWebApi();
const inner = (
<div
className={joinClasses(
style.modelCard,
!editMode && style.overflowHidden,
'border-gray-300 bg-white shadow-lg hover:shadow-2xl dark:border-gray-700 dark:bg-fade-900'
)}
>
{card}
</div>
);
if (!lazy) return inner;
return (
<LazyLoadComponent
placeholder={
<div
className={`${style.modelCard} border-gray-300 bg-white shadow-lg hover:shadow-2xl dark:border-gray-700 dark:bg-fade-900`}
/>
}
>
{inner}
</LazyLoadComponent>
);
};
export const ModelCard = memo(({ id, model, lazy = false }: ModelCardProps) => {
return useMakeLazyCard(
lazy,
<ModelCardContent
id={id}
model={model}
/>
);
});
export const CollectionCard = memo(({ id, collection, preview, lazy = false }: CollectionCardProps) => {
return useMakeLazyCard(
lazy,
<CollectionCardContent
collection={collection}
id={id}
preview={preview}
/>
);
});
function AccentTag({ children }: React.PropsWithChildren<unknown>) {
return <div className={`${style.tagBase} bg-accent-600 text-sm text-gray-100`}>{children}</div>;
}