@prenta/admin 2.7.0 → 2.9.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 (211) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/design-system/tokens.css +17 -0
  3. package/design-system/tokens.json +35 -1
  4. package/design-system/tokens.scss +15 -0
  5. package/design-system/tokens.tailwind.cjs +14 -0
  6. package/dist/AdminRoot.d.ts.map +1 -1
  7. package/dist/AdminRoot.js +7 -7
  8. package/dist/AdminRoot.js.map +1 -1
  9. package/dist/__tests__/components/batch-ai-actions.test.js +4 -0
  10. package/dist/__tests__/components/batch-ai-actions.test.js.map +1 -1
  11. package/dist/__tests__/lib/batch-wizard.test.d.ts +2 -0
  12. package/dist/__tests__/lib/batch-wizard.test.d.ts.map +1 -0
  13. package/dist/__tests__/lib/batch-wizard.test.js +227 -0
  14. package/dist/__tests__/lib/batch-wizard.test.js.map +1 -0
  15. package/dist/__tests__/lib/job-drawer.test.js +2 -0
  16. package/dist/__tests__/lib/job-drawer.test.js.map +1 -1
  17. package/dist/__tests__/lib/job-view.test.js +100 -3
  18. package/dist/__tests__/lib/job-view.test.js.map +1 -1
  19. package/dist/__tests__/lib/jobs-service.test.js +74 -2
  20. package/dist/__tests__/lib/jobs-service.test.js.map +1 -1
  21. package/dist/__tests__/views/appearance-chrome.render.test.d.ts +2 -0
  22. package/dist/__tests__/views/appearance-chrome.render.test.d.ts.map +1 -0
  23. package/dist/__tests__/views/appearance-chrome.render.test.js +688 -0
  24. package/dist/__tests__/views/appearance-chrome.render.test.js.map +1 -0
  25. package/dist/__tests__/views/header-footer-form.test.d.ts +2 -0
  26. package/dist/__tests__/views/header-footer-form.test.d.ts.map +1 -0
  27. package/dist/__tests__/views/header-footer-form.test.js +266 -0
  28. package/dist/__tests__/views/header-footer-form.test.js.map +1 -0
  29. package/dist/__tests__/views/jobs-drawer.render.test.js +442 -2
  30. package/dist/__tests__/views/jobs-drawer.render.test.js.map +1 -1
  31. package/dist/__tests__/views/jobs-list.render.test.js +7 -5
  32. package/dist/__tests__/views/jobs-list.render.test.js.map +1 -1
  33. package/dist/__tests__/views/jobs-wizard.render.test.d.ts +2 -0
  34. package/dist/__tests__/views/jobs-wizard.render.test.d.ts.map +1 -0
  35. package/dist/__tests__/views/jobs-wizard.render.test.js +588 -0
  36. package/dist/__tests__/views/jobs-wizard.render.test.js.map +1 -0
  37. package/dist/__tests__/views/run-with-ai.render.test.d.ts +2 -0
  38. package/dist/__tests__/views/run-with-ai.render.test.d.ts.map +1 -0
  39. package/dist/__tests__/views/run-with-ai.render.test.js +689 -0
  40. package/dist/__tests__/views/run-with-ai.render.test.js.map +1 -0
  41. package/dist/components/BatchAiActions.d.ts +8 -1
  42. package/dist/components/BatchAiActions.d.ts.map +1 -1
  43. package/dist/components/BatchAiActions.js +3 -2
  44. package/dist/components/BatchAiActions.js.map +1 -1
  45. package/dist/lib/api.d.ts +2 -0
  46. package/dist/lib/api.d.ts.map +1 -1
  47. package/dist/lib/api.js +8 -1
  48. package/dist/lib/api.js.map +1 -1
  49. package/dist/lib/batch-wizard.d.ts +118 -0
  50. package/dist/lib/batch-wizard.d.ts.map +1 -0
  51. package/dist/lib/batch-wizard.js +216 -0
  52. package/dist/lib/batch-wizard.js.map +1 -0
  53. package/dist/lib/chrome-service.d.ts +69 -0
  54. package/dist/lib/chrome-service.d.ts.map +1 -0
  55. package/dist/lib/chrome-service.js +108 -0
  56. package/dist/lib/chrome-service.js.map +1 -0
  57. package/dist/lib/job-view.d.ts +43 -0
  58. package/dist/lib/job-view.d.ts.map +1 -1
  59. package/dist/lib/job-view.js +57 -1
  60. package/dist/lib/job-view.js.map +1 -1
  61. package/dist/lib/jobs-service.d.ts +48 -1
  62. package/dist/lib/jobs-service.d.ts.map +1 -1
  63. package/dist/lib/jobs-service.js +50 -0
  64. package/dist/lib/jobs-service.js.map +1 -1
  65. package/dist/prenta-admin.css +1 -1
  66. package/dist/views/AppearanceChrome.d.ts +22 -4
  67. package/dist/views/AppearanceChrome.d.ts.map +1 -1
  68. package/dist/views/AppearanceChrome.js +451 -15
  69. package/dist/views/AppearanceChrome.js.map +1 -1
  70. package/dist/views/CollectionList.d.ts +3 -1
  71. package/dist/views/CollectionList.d.ts.map +1 -1
  72. package/dist/views/CollectionList.js +5 -2
  73. package/dist/views/CollectionList.js.map +1 -1
  74. package/dist/views/Jobs/CancelJobDialog.d.ts +7 -1
  75. package/dist/views/Jobs/CancelJobDialog.d.ts.map +1 -1
  76. package/dist/views/Jobs/CancelJobDialog.js +13 -3
  77. package/dist/views/Jobs/CancelJobDialog.js.map +1 -1
  78. package/dist/views/Jobs/DrawerOverview.d.ts +6 -2
  79. package/dist/views/Jobs/DrawerOverview.d.ts.map +1 -1
  80. package/dist/views/Jobs/DrawerOverview.js +6 -3
  81. package/dist/views/Jobs/DrawerOverview.js.map +1 -1
  82. package/dist/views/Jobs/DrawerResults.d.ts +13 -3
  83. package/dist/views/Jobs/DrawerResults.d.ts.map +1 -1
  84. package/dist/views/Jobs/DrawerResults.js +37 -3
  85. package/dist/views/Jobs/DrawerResults.js.map +1 -1
  86. package/dist/views/Jobs/DrawerReview.d.ts +7 -0
  87. package/dist/views/Jobs/DrawerReview.d.ts.map +1 -1
  88. package/dist/views/Jobs/DrawerReview.js +1 -1
  89. package/dist/views/Jobs/DrawerReview.js.map +1 -1
  90. package/dist/views/Jobs/JobDrawer.d.ts +26 -3
  91. package/dist/views/Jobs/JobDrawer.d.ts.map +1 -1
  92. package/dist/views/Jobs/JobDrawer.js +30 -5
  93. package/dist/views/Jobs/JobDrawer.js.map +1 -1
  94. package/dist/views/Jobs/JobsTable.d.ts +4 -2
  95. package/dist/views/Jobs/JobsTable.d.ts.map +1 -1
  96. package/dist/views/Jobs/JobsTable.js +6 -5
  97. package/dist/views/Jobs/JobsTable.js.map +1 -1
  98. package/dist/views/Jobs/NewBatchJob.d.ts +34 -0
  99. package/dist/views/Jobs/NewBatchJob.d.ts.map +1 -0
  100. package/dist/views/Jobs/NewBatchJob.js +282 -0
  101. package/dist/views/Jobs/NewBatchJob.js.map +1 -0
  102. package/dist/views/Jobs/RunWithAi.d.ts +25 -0
  103. package/dist/views/Jobs/RunWithAi.d.ts.map +1 -0
  104. package/dist/views/Jobs/RunWithAi.js +46 -0
  105. package/dist/views/Jobs/RunWithAi.js.map +1 -0
  106. package/dist/views/Jobs/WorkerStrip.d.ts +3 -1
  107. package/dist/views/Jobs/WorkerStrip.d.ts.map +1 -1
  108. package/dist/views/Jobs/WorkerStrip.js +2 -2
  109. package/dist/views/Jobs/WorkerStrip.js.map +1 -1
  110. package/dist/views/Jobs/focus.d.ts +6 -0
  111. package/dist/views/Jobs/focus.d.ts.map +1 -1
  112. package/dist/views/Jobs/focus.js +10 -0
  113. package/dist/views/Jobs/focus.js.map +1 -1
  114. package/dist/views/Jobs.d.ts +14 -3
  115. package/dist/views/Jobs.d.ts.map +1 -1
  116. package/dist/views/Jobs.js +144 -32
  117. package/dist/views/Jobs.js.map +1 -1
  118. package/dist/views/MediaBrowser.d.ts +5 -1
  119. package/dist/views/MediaBrowser.d.ts.map +1 -1
  120. package/dist/views/MediaBrowser.js +6 -2
  121. package/dist/views/MediaBrowser.js.map +1 -1
  122. package/dist/views/Pages/PagesListView.d.ts +5 -1
  123. package/dist/views/Pages/PagesListView.d.ts.map +1 -1
  124. package/dist/views/Pages/PagesListView.js +7 -4
  125. package/dist/views/Pages/PagesListView.js.map +1 -1
  126. package/dist/views/Posts/PostsListView.d.ts +5 -1
  127. package/dist/views/Posts/PostsListView.d.ts.map +1 -1
  128. package/dist/views/Posts/PostsListView.js +7 -4
  129. package/dist/views/Posts/PostsListView.js.map +1 -1
  130. package/dist/views/header-footer/AnnouncementTab.d.ts +15 -0
  131. package/dist/views/header-footer/AnnouncementTab.d.ts.map +1 -0
  132. package/dist/views/header-footer/AnnouncementTab.js +43 -0
  133. package/dist/views/header-footer/AnnouncementTab.js.map +1 -0
  134. package/dist/views/header-footer/ChromeField.d.ts +25 -0
  135. package/dist/views/header-footer/ChromeField.d.ts.map +1 -0
  136. package/dist/views/header-footer/ChromeField.js +99 -0
  137. package/dist/views/header-footer/ChromeField.js.map +1 -0
  138. package/dist/views/header-footer/ChromePreview.d.ts +31 -0
  139. package/dist/views/header-footer/ChromePreview.d.ts.map +1 -0
  140. package/dist/views/header-footer/ChromePreview.js +195 -0
  141. package/dist/views/header-footer/ChromePreview.js.map +1 -0
  142. package/dist/views/header-footer/DeveloperTab.d.ts +17 -0
  143. package/dist/views/header-footer/DeveloperTab.d.ts.map +1 -0
  144. package/dist/views/header-footer/DeveloperTab.js +98 -0
  145. package/dist/views/header-footer/DeveloperTab.js.map +1 -0
  146. package/dist/views/header-footer/RegionTab.d.ts +27 -0
  147. package/dist/views/header-footer/RegionTab.d.ts.map +1 -0
  148. package/dist/views/header-footer/RegionTab.js +80 -0
  149. package/dist/views/header-footer/RegionTab.js.map +1 -0
  150. package/dist/views/header-footer/chrome-form.d.ts +137 -0
  151. package/dist/views/header-footer/chrome-form.d.ts.map +1 -0
  152. package/dist/views/header-footer/chrome-form.js +518 -0
  153. package/dist/views/header-footer/chrome-form.js.map +1 -0
  154. package/dist/views/header-footer/code-samples.d.ts +30 -0
  155. package/dist/views/header-footer/code-samples.d.ts.map +1 -0
  156. package/dist/views/header-footer/code-samples.js +127 -0
  157. package/dist/views/header-footer/code-samples.js.map +1 -0
  158. package/dist/views/header-footer/controls.d.ts +57 -0
  159. package/dist/views/header-footer/controls.d.ts.map +1 -0
  160. package/dist/views/header-footer/controls.js +43 -0
  161. package/dist/views/header-footer/controls.js.map +1 -0
  162. package/package.json +3 -3
  163. package/src/AdminRoot.tsx +36 -6
  164. package/src/__tests__/components/batch-ai-actions.test.tsx +10 -0
  165. package/src/__tests__/lib/batch-wizard.test.ts +325 -0
  166. package/src/__tests__/lib/job-drawer.test.ts +2 -0
  167. package/src/__tests__/lib/job-view.test.ts +123 -2
  168. package/src/__tests__/lib/jobs-service.test.ts +92 -3
  169. package/src/__tests__/views/appearance-chrome.render.test.tsx +818 -0
  170. package/src/__tests__/views/header-footer-form.test.ts +333 -0
  171. package/src/__tests__/views/jobs-drawer.render.test.tsx +508 -2
  172. package/src/__tests__/views/jobs-list.render.test.tsx +8 -5
  173. package/src/__tests__/views/jobs-wizard.render.test.tsx +707 -0
  174. package/src/__tests__/views/run-with-ai.render.test.tsx +807 -0
  175. package/src/components/BatchAiActions.tsx +15 -2
  176. package/src/lib/api.ts +12 -1
  177. package/src/lib/batch-wizard.ts +313 -0
  178. package/src/lib/chrome-service.ts +200 -0
  179. package/src/lib/job-view.ts +95 -1
  180. package/src/lib/jobs-service.ts +92 -1
  181. package/src/styles/prenta-tokens.css +17 -0
  182. package/src/styles/theme.css +15 -0
  183. package/src/views/AppearanceChrome.tsx +704 -46
  184. package/src/views/CollectionList.tsx +20 -1
  185. package/src/views/Jobs/CancelJobDialog.tsx +20 -2
  186. package/src/views/Jobs/DrawerOverview.tsx +16 -3
  187. package/src/views/Jobs/DrawerResults.tsx +71 -2
  188. package/src/views/Jobs/DrawerReview.tsx +1 -1
  189. package/src/views/Jobs/JobDrawer.tsx +88 -4
  190. package/src/views/Jobs/JobsTable.tsx +11 -4
  191. package/src/views/Jobs/NewBatchJob.tsx +857 -0
  192. package/src/views/Jobs/RunWithAi.tsx +89 -0
  193. package/src/views/Jobs/WorkerStrip.tsx +16 -11
  194. package/src/views/Jobs/focus.ts +11 -0
  195. package/src/views/Jobs.tsx +212 -50
  196. package/src/views/MediaBrowser.tsx +20 -1
  197. package/src/views/Pages/PagesListView.tsx +28 -3
  198. package/src/views/Posts/PostsListView.tsx +26 -3
  199. package/src/views/header-footer/AnnouncementTab.tsx +231 -0
  200. package/src/views/header-footer/ChromeField.tsx +358 -0
  201. package/src/views/header-footer/ChromePreview.tsx +497 -0
  202. package/src/views/header-footer/DeveloperTab.tsx +331 -0
  203. package/src/views/header-footer/RegionTab.tsx +398 -0
  204. package/src/views/header-footer/chrome-form.ts +603 -0
  205. package/src/views/header-footer/code-samples.ts +146 -0
  206. package/src/views/header-footer/controls.tsx +166 -0
  207. package/dist/views/Jobs/placeholders.d.ts +0 -6
  208. package/dist/views/Jobs/placeholders.d.ts.map +0 -1
  209. package/dist/views/Jobs/placeholders.js +0 -20
  210. package/dist/views/Jobs/placeholders.js.map +0 -1
  211. package/src/views/Jobs/placeholders.tsx +0 -66
