@prenta/admin 2.11.1 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/__tests__/components/modal.render.test.js +30 -1
  3. package/dist/__tests__/components/modal.render.test.js.map +1 -1
  4. package/dist/__tests__/lib/media-download.test.d.ts +2 -0
  5. package/dist/__tests__/lib/media-download.test.d.ts.map +1 -0
  6. package/dist/__tests__/lib/media-download.test.js +14 -0
  7. package/dist/__tests__/lib/media-download.test.js.map +1 -0
  8. package/dist/__tests__/views/media-browser.render.test.d.ts +2 -0
  9. package/dist/__tests__/views/media-browser.render.test.d.ts.map +1 -0
  10. package/dist/__tests__/views/media-browser.render.test.js +270 -0
  11. package/dist/__tests__/views/media-browser.render.test.js.map +1 -0
  12. package/dist/__tests__/views/media-grid.test.d.ts +2 -0
  13. package/dist/__tests__/views/media-grid.test.d.ts.map +1 -0
  14. package/dist/__tests__/views/media-grid.test.js +31 -0
  15. package/dist/__tests__/views/media-grid.test.js.map +1 -0
  16. package/dist/components/MediaLightbox.d.ts +22 -0
  17. package/dist/components/MediaLightbox.d.ts.map +1 -0
  18. package/dist/components/MediaLightbox.js +38 -0
  19. package/dist/components/MediaLightbox.js.map +1 -0
  20. package/dist/components/ui/ConfirmDialog.d.ts +3 -1
  21. package/dist/components/ui/ConfirmDialog.d.ts.map +1 -1
  22. package/dist/components/ui/ConfirmDialog.js +2 -2
  23. package/dist/components/ui/ConfirmDialog.js.map +1 -1
  24. package/dist/components/ui/Modal.d.ts +7 -1
  25. package/dist/components/ui/Modal.d.ts.map +1 -1
  26. package/dist/components/ui/Modal.js +4 -2
  27. package/dist/components/ui/Modal.js.map +1 -1
  28. package/dist/lib/dialog-return-focus.d.ts +10 -0
  29. package/dist/lib/dialog-return-focus.d.ts.map +1 -0
  30. package/dist/lib/dialog-return-focus.js +26 -0
  31. package/dist/lib/dialog-return-focus.js.map +1 -0
  32. package/dist/lib/media-download.d.ts +8 -0
  33. package/dist/lib/media-download.d.ts.map +1 -0
  34. package/dist/lib/media-download.js +21 -0
  35. package/dist/lib/media-download.js.map +1 -0
  36. package/dist/prenta-admin.css +1 -1
  37. package/dist/views/MediaBrowser.d.ts +37 -0
  38. package/dist/views/MediaBrowser.d.ts.map +1 -1
  39. package/dist/views/MediaBrowser.js +217 -80
  40. package/dist/views/MediaBrowser.js.map +1 -1
  41. package/dist/views/MediaSeoAudit.d.ts +6 -0
  42. package/dist/views/MediaSeoAudit.d.ts.map +1 -1
  43. package/dist/views/MediaSeoAudit.js +12 -0
  44. package/dist/views/MediaSeoAudit.js.map +1 -1
  45. package/package.json +5 -5
  46. package/src/__tests__/components/modal.render.test.tsx +52 -0
  47. package/src/__tests__/lib/media-download.test.ts +23 -0
  48. package/src/__tests__/views/media-browser.render.test.tsx +325 -0
  49. package/src/__tests__/views/media-grid.test.ts +34 -0
  50. package/src/components/MediaLightbox.tsx +127 -0
  51. package/src/components/ui/ConfirmDialog.tsx +4 -0
  52. package/src/components/ui/Modal.tsx +10 -0
  53. package/src/lib/dialog-return-focus.ts +33 -0
  54. package/src/lib/media-download.ts +18 -0
  55. package/src/views/MediaBrowser.tsx +403 -172
  56. package/src/views/MediaSeoAudit.tsx +11 -0
@@ -7,7 +7,7 @@ import {
7
7
  Search,
8
8
  Trash2,
9
9
  Download,
10
- FileText,
10
+ Maximize2,
11
11
  ArrowUpDown,
12
12
  ArrowUp,
13
13
  ArrowDown,
@@ -33,11 +33,20 @@ import { cmsApi } from '../lib/api.js'
33
33
  import { useVirtualWindow, VirtualTablePadding } from '../components/virtual-window.js'
34
34
  import { FolderTree, type FolderSelection } from '../components/FolderTree.js'
35
35
  import { Button } from '../components/ui/Button.js'
36
+ import { ConfirmDialog } from '../components/ui/ConfirmDialog.js'
37
+ import { MediaLightbox } from '../components/MediaLightbox.js'
38
+ import { mediaDownloadUrl } from '../lib/media-download.js'
36
39
  import { TIER_MEDIA } from '../lib/tiers.js'
37
40
  import { useSheetFocus } from '../lib/sheet-focus.js'
38
41
  import { StatusBadge } from '../components/ui/StatusBadge.js'
39
42
  import { CountChip } from '../components/ui/CountChip.js'
40
- import { hasLowQualityAlt, MediaSeoAudit, type MediaSeoAuditItem } from './MediaSeoAudit.js'
43
+ import { FilterPill } from '../components/ui/FilterPill.js'
44
+ import {
45
+ hasLowQualityAlt,
46
+ MediaSeoAudit,
47
+ scoreBadgeClass,
48
+ type MediaSeoAuditItem,
49
+ } from './MediaSeoAudit.js'
41
50
  import { pickedImages } from '../lib/batch-wizard.js'
42
51
  import { RunWithAi } from './Jobs/RunWithAi.js'
43
52
 
@@ -77,15 +86,35 @@ function isImageMedia(item: MediaItem): boolean {
77
86
  )
78
87
  }
