@purposeinplay/payload-ai-translate 0.1.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 (301) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +714 -0
  3. package/dist/alerts-collection.d.ts +21 -0
  4. package/dist/alerts-collection.js +159 -0
  5. package/dist/api.d.ts +4 -0
  6. package/dist/api.js +918 -0
  7. package/dist/bulk-translate-batches-collection.d.ts +29 -0
  8. package/dist/bulk-translate-batches-collection.js +404 -0
  9. package/dist/bulk-translate-units-collection.d.ts +35 -0
  10. package/dist/bulk-translate-units-collection.js +310 -0
  11. package/dist/client/estimated-cost-cell.d.ts +6 -0
  12. package/dist/client/estimated-cost-cell.js +12 -0
  13. package/dist/client/excluded-fields-field.d.ts +45 -0
  14. package/dist/client/excluded-fields-field.js +553 -0
  15. package/dist/client/field-translate-button.d.ts +6 -0
  16. package/dist/client/field-translate-button.js +199 -0
  17. package/dist/client/index.d.ts +6 -0
  18. package/dist/client/index.js +6 -0
  19. package/dist/client/lib/use-global-kill-switches.d.ts +20 -0
  20. package/dist/client/lib/use-global-kill-switches.js +58 -0
  21. package/dist/client/translate-button.d.ts +2 -0
  22. package/dist/client/translate-button.js +228 -0
  23. package/dist/client/translate-modal.d.ts +16 -0
  24. package/dist/client/translate-modal.js +549 -0
  25. package/dist/client/translation-progress.d.ts +10 -0
  26. package/dist/client/translation-progress.js +297 -0
  27. package/dist/components/TranslationNavGroup.d.ts +45 -0
  28. package/dist/components/TranslationNavGroup.js +104 -0
  29. package/dist/defaults.d.ts +11 -0
  30. package/dist/defaults.js +16 -0
  31. package/dist/endpoints/client-config.d.ts +44 -0
  32. package/dist/endpoints/client-config.js +145 -0
  33. package/dist/endpoints/estimate.d.ts +5 -0
  34. package/dist/endpoints/estimate.js +237 -0
  35. package/dist/endpoints/progress.d.ts +2 -0
  36. package/dist/endpoints/progress.js +314 -0
  37. package/dist/endpoints/translate.d.ts +11 -0
  38. package/dist/endpoints/translate.js +376 -0
  39. package/dist/endpoints/translation-hub/_helpers.d.ts +140 -0
  40. package/dist/endpoints/translation-hub/_helpers.js +297 -0
  41. package/dist/endpoints/translation-hub/active.d.ts +21 -0
  42. package/dist/endpoints/translation-hub/active.js +220 -0
  43. package/dist/endpoints/translation-hub/cancel.d.ts +22 -0
  44. package/dist/endpoints/translation-hub/cancel.js +233 -0
  45. package/dist/endpoints/translation-hub/enqueue.d.ts +70 -0
  46. package/dist/endpoints/translation-hub/enqueue.js +529 -0
  47. package/dist/endpoints/translation-hub/failures.d.ts +12 -0
  48. package/dist/endpoints/translation-hub/failures.js +67 -0
  49. package/dist/endpoints/translation-hub/force-reset.d.ts +20 -0
  50. package/dist/endpoints/translation-hub/force-reset.js +144 -0
  51. package/dist/endpoints/translation-hub/index.d.ts +21 -0
  52. package/dist/endpoints/translation-hub/index.js +20 -0
  53. package/dist/endpoints/translation-hub/list.d.ts +40 -0
  54. package/dist/endpoints/translation-hub/list.js +182 -0
  55. package/dist/endpoints/translation-hub/preflight.d.ts +19 -0
  56. package/dist/endpoints/translation-hub/preflight.js +141 -0
  57. package/dist/endpoints/translation-hub/retry-failed.d.ts +38 -0
  58. package/dist/endpoints/translation-hub/retry-failed.js +235 -0
  59. package/dist/endpoints/translation-hub/revert.d.ts +88 -0
  60. package/dist/endpoints/translation-hub/revert.js +405 -0
  61. package/dist/endpoints/translation-hub/status.d.ts +45 -0
  62. package/dist/endpoints/translation-hub/status.js +391 -0
  63. package/dist/endpoints/translation-hub/usage-summary.d.ts +114 -0
  64. package/dist/endpoints/translation-hub/usage-summary.js +481 -0
  65. package/dist/exports/client.d.ts +6 -0
  66. package/dist/exports/client.js +6 -0
  67. package/dist/exports/components.d.ts +6 -0
  68. package/dist/exports/components.js +5 -0
  69. package/dist/exports/index.d.ts +8 -0
  70. package/dist/exports/index.js +7 -0
  71. package/dist/exports/providers.d.ts +9 -0
  72. package/dist/exports/providers.js +5 -0
  73. package/dist/exports/views-client.d.ts +23 -0
  74. package/dist/exports/views-client.js +22 -0
  75. package/dist/exports/views.d.ts +30 -0
  76. package/dist/exports/views.js +29 -0
  77. package/dist/hooks/after-change-global.d.ts +4 -0
  78. package/dist/hooks/after-change-global.js +109 -0
  79. package/dist/hooks/after-change.d.ts +16 -0
  80. package/dist/hooks/after-change.js +205 -0
  81. package/dist/hooks/after-delete.d.ts +30 -0
  82. package/dist/hooks/after-delete.js +95 -0
  83. package/dist/index.d.ts +5 -0
  84. package/dist/index.js +5 -0
  85. package/dist/jobs-collection.d.ts +17 -0
  86. package/dist/jobs-collection.js +139 -0
  87. package/dist/lexical/classifier.d.ts +3 -0
  88. package/dist/lexical/classifier.js +108 -0
  89. package/dist/lexical/deserializer.d.ts +4 -0
  90. package/dist/lexical/deserializer.js +263 -0
  91. package/dist/lexical/placeholder-integrity.d.ts +6 -0
  92. package/dist/lexical/placeholder-integrity.js +21 -0
  93. package/dist/lexical/placeholders.d.ts +21 -0
  94. package/dist/lexical/placeholders.js +117 -0
  95. package/dist/lexical/serializer.d.ts +21 -0
  96. package/dist/lexical/serializer.js +233 -0
  97. package/dist/lexical/types.d.ts +32 -0
  98. package/dist/lexical/types.js +1 -0
  99. package/dist/lib/auth-diagnostics.d.ts +14 -0
  100. package/dist/lib/auth-diagnostics.js +19 -0
  101. package/dist/lib/batch-counts.d.ts +58 -0
  102. package/dist/lib/batch-counts.js +105 -0
  103. package/dist/lib/bulk-translate-migrations.d.ts +92 -0
  104. package/dist/lib/bulk-translate-migrations.js +153 -0
  105. package/dist/lib/coalescing-queue.d.ts +38 -0
  106. package/dist/lib/coalescing-queue.js +69 -0
  107. package/dist/lib/content-extractor.d.ts +16 -0
  108. package/dist/lib/content-extractor.js +410 -0
  109. package/dist/lib/content-hash.d.ts +1 -0
  110. package/dist/lib/content-hash.js +19 -0
  111. package/dist/lib/content-patcher.d.ts +15 -0
  112. package/dist/lib/content-patcher.js +293 -0
  113. package/dist/lib/cost-guards.d.ts +2 -0
  114. package/dist/lib/cost-guards.js +18 -0
  115. package/dist/lib/daily-spend-cap.d.ts +58 -0
  116. package/dist/lib/daily-spend-cap.js +233 -0
  117. package/dist/lib/effective-locales.d.ts +181 -0
  118. package/dist/lib/effective-locales.js +302 -0
  119. package/dist/lib/error-messages.d.ts +245 -0
  120. package/dist/lib/error-messages.js +626 -0
  121. package/dist/lib/events.d.ts +39 -0
  122. package/dist/lib/events.js +146 -0
  123. package/dist/lib/exclude-fields.d.ts +3 -0
  124. package/dist/lib/exclude-fields.js +64 -0
  125. package/dist/lib/field-breadcrumb.d.ts +31 -0
  126. package/dist/lib/field-breadcrumb.js +227 -0
  127. package/dist/lib/field-diff.d.ts +1 -0
  128. package/dist/lib/field-diff.js +25 -0
  129. package/dist/lib/field-empty.d.ts +2 -0
  130. package/dist/lib/field-empty.js +68 -0
  131. package/dist/lib/field-resolver.d.ts +3 -0
  132. package/dist/lib/field-resolver.js +164 -0
  133. package/dist/lib/group-soft-skips.d.ts +39 -0
  134. package/dist/lib/group-soft-skips.js +45 -0
  135. package/dist/lib/locale-merge.d.ts +44 -0
  136. package/dist/lib/locale-merge.js +357 -0
  137. package/dist/lib/locale-row-check.d.ts +30 -0
  138. package/dist/lib/locale-row-check.js +64 -0
  139. package/dist/lib/logger.d.ts +74 -0
  140. package/dist/lib/logger.js +97 -0
  141. package/dist/lib/manual-edit-guard.d.ts +128 -0
  142. package/dist/lib/manual-edit-guard.js +393 -0
  143. package/dist/lib/output-validation.d.ts +48 -0
  144. package/dist/lib/output-validation.js +148 -0
  145. package/dist/lib/payload-read.d.ts +16 -0
  146. package/dist/lib/payload-read.js +51 -0
  147. package/dist/lib/per-doc-claim.d.ts +90 -0
  148. package/dist/lib/per-doc-claim.js +140 -0
  149. package/dist/lib/per-doc-lock.d.ts +94 -0
  150. package/dist/lib/per-doc-lock.js +119 -0
  151. package/dist/lib/persist-usage.d.ts +91 -0
  152. package/dist/lib/persist-usage.js +116 -0
  153. package/dist/lib/progress-store.d.ts +103 -0
  154. package/dist/lib/progress-store.js +314 -0
  155. package/dist/lib/rate-limiter.d.ts +3 -0
  156. package/dist/lib/rate-limiter.js +53 -0
  157. package/dist/lib/snapshot-select.d.ts +43 -0
  158. package/dist/lib/snapshot-select.js +108 -0
  159. package/dist/lib/translate-prompt.d.ts +31 -0
  160. package/dist/lib/translate-prompt.js +66 -0
  161. package/dist/lib/translation-token-bucket.d.ts +57 -0
  162. package/dist/lib/translation-token-bucket.js +365 -0
  163. package/dist/lib/truncate-source-value.d.ts +1 -0
  164. package/dist/lib/truncate-source-value.js +27 -0
  165. package/dist/manual-edit-collection.d.ts +22 -0
  166. package/dist/manual-edit-collection.js +124 -0
  167. package/dist/plugin.d.ts +3 -0
  168. package/dist/plugin.js +934 -0
  169. package/dist/providers/ai-sdk-adapter.d.ts +35 -0
  170. package/dist/providers/ai-sdk-adapter.js +100 -0
  171. package/dist/providers/anthropic.d.ts +31 -0
  172. package/dist/providers/anthropic.js +66 -0
  173. package/dist/providers/custom.d.ts +36 -0
  174. package/dist/providers/custom.js +24 -0
  175. package/dist/providers/gemini.d.ts +20 -0
  176. package/dist/providers/gemini.js +48 -0
  177. package/dist/providers/mock.d.ts +2 -0
  178. package/dist/providers/mock.js +29 -0
  179. package/dist/providers/openai.d.ts +28 -0
  180. package/dist/providers/openai.js +69 -0
  181. package/dist/settings-global.d.ts +74 -0
  182. package/dist/settings-global.js +216 -0
  183. package/dist/tasks/bulk-translate-coordinator.d.ts +115 -0
  184. package/dist/tasks/bulk-translate-coordinator.js +708 -0
  185. package/dist/tasks/bulk-translate-doc-task.d.ts +142 -0
  186. package/dist/tasks/bulk-translate-doc-task.js +1000 -0
  187. package/dist/tasks/bulk-translate-janitor.d.ts +87 -0
  188. package/dist/tasks/bulk-translate-janitor.js +311 -0
  189. package/dist/tasks/translate-job-task.d.ts +51 -0
  190. package/dist/tasks/translate-job-task.js +154 -0
  191. package/dist/translate.d.ts +113 -0
  192. package/dist/translate.js +911 -0
  193. package/dist/translation-daily-spend-collection.d.ts +24 -0
  194. package/dist/translation-daily-spend-collection.js +133 -0
  195. package/dist/translation-rate-limits-collection.d.ts +30 -0
  196. package/dist/translation-rate-limits-collection.js +144 -0
  197. package/dist/types.d.ts +672 -0
  198. package/dist/types.js +1 -0
  199. package/dist/usage-collection.d.ts +14 -0
  200. package/dist/usage-collection.js +377 -0
  201. package/dist/views/BulkRunsHub/BatchRow.d.ts +32 -0
  202. package/dist/views/BulkRunsHub/BatchRow.js +1222 -0
  203. package/dist/views/BulkRunsHub/BucketRow.d.ts +62 -0
  204. package/dist/views/BulkRunsHub/BucketRow.js +982 -0
  205. package/dist/views/BulkRunsHub/BulkRunsHub.client.d.ts +18 -0
  206. package/dist/views/BulkRunsHub/BulkRunsHub.client.js +331 -0
  207. package/dist/views/BulkRunsHub/EmptyState.d.ts +6 -0
  208. package/dist/views/BulkRunsHub/EmptyState.js +64 -0
  209. package/dist/views/BulkRunsHub/FilterBar.d.ts +16 -0
  210. package/dist/views/BulkRunsHub/FilterBar.js +284 -0
  211. package/dist/views/BulkRunsHub/InFlightBanner.d.ts +14 -0
  212. package/dist/views/BulkRunsHub/InFlightBanner.js +59 -0
  213. package/dist/views/BulkRunsHub/StatusBadge.d.ts +64 -0
  214. package/dist/views/BulkRunsHub/StatusBadge.js +248 -0
  215. package/dist/views/BulkRunsHub/SummaryStrip.d.ts +22 -0
  216. package/dist/views/BulkRunsHub/SummaryStrip.js +249 -0
  217. package/dist/views/BulkRunsHub/bucket-grouping.d.ts +200 -0
  218. package/dist/views/BulkRunsHub/bucket-grouping.js +344 -0
  219. package/dist/views/BulkRunsHub/bucketFailureSummary.d.ts +9 -0
  220. package/dist/views/BulkRunsHub/bucketFailureSummary.js +36 -0
  221. package/dist/views/BulkRunsHub/dedupedStatusFetch.d.ts +5 -0
  222. package/dist/views/BulkRunsHub/dedupedStatusFetch.js +45 -0
  223. package/dist/views/BulkRunsHub/index.d.ts +17 -0
  224. package/dist/views/BulkRunsHub/index.js +80 -0
  225. package/dist/views/BulkRunsHub/urlFilters.d.ts +14 -0
  226. package/dist/views/BulkRunsHub/urlFilters.js +50 -0
  227. package/dist/views/BulkRunsHub/useBulkRunsList.d.ts +26 -0
  228. package/dist/views/BulkRunsHub/useBulkRunsList.js +204 -0
  229. package/dist/views/BulkRunsHub/useUrlFilters.d.ts +10 -0
  230. package/dist/views/BulkRunsHub/useUrlFilters.js +88 -0
  231. package/dist/views/TranslationHub/ActiveJobs.d.ts +6 -0
  232. package/dist/views/TranslationHub/ActiveJobs.js +320 -0
  233. package/dist/views/TranslationHub/AdvancedPanel.d.ts +17 -0
  234. package/dist/views/TranslationHub/AdvancedPanel.js +996 -0
  235. package/dist/views/TranslationHub/AlertBanner.d.ts +6 -0
  236. package/dist/views/TranslationHub/AlertBanner.js +568 -0
  237. package/dist/views/TranslationHub/AuditPanel.d.ts +6 -0
  238. package/dist/views/TranslationHub/AuditPanel.helpers.d.ts +44 -0
  239. package/dist/views/TranslationHub/AuditPanel.helpers.js +71 -0
  240. package/dist/views/TranslationHub/AuditPanel.js +1367 -0
  241. package/dist/views/TranslationHub/BulkTranslate.types.d.ts +242 -0
  242. package/dist/views/TranslationHub/BulkTranslate.types.js +36 -0
  243. package/dist/views/TranslationHub/BulkTranslateFailureDrawer.d.ts +19 -0
  244. package/dist/views/TranslationHub/BulkTranslateFailureDrawer.js +332 -0
  245. package/dist/views/TranslationHub/BulkTranslateMonitor.d.ts +28 -0
  246. package/dist/views/TranslationHub/BulkTranslateMonitor.js +305 -0
  247. package/dist/views/TranslationHub/BulkTranslateNarrowViewportBanner.d.ts +3 -0
  248. package/dist/views/TranslationHub/BulkTranslateNarrowViewportBanner.js +42 -0
  249. package/dist/views/TranslationHub/BulkTranslatePostEnqueueTransition.d.ts +26 -0
  250. package/dist/views/TranslationHub/BulkTranslatePostEnqueueTransition.js +95 -0
  251. package/dist/views/TranslationHub/BulkTranslatePreflightModal.d.ts +22 -0
  252. package/dist/views/TranslationHub/BulkTranslatePreflightModal.js +879 -0
  253. package/dist/views/TranslationHub/BulkTranslateTerminalCard.d.ts +29 -0
  254. package/dist/views/TranslationHub/BulkTranslateTerminalCard.js +445 -0
  255. package/dist/views/TranslationHub/BulkTranslateTrigger.d.ts +66 -0
  256. package/dist/views/TranslationHub/BulkTranslateTrigger.js +161 -0
  257. package/dist/views/TranslationHub/EditorRecentRunsPanel.d.ts +33 -0
  258. package/dist/views/TranslationHub/EditorRecentRunsPanel.js +290 -0
  259. package/dist/views/TranslationHub/Hub.client.d.ts +74 -0
  260. package/dist/views/TranslationHub/Hub.client.js +357 -0
  261. package/dist/views/TranslationHub/ModelCombobox.d.ts +14 -0
  262. package/dist/views/TranslationHub/ModelCombobox.js +415 -0
  263. package/dist/views/TranslationHub/PerCollectionConfig.d.ts +10 -0
  264. package/dist/views/TranslationHub/PerCollectionConfig.helpers.d.ts +16 -0
  265. package/dist/views/TranslationHub/PerCollectionConfig.helpers.js +19 -0
  266. package/dist/views/TranslationHub/PerCollectionConfig.js +759 -0
  267. package/dist/views/TranslationHub/SettingsRail.d.ts +11 -0
  268. package/dist/views/TranslationHub/SettingsRail.js +382 -0
  269. package/dist/views/TranslationHub/StatusStrip.d.ts +6 -0
  270. package/dist/views/TranslationHub/StatusStrip.js +451 -0
  271. package/dist/views/TranslationHub/UsageTable.d.ts +6 -0
  272. package/dist/views/TranslationHub/UsageTable.helpers.d.ts +69 -0
  273. package/dist/views/TranslationHub/UsageTable.helpers.js +49 -0
  274. package/dist/views/TranslationHub/UsageTable.js +1240 -0
  275. package/dist/views/TranslationHub/alertGrouping.d.ts +70 -0
  276. package/dist/views/TranslationHub/alertGrouping.js +99 -0
  277. package/dist/views/TranslationHub/index.d.ts +20 -0
  278. package/dist/views/TranslationHub/index.js +109 -0
  279. package/dist/views/TranslationHub/tabNavigation.d.ts +53 -0
  280. package/dist/views/TranslationHub/tabNavigation.js +74 -0
  281. package/dist/views/TranslationHub/terminalBannerVisibility.d.ts +33 -0
  282. package/dist/views/TranslationHub/terminalBannerVisibility.js +124 -0
  283. package/dist/views/TranslationHub/useBulkTranslateActive.d.ts +49 -0
  284. package/dist/views/TranslationHub/useBulkTranslateActive.js +251 -0
  285. package/dist/views/TranslationHub/useFocusTrap.d.ts +6 -0
  286. package/dist/views/TranslationHub/useFocusTrap.js +81 -0
  287. package/dist/views/TranslationHub/useTranslationHubUsageSummary.d.ts +77 -0
  288. package/dist/views/TranslationHub/useTranslationHubUsageSummary.js +267 -0
  289. package/dist/views/shared/EditorError.d.ts +97 -0
  290. package/dist/views/shared/EditorError.js +205 -0
  291. package/dist/views/shared/ModelCell.d.ts +18 -0
  292. package/dist/views/shared/ModelCell.js +31 -0
  293. package/dist/views/shared/docHref.d.ts +16 -0
  294. package/dist/views/shared/docHref.js +26 -0
  295. package/dist/views/shared/fetch-error-body.d.ts +25 -0
  296. package/dist/views/shared/fetch-error-body.js +42 -0
  297. package/dist/views/shared/filterPillStyle.d.ts +35 -0
  298. package/dist/views/shared/filterPillStyle.js +40 -0
  299. package/dist/views/shared/format.d.ts +75 -0
  300. package/dist/views/shared/format.js +131 -0
  301. package/package.json +141 -0