@@ -0,0 +1,89 @@
1
+ 'use client'
2
+
3
+ import { useId, useState } from 'react'
4
+ import { toast } from 'sonner'
5
+ import type { Job } from '@prenta/core'
6
+ import { MEDIA_SOURCE, type ListPick } from '../../lib/batch-wizard.js'
7
+ import { canActOnJobs, jobTotal } from '../../lib/job-view.js'
8
+ import { JobIcons } from './icons.js'
9
+ import { NewBatchJob } from './NewBatchJob.js'
10
+
11
+ export interface RunWithAiProps {
12
+ /** The picked documents, or why they can't run as one batch. */
13
+ pick: ListPick
14
+ /** The signed-in user's role: the roles that may queue jobs see the button. */
15
+ role?: string | null
16
+ /** The CMS config, for the wizard's content labels. */
17
+ config?: unknown
18
+ onNavigate: (path: string) => void
19
+ /** The bulk bar's own button style. */
20
+ className?: string
21
+ }
22
+
23
+ /** The toast after queuing: what was queued, and a way to the job in Jobs. */
24
+ function queuedToast(job: Job, onNavigate: (path: string) => void): void {
25
+ toast(`“${job.title}” queued · ${jobTotal(job)} documents`, {
26
+ action: {
27
+ label: 'View job',
28
+ onClick: () => onNavigate(`/jobs?job=${encodeURIComponent(job.id)}`),
29
+ },
30
+ })
31
+ }
32
+
33
+ /**
34
+ * A list's "Run with AI…" (TRACKING P-21): opens the New batch AI job wizard
35
+ * at step 1 with the picked documents, so step 2 shows "{n} selected". Picked
36
+ * images start on alt text. Only roles that may queue jobs see it, as for
37
+ * Jobs' New batch AI job (`canActOnJobs`).
38
+ *
39
+ * After queuing, the list keeps its selection, so another change can run on
40
+ * the same documents, and focus returns here. The toast links to the job.
41
+ * When the pick can't run as one batch (posts of two types, no images), the
42
+ * button stays focusable and says why.
43
+ */
44
+ export function RunWithAi({ pick, role = null, config, onNavigate, className }: RunWithAiProps) {
45
+ const [open, setOpen] = useState(false)
46
+ const reasonId = useId()
47
+ if (!canActOnJobs({ role, userId: null })) return null
48
+ const { selection, reason } = pick
49
+ return (
50
+ <>
51
+ <button
52
+ type="button"
53
+ data-run-with-ai=""
54
+ aria-haspopup="dialog"
55
+ aria-disabled={reason ? true : undefined}
56
+ aria-describedby={reason ? reasonId : undefined}
57
+ title={reason ?? undefined}
58
+ onClick={(event) => {
59
+ // Safari doesn't focus a clicked button, and the wizard returns
60
+ // focus to whatever had it when it opened.
61
+ event.currentTarget.focus()
62
+ if (selection) setOpen(true)
63
+ }}
64
+ className={`inline-flex items-center gap-1.5 aria-disabled:cursor-not-allowed aria-disabled:opacity-50 ${className ?? ''}`}
65
+ >
66
+ <JobIcons.spark size={13} />
67
+ Run with AI…
68
+ </button>
69
+ {reason ? (
70
+ <span id={reasonId} hidden>
71
+ {reason}
72
+ </span>
73
+ ) : null}
74
+ {open && selection ? (
75
+ <NewBatchJob
76
+ role={role}
77
+ config={config}
78
+ selection={selection}
79
+ operation={selection.source === MEDIA_SOURCE.id ? 'alt' : undefined}
80
+ onClose={() => setOpen(false)}
81
+ onQueued={(job) => {
82
+ setOpen(false)
83
+ queuedToast(job, onNavigate)
84
+ }}
85
+ />
86
+ ) : null}
87
+ </>
88
+ )
89
+ }
@@ -77,6 +77,8 @@ export interface WorkerStripProps {
77
77
  rows: JobRow[]
78
78
  /** Live is on and the tab is visible: the clock ticks every second. */
79
79
  ticking: boolean
80
+ /** The viewer may start a scan. Viewers get no Run now (the server refuses it). */
81
+ canRun: boolean
80
82
  onRunNow: (type: RunnableJobType) => Promise<void>
81
83
  }