79
88
 
80
- function matchesMediaType(item: MediaItem, filterType: string): boolean {
81
- if (filterType === 'all') return true
82
- if (filterType === 'image') return item.type?.startsWith('image/') || isImageMedia(item)
83
- if (filterType === 'video') return item.type?.startsWith('video/')
84
- if (filterType === 'document')
85
- return !item.type?.startsWith('image/') && !item.type?.startsWith('video/')
86
- return item.type === filterType
89
+ /** Media.html `.type-pills` kinds, disjoint; `document` (Word, Excel, …) is repo-only. */
90
+ export type MediaKind = 'image' | 'video' | 'pdf' | 'svg' | 'document'
91
+ export type MediaTypeFilter = 'all' | MediaKind
92
+
93
+ export function mediaKind(item: Pick<MediaItem, 'type' | 'name' | 'url'>): MediaKind {
94
+ const file = `${item.name ?? ''} ${item.url ?? ''}`
95
+ if (item.type === 'image/svg+xml' || /\.svg(\s|$|\?)/i.test(file)) return 'svg'
96
+ if (item.type?.startsWith('image/') || /\.(avif|gif|jpe?g|png|webp)(\s|$|\?)/i.test(file))
97
+ return 'image'
98
+ if (item.type?.startsWith('video/')) return 'video'
99
+ if (item.type === 'application/pdf' || /\.pdf(\s|$|\?)/i.test(file)) return 'pdf'
100
+ return 'document'
101
+ }
102
+
103
+ function matchesMediaType(item: MediaItem, filter: MediaTypeFilter): boolean {
104
+ return filter === 'all' || mediaKind(item) === filter
87
105
  }
88
106
 
107
+ const TYPE_PILLS: ReadonlyArray<{ value: MediaTypeFilter; label: string }> = [
108
+ { value: 'all', label: 'All' },
109
+ { value: 'image', label: 'Images' },
110
+ { value: 'video', label: 'Video' },
111
+ { value: 'pdf', label: 'PDF' },
112
+ { value: 'svg', label: 'SVG' },
113
+ { value: 'document', label: 'Documents' },
114
+ ]
115
+
116
+ type SortPreset = 'date' | 'name' | 'score' | 'size'
117
+
89
118
  function formatStorage(bytes: number): string {
90
119
  if (bytes <= 0) return '0 B'
91
120
  const units = ['B', 'KB', 'MB', 'GB', 'TB']
@@ -94,13 +123,17 @@ function formatStorage(bytes: number): string {
94
123
  return `${value >= 100 || i === 0 ? Math.round(value) : value.toFixed(1)} ${units[i]}`
95
124
  }
96
125
 
97
- function MediaPreview({ item }: { item: MediaItem }) {
126
+ function mediaAlt(item: MediaItem): string {
127
+ return item.altTag || item.title || item.name || 'Media preview'
128
+ }
129
+
130
+ function MediaPreview({ item, fit = 'cover' }: { item: MediaItem; fit?: 'cover' | 'contain' }) {
98
131
  if (isImageMedia(item)) {
99
132
  return (
100
133
  <img
101
134
  src={item.url}
102
- alt={item.altTag || item.title || item.name || 'Media preview'}
103
- className="h-full w-full object-cover"
135
+ alt={mediaAlt(item)}
136
+ className={`h-full w-full ${fit === 'contain' ? 'object-contain' : 'object-cover'}`}
104
137
  loading="lazy"
105
138
  />
106
139
  )
@@ -109,7 +142,33 @@ function MediaPreview({ item }: { item: MediaItem }) {
109
142
  return <FileImage className="text-muted-foreground h-6 w-6 sm:h-8 sm:w-8" />
110
143
  }
111
144
 
112
- type MediaSortKey = 'name' | 'type' | 'size' | 'date'
145
+ type MediaSortKey = 'name' | 'type' | 'size' | 'date' | 'score'
146
+
147
+ /** The sort select's presets (Media.html `.sort-sel`); list-column sorts that match none read "custom". */
148
+ function sortPresetOf(sort: SortConfig<MediaSortKey> | null): SortPreset | 'custom' {
149
+ if (!sort) return 'date'
150
+ if (sort.key === 'name' && sort.direction === 'asc') return 'name'
151
+ if (sort.key === 'score' && sort.direction === 'desc') return 'score'
152
+ if (sort.key === 'size' && sort.direction === 'desc') return 'size'
153
+ return 'custom'
154
+ }
155
+
156
+ function sortForPreset(preset: SortPreset): SortConfig<MediaSortKey> | null {
157
+ switch (preset) {
158
+ case 'date':
159
+ return null
160
+ case 'name':
161
+ return { key: 'name', direction: 'asc' }
162
+ case 'score':
163
+ return { key: 'score', direction: 'desc' }
164
+ case 'size':
165
+ return { key: 'size', direction: 'desc' }
166
+ default: {
167
+ const unreachable: never = preset
168
+ return unreachable
169
+ }
170
+ }
171
+ }
113
172
 
114
173
  export interface MediaBrowserProps {
115
174
  onNavigate?: (path: string) => void
@@ -122,30 +181,23 @@ export interface MediaBrowserProps {
122
181
  currentPath?: string
123
182
  }
124
183
 
125
- function gridColumnCount(width: number, panelOpen: boolean): number {
126
- if (panelOpen) {
127
- if (width >= 1024) return 4
128
- if (width >= 768) return 3
129
- return 2
130
- }
131
- if (width >= 1536) return 8
132
- if (width >= 1280) return 6
133
- if (width >= 1024) return 5
134
- if (width >= 768) return 4
135
- if (width >= 640) return 3
136
- return 2
137
- }
138
-
139
- function useGridColumns(panelOpen: boolean): number {
140
- const [width, setWidth] = useState(() =>
141
- typeof window === 'undefined' ? 1280 : window.innerWidth,
184
+ // Media.html `.media-grid` / `.media-thumb`. Grid rows are virtualised at a
185
+ // fixed stride, so card height must not depend on card width.
186
+ const MEDIA_CARD_MIN_WIDTH = 220
187
+ const MEDIA_GRID_GAP = 14
188
+ const MEDIA_GRID_PADDING_X = 28
189
+ const MEDIA_CARD_HEIGHT = 172
190
+ export const MEDIA_GRID_ROW_HEIGHT = MEDIA_CARD_HEIGHT + MEDIA_GRID_GAP
191
+ /** `POST /media/bulk-delete` accepts at most this many ids (`BULK_MEDIA_MAX_ITEMS`). */
192
+ const BULK_DELETE_CHUNK = 50
193
+
194
+ /** Columns that fit `auto-fill, minmax(220px, 1fr)` in the grid's content box. */
195
+ export function mediaGridColumns(contentWidth: number): number {
196
+ if (contentWidth <= 0) return 4
197
+ return Math.max(
198
+ 1,
199
+ Math.floor((contentWidth + MEDIA_GRID_GAP) / (MEDIA_CARD_MIN_WIDTH + MEDIA_GRID_GAP)),
142
200
  )
143
- useEffect(() => {
144
- const onResize = () => setWidth(window.innerWidth)
145
- window.addEventListener('resize', onResize)
146
- return () => window.removeEventListener('resize', onResize)
147
- }, [])
148
- return gridColumnCount(width, panelOpen)
149
201
  }
150
202
 
151
203
  function buildMediaApiUrl(folderSel: FolderSelection): string {
@@ -183,10 +235,12 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
183
235
  const [activeTab, setActiveTab] = useState<'library' | 'audit'>('library')
184
236
  const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid')
185
237
  const [searchQuery, setSearchQuery] = useState('')
186
- const [filterType, setFilterType] = useState('all')
238
+ const [filterType, setFilterType] = useState<MediaTypeFilter>('all')
187
239
  const [selectedMedia, setSelectedMedia] = useState<Array<number | string>>([])
188
240
  const [sortConfig, setSortConfig] = useState<SortConfig<MediaSortKey> | null>(null)
189
241
  const [activeItem, setActiveItem] = useState<MediaItem | null>(null)
242
+ const [pendingDelete, setPendingDelete] = useState<Array<number | string> | null>(null)
243
+ const [lightboxId, setLightboxId] = useState<number | string | null>(null)
190
244
 
191
245
  const [editAlt, setEditAlt] = useState('')
192
246
  const [editIsDecorative, setEditIsDecorative] = useState(false)
@@ -216,6 +270,8 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
216
270
  let cmp: number
217
271
  if (sortConfig.key === 'size') {
218
272
  cmp = a.sizeBytes - b.sizeBytes
273
+ } else if (sortConfig.key === 'score') {
274
+ cmp = (a.seoScore ?? 0) - (b.seoScore ?? 0)
219
275
  } else {
220
276
  cmp = String(a[sortConfig.key]).localeCompare(String(b[sortConfig.key]))
221
277
  }
@@ -225,16 +281,70 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
225
281
  return results
226
282
  }, [mediaItems, searchQuery, filterType, sortConfig])
227
283
 
228
- const gridColumns = useGridColumns(activeItem !== null)
284
+ const typeCounts = useMemo(() => {
285
+ const counts: Record<MediaTypeFilter, number> = {
286
+ all: mediaItems.length,
287
+ image: 0,
288
+ video: 0,
289
+ pdf: 0,
290
+ svg: 0,
291
+ document: 0,
292
+ }
293
+ for (const item of mediaItems) counts[mediaKind(item)]++
294
+ return counts
295
+ }, [mediaItems])
296
+
297
+ // The viewer steps through the images on screen. If search or a type pill
298
+ // has hidden the open panel's image, it shows that image alone rather than
299
+ // doing nothing.
300
+ const lightboxItems = useMemo(() => {
301
+ const toViewerItem = (item: MediaItem) => ({
302
+ id: item.id,
303
+ url: item.url,
304
+ name: item.name,
305
+ alt: mediaAlt(item),
306
+ dimensions: item.dimensions,
307
+ })
308
+ const onScreen = filteredAndSorted.filter(isImageMedia).map(toViewerItem)
309
+ if (
310
+ activeItem &&
311
+ isImageMedia(activeItem) &&
312
+ !onScreen.some((item) => item.id === activeItem.id)
313
+ ) {
314
+ return [toViewerItem(activeItem)]
315
+ }
316
+ return onScreen
317
+ }, [filteredAndSorted, activeItem])
318
+
319
+ useEffect(() => {
320
+ if (lightboxId !== null && !lightboxItems.some((item) => item.id === lightboxId)) {
321
+ setLightboxId(null)
322
+ }
323
+ }, [lightboxId, lightboxItems])
324
+
229
325
  const gridScrollRef = useRef<HTMLDivElement>(null)
326
+ const [gridWidth, setGridWidth] = useState(0)
327
+ const gridMounted = viewMode === 'grid' && activeTab === 'library' && filteredAndSorted.length > 0
328
+ useEffect(() => {
329
+ const el = gridScrollRef.current
330
+ if (!gridMounted || !el) return
331
+ const measure = () => setGridWidth(el.clientWidth - MEDIA_GRID_PADDING_X * 2)
332
+ measure()
333
+ if (typeof ResizeObserver === 'undefined') return
334
+ const observer = new ResizeObserver(measure)
335
+ observer.observe(el)
336
+ return () => observer.disconnect()
337
+ }, [gridMounted, loading])
338
+ const gridColumns = mediaGridColumns(gridWidth)
230
339
  const listScrollRef = useRef<HTMLDivElement>(null)
340
+ const focusLibrary = useCallback(() => gridScrollRef.current ?? listScrollRef.current, [])
231
341
  /** The grid's or list's scroll position when a card opened the detail panel. */
232
342
  const savedListScroll = useRef(0)
233
343
  const gridRowCount = gridColumns > 0 ? Math.ceil(filteredAndSorted.length / gridColumns) : 0
234
344
  const gridWindow = useVirtualWindow({
235
345
  count: gridRowCount,
236
346
  scrollRef: gridScrollRef,
237
- estimateSize: 220,
347
+ estimateSize: MEDIA_GRID_ROW_HEIGHT,
238
348
  overscan: 3,
239
349
  })
240
350
  const listWindow = useVirtualWindow({
@@ -349,15 +459,48 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
349
459
  }
350
460
  }
351
461
 
352
- const deleteMedia = async (id: number | string) => {
353
- const res = await cmsApi(`/media/${id}`, { method: 'DELETE' })
354
- if (res.error) {
355
- toast.error(res.error)
462
+ const deleteMedia = async (ids: Array<number | string>) => {
463
+ let deleted: Array<number | string> = []
464
+ let failed = 0
465
+ if (ids.length === 1) {
466
+ const res = await cmsApi(`/media/${ids[0]}`, { method: 'DELETE' })
467
+ if (res.error) {
468
+ toast.error(res.error)
469
+ return
470
+ }
471
+ deleted = ids
356
472
  } else {
357
- toast.success('Media deleted')
358
- if (activeItem?.id === id) closeDetail()
359
- refetch()
473
+ for (let i = 0; i < ids.length; i += BULK_DELETE_CHUNK) {
474
+ const chunk = ids.slice(i, i + BULK_DELETE_CHUNK)
475
+ const res = await cmsApi<{ results: Array<{ id: string; ok: boolean }> }>(
476
+ '/media/bulk-delete',
477
+ { method: 'POST', body: JSON.stringify({ ids: chunk.map(String), confirm: true }) },
478
+ )
479
+ if (res.error || !res.data) {
480
+ failed += chunk.length
481
+ continue
482
+ }
483
+ const ok = new Set(res.data.results.filter((r) => r.ok).map((r) => r.id))
484
+ deleted.push(...chunk.filter((id) => ok.has(String(id))))
485
+ failed += chunk.length - ok.size
486
+ }
487
+ }
488
+
489
+ if (deleted.length > 0) {
490
+ toast.success(deleted.length === 1 ? 'Media deleted' : `${deleted.length} files deleted`)
491
+ }
492
+ if (failed > 0) toast.error(`${failed} file${failed === 1 ? '' : 's'} couldn't be deleted`)
493
+ const gone = new Set(deleted.map(String))
494
+ if (activeItem && gone.has(String(activeItem.id))) {
495
+ closeDetail()
496
+ // The confirm dialog returned focus to the panel's Delete, which this removes.
497
+ requestAnimationFrame(() => {
498
+ const focused = document.activeElement
499
+ if (!focused || focused === document.body || !focused.isConnected) focusLibrary()?.focus()
500
+ })
360
501
  }
502
+ setSelectedMedia((prev) => prev.filter((id) => !gone.has(String(id))))
503
+ if (deleted.length > 0) refetch()
361
504
  }
362
505
 
363
506
  const handleAiGenerate = async (field: 'alt' | 'title' | 'optimize') => {
@@ -415,7 +558,7 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
415
558
  .replace(/[-_]/g, ' ')
416
559
  .trim()
417
560
  if (field === 'alt') {
418
- setEditAlt(base ? `Image of ${base}` : '')
561
+ setEditAlt(base)
419
562
  } else {
420
563
  setEditTitle(base.replace(/\b\w/g, (c) => c.toUpperCase()))
421
564
  }
@@ -541,8 +684,8 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
541
684
  // header), so the card that opened it leaves the screen. Focus moves into
542
685
  // the panel and back when it closes. The hidden scroll box comes back at the
543
686
  // top, and the virtualised window wouldn't follow (no scroll event), so its
544
- // position, saved when a card opens the panel, goes back first. Grid cards
545
- // aren't focusable, so focus lands on the list itself.
687
+ // position, saved when a card opens the panel, goes back first. If the
688
+ // opener is gone (virtualised out), focus lands on the list itself.
546
689
  useSheetFocus(panelOpen, detailPanelRef, {
547
690
  restore: () => {
548
691
  const scroller = gridScrollRef.current ?? listScrollRef.current
@@ -730,58 +873,99 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
730
873
 
731
874
  <div className="mobile:min-h-0 flex min-w-0 flex-1 flex-col">
732
875
  <div className="border-border bg-card mb-4 rounded-lg border">
733
- <div className="flex items-center justify-between p-3">
734
- <div className="flex flex-1 items-center gap-3">
735
- <div className="relative max-w-md flex-1">
736
- <Search
737
- className="text-muted-foreground absolute top-1/2 left-3 h-4 w-4 -translate-y-1/2"
738
- aria-hidden
739
- />
740
- <input
741
- type="search"
742
- placeholder="Search media..."
743
- aria-label="Search media"
744
- value={searchQuery}
745
- onChange={(e) => setSearchQuery(e.target.value)}
746
- className="border-input focus:ring-ring w-full rounded-lg border py-2 pr-3 pl-9 text-sm focus:ring-2 focus:outline-none"
747
- />
748
- </div>
749
- <select
750
- value={filterType}
751
- onChange={(e) => setFilterType(e.target.value)}
752
- aria-label="Filter by type"
753
- className="border-input focus:ring-ring rounded-lg border px-3 py-2 text-sm focus:ring-2 focus:outline-none"
754
- >
755
- <option value="all">All Types</option>
756
- <option value="image">Images</option>
757
- <option value="video">Videos</option>
758
- <option value="document">Documents</option>
759
- </select>
876
+ {/* Media.html `.mc-toolbar`. */}
877
+ <div className="flex flex-wrap items-center gap-2 px-3 py-3">
878
+ <label className="flex max-w-[280px] min-w-[180px] flex-1 items-center gap-[7px] rounded-[8px] border border-[var(--bdr)] bg-[var(--bg)] px-[11px] py-[6px] text-[var(--muted)] transition-colors focus-within:border-[var(--acc-m)]">
879
+ <Search size={13} aria-hidden />
880
+ <input
881
+ type="search"
882
+ placeholder="Search files…"
883
+ aria-label="Search files"
884
+ value={searchQuery}
885
+ onChange={(e) => setSearchQuery(e.target.value)}
886
+ className="w-full bg-transparent text-[13px] text-[var(--txt)] outline-none placeholder:text-[var(--muted)]"
887
+ />
888
+ {searchQuery && (
889
+ <button
890
+ type="button"
891
+ onClick={() => setSearchQuery('')}
892
+ aria-label="Clear search"
893
+ className="shrink-0 leading-none text-[var(--muted)] hover:text-[var(--txt)]"
894
+ >
895
+ <X size={12} aria-hidden />
896
+ </button>
897
+ )}
898
+ </label>
899
+ <div className="flex flex-wrap gap-[3px]" role="group" aria-label="Filter by type">
900
+ {TYPE_PILLS.filter(
901
+ (pill) =>
902
+ pill.value !== 'document' ||
903
+ typeCounts.document > 0 ||
904
+ filterType === 'document',
905
+ ).map((pill) => (
906
+ <FilterPill
907
+ key={pill.value}
908
+ active={filterType === pill.value}
909
+ count={typeCounts[pill.value]}
910
+ onClick={() => setFilterType(pill.value)}
911
+ >
912
+ {pill.label}
913
+ </FilterPill>
914
+ ))}
760
915
  </div>
916
+ <select
917
+ aria-label="Sort media"
918
+ value={sortPresetOf(sortConfig)}
919
+ onChange={(e) => {
920
+ const preset = e.target.value
921
+ if (preset === 'custom') return
922
+ setSortConfig(sortForPreset(preset as SortPreset))
923
+ }}
924
+ className="cursor-pointer rounded-[7px] border border-[var(--bdr)] bg-[var(--bg)] px-2 py-[5px] text-[12px] text-[var(--sub)] focus-visible:ring-2 focus-visible:ring-[var(--ring)] focus-visible:outline-none"
925
+ >
926
+ <option value="date">Recent first</option>
927
+ <option value="name">Name A–Z</option>
928
+ <option value="score">SEO score</option>
929
+ <option value="size">File size</option>
930
+ {sortPresetOf(sortConfig) === 'custom' && (
931
+ <option value="custom" disabled>
932
+ List column
933
+ </option>
934
+ )}
935
+ </select>
761
936
  <div
762
- className="bg-muted flex items-center gap-1 rounded-lg p-1"
937
+ className="flex overflow-hidden rounded-[7px] border border-[var(--bdr)]"
763
938
  role="group"
764
939
  aria-label="Media view"
765
940
  >
766
- <button
767
- type="button"
768
- onClick={() => setViewMode('grid')}
769
- aria-label="Grid view"
770
- aria-pressed={viewMode === 'grid'}
771
- className={`rounded p-1.5 transition-colors ${viewMode === 'grid' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`}
772
- >
773
- <Grid3x3 className="h-4 w-4" aria-hidden />
774
- </button>
775
- <button
776
- type="button"
777
- onClick={() => setViewMode('list')}
778
- aria-label="List view"
779
- aria-pressed={viewMode === 'list'}
780
- className={`rounded p-1.5 transition-colors ${viewMode === 'list' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`}
781
- >
782
- <List className="h-4 w-4" aria-hidden />
783
- </button>
941
+ {(
942
+ [
943
+ ['grid', 'Grid view', Grid3x3],
944
+ ['list', 'List view', List],
945
+ ] as const
946
+ ).map(([mode, label, Icon]) => (
947
+ <button
948
+ key={mode}
949
+ type="button"
950
+ onClick={() => setViewMode(mode)}
951
+ aria-label={label}
952
+ aria-pressed={viewMode === mode}
953
+ className={`flex h-[30px] w-[30px] items-center justify-center transition-colors focus-visible:ring-2 focus-visible:ring-[var(--ring)] focus-visible:outline-none focus-visible:ring-inset ${
954
+ viewMode === mode
955
+ ? 'bg-[var(--acc-l)] text-[var(--acc)]'
956
+ : 'text-[var(--muted)] hover:bg-[var(--bg)] hover:text-[var(--sub)]'
957
+ }`}
958
+ >
959
+ <Icon size={14} aria-hidden />
960
+ </button>
961
+ ))}
784
962
  </div>
963
+ <span
964
+ className="ml-auto shrink-0 text-[12px] whitespace-nowrap text-[var(--muted)]"
965
+ aria-live="polite"
966
+ >
967
+ {filteredAndSorted.length} file{filteredAndSorted.length === 1 ? '' : 's'}
968
+ </span>
785
969
  </div>
786
970
  </div>
787
971
 
@@ -806,10 +990,7 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
806
990
  />
807
991
  ) : null}
808
992
  <button
809
- onClick={async () => {
810
- for (const id of selectedMedia) await deleteMedia(id)
811
- setSelectedMedia([])
812
- }}
993
+ onClick={() => setPendingDelete(selectedMedia)}
813
994
  className="bg-destructive text-destructive-foreground hover:bg-destructive/90 rounded-lg px-3 py-1.5 text-sm transition-colors"
814
995
  >
815
996
  Delete Selected
@@ -849,19 +1030,21 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
849
1030
  {viewMode === 'grid' ? (
850
1031
  <div
851
1032
  ref={gridScrollRef}
852
- // The grid's cards aren't focusable yet, so the scroller
853
- // itself takes Tab and arrow-key scrolling (Media.html
854
- // `.mc-grid-area`; axe scrollable-region-focusable).
1033
+ // Media.html `.mc-grid-area`: the scroller takes arrow-key
1034
+ // scrolling (axe scrollable-region-focusable).
855
1035
  tabIndex={0}
856
1036
  role="region"
857
1037
  aria-label="Media library"
858
- className="h-full overflow-y-auto p-2 sm:p-3"
1038
+ className="h-full overflow-y-auto px-[28px] py-[20px]"
859
1039
  >
860
- <div className="relative" style={{ height: gridRowCount * 220 }}>
1040
+ <div
1041
+ className="relative"
1042
+ style={{ height: gridRowCount * MEDIA_GRID_ROW_HEIGHT }}
1043
+ >
861
1044
  {gridWindow.rows.map((virtualRow) => (
862
1045
  <div
863
1046
  key={virtualRow.key}
864
- className="grid gap-3 sm:gap-4"
1047
+ className="grid gap-[14px]"
865
1048
  style={{
866
1049
  position: 'absolute',
867
1050
  top: 0,
@@ -878,55 +1061,75 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
878
1061
  )
879
1062
  .map((item) => {
880
1063
  const isActive = activeItem?.id === item.id
881
- const hasIssues =
882
- !hasAccessibleAlt(item) ||
883
- !item.title ||
884
- item.usedOn?.length === 0
885
- const typeLabel = (
886
- item.name.includes('.')
887
- ? item.name.split('.').pop()!
888
- : item.format || item.type || ''
889
- )
890
- .replace(/^.*\//, '')
891
- .toUpperCase()
1064
+ const isSelected = selectedMedia.includes(item.id)
1065
+ // Media.html `hasMissing`: the card's "SEO" tag.
1066
+ const missingSeo = !hasAccessibleAlt(item) || !item.title?.trim()
1067
+ const score = item.seoScore
892
1068
  return (
893
1069
  <div
894
1070
  key={item.id}
895
- className={`group relative flex cursor-pointer flex-col overflow-hidden rounded-lg border-2 transition-all ${
896
- isActive
897
- ? 'border-primary ring-primary/30 ring-2'
898
- : selectedMedia.includes(item.id)
899
- ? 'border-primary/60 ring-primary/20 ring-1'
900
- : 'border-border hover:border-input'
1071
+ data-media-card=""
1072
+ className={`group relative flex h-[172px] flex-col overflow-hidden rounded-[9px] border bg-[var(--card)] transition-[border-color,box-shadow] has-[button:focus-visible]:ring-2 has-[button:focus-visible]:ring-[var(--ring)] ${
1073
+ isActive || isSelected
1074
+ ? 'border-[var(--acc)] shadow-[0_0_0_2px_var(--acc-m)]'
1075
+ : 'border-[var(--bdr)] hover:border-[var(--acc-m)] hover:shadow-[var(--shd2)]'
901
1076
  }`}
902
- onClick={() => openDetail(item)}
903
1077
  draggable
904
1078
  onDragStart={(e) => handleDragStart(e, item.id)}
905
1079
  >
906
- <div className="relative aspect-square w-full overflow-hidden">
1080
+ {/* Stretched over the card so the whole tile opens it; the
1081
+ select control sits above it. */}
1082
+ <button
1083
+ type="button"
1084
+ onClick={() => openDetail(item)}
1085
+ aria-label={[
1086
+ `Open ${item.name}`,
1087
+ score !== undefined ? `SEO score ${score}` : '',
1088
+ missingSeo ? 'missing SEO' : '',
1089
+ ]
1090
+ .filter(Boolean)
1091
+ .join(', ')}
1092
+ aria-current={isActive ? 'true' : undefined}
1093
+ className="absolute inset-0 z-[1] cursor-pointer focus:outline-none"
1094
+ />
1095
+ <div className="relative h-[118px] w-full shrink-0 overflow-hidden">
907
1096
  <div className="bg-muted flex h-full w-full items-center justify-center">
908
1097
  <MediaPreview item={item} />
909
1098
  </div>
910
- {hasIssues && (
911
- <div
912
- className="bg-warning absolute top-1.5 left-1.5 flex h-5 w-5 items-center justify-center rounded-full"
913
- title="Needs attention"
1099
+ {score !== undefined && (
1100
+ <span
1101
+ aria-hidden
1102
+ className={`absolute top-[7px] right-[7px] flex h-[22px] min-w-[22px] items-center justify-center rounded-[11px] px-[5px] text-[9.5px] font-bold tracking-[.2px] tabular-nums shadow-[var(--shd)] ${scoreBadgeClass(score)}`}
914
1103
  >
915
- <AlertTriangle className="text-warning-foreground h-3 w-3" />
916
- </div>
1104
+ {score}
1105
+ </span>
917
1106
  )}
918
- <div
919
- className="absolute top-1.5 right-1.5 opacity-0 transition-opacity group-hover:opacity-100"
1107
+ <span
1108
+ aria-hidden
1109
+ className="absolute bottom-[7px] left-[7px] rounded-[4px] bg-[var(--card)]/85 px-[6px] py-[2px] text-[9.5px] font-semibold tracking-[.3px] text-[var(--txt)] uppercase backdrop-blur-[4px]"
1110
+ >
1111
+ {mediaKind(item)}
1112
+ </span>
1113
+ <button
1114
+ type="button"
1115
+ role="checkbox"
1116
+ aria-checked={isSelected}
1117
+ aria-label={`Select ${item.name}`}
1118
+ className={`focus-visible:ring-ring absolute top-[7px] left-[7px] z-[2] rounded transition-opacity group-hover:opacity-100 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:outline-none [@media(hover:none)]:opacity-100 ${
1119
+ isSelected || selectedMedia.length > 0
1120
+ ? 'opacity-100'
1121
+ : 'opacity-0'
1122
+ }`}
920
1123
  onClick={(e) => handleCheckbox(e, item.id)}
921
1124
  >
922
- <div
1125
+ <span
923
1126
  className={`flex h-5 w-5 items-center justify-center rounded border-2 transition-colors ${
924
- selectedMedia.includes(item.id)
1127
+ isSelected
925
1128
  ? 'border-primary bg-primary'
926
1129
  : 'border-input bg-card/80'
927
1130
  }`}
928
1131
  >
929
- {selectedMedia.includes(item.id) && (
1132
+ {isSelected && (
930
1133
  <svg
931
1134
  className="text-primary-foreground h-3 w-3"
932
1135
  fill="none"
@@ -941,23 +1144,23 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
941
1144
  />
942
1145
  </svg>
943
1146
  )}
944
- </div>
945
- </div>
1147
+ </span>
1148
+ </button>
946
1149
  </div>
947
- <div className="border-border bg-card flex items-center gap-2 border-t px-2 py-1.5">
948
- <span
949
- className={`mt-0.5 h-2 w-2 shrink-0 self-start rounded-full ${
950
- hasIssues ? 'bg-warning' : 'bg-success'
951
- }`}
952
- title={hasIssues ? 'Needs attention' : 'SEO complete'}
953
- />
954
- <div className="min-w-0 flex-1">
955
- <p className="text-foreground truncate text-xs font-medium">
956
- {item.name}
957
- </p>
958
- <p className="text-muted-foreground truncate text-xs">
959
- {[typeLabel, item.size].filter(Boolean).join(' · ')}
960
- </p>
1150
+ <div className="min-h-0 flex-1 px-[10px] pt-[8px] pb-[10px]">
1151
+ <p className="truncate text-[12px] font-medium text-[var(--txt)]">
1152
+ {item.name}
1153
+ </p>
1154
+ <div className="mt-[3px] flex items-center gap-[5px]">
1155
+ <span className="text-[11px] text-[var(--muted)]">
1156
+ {item.size}
1157
+ </span>
1158
+ {missingSeo && (
1159
+ <span className="ml-auto inline-flex items-center gap-[3px] rounded-[3px] bg-[var(--err-l)] px-[5px] py-px text-[10px] font-medium text-[var(--err)]">
1160
+ <AlertTriangle size={9} aria-hidden />
1161
+ SEO
1162
+ </span>
1163
+ )}
961
1164
  </div>
962
1165
  </div>
963
1166
  </div>
@@ -1099,9 +1302,20 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
1099
1302
 
1100
1303
  <div className="flex-1 overflow-y-auto">
1101
1304
  <div className="border-border border-b p-4">
1102
- <div className="bg-muted flex aspect-video items-center justify-center overflow-hidden rounded-lg">
1305
+ <div className="bg-muted relative flex aspect-video items-center justify-center overflow-hidden rounded-lg">
1103
1306
  {isImageMedia(activeItem) ? (
1104
- <MediaPreview item={activeItem} />
1307
+ <>
1308
+ <MediaPreview item={activeItem} fit="contain" />
1309
+ <button
1310
+ type="button"
1311
+ onClick={() => setLightboxId(activeItem.id)}
1312
+ aria-label="View full size"
1313
+ title="View full size"
1314
+ className="border-border bg-card/90 text-foreground hover:bg-card focus-visible:ring-ring prenta-touch-target absolute top-2 right-2 flex h-8 w-8 items-center justify-center rounded-md border shadow-sm transition-colors focus-visible:ring-2 focus-visible:outline-none"
1315
+ >
1316
+ <Maximize2 size={14} aria-hidden />
1317
+ </button>
1318
+ </>
1105
1319
  ) : (
1106
1320
  <ImageIcon className="text-muted-foreground h-12 w-12" />
1107
1321
  )}
@@ -1412,20 +1626,6 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
1412
1626
  </div>
1413
1627
  </div>
1414
1628
  </button>
1415
- <button
1416
- type="button"
1417
- className="border-border hover:bg-muted flex w-full items-center gap-2 rounded-lg border p-4 text-left transition-colors"
1418
- >
1419
- <Bot className="text-muted-foreground h-5 w-5 shrink-0" />
1420
- <div className="flex-1">
1421
- <div className="text-foreground text-sm font-medium">
1422
- AI Content Analysis
1423
- </div>
1424
- <div className="text-muted-foreground mt-0.5 text-xs">
1425
- Detect objects, faces, text, and suggest categories
1426
- </div>
1427
- </div>
1428
- </button>
1429
1629
  </div>
1430
1630
  </div>
1431
1631
 
@@ -1439,17 +1639,20 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
1439
1639
  {saving && <Loader2 className="h-4 w-4 animate-spin" />}
1440
1640
  {saving ? 'Saving...' : 'Save Changes'}
1441
1641
  </button>
1442
- <button
1443
- type="button"
1444
- className="hover:bg-muted rounded-lg p-2 transition-colors"
1642
+ <a
1643
+ href={mediaDownloadUrl(activeItem.url)}
1644
+ download={activeItem.name}
1645
+ target="_blank"
1646
+ rel="noopener noreferrer"
1647
+ className="hover:bg-muted focus-visible:ring-ring rounded-lg p-2 transition-colors focus-visible:ring-2 focus-visible:outline-none"
1445
1648
  title="Download"
1446
1649
  aria-label="Download"
1447
1650
  >
1448
1651
  <Download className="text-muted-foreground h-4 w-4" aria-hidden />
1449
- </button>
1652
+ </a>
1450
1653
  <button
1451
1654
  type="button"
1452
- onClick={() => deleteMedia(activeItem.id)}
1655
+ onClick={() => setPendingDelete([activeItem.id])}
1453
1656
  className="hover:bg-muted rounded-lg p-2 transition-colors"
1454
1657
  title="Delete"
1455
1658
  aria-label="Delete"
@@ -1464,6 +1667,34 @@ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBro
1464
1667
  </div>
1465
1668
  </div>
1466
1669
  )}
1670
+
1671
+ <ConfirmDialog
1672
+ open={pendingDelete !== null}
1673
+ onClose={() => setPendingDelete(null)}
1674
+ onConfirm={() => {
1675
+ if (pendingDelete) void deleteMedia(pendingDelete)
1676
+ }}
1677
+ returnFocusFallback={focusLibrary}
1678
+ title={
1679
+ pendingDelete?.length === 1
1680
+ ? 'Delete file?'
1681
+ : `Delete ${pendingDelete?.length ?? 0} files?`
1682
+ }
1683
+ description={
1684
+ pendingDelete?.length === 1
1685
+ ? 'This permanently deletes the file from storage. Pages that use it will show a broken image.'
1686
+ : 'This permanently deletes these files from storage. Pages that use them will show broken images.'
1687
+ }
1688
+ confirmLabel="Delete"
1689
+ destructive
1690
+ />
1691
+
1692
+ <MediaLightbox
1693
+ items={lightboxItems}
1694
+ activeId={lightboxId}
1695
+ onActiveChange={setLightboxId}
1696
+ onClose={() => setLightboxId(null)}
1697
+ />
1467
1698
  </div>
1468
1699
  )
1469
1700
  }