@@ -0,0 +1,451 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useEffect, useMemo, useState } from 'react';
4
+ import { filterPillColors } from '../shared/filterPillStyle.js';
5
+ import { formatCost } from '../shared/format.js';
6
+ import { useTranslationHubUsageSummary } from './useTranslationHubUsageSummary.js';
7
+ const CARD_STYLE = {
8
+ flex: 1,
9
+ padding: '1rem 1.25rem',
10
+ background: 'var(--theme-elevation-50)',
11
+ border: '1px solid var(--theme-elevation-150)',
12
+ borderRadius: '6px'
13
+ };
14
+ const LABEL_STYLE = {
15
+ margin: 0,
16
+ fontSize: '0.75rem',
17
+ fontWeight: 600,
18
+ textTransform: 'uppercase',
19
+ letterSpacing: '0.05em',
20
+ color: 'var(--theme-elevation-500)'
21
+ };
22
+ const VALUE_STYLE = {
23
+ margin: '0.25rem 0 0',
24
+ fontSize: '1.5rem',
25
+ fontWeight: 600,
26
+ color: 'var(--theme-elevation-1000)'
27
+ };
28
+ const SUBVALUE_STYLE = {
29
+ margin: '0.25rem 0 0',
30
+ fontSize: '0.75rem',
31
+ color: 'var(--theme-elevation-500)'
32
+ };
33
+ function fmtNum(n) {
34
+ if (n >= 1_000_000) {
35
+ return `${(n / 1_000_000).toFixed(1)}M`;
36
+ }
37
+ if (n >= 1000) {
38
+ return `${(n / 1000).toFixed(1)}K`;
39
+ }
40
+ return n.toString();
41
+ }
42
+ const TIME_RANGE_LABEL = {
43
+ '7d': 'Last 7 days',
44
+ '30d': 'Last 30 days',
45
+ '90d': 'Last 90 days',
46
+ all: 'All time',
47
+ custom: 'Custom'
48
+ };
49
+ export const StatusStrip = ({ basePath })=>{
50
+ const [stats, setStats] = useState(null);
51
+ const [error, setError] = useState(null);
52
+ const [range, setRange] = useState('7d');
53
+ // HUB-5 (v1.2.6): Custom range inputs mirror AuditPanel's pattern.
54
+ const [customFrom, setCustomFrom] = useState('');
55
+ const [customTo, setCustomTo] = useState('');
56
+ // Total translatable surfaces (collections + globals registered in
57
+ // plugin config). Used to label the per-collection breakdown as
58
+ // "X of Y active" so editors don't misread `(8)` as "we lost 2
59
+ // surfaces." See NEW-23.
60
+ const [totalSurfaces, setTotalSurfaces] = useState(null);
61
+ useEffect(()=>{
62
+ let cancelled = false;
63
+ fetch(`${basePath}/api/ai-translate/client-config`, {
64
+ credentials: 'include'
65
+ }).then((r)=>r.ok ? r.json() : Promise.reject(new Error(`HTTP ${r.status}`))).then((d)=>{
66
+ if (cancelled) return;
67
+ setTotalSurfaces(Object.keys(d.translatableFieldsBySlug ?? {}).length);
68
+ }).catch(()=>{
69
+ // Non-fatal: the breakdown still renders with just the active count.
70
+ });
71
+ return ()=>{
72
+ cancelled = true;
73
+ };
74
+ }, [
75
+ basePath
76
+ ]);
77
+ // ROUND2-4: switched from per-mount `fetch` + `useEffect` to the
78
+ // module-level singleton hook. StatusStrip and AuditPanel now share
79
+ // one in-flight request per `(basePath, range)` key, collapsing the
80
+ // paired-burst pattern (and the React 18 StrictMode dev-double-render
81
+ // double-billing) into a single call per Hub mount. Mirrors the
82
+ // NEW-5 fix to `useBulkTranslateActive`.
83
+ const rangeKey = useMemo(()=>{
84
+ if (range === 'custom') {
85
+ return {
86
+ kind: 'custom',
87
+ ...customFrom ? {
88
+ from: customFrom
89
+ } : {},
90
+ ...customTo ? {
91
+ to: customTo
92
+ } : {}
93
+ };
94
+ }
95
+ return {
96
+ kind: range
97
+ };
98
+ }, [
99
+ range,
100
+ customFrom,
101
+ customTo
102
+ ]);
103
+ const { data: summary, error: summaryError, refetch: refetchSummary } = useTranslationHubUsageSummary(basePath, rangeKey);
104
+ useEffect(()=>{
105
+ if (summaryError) {
106
+ setError(summaryError);
107
+ return;
108
+ }
109
+ setError(null);
110
+ if (!summary) return;
111
+ const nextStats = {
112
+ totalRuns: summary.totals.runs,
113
+ totalSucceeded: summary.totals.succeeded,
114
+ totalPreserved: summary.totals.preserved ?? 0,
115
+ totalFailed: summary.totals.failed,
116
+ totalInputTokens: summary.totals.inputTokens,
117
+ totalOutputTokens: summary.totals.outputTokens,
118
+ totalCostUsd: summary.totals.costUsd,
119
+ truncated: summary.totals.truncated,
120
+ totalMatching: summary.totals.totalMatching,
121
+ perCollection: summary.byCollection.map((b)=>({
122
+ slug: b.slug,
123
+ runs: b.runs,
124
+ tokens: b.tokens,
125
+ costUsd: b.costUsd
126
+ }))
127
+ };
128
+ setStats(nextStats);
129
+ }, [
130
+ summary,
131
+ summaryError
132
+ ]);
133
+ if (error) {
134
+ return /*#__PURE__*/ _jsxs("div", {
135
+ style: {
136
+ padding: '0.5rem 0.75rem',
137
+ color: 'var(--theme-error-500, #b91c1c)',
138
+ fontSize: '0.875rem',
139
+ display: 'flex',
140
+ alignItems: 'center',
141
+ gap: '0.75rem',
142
+ flexWrap: 'wrap'
143
+ },
144
+ children: [
145
+ /*#__PURE__*/ _jsx("span", {
146
+ children: "Couldn’t load the translation stats. Refresh the page, or contact engineering if it keeps happening."
147
+ }),
148
+ /*#__PURE__*/ _jsx("button", {
149
+ onClick: ()=>refetchSummary(),
150
+ style: {
151
+ padding: '0.3rem 0.75rem',
152
+ background: 'transparent',
153
+ border: '1px solid var(--theme-error-500, #b91c1c)',
154
+ borderRadius: '4px',
155
+ color: 'var(--theme-error-500, #b91c1c)',
156
+ fontSize: '0.75rem',
157
+ fontWeight: 500,
158
+ cursor: 'pointer'
159
+ },
160
+ type: "button",
161
+ children: "Retry"
162
+ })
163
+ ]
164
+ });
165
+ }
166
+ return /*#__PURE__*/ _jsxs("div", {
167
+ style: {
168
+ display: 'flex',
169
+ flexDirection: 'column',
170
+ gap: '0.75rem'
171
+ },
172
+ children: [
173
+ /*#__PURE__*/ _jsxs("div", {
174
+ style: {
175
+ display: 'flex',
176
+ alignItems: 'center',
177
+ justifyContent: 'space-between',
178
+ gap: '1rem'
179
+ },
180
+ children: [
181
+ /*#__PURE__*/ _jsxs("div", {
182
+ style: {
183
+ display: 'flex',
184
+ gap: '0.25rem',
185
+ alignItems: 'center',
186
+ flexWrap: 'wrap'
187
+ },
188
+ children: [
189
+ Object.keys(TIME_RANGE_LABEL).map((t)=>{
190
+ const isActive = range === t;
191
+ const colors = filterPillColors(isActive);
192
+ return /*#__PURE__*/ _jsx("button", {
193
+ "aria-pressed": isActive,
194
+ onClick: ()=>setRange(t),
195
+ style: {
196
+ padding: '0.3rem 0.65rem',
197
+ fontSize: '0.75rem',
198
+ background: colors.background,
199
+ border: colors.border,
200
+ borderRadius: '4px',
201
+ color: colors.color,
202
+ cursor: 'pointer',
203
+ fontWeight: isActive ? 600 : 400
204
+ },
205
+ type: "button",
206
+ children: TIME_RANGE_LABEL[t]
207
+ }, t);
208
+ }),
209
+ range === 'custom' && /*#__PURE__*/ _jsxs("div", {
210
+ style: {
211
+ display: 'flex',
212
+ gap: '0.5rem',
213
+ alignItems: 'center',
214
+ marginLeft: '0.5rem'
215
+ },
216
+ children: [
217
+ /*#__PURE__*/ _jsxs("label", {
218
+ style: {
219
+ fontSize: '0.75rem',
220
+ color: 'var(--theme-elevation-700)'
221
+ },
222
+ children: [
223
+ "From",
224
+ ' ',
225
+ /*#__PURE__*/ _jsx("input", {
226
+ onChange: (e)=>setCustomFrom(e.target.value),
227
+ style: {
228
+ padding: '0.25rem',
229
+ fontSize: '0.75rem'
230
+ },
231
+ type: "date",
232
+ value: customFrom,
233
+ max: customTo || undefined
234
+ })
235
+ ]
236
+ }),
237
+ /*#__PURE__*/ _jsxs("label", {
238
+ style: {
239
+ fontSize: '0.75rem',
240
+ color: 'var(--theme-elevation-700)'
241
+ },
242
+ children: [
243
+ "To",
244
+ ' ',
245
+ /*#__PURE__*/ _jsx("input", {
246
+ onChange: (e)=>setCustomTo(e.target.value),
247
+ style: {
248
+ padding: '0.25rem',
249
+ fontSize: '0.75rem'
250
+ },
251
+ type: "date",
252
+ value: customTo,
253
+ min: customFrom || undefined
254
+ })
255
+ ]
256
+ })
257
+ ]
258
+ })
259
+ ]
260
+ }),
261
+ stats?.truncated && /*#__PURE__*/ _jsxs("span", {
262
+ style: {
263
+ fontSize: '0.75rem',
264
+ color: 'var(--theme-warning-500, #d97706)'
265
+ },
266
+ title: `Showing ${stats.totalRuns} of ${stats.totalMatching} matching rows — totals are partial. Narrow the time range or filter on the underlying collection for full accuracy.`,
267
+ children: [
268
+ "⚠ Showing ",
269
+ stats.totalRuns,
270
+ " of ",
271
+ stats.totalMatching
272
+ ]
273
+ })
274
+ ]
275
+ }),
276
+ /*#__PURE__*/ _jsxs("div", {
277
+ style: {
278
+ display: 'flex',
279
+ gap: '1rem'
280
+ },
281
+ children: [
282
+ /*#__PURE__*/ _jsxs("div", {
283
+ style: CARD_STYLE,
284
+ children: [
285
+ /*#__PURE__*/ _jsx("p", {
286
+ style: LABEL_STYLE,
287
+ children: "Total runs"
288
+ }),
289
+ /*#__PURE__*/ _jsx("p", {
290
+ style: VALUE_STYLE,
291
+ children: stats ? fmtNum(stats.totalRuns) : '—'
292
+ }),
293
+ /*#__PURE__*/ _jsx("p", {
294
+ style: SUBVALUE_STYLE,
295
+ title: "A run counts as succeeded only if it wrote at least one field. Preserved-only runs (no LLM call needed — source unchanged or manual-edit guard) are reported separately to match the Audit & Cost tab.",
296
+ children: stats ? `${stats.totalSucceeded} succeeded · ${stats.totalFailed} failed${stats.totalPreserved > 0 ? ` · ${stats.totalPreserved} preserved` : ''}` : 'Loading…'
297
+ })
298
+ ]
299
+ }),
300
+ /*#__PURE__*/ _jsxs("div", {
301
+ style: CARD_STYLE,
302
+ children: [
303
+ /*#__PURE__*/ _jsx("p", {
304
+ style: LABEL_STYLE,
305
+ children: "Tokens consumed"
306
+ }),
307
+ /*#__PURE__*/ _jsx("p", {
308
+ style: VALUE_STYLE,
309
+ children: stats ? fmtNum(stats.totalInputTokens + stats.totalOutputTokens) : '—'
310
+ }),
311
+ /*#__PURE__*/ _jsx("p", {
312
+ style: SUBVALUE_STYLE,
313
+ children: stats ? `${fmtNum(stats.totalInputTokens)} in · ${fmtNum(stats.totalOutputTokens)} out` : 'Loading…'
314
+ })
315
+ ]
316
+ }),
317
+ /*#__PURE__*/ _jsxs("div", {
318
+ style: CARD_STYLE,
319
+ children: [
320
+ /*#__PURE__*/ _jsx("p", {
321
+ style: LABEL_STYLE,
322
+ children: "Estimated cost"
323
+ }),
324
+ /*#__PURE__*/ _jsx("p", {
325
+ style: VALUE_STYLE,
326
+ children: stats ? formatCost(stats.totalCostUsd) : '—'
327
+ }),
328
+ /*#__PURE__*/ _jsxs("p", {
329
+ style: SUBVALUE_STYLE,
330
+ title: "Pre-computed server-side from per-row cost_usd. Preserved-only and skipped rows contribute zero. Accurate at any window size.",
331
+ children: [
332
+ TIME_RANGE_LABEL[range],
333
+ " · server-aggregated from recorded costs"
334
+ ]
335
+ })
336
+ ]
337
+ })
338
+ ]
339
+ }),
340
+ stats && stats.perCollection.length > 1 && /*#__PURE__*/ _jsxs("details", {
341
+ style: {
342
+ padding: '0.5rem 0.75rem',
343
+ background: 'var(--theme-elevation-50)',
344
+ border: '1px solid var(--theme-elevation-150)',
345
+ borderRadius: '6px',
346
+ fontSize: '0.8rem'
347
+ },
348
+ children: [
349
+ /*#__PURE__*/ _jsxs("summary", {
350
+ style: {
351
+ cursor: 'pointer',
352
+ color: 'var(--theme-elevation-800)',
353
+ fontWeight: 500
354
+ },
355
+ title: totalSurfaces !== null ? `${stats.perCollection.length} surface${stats.perCollection.length === 1 ? '' : 's'} had translation activity in this window. ${totalSurfaces} surface${totalSurfaces === 1 ? ' is' : 's are'} translatable overall.` : undefined,
356
+ children: [
357
+ "Per-collection breakdown (",
358
+ totalSurfaces !== null && totalSurfaces >= stats.perCollection.length ? `${stats.perCollection.length} of ${totalSurfaces} active` : stats.perCollection.length,
359
+ ")"
360
+ ]
361
+ }),
362
+ /*#__PURE__*/ _jsxs("table", {
363
+ style: {
364
+ width: '100%',
365
+ borderCollapse: 'collapse',
366
+ fontSize: '0.75rem',
367
+ marginTop: '0.5rem'
368
+ },
369
+ children: [
370
+ /*#__PURE__*/ _jsx("thead", {
371
+ children: /*#__PURE__*/ _jsxs("tr", {
372
+ style: {
373
+ color: 'var(--theme-elevation-500)'
374
+ },
375
+ children: [
376
+ /*#__PURE__*/ _jsx("th", {
377
+ style: {
378
+ textAlign: 'left',
379
+ padding: '0.25rem'
380
+ },
381
+ children: "Collection / global"
382
+ }),
383
+ /*#__PURE__*/ _jsx("th", {
384
+ style: {
385
+ textAlign: 'right',
386
+ padding: '0.25rem'
387
+ },
388
+ children: "Runs"
389
+ }),
390
+ /*#__PURE__*/ _jsx("th", {
391
+ style: {
392
+ textAlign: 'right',
393
+ padding: '0.25rem'
394
+ },
395
+ children: "Tokens"
396
+ }),
397
+ /*#__PURE__*/ _jsx("th", {
398
+ style: {
399
+ textAlign: 'right',
400
+ padding: '0.25rem'
401
+ },
402
+ children: "Cost"
403
+ })
404
+ ]
405
+ })
406
+ }),
407
+ /*#__PURE__*/ _jsx("tbody", {
408
+ children: stats.perCollection.map((b)=>/*#__PURE__*/ _jsxs("tr", {
409
+ children: [
410
+ /*#__PURE__*/ _jsx("td", {
411
+ style: {
412
+ padding: '0.25rem'
413
+ },
414
+ children: /*#__PURE__*/ _jsx("code", {
415
+ children: b.slug
416
+ })
417
+ }),
418
+ /*#__PURE__*/ _jsx("td", {
419
+ style: {
420
+ padding: '0.25rem',
421
+ textAlign: 'right',
422
+ color: 'var(--theme-elevation-800)'
423
+ },
424
+ children: fmtNum(b.runs)
425
+ }),
426
+ /*#__PURE__*/ _jsx("td", {
427
+ style: {
428
+ padding: '0.25rem',
429
+ textAlign: 'right',
430
+ color: 'var(--theme-elevation-800)'
431
+ },
432
+ children: fmtNum(b.tokens)
433
+ }),
434
+ /*#__PURE__*/ _jsx("td", {
435
+ style: {
436
+ padding: '0.25rem',
437
+ textAlign: 'right',
438
+ color: 'var(--theme-elevation-800)'
439
+ },
440
+ children: formatCost(b.costUsd)
441
+ })
442
+ ]
443
+ }, b.slug))
444
+ })
445
+ ]
446
+ })
447
+ ]
448
+ })
449
+ ]
450
+ });
451
+ };
@@ -0,0 +1,6 @@
1
+ import type React from 'react';
2
+ interface Props {
3
+ basePath: string;
4
+ }
5
+ export declare const UsageTable: React.FC<Props>;
6
+ export {};
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Re-exports the pure helper functions from UsageTable.tsx so they can be
3
+ * unit-tested without importing the full React component (which requires
4
+ * jsdom and drags in browser-only deps).
5
+ *
6
+ * These three functions have zero side effects and no I/O — extracting them
7
+ * here is the minimal change needed to make them testable. The component
8
+ * continues to import them from here rather than defining them inline.
9
+ */
10
+ export type UsageRow = {
11
+ id: number;
12
+ kind: 'collection' | 'global';
13
+ slug: string;
14
+ documentId?: string | null;
15
+ status: 'succeeded' | 'failed';
16
+ sourceLocale: string;
17
+ succeededCount: number;
18
+ failedCount: number;
19
+ fieldsTranslated?: number | null;
20
+ fieldsHashSkipped?: number | null;
21
+ fieldsPreserved?: number | null;
22
+ fieldsSoftSkipped?: number | null;
23
+ targetLocales?: Array<{
24
+ locale: string;
25
+ status?: 'succeeded' | 'failed' | null;
26
+ error?: string | null;
27
+ /** Editor-facing code (1.2.5+) used by the UI to render a friendly
28
+ * per-locale error chip. Empty on legacy rows. */
29
+ errorCode?: string | null;
30
+ failedFields?: Array<{
31
+ path: string;
32
+ }> | null;
33
+ }> | null;
34
+ softSkippedFields?: Array<{
35
+ path: string;
36
+ locale: string;
37
+ reason?: string | null;
38
+ /** Editor-facing code (1.2.5+) used by the UI to render a friendly
39
+ * soft-skip reason chip. Empty on legacy rows. */
40
+ reasonCode?: string | null;
41
+ /** Source-locale text the AI kept as-is (1.2.8+). Empty on legacy rows. */
42
+ sourceValue?: string | null;
43
+ }> | null;
44
+ preservedFields?: Array<{
45
+ path: string;
46
+ locale: string;
47
+ }> | null;
48
+ inputTokens: number;
49
+ outputTokens: number;
50
+ estimatedCostUsd?: number | null;
51
+ model?: string | null;
52
+ durationMs?: number | null;
53
+ error?: string | null;
54
+ createdAt: string;
55
+ };
56
+ export type FieldBreakdown = {
57
+ translated: number;
58
+ skipped: number;
59
+ hashSkipped: number;
60
+ preserved: number;
61
+ softSkipped: number;
62
+ failed: number;
63
+ hasWork: boolean;
64
+ hasBreakdown: boolean;
65
+ };
66
+ export type DerivedStatus = 'failed' | 'needs-review' | 'succeeded' | 'no-changes' | 'preserved' | 'legacy';
67
+ export declare function deriveBreakdown(r: UsageRow): FieldBreakdown;
68
+ export declare function buildSkippedTooltip(b: FieldBreakdown): string;
69
+ export declare function deriveStatus(r: UsageRow, b: FieldBreakdown): DerivedStatus;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Re-exports the pure helper functions from UsageTable.tsx so they can be
3
+ * unit-tested without importing the full React component (which requires
4
+ * jsdom and drags in browser-only deps).
5
+ *
6
+ * These three functions have zero side effects and no I/O — extracting them
7
+ * here is the minimal change needed to make them testable. The component
8
+ * continues to import them from here rather than defining them inline.
9
+ */ // ---------------------------------------------------------------------------
10
+ // Types (duplicated from UsageTable.tsx to keep this file self-contained)
11
+ // ---------------------------------------------------------------------------
12
+ // ---------------------------------------------------------------------------
13
+ // Pure helpers
14
+ // ---------------------------------------------------------------------------
15
+ export function deriveBreakdown(r) {
16
+ const translated = r.fieldsTranslated ?? 0;
17
+ const hashSkipped = r.fieldsHashSkipped ?? 0;
18
+ const preserved = r.fieldsPreserved ?? 0;
19
+ const softSkipped = r.fieldsSoftSkipped ?? 0;
20
+ const failed = r.failedCount;
21
+ const skipped = hashSkipped + preserved + softSkipped;
22
+ const hasBreakdown = r.fieldsTranslated != null || r.fieldsHashSkipped != null || r.fieldsPreserved != null || r.fieldsSoftSkipped != null;
23
+ return {
24
+ translated,
25
+ skipped,
26
+ hashSkipped,
27
+ preserved,
28
+ softSkipped,
29
+ failed,
30
+ hasWork: translated > 0,
31
+ hasBreakdown
32
+ };
33
+ }
34
+ export function buildSkippedTooltip(b) {
35
+ const parts = [];
36
+ if (b.hashSkipped > 0) parts.push(`${b.hashSkipped} unchanged`);
37
+ if (b.preserved > 0) parts.push(`${b.preserved} preserved (manual edit)`);
38
+ if (b.softSkipped > 0) parts.push(`${b.softSkipped} soft-skipped (LLM echo / validator)`);
39
+ return parts.length === 0 ? '' : `${b.skipped} skipped: ${parts.join(' · ')}`;
40
+ }
41
+ export function deriveStatus(r, b) {
42
+ if (r.status === 'failed' || b.failed > 0) return 'failed';
43
+ if (!b.hasBreakdown) return 'legacy';
44
+ if (b.softSkipped > 0) return 'needs-review';
45
+ if (b.translated > 0) return 'succeeded';
46
+ if (b.preserved > 0 && b.hashSkipped === 0) return 'preserved';
47
+ if (b.hashSkipped > 0) return 'no-changes';
48
+ return 'legacy';
49
+ }