-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreview.tsx
More file actions
496 lines (468 loc) · 11.5 KB
/
Preview.tsx
File metadata and controls
496 lines (468 loc) · 11.5 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import CloseIcon from '@mui/icons-material/Close';
import FavoriteIcon from '@mui/icons-material/Favorite';
import HeartBrokenIcon from '@mui/icons-material/HeartBroken';
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
import ZoomInIcon from '@mui/icons-material/ZoomIn';
import {
Box,
Button,
CircularProgress,
Container,
Dialog,
DialogActions,
DialogContent,
Divider,
Tooltip,
Typography,
} from '@mui/material';
import CardMedia from '@mui/material/CardMedia';
import { useQuery } from '@tanstack/react-query';
import { getPhoto } from 'api/api';
import { useEffect, useState } from 'react';
interface PreviewProps {
isOpen: boolean;
media: {
id: string;
dateCreated: string;
mediaType?: string;
isFavorite?: boolean;
dateMediaTaken?: string;
dateMediaCreated?: string;
filename?: string;
sizeInBytes?: number;
width?: number;
height?: number;
};
handlePrev: () => void;
handleNext: () => void;
disablePrevButton: boolean;
disableNextButton: boolean;
onClose: () => void;
handleSingleFavorites?: (id: string, actionAdd: boolean) => void;
}
/** Format bytes → human-readable string (e.g. "3.2 MB") */
const formatBytes = (bytes: number): string => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
/** Format an ISO date string → readable local date+time */
const formatDate = (iso: string): string => {
try {
return new Date(iso).toLocaleString(undefined, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
} catch {
return iso;
}
};
// ── Info pane row ─────────────────────────────────────────────────────────────
interface InfoRowProps {
label: string;
value: string;
}
const InfoRow = ({ label, value }: InfoRowProps) => (
<Box sx={{ py: 1.5 }}>
<Typography
variant="caption"
sx={{
color: 'text.secondary',
textTransform: 'uppercase',
letterSpacing: '0.08em',
fontSize: '0.68rem',
}}
>
{label}
</Typography>
<Typography variant="body2" sx={{ mt: 0.3, wordBreak: 'break-all' }}>
{value}
</Typography>
</Box>
);
// ── Main component ─────────────────────────────────────────────────────────────
const Preview = ({
isOpen,
media,
onClose,
handlePrev,
handleNext,
disablePrevButton,
disableNextButton,
handleSingleFavorites,
}: PreviewProps) => {
const { data: url, isLoading } = useQuery({
queryKey: ['getPhoto', media.id],
queryFn: () => getPhoto(media.id),
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const date = new Date(media.dateCreated).toDateString();
const [zoom, setZoom] = useState(false);
const [infoOpen, setInfoOpen] = useState(false);
const handleZoomIn = () => {
setZoom(true);
};
const handleZoomOut = () => {
setZoom(false);
};
const handleFavorite = () => {
handleSingleFavorites?.(media.id, !media.isFavorite);
};
// Close info pane when media changes
useEffect(() => {
setInfoOpen(false);
}, [media.id]);
useEffect(() => {
const handleKeyLeft = (e: KeyboardEvent) => {
if (e.key === 'ArrowLeft') {
handlePrev();
}
};
const handleKeyRight = (e: KeyboardEvent) => {
if (e.key === 'ArrowRight' && !disableNextButton) {
handleNext();
}
};
document.addEventListener('keydown', handleKeyLeft);
document.addEventListener('keydown', handleKeyRight);
return () => {
document.removeEventListener('keydown', handleKeyLeft);
document.removeEventListener('keydown', handleKeyRight);
};
}, [media]);
// Shared toolbar button style
const toolbarBtnSx = {
minWidth: 32,
p: '4px',
color: 'gray',
'&:hover': {
backgroundColor: 'transparent',
color: 'currentColor',
},
};
return (
<>
{zoom && (
<Container>
<Box
onClick={handleZoomOut}
sx={{
textAlign: 'center',
position: 'absolute',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
zIndex: 10000,
width: '100%',
height: '100%',
maxWidth: '100%',
maxHeight: '100%',
overflow: 'auto',
cursor: 'zoom-out',
backgroundColor: 'rgba(0, 0, 0)',
}}
>
{ media.mediaType !== "video" && (<img src={url} alt="image" />)}
</Box>
</Container>
)}
<Dialog open={isOpen} onClose={onClose} fullScreen>
{/* ── Top toolbar ── */}
<Box
sx={{
width: '100%',
display: 'flex',
justifyContent: 'flex-end', // align all buttons to the right
alignItems: 'center',
px: '5px',
gap: 0.5, // reduce space between icons
}}
>
{/* HeartBroken / Favorite */}
{handleSingleFavorites && (
<Button onClick={handleFavorite} disableRipple sx={toolbarBtnSx}>
{media.isFavorite ? (
<Tooltip title="Remove from Favorites">
<HeartBrokenIcon />
</Tooltip>
) : (
<Tooltip title="Add to Favorites">
<FavoriteIcon />
</Tooltip>
)}
</Button>
)}
{/* ── Info button (between HeartBroken and ZoomIn) ── */}
<Button
onClick={() => setInfoOpen((prev) => !prev)}
disableRipple
sx={{
...toolbarBtnSx,
color: infoOpen ? 'primary.main' : 'gray',
}}
>
<Tooltip title="Info">
<InfoOutlinedIcon />
</Tooltip>
</Button>
{/* Zoom In */}
{media.mediaType !== 'video' && (
<Button onClick={handleZoomIn} disableRipple sx={toolbarBtnSx}>
<Tooltip title="Zoom In">
<ZoomInIcon />
</Tooltip>
</Button>
)}
{/* Close */}
<Button onClick={onClose} disableRipple sx={toolbarBtnSx}>
<Tooltip title="Close Preview">
<CloseIcon />
</Tooltip>
</Button>
</Box>
{/* ── Main content area ── */}
<DialogContent
sx={{
p: 0,
overflow: 'hidden',
position: 'relative',
display: 'flex',
}}
>
{/* Media area — shrinks when info pane opens */}
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
height: '100%',
flex: 1,
minWidth: 0,
transition: 'all 0.3s ease',
}}
>
<Button
onClick={handlePrev}
disableRipple
disabled={disablePrevButton}
sx={{
flex: 1,
height: '100%',
color: 'gray',
'&:hover': {
backgroundColor: 'transparent',
color: 'currentColor',
},
}}
>
<Tooltip title="Previous">
<ChevronLeftIcon
sx={{
position: 'absolute',
left: '10px',
top: '50%',
transform: 'translateY(-50%)',
}}
fontSize="large"
/>
</Tooltip>
</Button>
<Box
sx={{
height: '100%',
display: 'flex',
justifyContent: 'center',
flex: 1,
minWidth: 0,
}}
>
{isLoading ? (
<CircularProgress sx={{ my: 'auto' }} />
) : (
<Box
onClick={media.mediaType !== 'video' ? handleZoomIn : undefined}
sx={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
position: 'relative',
}}
>
{media.mediaType !== 'video' && (
<img
src={url}
alt="image"
style={{
objectFit: 'contain',
maxWidth: '100%',
maxHeight: '100%',
cursor: 'zoom-in',
}}
/>
)}
{media.mediaType === 'video' && (
<CardMedia
component='video'
src={url}
sx={{
display: 'flex',
objectFit: 'contain',
maxWidth: '100%',
maxHeight: '100%'
}}
controls
/>)}
</Box>
)}
</Box>
<Button
onClick={handleNext}
disableRipple
disabled={disableNextButton}
sx={{
flex: 1,
height: '100%',
color: 'gray',
'&:hover': {
backgroundColor: 'transparent',
color: 'currentColor',
},
}}
>
<Tooltip title="Next">
<ChevronRightIcon
fontSize="large"
sx={{
position: 'absolute',
right: '10px',
top: '50%',
transform: 'translateY(-50%)',
}}
/>
</Tooltip>
</Button>
</Box>
{/* ── Sliding Info Pane — absolutely positioned, slides in over the right edge ── */}
<Box
sx={{
position: 'absolute',
top: 0,
right: 0,
height: '100%',
width: 300,
transform: infoOpen ? 'translateX(0)' : 'translateX(100%)',
transition: 'transform 0.3s ease',
borderLeft: '1px solid',
borderColor: 'divider',
bgcolor: 'background.paper',
boxSizing: 'border-box',
px: 2,
py: 2,
overflowY: 'auto',
overflowX: 'hidden',
zIndex: 10,
}}
>
{/* Pane header */}
<Box
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
mb: 1,
}}
>
<Typography variant="subtitle1" fontWeight={600}>
Info
</Typography>
<Button
onClick={() => setInfoOpen(false)}
disableRipple
sx={{
minWidth: 32,
p: '4px',
color: 'gray',
'&:hover': { backgroundColor: 'transparent' },
}}
>
<CloseIcon fontSize="small" />
</Button>
</Box>
<Divider sx={{ mb: 1 }} />
{/* Info rows — only rendered when data is present */}
{media.filename && (
<InfoRow label="Filename" value={media.filename} />
)}
{media.dateMediaTaken && (
<>
<InfoRow
label="Date Taken"
value={formatDate(media.dateMediaTaken)}
/>
<Divider />
</>
)}
{media.dateMediaCreated && (
<>
<InfoRow
label="Date Created"
value={formatDate(media.dateMediaCreated)}
/>
<Divider />
</>
)}
{(media.width || media.height) && (
<>
<InfoRow
label="Dimensions"
value={
[
media.width && `${media.width}`,
media.height && `${media.height}`,
]
.filter(Boolean)
.join(' × ') + ' px'
}
/>
<Divider />
</>
)}
{media.sizeInBytes != null && (
<InfoRow
label="File Size"
value={formatBytes(media.sizeInBytes)}
/>
)}
</Box>
</DialogContent>
<DialogActions>
<Box
sx={{
width: '100%',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
px: 2,
}}
>
{/* <Typography color="text.secondary" fontSize={'small'}>
Date Created: {date}
</Typography> */}
</Box>
</DialogActions>
</Dialog>
</>
);
};
export default Preview;