@prenta/admin 2.11.2 → 2.12.1
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.
- package/CHANGELOG.md +21 -0
- package/dist/__tests__/components/modal.render.test.js +30 -1
- package/dist/__tests__/components/modal.render.test.js.map +1 -1
- package/dist/__tests__/lib/media-download.test.d.ts +2 -0
- package/dist/__tests__/lib/media-download.test.d.ts.map +1 -0
- package/dist/__tests__/lib/media-download.test.js +14 -0
- package/dist/__tests__/lib/media-download.test.js.map +1 -0
- package/dist/__tests__/views/media-browser.render.test.d.ts +2 -0
- package/dist/__tests__/views/media-browser.render.test.d.ts.map +1 -0
- package/dist/__tests__/views/media-browser.render.test.js +270 -0
- package/dist/__tests__/views/media-browser.render.test.js.map +1 -0
- package/dist/__tests__/views/media-grid.test.js +12 -1
- package/dist/__tests__/views/media-grid.test.js.map +1 -1
- package/dist/__tests__/views/search-data-card.render.test.d.ts +2 -0
- package/dist/__tests__/views/search-data-card.render.test.d.ts.map +1 -0
- package/dist/__tests__/views/search-data-card.render.test.js +236 -0
- package/dist/__tests__/views/search-data-card.render.test.js.map +1 -0
- package/dist/__tests__/views/seo-settings.render.test.js +8 -4
- package/dist/__tests__/views/seo-settings.render.test.js.map +1 -1
- package/dist/components/MediaLightbox.d.ts +22 -0
- package/dist/components/MediaLightbox.d.ts.map +1 -0
- package/dist/components/MediaLightbox.js +38 -0
- package/dist/components/MediaLightbox.js.map +1 -0
- package/dist/components/ui/ConfirmDialog.d.ts +3 -1
- package/dist/components/ui/ConfirmDialog.d.ts.map +1 -1
- package/dist/components/ui/ConfirmDialog.js +2 -2
- package/dist/components/ui/ConfirmDialog.js.map +1 -1
- package/dist/components/ui/Modal.d.ts +7 -1
- package/dist/components/ui/Modal.d.ts.map +1 -1
- package/dist/components/ui/Modal.js +4 -2
- package/dist/components/ui/Modal.js.map +1 -1
- package/dist/lib/dialog-return-focus.d.ts +10 -0
- package/dist/lib/dialog-return-focus.d.ts.map +1 -0
- package/dist/lib/dialog-return-focus.js +26 -0
- package/dist/lib/dialog-return-focus.js.map +1 -0
- package/dist/lib/media-download.d.ts +8 -0
- package/dist/lib/media-download.d.ts.map +1 -0
- package/dist/lib/media-download.js +21 -0
- package/dist/lib/media-download.js.map +1 -0
- package/dist/prenta-admin.css +1 -1
- package/dist/views/MediaBrowser.d.ts +34 -0
- package/dist/views/MediaBrowser.d.ts.map +1 -1
- package/dist/views/MediaBrowser.js +190 -51
- package/dist/views/MediaBrowser.js.map +1 -1
- package/dist/views/MediaSeoAudit.d.ts +6 -0
- package/dist/views/MediaSeoAudit.d.ts.map +1 -1
- package/dist/views/MediaSeoAudit.js +12 -0
- package/dist/views/MediaSeoAudit.js.map +1 -1
- package/dist/views/settings/SearchDataCard.d.ts.map +1 -1
- package/dist/views/settings/SearchDataCard.js +162 -24
- package/dist/views/settings/SearchDataCard.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/components/modal.render.test.tsx +52 -0
- package/src/__tests__/lib/media-download.test.ts +23 -0
- package/src/__tests__/views/media-browser.render.test.tsx +325 -0
- package/src/__tests__/views/media-grid.test.ts +13 -1
- package/src/__tests__/views/search-data-card.render.test.tsx +275 -0
- package/src/__tests__/views/seo-settings.render.test.tsx +15 -5
- package/src/components/MediaLightbox.tsx +127 -0
- package/src/components/ui/ConfirmDialog.tsx +4 -0
- package/src/components/ui/Modal.tsx +10 -0
- package/src/lib/dialog-return-focus.ts +33 -0
- package/src/lib/media-download.ts +18 -0
- package/src/views/MediaBrowser.tsx +367 -143
- package/src/views/MediaSeoAudit.tsx +11 -0
- package/src/views/settings/SearchDataCard.tsx +306 -45
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Search data card (#1102 Tier 2): the key saves apart from the property, the
|
|
7
|
+
* property is validated in the field, and once a key is stored the property is
|
|
8
|
+
* picked from what the key can read.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type Reply = { data?: unknown; error?: string; status: number }
|
|
12
|
+
|
|
13
|
+
const server = vi.hoisted(() => ({
|
|
14
|
+
hasKey: false,
|
|
15
|
+
propertyUrl: '',
|
|
16
|
+
properties: null as null | Array<{ siteUrl: string; permissionLevel: string }>,
|
|
17
|
+
propertiesError: null as null | string,
|
|
18
|
+
keyError: null as null | string,
|
|
19
|
+
puts: [] as Array<Record<string, string>>,
|
|
20
|
+
/** When set, the next properties lookup waits for this. */
|
|
21
|
+
propertiesGate: null as null | Promise<void>,
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
const cmsApi = vi.hoisted(() =>
|
|
25
|
+
vi.fn(async (endpoint: string, init: { method?: string; body?: string } = {}): Promise<Reply> => {
|
|
26
|
+
const method = init.method ?? 'GET'
|
|
27
|
+
if (endpoint === '/seo/search-console' && method === 'GET') {
|
|
28
|
+
return {
|
|
29
|
+
status: 200,
|
|
30
|
+
data: {
|
|
31
|
+
propertyUrl: server.propertyUrl,
|
|
32
|
+
hasServiceAccountKey: server.hasKey,
|
|
33
|
+
serviceAccountEmail: server.hasKey ? 'sync@acme.iam.gserviceaccount.com' : null,
|
|
34
|
+
hasCruxApiKey: false,
|
|
35
|
+
connected: false,
|
|
36
|
+
lastSyncAt: null,
|
|
37
|
+
syncStatus: null,
|
|
38
|
+
suggestedProperty: 'sc-domain:acme.test',
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (endpoint === '/seo/search-console' && method === 'PUT') {
|
|
43
|
+
const body = JSON.parse(init.body ?? '{}') as Record<string, string>
|
|
44
|
+
server.puts.push(body)
|
|
45
|
+
if ('serviceAccountKey' in body && server.keyError) {
|
|
46
|
+
return { status: 400, error: server.keyError }
|
|
47
|
+
}
|
|
48
|
+
if ('serviceAccountKey' in body) server.hasKey = Boolean(body.serviceAccountKey)
|
|
49
|
+
if ('propertyUrl' in body) server.propertyUrl = body.propertyUrl!
|
|
50
|
+
return { status: 200, data: { ok: true } }
|
|
51
|
+
}
|
|
52
|
+
if (endpoint === '/seo/search-console/properties') {
|
|
53
|
+
const gate = server.propertiesGate
|
|
54
|
+
server.propertiesGate = null
|
|
55
|
+
const answer = {
|
|
56
|
+
properties: server.properties ?? [],
|
|
57
|
+
error: server.propertiesError,
|
|
58
|
+
}
|
|
59
|
+
if (gate) await gate
|
|
60
|
+
if (answer.error) {
|
|
61
|
+
return { status: 200, data: { ok: false, code: 'auth-failed', message: answer.error } }
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
status: 200,
|
|
65
|
+
data: {
|
|
66
|
+
ok: true,
|
|
67
|
+
properties: answer.properties,
|
|
68
|
+
serviceAccountEmail: 'sync@acme.iam.gserviceaccount.com',
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { status: 200, data: {} }
|
|
73
|
+
}),
|
|
74
|
+
)
|
|
75
|
+
vi.mock('../../lib/api.js', () => ({ cmsApi }))
|
|
76
|
+
|
|
77
|
+
const toast = vi.hoisted(() => ({
|
|
78
|
+
success: vi.fn(),
|
|
79
|
+
error: vi.fn(),
|
|
80
|
+
warning: vi.fn(),
|
|
81
|
+
info: vi.fn(),
|
|
82
|
+
}))
|
|
83
|
+
vi.mock('sonner', () => ({ toast }))
|
|
84
|
+
|
|
85
|
+
const { SearchDataCard } = await import('../../views/settings/SearchDataCard.js')
|
|
86
|
+
|
|
87
|
+
const KEY = '{"client_email":"sync@acme.iam.gserviceaccount.com","private_key":"k"}'
|
|
88
|
+
|
|
89
|
+
beforeEach(() => {
|
|
90
|
+
Object.assign(server, {
|
|
91
|
+
hasKey: false,
|
|
92
|
+
propertyUrl: '',
|
|
93
|
+
properties: null,
|
|
94
|
+
propertiesError: null,
|
|
95
|
+
keyError: null,
|
|
96
|
+
puts: [],
|
|
97
|
+
propertiesGate: null,
|
|
98
|
+
})
|
|
99
|
+
vi.clearAllMocks()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('Search data card: saving', () => {
|
|
103
|
+
it('keeps a good key when the property is malformed', async () => {
|
|
104
|
+
render(<SearchDataCard canEdit />)
|
|
105
|
+
fireEvent.change(await screen.findByLabelText('Search Console property'), {
|
|
106
|
+
target: { value: 'acme.test' },
|
|
107
|
+
})
|
|
108
|
+
fireEvent.change(screen.getByLabelText(/Service account key/), { target: { value: KEY } })
|
|
109
|
+
fireEvent.click(screen.getByRole('button', { name: /Save credentials/ }))
|
|
110
|
+
|
|
111
|
+
await waitFor(() => expect(server.hasKey).toBe(true))
|
|
112
|
+
expect(server.puts).toEqual([{ serviceAccountKey: KEY }])
|
|
113
|
+
expect(toast.success).toHaveBeenCalledWith(
|
|
114
|
+
'Saved the service account key. Fix the highlighted field to save the rest.',
|
|
115
|
+
)
|
|
116
|
+
// The typed property survives the reload, with the error beside it.
|
|
117
|
+
const field = (await screen.findByLabelText('Search Console property')) as HTMLInputElement
|
|
118
|
+
await waitFor(() => expect(field.value).toBe('acme.test'))
|
|
119
|
+
expect(field.getAttribute('aria-invalid')).toBe('true')
|
|
120
|
+
expect(screen.getByRole('alert').textContent).toMatch(/domain property/)
|
|
121
|
+
expect(await screen.findByText('Key stored')).toBeTruthy()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('shows a rejected key under the key field and still saves the property', async () => {
|
|
125
|
+
server.keyError = 'Service account key is missing "private_key". Paste the full JSON key file.'
|
|
126
|
+
render(<SearchDataCard canEdit />)
|
|
127
|
+
fireEvent.change(await screen.findByLabelText('Search Console property'), {
|
|
128
|
+
target: { value: 'sc-domain:acme.test' },
|
|
129
|
+
})
|
|
130
|
+
fireEvent.change(screen.getByLabelText(/Service account key/), { target: { value: '{}' } })
|
|
131
|
+
fireEvent.click(screen.getByRole('button', { name: /Save credentials/ }))
|
|
132
|
+
|
|
133
|
+
const alert = await screen.findByRole('alert')
|
|
134
|
+
expect(alert.textContent).toContain('missing "private_key"')
|
|
135
|
+
expect(screen.getByLabelText(/Service account key/).getAttribute('aria-invalid')).toBe('true')
|
|
136
|
+
expect(server.propertyUrl).toBe('sc-domain:acme.test')
|
|
137
|
+
expect(toast.error).not.toHaveBeenCalled()
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('Search data card: property field', () => {
|
|
142
|
+
it('validates the format when the field loses focus', async () => {
|
|
143
|
+
render(<SearchDataCard canEdit />)
|
|
144
|
+
const field = await screen.findByLabelText('Search Console property')
|
|
145
|
+
fireEvent.change(field, { target: { value: 'www.acme.test' } })
|
|
146
|
+
fireEvent.blur(field)
|
|
147
|
+
expect((await screen.findByRole('alert')).textContent).toMatch(/sc-domain:example\.com/)
|
|
148
|
+
|
|
149
|
+
fireEvent.change(field, { target: { value: 'https://www.acme.test/' } })
|
|
150
|
+
await waitFor(() => expect(screen.queryByRole('alert')).toBeNull())
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('keeps the suggestion visible while the value is invalid', async () => {
|
|
154
|
+
render(<SearchDataCard canEdit />)
|
|
155
|
+
const field = await screen.findByLabelText('Search Console property')
|
|
156
|
+
expect(screen.getByRole('button', { name: 'Use sc-domain:acme.test' })).toBeTruthy()
|
|
157
|
+
fireEvent.change(field, { target: { value: 'acme' } })
|
|
158
|
+
fireEvent.click(screen.getByRole('button', { name: 'Use sc-domain:acme.test' }))
|
|
159
|
+
expect((field as HTMLInputElement).value).toBe('sc-domain:acme.test')
|
|
160
|
+
expect(screen.queryByRole('button', { name: 'Use sc-domain:acme.test' })).toBeNull()
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
describe('Search data card: property picker', () => {
|
|
165
|
+
it('lists the properties the stored key can read', async () => {
|
|
166
|
+
server.hasKey = true
|
|
167
|
+
server.properties = [
|
|
168
|
+
{ siteUrl: 'https://acme.test/', permissionLevel: 'siteFullUser' },
|
|
169
|
+
{ siteUrl: 'sc-domain:acme.test', permissionLevel: 'siteOwner' },
|
|
170
|
+
]
|
|
171
|
+
render(<SearchDataCard canEdit />)
|
|
172
|
+
const select = (await screen.findByRole('combobox', {
|
|
173
|
+
name: 'Search Console property',
|
|
174
|
+
})) as HTMLSelectElement
|
|
175
|
+
expect([...select.options].map((o) => o.value)).toEqual([
|
|
176
|
+
'',
|
|
177
|
+
'https://acme.test/',
|
|
178
|
+
'sc-domain:acme.test',
|
|
179
|
+
])
|
|
180
|
+
fireEvent.change(select, { target: { value: 'sc-domain:acme.test' } })
|
|
181
|
+
fireEvent.click(screen.getByRole('button', { name: /Save credentials/ }))
|
|
182
|
+
await waitFor(() => expect(server.puts).toEqual([{ propertyUrl: 'sc-domain:acme.test' }]))
|
|
183
|
+
|
|
184
|
+
fireEvent.click(screen.getByRole('button', { name: 'Enter it manually' }))
|
|
185
|
+
expect(screen.getByRole('textbox', { name: 'Search Console property' })).toBeTruthy()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it('flags a saved property the key can’t read', async () => {
|
|
189
|
+
server.hasKey = true
|
|
190
|
+
server.propertyUrl = 'sc-domain:old.test'
|
|
191
|
+
server.properties = [{ siteUrl: 'sc-domain:acme.test', permissionLevel: 'siteOwner' }]
|
|
192
|
+
render(<SearchDataCard canEdit />)
|
|
193
|
+
await screen.findByRole('combobox', { name: 'Search Console property' })
|
|
194
|
+
expect(
|
|
195
|
+
screen.getByRole('option', { name: 'sc-domain:old.test (not granted to this key)' }),
|
|
196
|
+
).toBeTruthy()
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
it('says which email to add when the key can’t read anything yet', async () => {
|
|
200
|
+
server.hasKey = true
|
|
201
|
+
server.properties = []
|
|
202
|
+
render(<SearchDataCard canEdit />)
|
|
203
|
+
expect(await screen.findByText(/No properties yet/)).toBeTruthy()
|
|
204
|
+
expect(screen.getAllByText('sync@acme.iam.gserviceaccount.com').length).toBeGreaterThan(0)
|
|
205
|
+
|
|
206
|
+
server.properties = [{ siteUrl: 'sc-domain:acme.test', permissionLevel: 'siteOwner' }]
|
|
207
|
+
fireEvent.click(screen.getByRole('button', { name: 'refresh the list' }))
|
|
208
|
+
expect(await screen.findByRole('combobox', { name: 'Search Console property' })).toBeTruthy()
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
it('ignores a slow lookup for a key that was removed meanwhile', async () => {
|
|
212
|
+
server.hasKey = true
|
|
213
|
+
server.properties = [{ siteUrl: 'sc-domain:old-account.test', permissionLevel: 'siteOwner' }]
|
|
214
|
+
let release!: () => void
|
|
215
|
+
server.propertiesGate = new Promise<void>((resolve) => (release = resolve))
|
|
216
|
+
render(<SearchDataCard canEdit />)
|
|
217
|
+
|
|
218
|
+
fireEvent.click(await screen.findByRole('button', { name: /Remove stored key/ }))
|
|
219
|
+
await waitFor(() => expect(server.hasKey).toBe(false))
|
|
220
|
+
await waitFor(() => expect(screen.queryByText('Key stored')).toBeNull())
|
|
221
|
+
release()
|
|
222
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
223
|
+
|
|
224
|
+
expect(screen.queryByRole('combobox', { name: 'Search Console property' })).toBeNull()
|
|
225
|
+
expect(screen.queryByText('sc-domain:old-account.test')).toBeNull()
|
|
226
|
+
expect(screen.getByRole('textbox', { name: 'Search Console property' })).toBeTruthy()
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('shows an invalid kept value in the text field, with the fix, even when a list exists', async () => {
|
|
230
|
+
server.properties = [{ siteUrl: 'sc-domain:acme.test', permissionLevel: 'siteOwner' }]
|
|
231
|
+
render(<SearchDataCard canEdit />)
|
|
232
|
+
fireEvent.change(await screen.findByLabelText('Search Console property'), {
|
|
233
|
+
target: { value: 'acme.test' },
|
|
234
|
+
})
|
|
235
|
+
fireEvent.change(screen.getByLabelText(/Service account key/), { target: { value: KEY } })
|
|
236
|
+
fireEvent.click(screen.getByRole('button', { name: /Save credentials/ }))
|
|
237
|
+
|
|
238
|
+
await waitFor(() => expect(server.hasKey).toBe(true))
|
|
239
|
+
const field = await screen.findByRole('textbox', { name: 'Search Console property' })
|
|
240
|
+
await waitFor(() => expect((field as HTMLInputElement).value).toBe('acme.test'))
|
|
241
|
+
expect(screen.getByRole('button', { name: 'Use sc-domain:acme.test' })).toBeTruthy()
|
|
242
|
+
|
|
243
|
+
fireEvent.click(screen.getByRole('button', { name: 'Choose from your properties' }))
|
|
244
|
+
expect(
|
|
245
|
+
(screen.getByRole('combobox', { name: 'Search Console property' }) as HTMLSelectElement)
|
|
246
|
+
.value,
|
|
247
|
+
).toBe('')
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it('still shows the list when a replacement key is rejected mid-lookup', async () => {
|
|
251
|
+
server.hasKey = true
|
|
252
|
+
server.keyError = 'Service account key is not valid JSON.'
|
|
253
|
+
server.properties = [{ siteUrl: 'sc-domain:acme.test', permissionLevel: 'siteOwner' }]
|
|
254
|
+
let release!: () => void
|
|
255
|
+
server.propertiesGate = new Promise<void>((resolve) => (release = resolve))
|
|
256
|
+
render(<SearchDataCard canEdit />)
|
|
257
|
+
|
|
258
|
+
fireEvent.change(await screen.findByLabelText(/Service account key/), {
|
|
259
|
+
target: { value: 'nope' },
|
|
260
|
+
})
|
|
261
|
+
fireEvent.click(screen.getByRole('button', { name: /Save credentials/ }))
|
|
262
|
+
expect((await screen.findByRole('alert')).textContent).toContain('not valid JSON')
|
|
263
|
+
release()
|
|
264
|
+
|
|
265
|
+
expect(await screen.findByRole('combobox', { name: 'Search Console property' })).toBeTruthy()
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('falls back to typing the property when the list can’t load', async () => {
|
|
269
|
+
server.hasKey = true
|
|
270
|
+
server.propertiesError = 'Search Console API returned HTTP 403.'
|
|
271
|
+
render(<SearchDataCard canEdit />)
|
|
272
|
+
expect(await screen.findByText(/Couldn’t list your properties/)).toBeTruthy()
|
|
273
|
+
expect(screen.getByRole('textbox', { name: 'Search Console property' })).toBeTruthy()
|
|
274
|
+
})
|
|
275
|
+
})
|
|
@@ -349,12 +349,22 @@ describe('SeoSettingsTab', () => {
|
|
|
349
349
|
expect.objectContaining({ method: 'PUT' }),
|
|
350
350
|
),
|
|
351
351
|
)
|
|
352
|
-
|
|
353
|
-
|
|
352
|
+
// The key and the property save separately, key first (#1102).
|
|
353
|
+
await waitFor(() =>
|
|
354
|
+
expect(
|
|
355
|
+
cmsApi.mock.calls.filter(
|
|
356
|
+
([e, o]) => e === '/seo/search-console' && (o as { method?: string })?.method === 'PUT',
|
|
357
|
+
),
|
|
358
|
+
).toHaveLength(2),
|
|
354
359
|
)
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
360
|
+
const bodies = cmsApi.mock.calls
|
|
361
|
+
.filter(
|
|
362
|
+
([e, o]) => e === '/seo/search-console' && (o as { method?: string })?.method === 'PUT',
|
|
363
|
+
)
|
|
364
|
+
.map(([, o]) => JSON.parse((o as { body: string }).body))
|
|
365
|
+
expect(Object.keys(bodies[0])).toEqual(['serviceAccountKey'])
|
|
366
|
+
expect(bodies[0].serviceAccountKey).toContain('svc@acme.iam.gserviceaccount.com')
|
|
367
|
+
expect(bodies[1]).toEqual({ propertyUrl: 'sc-domain:acme.test' })
|
|
358
368
|
})
|
|
359
369
|
|
|
360
370
|
it('renders Search data, AI visibility, SEO Autopilot, and the digest in that order', async () => {
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as Dialog from '@radix-ui/react-dialog'
|
|
4
|
+
import { ChevronLeft, ChevronRight, X } from 'lucide-react'
|
|
5
|
+
import { adminPortalContainer } from '../lib/portal-container.js'
|
|
6
|
+
import { useDialogReturnFocus } from '../lib/dialog-return-focus.js'
|
|
7
|
+
|
|
8
|
+
interface MediaLightboxItem {
|
|
9
|
+
id: string | number
|
|
10
|
+
url: string
|
|
11
|
+
name: string
|
|
12
|
+
alt: string
|
|
13
|
+
dimensions?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface MediaLightboxProps {
|
|
17
|
+
items: MediaLightboxItem[]
|
|
18
|
+
/** Id of the item on screen; `null` closes the viewer. */
|
|
19
|
+
activeId: string | number | null
|
|
20
|
+
onActiveChange: (id: string | number) => void
|
|
21
|
+
onClose: () => void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Full-size image viewer. The whole image is shown (`object-contain`), never
|
|
26
|
+
* cropped. ← and → (and the buttons) step through `items`; Radix supplies the
|
|
27
|
+
* focus trap, Escape, and focus return to the opener.
|
|
28
|
+
*/
|
|
29
|
+
export function MediaLightbox({ items, activeId, onActiveChange, onClose }: MediaLightboxProps) {
|
|
30
|
+
const index = activeId === null ? -1 : items.findIndex((item) => item.id === activeId)
|
|
31
|
+
const item = index >= 0 ? items[index] : undefined
|
|
32
|
+
const hasPrev = index > 0
|
|
33
|
+
const hasNext = index >= 0 && index < items.length - 1
|
|
34
|
+
const returnFocus = useDialogReturnFocus(item !== undefined)
|
|
35
|
+
|
|
36
|
+
const step = (delta: -1 | 1) => {
|
|
37
|
+
const next = items[index + delta]
|
|
38
|
+
if (next) onActiveChange(next.id)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const navButton =
|
|
42
|
+
'border-border bg-card text-foreground hover:bg-muted focus-visible:ring-ring prenta-touch-target absolute top-1/2 flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border shadow-sm transition-colors focus-visible:ring-2 focus-visible:outline-none aria-disabled:cursor-default aria-disabled:opacity-40'
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<Dialog.Root
|
|
46
|
+
open={item !== undefined}
|
|
47
|
+
onOpenChange={(open) => {
|
|
48
|
+
if (!open) onClose()
|
|
49
|
+
}}
|
|
50
|
+
>
|
|
51
|
+
<Dialog.Portal container={adminPortalContainer()}>
|
|
52
|
+
<Dialog.Overlay className="fixed inset-0 z-60 bg-black/70" />
|
|
53
|
+
<Dialog.Content
|
|
54
|
+
onCloseAutoFocus={returnFocus}
|
|
55
|
+
className="bg-card text-card-foreground border-border mobile:inset-0 mobile:rounded-none fixed inset-4 z-60 flex flex-col overflow-hidden rounded-[14px] border shadow-xl focus:outline-none sm:inset-8"
|
|
56
|
+
onKeyDown={(event) => {
|
|
57
|
+
if (event.key === 'ArrowLeft') {
|
|
58
|
+
event.preventDefault()
|
|
59
|
+
step(-1)
|
|
60
|
+
} else if (event.key === 'ArrowRight') {
|
|
61
|
+
event.preventDefault()
|
|
62
|
+
step(1)
|
|
63
|
+
}
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
{item && (
|
|
67
|
+
<>
|
|
68
|
+
<div className="border-border flex shrink-0 items-center gap-3 border-b px-4 py-3">
|
|
69
|
+
<div className="min-w-0 flex-1">
|
|
70
|
+
<Dialog.Title className="text-foreground truncate text-sm font-medium">
|
|
71
|
+
{item.name}
|
|
72
|
+
</Dialog.Title>
|
|
73
|
+
<Dialog.Description className="text-muted-foreground flex gap-2 text-xs">
|
|
74
|
+
<span className="whitespace-nowrap">
|
|
75
|
+
{index + 1} of {items.length}
|
|
76
|
+
</span>
|
|
77
|
+
{item.dimensions && (
|
|
78
|
+
<span className="whitespace-nowrap">{item.dimensions}</span>
|
|
79
|
+
)}
|
|
80
|
+
</Dialog.Description>
|
|
81
|
+
</div>
|
|
82
|
+
<Dialog.Close asChild>
|
|
83
|
+
<button
|
|
84
|
+
type="button"
|
|
85
|
+
aria-label="Close viewer"
|
|
86
|
+
className="text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:ring-ring prenta-touch-target flex h-8 w-8 items-center justify-center rounded-md transition-colors focus-visible:ring-2 focus-visible:outline-none"
|
|
87
|
+
>
|
|
88
|
+
<X size={16} aria-hidden />
|
|
89
|
+
</button>
|
|
90
|
+
</Dialog.Close>
|
|
91
|
+
</div>
|
|
92
|
+
<div className="bg-muted relative flex min-h-0 flex-1 items-center justify-center p-4 sm:px-16">
|
|
93
|
+
<img
|
|
94
|
+
src={item.url}
|
|
95
|
+
alt={item.alt}
|
|
96
|
+
className="max-h-full max-w-full object-contain"
|
|
97
|
+
/>
|
|
98
|
+
{items.length > 1 && (
|
|
99
|
+
<>
|
|
100
|
+
<button
|
|
101
|
+
type="button"
|
|
102
|
+
onClick={() => step(-1)}
|
|
103
|
+
aria-disabled={!hasPrev}
|
|
104
|
+
aria-label="Previous image"
|
|
105
|
+
className={`${navButton} left-3`}
|
|
106
|
+
>
|
|
107
|
+
<ChevronLeft size={18} aria-hidden />
|
|
108
|
+
</button>
|
|
109
|
+
<button
|
|
110
|
+
type="button"
|
|
111
|
+
onClick={() => step(1)}
|
|
112
|
+
aria-disabled={!hasNext}
|
|
113
|
+
aria-label="Next image"
|
|
114
|
+
className={`${navButton} right-3`}
|
|
115
|
+
>
|
|
116
|
+
<ChevronRight size={18} aria-hidden />
|
|
117
|
+
</button>
|
|
118
|
+
</>
|
|
119
|
+
)}
|
|
120
|
+
</div>
|
|
121
|
+
</>
|
|
122
|
+
)}
|
|
123
|
+
</Dialog.Content>
|
|
124
|
+
</Dialog.Portal>
|
|
125
|
+
</Dialog.Root>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
@@ -12,6 +12,8 @@ export interface ConfirmDialogProps {
|
|
|
12
12
|
confirmLabel?: string
|
|
13
13
|
cancelLabel?: string
|
|
14
14
|
destructive?: boolean
|
|
15
|
+
/** See `Modal`'s `returnFocusFallback`. */
|
|
16
|
+
returnFocusFallback?: () => HTMLElement | null
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export function ConfirmDialog({
|
|
@@ -23,12 +25,14 @@ export function ConfirmDialog({
|
|
|
23
25
|
confirmLabel = 'Confirm',
|
|
24
26
|
cancelLabel = 'Cancel',
|
|
25
27
|
destructive = false,
|
|
28
|
+
returnFocusFallback,
|
|
26
29
|
}: ConfirmDialogProps) {
|
|
27
30
|
return (
|
|
28
31
|
<Modal
|
|
29
32
|
open={open}
|
|
30
33
|
onClose={onClose}
|
|
31
34
|
title={title}
|
|
35
|
+
returnFocusFallback={returnFocusFallback}
|
|
32
36
|
actions={
|
|
33
37
|
<>
|
|
34
38
|
<Button variant="ghost" onClick={onClose}>
|
|
@@ -4,6 +4,7 @@ import * as Dialog from '@radix-ui/react-dialog'
|
|
|
4
4
|
import { X } from 'lucide-react'
|
|
5
5
|
import { type ReactNode, type RefObject, useRef } from 'react'
|
|
6
6
|
import { adminPortalContainer } from '../../lib/portal-container.js'
|
|
7
|
+
import { useDialogReturnFocus } from '../../lib/dialog-return-focus.js'
|
|
7
8
|
|
|
8
9
|
const MODAL_SIZE_CLASS = {
|
|
9
10
|
md: 'max-w-lg',
|
|
@@ -49,6 +50,12 @@ export interface ModalProps {
|
|
|
49
50
|
* The dialog stays open until the parent calls `onClose()`.
|
|
50
51
|
*/
|
|
51
52
|
onDismiss?: () => void
|
|
53
|
+
/**
|
|
54
|
+
* Focused on close when the control that opened the dialog is gone (for
|
|
55
|
+
* example the row a confirmed delete removed). Otherwise focus returns to
|
|
56
|
+
* that control.
|
|
57
|
+
*/
|
|
58
|
+
returnFocusFallback?: () => HTMLElement | null
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
/**
|
|
@@ -88,9 +95,11 @@ export function Modal({
|
|
|
88
95
|
description,
|
|
89
96
|
initialFocusRef,
|
|
90
97
|
onDismiss,
|
|
98
|
+
returnFocusFallback,
|
|
91
99
|
}: ModalProps) {
|
|
92
100
|
const scrollBody = size === 'xl'
|
|
93
101
|
const contentRef = useRef<HTMLDivElement>(null)
|
|
102
|
+
const returnFocus = useDialogReturnFocus(open, returnFocusFallback)
|
|
94
103
|
|
|
95
104
|
function handleDismissAttempt() {
|
|
96
105
|
if (onDismiss) onDismiss()
|
|
@@ -125,6 +134,7 @@ export function Modal({
|
|
|
125
134
|
event.preventDefault()
|
|
126
135
|
handleDismissAttempt()
|
|
127
136
|
}}
|
|
137
|
+
onCloseAutoFocus={returnFocus}
|
|
128
138
|
onOpenAutoFocus={
|
|
129
139
|
initialFocusRef
|
|
130
140
|
? (event) => {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Focus return for a controlled Radix dialog with no `Dialog.Trigger`. Radix's
|
|
5
|
+
* `onCloseAutoFocus` only focuses its trigger, so without one, closing drops
|
|
6
|
+
* focus on `<body>`. This records the focused element when `open` turns true
|
|
7
|
+
* (a layout effect, before Radix's FocusScope moves focus in) and returns
|
|
8
|
+
* focus there on close. If that element has left the DOM (a deleted row),
|
|
9
|
+
* `fallback` is focused instead.
|
|
10
|
+
*/
|
|
11
|
+
export function useDialogReturnFocus(
|
|
12
|
+
open: boolean,
|
|
13
|
+
fallback?: () => HTMLElement | null,
|
|
14
|
+
): (event: Event) => void {
|
|
15
|
+
const opener = useRef<HTMLElement | null>(null)
|
|
16
|
+
|
|
17
|
+
useLayoutEffect(() => {
|
|
18
|
+
if (!open) return
|
|
19
|
+
const active = document.activeElement
|
|
20
|
+
opener.current = active instanceof HTMLElement && active !== document.body ? active : null
|
|
21
|
+
}, [open])
|
|
22
|
+
|
|
23
|
+
return useCallback(
|
|
24
|
+
(event: Event) => {
|
|
25
|
+
event.preventDefault()
|
|
26
|
+
const el = opener.current
|
|
27
|
+
opener.current = null
|
|
28
|
+
const target = el?.isConnected ? el : (fallback?.() ?? null)
|
|
29
|
+
target?.focus()
|
|
30
|
+
},
|
|
31
|
+
[fallback],
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL that saves the file instead of opening it. Browsers ignore the
|
|
3
|
+
* `download` attribute on cross-origin links, so for Vercel Blob (the default
|
|
4
|
+
* media store) the attachment disposition comes from `?download=1`. Other
|
|
5
|
+
* hosts get the URL unchanged and rely on `download` for same-origin files.
|
|
6
|
+
*/
|
|
7
|
+
export function mediaDownloadUrl(url: string): string {
|
|
8
|
+
let parsed: URL
|
|
9
|
+
try {
|
|
10
|
+
parsed = new URL(url)
|
|
11
|
+
} catch {
|
|
12
|
+
return url
|
|
13
|
+
}
|
|
14
|
+
const host = parsed.hostname
|
|
15
|
+
if (host !== 'blob.vercel-storage.com' && !host.endsWith('.blob.vercel-storage.com')) return url
|
|
16
|
+
parsed.searchParams.set('download', '1')
|
|
17
|
+
return parsed.toString()
|
|
18
|
+
}
|