82
84
 
@@ -91,6 +93,7 @@ export function WorkerStrip({
91
93
  loading,
92
94
  rows,
93
95
  ticking,
96
+ canRun,
94
97
  onRunNow,
95
98
  }: WorkerStripProps) {
96
99
  const now = useNow(ticking) + skewMs
@@ -125,17 +128,19 @@ export function WorkerStrip({
125
128
  <Text label={JOB_TYPES[type].label}>
126
129
  {when} {next ? <small>· {next}</small> : null}
127
130
  </Text>
128
- <BusyButton
129
- variant="secondary"
130
- size="sm"
131
- className="ml-auto"
132
- leftIcon={<JobIcons.play size={11} />}
133
- busy={busy === type}
134
- aria-label={`Run now: ${JOB_TYPES[type].label}`}
135
- onClick={() => void run(type)}
136
- >
137
- Run now
138
- </BusyButton>
131
+ {canRun ? (
132
+ <BusyButton
133
+ variant="secondary"
134
+ size="sm"
135
+ className="ml-auto"
136
+ leftIcon={<JobIcons.play size={11} />}
137
+ busy={busy === type}
138
+ aria-label={`Run now: ${JOB_TYPES[type].label}`}
139
+ onClick={() => void run(type)}
140
+ >
141
+ Run now
142
+ </BusyButton>
143
+ ) : null}
139
144
  </Cell>
140
145
  )
141
146
  })}
@@ -31,6 +31,17 @@ export function focusJobRow(id: string): void {
31
31
  else focusJobsFallback()
32
32
  }
33
33
 
34
+ /**
35
+ * Focus the open drawer's tab panel, where the job's new state shows, when a
36
+ * footer action's button went away (a cancel, a retry). The row is behind the
37
+ * drawer, which keeps focus inside itself.
38
+ */
39
+ export function focusJobDrawer(): void {
40
+ const drawer = document.querySelector<HTMLElement>('[data-job-drawer]')
41
+ const target = drawer?.querySelector<HTMLElement>('[role="tabpanel"]') ?? drawer
42
+ target?.focus()
43
+ }
44
+
34
45
  /**
35
46
  * Focus return for the Jobs dialogs. Radix's modal Dialog only returns focus
36
47
  * to a `Dialog.Trigger`, and these open from several controls, so the control
@@ -2,18 +2,25 @@
2
2
 
3
3
  import { useEffect, useMemo, useRef, useState } from 'react'
4
4
  import { toast } from 'sonner'
5
+ import { parseRouteQuery } from '../router/index.js'
5
6
  import { Button } from '../components/ui/Button.js'
6
7
  import { PageHeader } from '../components/ui/PageHeader.js'
7
8
  import { Segmented } from '../components/ui/Segmented.js'
9
+ import { batchSources, type BatchOperation, type BatchSelection } from '../lib/batch-wizard.js'
8
10
  import {
9
11
  JOB_FILTERS,
10
12
  JOB_TYPES,
13
+ canActOnJobs,
14
+ canRetryJob,
11
15
  countJobs,
12
- jobType,
16
+ jobActions,
17
+ jobTotal,
13
18
  matchJob,
19
+ retryPoint,
14
20
  toJobRows,
15
21
  type JobFilter,
16
22
  type JobRow,
23
+ type JobViewer,
17
24
  } from '../lib/job-view.js'
18
25
  import {
19
26
  JOBS_LIST_LIMIT,
@@ -24,20 +31,48 @@ import {
24
31
  } from '../lib/jobs-service.js'
25
32
  import { BusyButton } from './Jobs/BusyButton.js'
26
33
  import { CancelJobDialog, staleRunning } from './Jobs/CancelJobDialog.js'
27
- import { focusJobRow } from './Jobs/focus.js'
34
+ import { focusJobDrawer, focusJobRow } from './Jobs/focus.js'
28
35
  import { JobsTable, type JobDrawerTab } from './Jobs/JobsTable.js'
29
36
  import { JobIcons } from './Jobs/icons.js'
30
- import { JobDrawer } from './Jobs/JobDrawer.js'
31
- import { NewBatchJob } from './Jobs/placeholders.js'
37
+ import { JobDrawer, type DrawerNotice } from './Jobs/JobDrawer.js'
38
+ import { NewBatchJob } from './Jobs/NewBatchJob.js'
32
39
  import { usePageVisible, useJobsFeed } from './Jobs/useJobsFeed.js'
33
40
  import { WorkerStrip } from './Jobs/WorkerStrip.js'
34
41
 
35
42
  export interface JobsProps {
36
43
  onNavigate: (path: string) => void
44
+ /** Router path with its query. `?job=<id>` opens that job's drawer once the list has it. */
45
+ currentPath?: string
46
+ /**
47
+ * The signed-in user's role. It decides which job actions show: viewers
48
+ * get none (the server refuses them anyway). Without one, none show.
49
+ */
50
+ role?: string | null
51
+ /** The signed-in user's id: a batch's Retry shows to the person who queued it. */
52
+ userId?: string | null
53
+ /** The CMS config: the batch wizard lists its page and post collections. */
54
+ config?: unknown
37
55
  }
38
56
 
39
57
  const FILTER_OPTIONS = JOB_FILTERS.map(({ id, label }) => ({ id, label }))
40
58
 
59
+ /**
60
+ * The open wizard: from the header with nothing picked, or from a scan's
61
+ * "Fix with AI" with that issue's documents and operation (P-21).
62
+ */
63
+ interface WizardState {
64
+ selection: BatchSelection | null
65
+ operation?: BatchOperation
66
+ /** Opened from the drawer, which closes once the job is queued. */
67
+ fromDrawer: boolean
68
+ }
69
+
70
+ /** What a run, retry or cancel came to, for a toast and the drawer's live region. */
71
+ interface Outcome {
72
+ text: string
73
+ tone: 'info' | 'error'
74
+ }
75
+
41
76
  /** `.jb-livebtn`: pauses and resumes the 5-second refresh. */
42
77
  function LiveButton({ live, onToggle }: { live: boolean; onToggle: () => void }) {
43
78
  return (
@@ -65,10 +100,10 @@ function LiveButton({ live, onToggle }: { live: boolean; onToggle: () => void })
65
100
  * seconds while Live is on and the tab is visible. Filters and counts work
66
101
  * on the loaded rows.
67
102
  *
68
- * Opening a job sets `open` and shows its drawer (P-18). The batch wizard
69
- * (P-20) renders from `./Jobs/placeholders.js` until it's built.
103
+ * Opening a job sets `open` and shows its drawer (P-18). New batch AI job
104
+ * opens the wizard (P-20); a queued job is added at the top of the list.
70
105
  */
71
- export function Jobs({ onNavigate }: JobsProps) {
106
+ export function Jobs({ onNavigate, currentPath, role = null, userId = null, config }: JobsProps) {
72
107
  const [live, setLive] = useState(true)
73
108
  const visible = usePageVisible()
74
109
  const feed = useJobsFeed(live && visible)
@@ -78,15 +113,46 @@ export function Jobs({ onNavigate }: JobsProps) {
78
113
  const [open, setOpen] = useState<{ id: string; tab: JobDrawerTab } | null>(null)
79
114
  const [cancelTarget, setCancelTarget] = useState<JobRow | null>(null)
80
115
  const cancelledId = useRef<string | null>(null)
81
- const [wizard, setWizard] = useState(false)
116
+ const [wizard, setWizard] = useState<WizardState | null>(null)
82
117
  const [retryingId, setRetryingId] = useState<string | null>(null)
118
+ /** Jobs with a retry request out, so a second click can't post another. */
119
+ const retrying = useRef(new Set<string>())
83
120
  /** A row whose action button just went away; its open button takes focus if focus was lost. */
84
121
  const refocus = useRef<string | null>(null)
122
+ /**
123
+ * The latest outcome, which the open drawer announces too: screen readers
124
+ * are kept inside the drawer (aria-modal), away from the toaster. Cleared
125
+ * whenever a drawer opens or closes, so a drawer never announces an older one.
126
+ */
127
+ const [notice, setNotice] = useState<DrawerNotice | null>(null)
128
+ /** A cancel's outcome, held until its dialog has gone (`onClosed`). */
129
+ const cancelOutcome = useRef<Outcome | null>(null)
130
+ const viewer = useMemo<JobViewer>(() => ({ role, userId }), [role, userId])
131
+ const sources = useMemo(() => batchSources(config), [config])
132
+
133
+ // A link to a job (`/jobs?job=<id>`, the toast after "Run with AI…"): open
134
+ // its drawer once the list has loaded. Each value is handled once, so
135
+ // closing the drawer sticks.
136
+ const linkedJob = useMemo(() => {
137
+ const query = currentPath ?? (typeof window !== 'undefined' ? window.location.search : '')
138
+ return parseRouteQuery(query).get('job')
139
+ }, [currentPath])
140
+ const handledLink = useRef<string | null>(null)
141
+ useEffect(() => {
142
+ if (!linkedJob || handledLink.current === linkedJob || feed.status !== 'ready') return
143
+ handledLink.current = linkedJob
144
+ if (feed.jobs.some((job) => job.id === linkedJob)) {
145
+ setOpen({ id: linkedJob, tab: null })
146
+ setNotice(null)
147
+ }
148
+ }, [linkedJob, feed.status, feed.jobs])
85
149
 
86
150
  useEffect(() => {
87
151
  const id = refocus.current
88
152
  if (!id) return
89
153
  refocus.current = null
154
+ // The drawer places focus after its own footer's actions.
155
+ if (open) return
90
156
  const current = document.activeElement
91
157
  if (current && current !== document.body && current.isConnected) return
92
158
  focusJobRow(id)
@@ -112,56 +178,97 @@ export function Jobs({ onNavigate }: JobsProps) {
112
178
  const openRow = open ? rows.find((row) => row.id === open.id) : undefined
113
179
  const ready = feed.status === 'ready'
114
180
 
115
- const openAt = (id: string, tab: JobDrawerTab = null) => setOpen({ id, tab })
181
+ const openAt = (id: string, tab: JobDrawerTab = null) => {
182
+ setOpen({ id, tab })
183
+ setNotice(null)
184
+ }
185
+
186
+ /** Toast an outcome, and have an open drawer announce it. */
187
+ function report(text: string, tone: Outcome['tone'] = 'info') {
188
+ if (tone === 'error') toast.error(text)
189
+ else toast(text)
190
+ setNotice((prev) => ({ text, n: (prev?.n ?? 0) + 1 }))
191
+ }
116
192
 
117
193
  async function runNow(t: RunnableJobType) {
118
194
  const res = await runJobNow(t)
119
195
  if (!res.ok) {
120
- toast.error(`${JOB_TYPES[t].label} didn’t start: ${res.error}`)
196
+ report(`${JOB_TYPES[t].label} didn’t start: ${res.error}`, 'error')
121
197
  return
122
198
  }
123
199
  const { job, reused } = res.value
124
200
  upsert(job)
125
201
  if (reused) {
126
- toast(`${JOB_TYPES[t].label} is already ${job.status}. Reusing that job.`)
127
202
  openAt(job.id)
203
+ report(`${JOB_TYPES[t].label} is already ${job.status}. Reusing that job.`)
128
204
  } else {
129
- toast(`${JOB_TYPES[t].label} queued. It starts on the next worker run.`)
205
+ report(`${JOB_TYPES[t].label} queued. It starts on the next worker run.`)
130
206
  }
131
207
  void reload()
132
208
  }
133
209
 
134
- async function retry(j: JobRow) {
210
+ /**
211
+ * Retry a failed job from where it stopped, or ("Retry now") one waiting
212
+ * out a failed slice's backoff. A 409 means it was retried elsewhere or
213
+ * isn't waiting any more, so the list is read again before the answer shows.
214
+ */
215
+ async function retry(j: JobRow, now = false) {
216
+ if (retrying.current.has(j.id)) return
217
+ retrying.current.add(j.id)
135
218
  setRetryingId(j.id)
136
- const res = await retryJob(j.id)
137
- setRetryingId(null)
138
- if (!res.ok) {
139
- toast.error(`“${j.title}” wasn’t retried: ${res.error}`)
140
- return
219
+ try {
220
+ const res = await retryJob(j.id)
221
+ if (res.ok) {
222
+ refocus.current = j.id
223
+ upsert(res.value)
224
+ report(now ? 'Retrying on the next worker run' : `Retrying from ${retryPoint(j)}`)
225
+ void reload()
226
+ return
227
+ }
228
+ const message = `“${j.title}” wasn’t retried: ${res.error}`
229
+ if (res.status === 409) await reload()
230
+ report(message, res.status === 409 ? 'info' : 'error')
231
+ } finally {
232
+ retrying.current.delete(j.id)
233
+ setRetryingId(null)
141
234
  }
142
- refocus.current = j.id
143
- upsert(res.value)
144
- toast(`Retrying from ${jobType(j.type).unit === 'pages' ? 'page' : 'document'} ${j.done + 1}`)
145
- void reload()
146
235
  }
147
236
 
237
+ function askCancel(row: JobRow) {
238
+ cancelledId.current = row.id
239
+ setCancelTarget(row)
240
+ }
241
+
242
+ /**
243
+ * The dialog's Cancel job. A cancel that lands after the job ended comes
244
+ * back as the job, unchanged. A 409 (it ended, or was cancelled, first) is
245
+ * shown once the list has been read again, so the dialog closes onto it.
246
+ */
148
247
  async function confirmCancel(j: JobRow) {
149
248
  const res = await cancelJob(j.id)
150
249
  if (!res.ok) {
151
- toast.error(`“${j.title}” wasn’t cancelled: ${res.error}`)
250
+ const text = `“${j.title}” wasn’t cancelled: ${res.error}`
251
+ if (res.status === 409) await reload()
252
+ cancelOutcome.current = { text, tone: res.status === 409 ? 'info' : 'error' }
152
253
  return
153
254
  }
154
255
  const job = res.value
155
256
  upsert(job)
156
- if (job.status === 'cancelled') toast('Job cancelled')
157
- else if (staleRunning(j)) toast('Stopping on the next worker run')
158
- else if (job.status === 'queued' || job.status === 'running') toast('Stopping after this slice')
159
- else toast(`“${j.title}” had already finished.`)
257
+ let text: string
258
+ if (job.status === 'cancelled') text = 'Job cancelled'
259
+ else if (staleRunning(j)) text = 'Stopping on the next worker run'
260
+ else if (job.status === 'queued' || job.status === 'running') text = 'Stopping after this slice'
261
+ else text = `“${j.title}” had already finished.`
262
+ cancelOutcome.current = { text, tone: 'info' }
160
263
  void reload()
161
264
  }
162
265
 
163
266
  const deadFirst = dead[0]
164
267
  const firstReview = revJobs[0]
268
+ // The dialog follows the job as the list refreshes, so its copy fits the state it's in.
269
+ const cancelRow = cancelTarget
270
+ ? (rows.find((row) => row.id === cancelTarget.id) ?? cancelTarget)
271
+ : null
165
272
  const deadCause = deadFirst?.error?.replace(/\.\s*$/, '')
166
273
 
167
274
  return (
@@ -173,13 +280,16 @@ export function Jobs({ onNavigate }: JobsProps) {
173
280
  actions={
174
281
  <>
175
282
  <LiveButton live={live} onToggle={() => setLive((value) => !value)} />
176
- <Button
177
- data-jobs-new=""
178
- leftIcon={<JobIcons.spark size={14} />}
179
- onClick={() => setWizard(true)}
180
- >
181
- New batch AI job
182
- </Button>
283
+ {/* Like Run now: the server refuses a batch to anyone else. */}
284
+ {canActOnJobs(viewer) ? (
285
+ <Button
286
+ data-jobs-new=""
287
+ leftIcon={<JobIcons.spark size={14} />}
288
+ onClick={() => setWizard({ selection: null, fromDrawer: false })}
289
+ >
290
+ New batch AI job
291
+ </Button>
292
+ ) : null}
183
293
  </>
184
294
  }
185
295
  />
@@ -191,6 +301,7 @@ export function Jobs({ onNavigate }: JobsProps) {
191
301
  loading={feed.status === 'loading'}
192
302
  rows={rows}
193
303
  ticking={live && visible}
304
+ canRun={canActOnJobs(viewer)}
194
305
  onRunNow={runNow}
195
306
  />
196
307
 
@@ -211,14 +322,16 @@ export function Jobs({ onNavigate }: JobsProps) {
211
322
  <Button variant="secondary" size="sm" onClick={() => openAt(deadFirst.id, 'overview')}>
212
323
  See why
213
324
  </Button>
214
- <BusyButton
215
- size="sm"
216
- leftIcon={<JobIcons.retry size={12} />}
217
- busy={retryingId === deadFirst.id}
218
- onClick={() => void retry(deadFirst)}
219
- >
220
- Retry
221
- </BusyButton>
325
+ {canRetryJob(deadFirst, viewer) ? (
326
+ <BusyButton
327
+ size="sm"
328
+ leftIcon={<JobIcons.retry size={12} />}
329
+ busy={retryingId === deadFirst.id}
330
+ onClick={() => void retry(deadFirst)}
331
+ >
332
+ Retry
333
+ </BusyButton>
334
+ ) : null}
222
335
  </div>
223
336
  ) : null}
224
337
 
@@ -278,11 +391,9 @@ export function Jobs({ onNavigate }: JobsProps) {
278
391
  status={feed.status}
279
392
  error={feed.error}
280
393
  retryingId={retryingId}
394
+ viewer={viewer}
281
395
  onOpen={openAt}
282
- onCancel={(row) => {
283
- cancelledId.current = row.id
284
- setCancelTarget(row)
285
- }}
396
+ onCancel={askCancel}
286
397
  onRetry={(row) => void retry(row)}
287
398
  onReload={reload}
288
399
  />
@@ -295,18 +406,69 @@ export function Jobs({ onNavigate }: JobsProps) {
295
406
  tab={open.tab}
296
407
  now={feed.fetchedAt}
297
408
  worker={feed.worker}
298
- onClose={() => setOpen(null)}
409
+ actions={jobActions(openRow, viewer)}
410
+ retrying={retryingId === openRow.id}
411
+ notice={notice}
412
+ onCancel={() => askCancel(openRow)}
413
+ onRetry={(now) => retry(openRow, now)}
414
+ onClose={() => {
415
+ setOpen(null)
416
+ setNotice(null)
417
+ }}
299
418
  onReload={() => void reload()}
300
419
  onNavigate={onNavigate}
420
+ fixWithAi={
421
+ canActOnJobs(viewer)
422
+ ? {
423
+ sources,
424
+ onFix: (selection, operation) =>
425
+ setWizard({ selection, operation, fromDrawer: true }),
426
+ }
427
+ : undefined
428
+ }
429
+ />
430
+ ) : null}
431
+ {wizard ? (
432
+ <NewBatchJob
433
+ role={role}
434
+ config={config}
435
+ selection={wizard.selection}
436
+ operation={wizard.operation}
437
+ onClose={() => setWizard(null)}
438
+ onQueued={(job) => {
439
+ upsert(job)
440
+ if (wizard.fromDrawer) {
441
+ setOpen(null)
442
+ setNotice(null)
443
+ }
444
+ setWizard(null)
445
+ setFilter('all')
446
+ setType('all')
447
+ toast(`“${job.title}” queued · ${jobTotal(job)} documents`)
448
+ void reload()
449
+ }}
450
+ onFocusFallback={(job) => {
451
+ if (job) focusJobRow(job.id)
452
+ else if (open) focusJobDrawer()
453
+ else document.querySelector<HTMLElement>('[data-jobs-new]')?.focus()
454
+ }}
301
455
  />
302
456
  ) : null}
303
- {wizard ? <NewBatchJob onClose={() => setWizard(false)} /> : null}
304
457
  <CancelJobDialog
305
- job={cancelTarget}
458
+ job={cancelRow}
306
459
  onClose={() => setCancelTarget(null)}
307
460
  onConfirm={confirmCancel}
308
461
  onFocusFallback={() => {
309
- if (cancelledId.current) focusJobRow(cancelledId.current)
462
+ // Opened from the drawer, whose Cancel job went with the cancel.
463
+ if (open) focusJobDrawer()
464
+ else if (cancelledId.current) focusJobRow(cancelledId.current)
465
+ }}
466
+ onClosed={() => {
467
+ // Screen readers are kept inside the dialog while it's open, so a
468
+ // toast or the drawer's announcement then could go unread.
469
+ const outcome = cancelOutcome.current
470
+ cancelOutcome.current = null
471
+ if (outcome) report(outcome.text, outcome.tone)
310
472
  }}
311
473
  />
312
474
  </div>
@@ -38,6 +38,8 @@ import { useSheetFocus } from '../lib/sheet-focus.js'
38
38
  import { StatusBadge } from '../components/ui/StatusBadge.js'
39
39
  import { CountChip } from '../components/ui/CountChip.js'
40
40
  import { hasLowQualityAlt, MediaSeoAudit, type MediaSeoAuditItem } from './MediaSeoAudit.js'
41
+ import { pickedImages } from '../lib/batch-wizard.js'
42
+ import { RunWithAi } from './Jobs/RunWithAi.js'
41
43
 
42
44
  interface MediaItem {
43
45
  id: number | string
@@ -111,6 +113,10 @@ type MediaSortKey = 'name' | 'type' | 'size' | 'date'
111
113
 
112
114
  export interface MediaBrowserProps {
113
115
  onNavigate?: (path: string) => void
116
+ /** The signed-in user's role: "Run with AI…" shows to the roles that may queue jobs. */
117
+ role?: string | null
118
+ /** The CMS config, for the batch wizard. */
119
+ config?: unknown
114
120
  /** Router path including the query string. `?selected=<mediaId>` (written by
115
121
  * the command palette) opens that item's detail panel once the list loads. */
116
122
  currentPath?: string
@@ -152,7 +158,7 @@ function buildMediaApiUrl(folderSel: FolderSelection): string {
152
158
  return `${base}&folderId=${folderSel.folderId}`
153
159
  }
154
160
 
155
- export function MediaBrowser({ onNavigate, currentPath }: MediaBrowserProps) {
161
+ export function MediaBrowser({ onNavigate, currentPath, role, config }: MediaBrowserProps) {
156
162
  const [folderSel, setFolderSel] = useState<FolderSelection>({ type: 'smart', smart: 'all' })
157
163
  const [sidebarOpen, setSidebarOpen] = useState(true)
158
164
  // Below 769px the folder tree stacks above the grid, so it starts closed
@@ -786,6 +792,19 @@ export function MediaBrowser({ onNavigate, currentPath }: MediaBrowserProps) {
786
792
  {selectedMedia.length} file{selectedMedia.length !== 1 ? 's' : ''} selected
787
793
  </span>
788
794
  <div className="flex items-center gap-2">
795
+ {onNavigate ? (
796
+ <RunWithAi
797
+ pick={pickedImages(
798
+ mediaItems
799
+ .filter((item) => selectedMedia.includes(item.id) && isImageMedia(item))
800
+ .map((item) => String(item.id)),
801
+ )}
802
+ role={role}
803
+ config={config}
804
+ onNavigate={onNavigate}
805
+ className="border-input bg-card hover:bg-muted rounded-lg border px-3 py-1.5 text-sm transition-colors"
806
+ />
807
+ ) : null}
789
808
  <button
790
809
  onClick={async () => {
791
810
  for (const id of selectedMedia) await deleteMedia(id)