@antelopejs/dms-lang 0.0.7 → 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 (245) hide show
  1. package/README.md +12 -6
  2. package/dist/pages/actions.d.ts +7 -0
  3. package/dist/pages/actions.js +15 -0
  4. package/dist/pages/actions.js.map +1 -0
  5. package/dist/pages/component-action.d.ts +2 -0
  6. package/dist/pages/component-action.js +10 -0
  7. package/dist/pages/component-action.js.map +1 -0
  8. package/dist/pages/header-actions.d.ts +9 -0
  9. package/dist/pages/header-actions.js +146 -0
  10. package/dist/pages/header-actions.js.map +1 -0
  11. package/dist/pages/index.d.ts +6 -0
  12. package/dist/pages/index.js +6 -0
  13. package/dist/pages/index.js.map +1 -1
  14. package/dist/pages/language.d.ts +10 -0
  15. package/dist/pages/language.js +89 -0
  16. package/dist/pages/language.js.map +1 -0
  17. package/dist/pages/languages-table.d.ts +2 -0
  18. package/dist/pages/languages-table.js +120 -0
  19. package/dist/pages/languages-table.js.map +1 -0
  20. package/dist/pages/module.d.ts +7 -1
  21. package/dist/pages/module.js +28 -2
  22. package/dist/pages/module.js.map +1 -1
  23. package/dist/pages/overview.d.ts +2 -1
  24. package/dist/pages/overview.js +83 -8
  25. package/dist/pages/overview.js.map +1 -1
  26. package/dist/pages/page-actions.d.ts +6 -0
  27. package/dist/pages/page-actions.js +10 -0
  28. package/dist/pages/page-actions.js.map +1 -0
  29. package/dist/pages/quick-actions.d.ts +1 -0
  30. package/dist/pages/quick-actions.js +56 -0
  31. package/dist/pages/quick-actions.js.map +1 -0
  32. package/dist/pages/translations.d.ts +7 -1
  33. package/dist/pages/translations.js +47 -10
  34. package/dist/pages/translations.js.map +1 -1
  35. package/dist/pages/workspace-guard.d.ts +1 -0
  36. package/dist/pages/workspace-guard.js +35 -0
  37. package/dist/pages/workspace-guard.js.map +1 -0
  38. package/dist/pages/workspace.d.ts +18 -0
  39. package/dist/pages/workspace.js +162 -0
  40. package/dist/pages/workspace.js.map +1 -0
  41. package/dist/pages/workspaces.d.ts +14 -0
  42. package/dist/pages/workspaces.js +88 -0
  43. package/dist/pages/workspaces.js.map +1 -0
  44. package/dist/routes/publicLocales.d.ts +3 -2
  45. package/dist/routes/publicLocales.js +39 -18
  46. package/dist/routes/publicLocales.js.map +1 -1
  47. package/dist/routes/translations.d.ts +59 -63
  48. package/dist/routes/translations.js +400 -119
  49. package/dist/routes/translations.js.map +1 -1
  50. package/dist/utils/announce.d.ts +9 -0
  51. package/dist/utils/announce.js +29 -0
  52. package/dist/utils/announce.js.map +1 -0
  53. package/dist/utils/blocks.d.ts +8 -0
  54. package/dist/utils/blocks.js +213 -0
  55. package/dist/utils/blocks.js.map +1 -0
  56. package/dist/utils/body.d.ts +1 -0
  57. package/dist/utils/body.js +21 -0
  58. package/dist/utils/body.js.map +1 -0
  59. package/dist/utils/cache.d.ts +3 -0
  60. package/dist/utils/cache.js +32 -0
  61. package/dist/utils/cache.js.map +1 -0
  62. package/dist/utils/catalog.d.ts +7 -0
  63. package/dist/utils/catalog.js +62 -0
  64. package/dist/utils/catalog.js.map +1 -0
  65. package/dist/utils/constants.d.ts +6 -0
  66. package/dist/utils/constants.js +10 -0
  67. package/dist/utils/constants.js.map +1 -0
  68. package/dist/utils/coverage.d.ts +2 -0
  69. package/dist/utils/coverage.js +11 -0
  70. package/dist/utils/coverage.js.map +1 -0
  71. package/dist/utils/dialogs.d.ts +7 -0
  72. package/dist/utils/dialogs.js +103 -0
  73. package/dist/utils/dialogs.js.map +1 -0
  74. package/dist/utils/errors.d.ts +40 -0
  75. package/dist/utils/errors.js +60 -0
  76. package/dist/utils/errors.js.map +1 -0
  77. package/dist/utils/export.d.ts +17 -0
  78. package/dist/utils/export.js +129 -0
  79. package/dist/utils/export.js.map +1 -0
  80. package/dist/utils/json.d.ts +10 -1
  81. package/dist/utils/json.js +127 -19
  82. package/dist/utils/json.js.map +1 -1
  83. package/dist/utils/key-stores.d.ts +3 -0
  84. package/dist/utils/key-stores.js +24 -0
  85. package/dist/utils/key-stores.js.map +1 -0
  86. package/dist/utils/keys.d.ts +23 -0
  87. package/dist/utils/keys.js +252 -0
  88. package/dist/utils/keys.js.map +1 -0
  89. package/dist/utils/languages.d.ts +27 -0
  90. package/dist/utils/languages.js +121 -0
  91. package/dist/utils/languages.js.map +1 -0
  92. package/dist/utils/locale-codes.d.ts +5 -0
  93. package/dist/utils/locale-codes.js +56 -0
  94. package/dist/utils/locale-codes.js.map +1 -0
  95. package/dist/utils/locale-discovery.d.ts +1 -0
  96. package/dist/utils/locale-discovery.js +19 -1
  97. package/dist/utils/locale-discovery.js.map +1 -1
  98. package/dist/utils/message-check.d.ts +1 -0
  99. package/dist/utils/message-check.js +93 -0
  100. package/dist/utils/message-check.js.map +1 -0
  101. package/dist/utils/message-syntax.d.ts +6 -0
  102. package/dist/utils/message-syntax.js +38 -0
  103. package/dist/utils/message-syntax.js.map +1 -0
  104. package/dist/utils/namespaces.d.ts +12 -0
  105. package/dist/utils/namespaces.js +52 -0
  106. package/dist/utils/namespaces.js.map +1 -0
  107. package/dist/utils/notices.d.ts +4 -0
  108. package/dist/utils/notices.js +60 -0
  109. package/dist/utils/notices.js.map +1 -0
  110. package/dist/utils/own.d.ts +2 -3
  111. package/dist/utils/own.js +17 -57
  112. package/dist/utils/own.js.map +1 -1
  113. package/dist/utils/public-url.d.ts +8 -0
  114. package/dist/utils/public-url.js +87 -0
  115. package/dist/utils/public-url.js.map +1 -0
  116. package/dist/utils/registry.d.ts +3 -1
  117. package/dist/utils/registry.js +26 -10
  118. package/dist/utils/registry.js.map +1 -1
  119. package/dist/utils/resolve.d.ts +18 -15
  120. package/dist/utils/resolve.js +98 -151
  121. package/dist/utils/resolve.js.map +1 -1
  122. package/dist/utils/review.d.ts +5 -0
  123. package/dist/utils/review.js +64 -0
  124. package/dist/utils/review.js.map +1 -0
  125. package/dist/utils/snippets.d.ts +7 -0
  126. package/dist/utils/snippets.js +129 -0
  127. package/dist/utils/snippets.js.map +1 -0
  128. package/dist/utils/summary.d.ts +23 -0
  129. package/dist/utils/summary.js +65 -0
  130. package/dist/utils/summary.js.map +1 -0
  131. package/dist/utils/tree.d.ts +2 -17
  132. package/dist/utils/tree.js +21 -126
  133. package/dist/utils/tree.js.map +1 -1
  134. package/dist/utils/workspaces.d.ts +45 -15
  135. package/dist/utils/workspaces.js +256 -164
  136. package/dist/utils/workspaces.js.map +1 -1
  137. package/dist/validation/key.schema.d.ts +71 -0
  138. package/dist/validation/key.schema.js +67 -0
  139. package/dist/validation/key.schema.js.map +1 -0
  140. package/dist/validation/query.schema.d.ts +17 -0
  141. package/dist/validation/query.schema.js +44 -0
  142. package/dist/validation/query.schema.js.map +1 -0
  143. package/dist/validation/validate.d.ts +2 -0
  144. package/dist/validation/validate.js +24 -0
  145. package/dist/validation/validate.js.map +1 -0
  146. package/dist/validation/workspace.schema.d.ts +145 -0
  147. package/dist/validation/workspace.schema.js +83 -0
  148. package/dist/validation/workspace.schema.js.map +1 -0
  149. package/frontend-vue/app/build/components/KeyAddModal.vue +340 -0
  150. package/frontend-vue/app/build/components/KeyEditorDrawer.vue +951 -0
  151. package/frontend-vue/app/build/components/KeyRenameModal.vue +172 -0
  152. package/frontend-vue/app/build/components/LanguageAddModal.vue +199 -0
  153. package/frontend-vue/app/build/components/LanguagePicker.vue +233 -0
  154. package/frontend-vue/app/build/components/LocaleTile.vue +52 -0
  155. package/frontend-vue/app/build/components/PlaceholderText.vue +42 -0
  156. package/frontend-vue/app/build/components/ShortcutKbds.vue +22 -0
  157. package/frontend-vue/app/build/components/WorkspaceCreateModal.vue +193 -0
  158. package/frontend-vue/app/build/components/WorkspaceRenameModal.vue +107 -0
  159. package/frontend-vue/app/build/components/form/FormFieldRow.vue +70 -0
  160. package/frontend-vue/app/build/components/form/FormRows.vue +25 -0
  161. package/frontend-vue/app/build/components/form/WorkspaceDetailsFields.vue +44 -0
  162. package/frontend-vue/app/build/components/matrix/MatrixCell.vue +488 -0
  163. package/frontend-vue/app/build/components/matrix/MatrixColumns.vue +177 -0
  164. package/frontend-vue/app/build/components/matrix/MatrixGroupHeader.vue +57 -0
  165. package/frontend-vue/app/build/components/matrix/MatrixKeyCell.vue +83 -0
  166. package/frontend-vue/app/build/components/matrix/MatrixToolbar.vue +167 -0
  167. package/frontend-vue/app/build/components/queue/QueueActions.vue +60 -0
  168. package/frontend-vue/app/build/components/queue/QueueDone.vue +185 -0
  169. package/frontend-vue/app/build/components/queue/QueueEditor.vue +374 -0
  170. package/frontend-vue/app/build/components/queue/QueueHeader.vue +162 -0
  171. package/frontend-vue/app/build/components/queue/QueueUpNext.vue +61 -0
  172. package/frontend-vue/app/build/composables/useCellEditor.ts +324 -0
  173. package/frontend-vue/app/build/composables/useCellMoves.ts +155 -0
  174. package/frontend-vue/app/build/composables/useCellSaves.ts +339 -0
  175. package/frontend-vue/app/build/composables/useKeyPanel.ts +284 -0
  176. package/frontend-vue/app/build/composables/useKeySaves.ts +280 -0
  177. package/frontend-vue/app/build/composables/useMatrixFilters.ts +213 -0
  178. package/frontend-vue/app/build/composables/useMatrixRows.ts +104 -0
  179. package/frontend-vue/app/build/composables/useMatrixView.ts +181 -0
  180. package/frontend-vue/app/build/composables/useQueueContext.ts +31 -0
  181. package/frontend-vue/app/build/composables/useQueueEditing.ts +53 -0
  182. package/frontend-vue/app/build/composables/useQueueOrphans.ts +54 -0
  183. package/frontend-vue/app/build/composables/useQueueSave.ts +290 -0
  184. package/frontend-vue/app/build/composables/useQueueShortcuts.ts +106 -0
  185. package/frontend-vue/app/build/composables/useQueueToasts.ts +99 -0
  186. package/frontend-vue/app/components/LanguageNamespaces.vue +77 -0
  187. package/frontend-vue/app/components/NamespaceCoverage.vue +222 -0
  188. package/frontend-vue/app/components/TranslationMatrix.vue +809 -0
  189. package/frontend-vue/app/components/TranslationQueue.vue +212 -0
  190. package/frontend-vue/app/components/WorkspaceDangerZone.vue +81 -0
  191. package/frontend-vue/app/components/WorkspaceGeneral.vue +232 -0
  192. package/frontend-vue/app/components/WorkspaceList.vue +454 -0
  193. package/frontend-vue/app/components/WorkspaceScope.vue +384 -0
  194. package/frontend-vue/app/composables/translation/useFirstRun.ts +8 -0
  195. package/frontend-vue/app/composables/translation/useI18nWorkspaces.ts +221 -133
  196. package/frontend-vue/app/composables/translation/useKeyActions.ts +224 -0
  197. package/frontend-vue/app/composables/translation/useLangFormat.ts +10 -0
  198. package/frontend-vue/app/composables/translation/useLangLinks.ts +80 -0
  199. package/frontend-vue/app/composables/translation/useLanguageActions.ts +159 -0
  200. package/frontend-vue/app/composables/translation/useLanguageQueue.ts +232 -0
  201. package/frontend-vue/app/composables/translation/useLocaleNames.ts +166 -0
  202. package/frontend-vue/app/composables/translation/useMutationToast.ts +51 -17
  203. package/frontend-vue/app/composables/translation/useQueueDrafts.ts +115 -0
  204. package/frontend-vue/app/composables/translation/useQueueSession.ts +182 -0
  205. package/frontend-vue/app/composables/translation/useServedConfirm.ts +152 -0
  206. package/frontend-vue/app/composables/translation/useWorkspaceActions.ts +259 -0
  207. package/frontend-vue/app/composables/translation/useWorkspaceContext.ts +46 -15
  208. package/frontend-vue/app/composables/translation/useWorkspaceFlat.ts +322 -70
  209. package/frontend-vue/app/composables/translation/useWorkspaceNameCheck.ts +55 -0
  210. package/frontend-vue/app/composables/translation/useWorkspaceRoute.ts +129 -0
  211. package/frontend-vue/app/config/shortcuts-registry.ts +72 -0
  212. package/frontend-vue/app/types/lang.ts +130 -0
  213. package/frontend-vue/app/utils/boundedCache.ts +22 -0
  214. package/frontend-vue/app/utils/download.ts +16 -0
  215. package/frontend-vue/app/utils/keySaveErrors.ts +61 -0
  216. package/frontend-vue/app/utils/keySaves.ts +108 -0
  217. package/frontend-vue/app/utils/languages.ts +272 -0
  218. package/frontend-vue/app/utils/matrixExport.ts +47 -0
  219. package/frontend-vue/app/utils/queueDrafts.ts +115 -0
  220. package/frontend-vue/app/utils/queuePass.ts +145 -0
  221. package/frontend-vue/app/utils/queueView.ts +22 -0
  222. package/frontend-vue/app/utils/shortcuts.ts +75 -0
  223. package/frontend-vue/app/utils/translations.ts +756 -0
  224. package/frontend-vue/app/utils/workspaceBackup.ts +119 -0
  225. package/frontend-vue/app/utils/workspaces.ts +45 -0
  226. package/frontend-vue/dms.frontend.build.ts +5 -0
  227. package/frontend-vue/dms.frontend.ts +9 -20
  228. package/frontend-vue/i18n/locales/lang-en-GB.json +792 -69
  229. package/frontend-vue/i18n/locales/lang-fr-FR.json +792 -69
  230. package/frontend-vue/package.json +8 -6
  231. package/package.json +8 -6
  232. package/dist/utils/mutations.d.ts +0 -3
  233. package/dist/utils/mutations.js +0 -35
  234. package/dist/utils/mutations.js.map +0 -1
  235. package/frontend-vue/app/build/components/pages/lang/TranslationMatrix.vue +0 -284
  236. package/frontend-vue/app/components/LanguageAddModal.vue +0 -78
  237. package/frontend-vue/app/components/WorkspaceEndpoint.vue +0 -29
  238. package/frontend-vue/app/components/WorkspaceSelector.vue +0 -180
  239. package/frontend-vue/app/components/WorkspaceStatus.vue +0 -24
  240. package/frontend-vue/app/composables/translation/useLanguageCatalog.ts +0 -90
  241. package/frontend-vue/app/composables/translation/useLocaleMeta.ts +0 -18
  242. package/frontend-vue/app/composables/translation/useTranslationProgress.ts +0 -73
  243. package/frontend-vue/app/custom-pages/lang/overview.vue +0 -397
  244. package/frontend-vue/app/custom-pages/lang/translations.vue +0 -498
  245. package/frontend-vue/app/plugins/workspace-sidebar-widget.ts +0 -12
@@ -1,30 +1,64 @@
1
1
  interface MutationError {
2
- data?: { statusMessage?: string };
3
- statusMessage?: string;
2
+ data?: unknown
4
3
  }
5
4
 
5
+ interface ErrorBody {
6
+ code?: unknown
7
+ }
8
+
9
+ interface MutationToastOptions {
10
+ title?: string
11
+ retry?: () => void
12
+ }
13
+
14
+ /** The error code the server gave a failed request (`key_exists`, …). */
15
+ export function serverErrorCode(error: unknown): string {
16
+ const data = (error as MutationError | undefined)?.data
17
+ if (typeof data !== 'object' || data === null) return ''
18
+ const { code } = data as ErrorBody
19
+ return typeof code === 'string' ? code : ''
20
+ }
21
+
22
+ /**
23
+ * Runs a write and reports a failure in a toast, worded by the DMS from the
24
+ * `$dms_lang.errors.*` key the server answers, with a Retry action.
25
+ */
6
26
  export const useMutationToast = () => {
7
- const { t } = useI18n();
8
- const toast = useToast();
27
+ const { t } = useI18n()
28
+ const toast = useToast()
29
+ const translate = (key: string, params: Record<string, unknown>) =>
30
+ t(key, params)
9
31
 
32
+ /** The server's message, translated; the generic server error otherwise. */
10
33
  function errorMessage(error: unknown): string {
11
- const e = error as MutationError;
12
- return (
13
- e?.data?.statusMessage ??
14
- e?.statusMessage ??
15
- t("dms_lang.translation.ws_save_error")
16
- );
34
+ return resolveApiMessage(translate, resolveApiErrorMessage(error))
35
+ }
36
+
37
+ function reportFailure(error: unknown, options: MutationToastOptions = {}) {
38
+ const actions = options.retry
39
+ ? [{ label: t('dms_lang.actions.retry'), onClick: options.retry }]
40
+ : undefined
41
+ toast.add({
42
+ title: options.title ?? t(resolveApiErrorTitle(error)),
43
+ description: errorMessage(error),
44
+ color: 'error',
45
+ icon: 'i-ph-warning-circle',
46
+ actions,
47
+ })
17
48
  }
18
49
 
19
- async function tryMutate(fn: () => Promise<unknown>): Promise<boolean> {
50
+ async function tryMutate(
51
+ fn: () => Promise<unknown>,
52
+ options?: MutationToastOptions,
53
+ ): Promise<boolean> {
20
54
  try {
21
- await fn();
22
- return true;
55
+ await fn()
56
+ return true
23
57
  } catch (error) {
24
- toast.add({ title: errorMessage(error), color: "error" });
25
- return false;
58
+ reportFailure(error, options)
59
+ return false
26
60
  }
27
61
  }
28
62
 
29
- return { tryMutate };
30
- };
63
+ return { tryMutate, reportFailure, errorMessage, serverErrorCode }
64
+ }
@@ -0,0 +1,115 @@
1
+ import { effectScope, type Ref } from 'vue'
2
+ import { useDmsState } from '#dms/frontend-module'
3
+ import type { TranslationValue } from '../../types/lang'
4
+ import { savedText } from '../../utils/keySaves'
5
+ import {
6
+ draftGroups,
7
+ draftIdOf,
8
+ settledDrafts,
9
+ takeDrafts,
10
+ withDraft,
11
+ withoutDraft,
12
+ type DraftCheck,
13
+ type DraftEntry,
14
+ type DraftGroup,
15
+ type QueueDraft,
16
+ type QueueDrafts,
17
+ type SentDraft,
18
+ } from '../../utils/queueDrafts'
19
+
20
+ const QUEUE_PAGE: LangPage = 'language'
21
+
22
+ let isClearedOnLeave = false
23
+
24
+ /**
25
+ * Drops the drafts once the page shows anything else than the Language page
26
+ * (the leave guard asked first). Watched once for the app, outside any page:
27
+ * switching language or workspace remounts the page on the same path and
28
+ * keeps them.
29
+ */
30
+ function clearOnLeave(path: () => string, drafts: Ref<QueueDrafts>) {
31
+ if (isClearedOnLeave || import.meta.env.SSR) return
32
+ isClearedOnLeave = true
33
+ effectScope(true).run(() =>
34
+ watch(
35
+ () => langPageOf(path()) === QUEUE_PAGE,
36
+ (isOnPage) => {
37
+ if (!isOnPage) drafts.value = {}
38
+ },
39
+ ),
40
+ )
41
+ }
42
+
43
+ /** The drafts of every workspace and language, kept in the app state. */
44
+ function useDraftStore() {
45
+ const { route } = useWorkspaceRoute()
46
+ const drafts = useDmsState<QueueDrafts>('dms-lang-queue-drafts', () => ({}))
47
+ clearOnLeave(() => route.path, drafts)
48
+ return drafts
49
+ }
50
+
51
+ type DraftIdOf = (key: string) => string
52
+
53
+ /** The writes of the drafts of the language shown, each key's draft by `idOf`. */
54
+ function draftWrites(drafts: Ref<QueueDrafts>, idOf: DraftIdOf) {
55
+ const draftOf = (key: string) => drafts.value[idOf(key)]
56
+ const set = (key: string, draft: QueueDraft) => {
57
+ drafts.value = withDraft(drafts.value, idOf(key), draft)
58
+ }
59
+ return {
60
+ /** Keeps a typed text, started on `saved` unless it already was a draft. */
61
+ write(key: string, value: string, saved: TranslationValue | undefined) {
62
+ const draft = draftOf(key)
63
+ set(key, { value, start: draft ? draft.start : savedText(saved) })
64
+ },
65
+ /** Keeps the text, now started on the value saved elsewhere (overwrite it). */
66
+ rebase(key: string, saved: TranslationValue | undefined) {
67
+ const draft = draftOf(key)
68
+ if (draft) set(key, { ...draft, start: savedText(saved) })
69
+ },
70
+ /** Once `sent` is saved: drops the draft, or keeps what was typed meanwhile. */
71
+ settle(key: string, sent: SentDraft) {
72
+ drafts.value = settledDrafts(drafts.value, idOf(key), sent)
73
+ },
74
+ drop(key: string) {
75
+ drafts.value = withoutDraft(drafts.value, idOf(key))
76
+ },
77
+ /** Takes out the drafts `isGone` names, and returns them. */
78
+ take(isGone: DraftCheck): DraftEntry[] {
79
+ const { kept, taken } = takeDrafts(drafts.value, isGone)
80
+ if (taken.length) drafts.value = kept
81
+ return taken
82
+ },
83
+ }
84
+ }
85
+
86
+ /**
87
+ * The drafts of the queue, per workspace, language and key, kept in the app
88
+ * state: switching language or workspace remounts the page, and the drafts
89
+ * left there are still there on the way back. They only go once the page is
90
+ * left (the leave guard asked first) or once saved or discarded.
91
+ */
92
+ export function useQueueDrafts(locale: Ref<string>) {
93
+ const { selectedWorkspace } = useI18nWorkspaces()
94
+ const drafts = useDraftStore()
95
+ const idOf = (key: string) =>
96
+ draftIdOf({ workspace: selectedWorkspace.value, locale: locale.value, key })
97
+ const draftOf = (key: string) => drafts.value[idOf(key)]
98
+ const groups = computed(() => draftGroups(drafts.value))
99
+ const isShown = (group: DraftGroup) =>
100
+ group.workspace === selectedWorkspace.value && group.locale === locale.value
101
+ return {
102
+ ...draftWrites(drafts, idOf),
103
+ /** Every draft, wherever it was typed: leaving the page loses them all. */
104
+ count: computed(() => Object.keys(drafts.value).length),
105
+ groups,
106
+ /** The groups of the other workspaces and languages than the one shown. */
107
+ elsewhere: computed(() => groups.value.filter((group) => !isShown(group))),
108
+ has: (key: string) => draftOf(key) !== undefined,
109
+ read: (key: string): string | undefined => draftOf(key)?.value,
110
+ /** The saved value a draft was started on; `undefined` without a draft. */
111
+ startOf: (key: string): string | null | undefined => draftOf(key)?.start,
112
+ }
113
+ }
114
+
115
+ export type QueueDraftStore = ReturnType<typeof useQueueDrafts>
@@ -0,0 +1,182 @@
1
+ import type { ComputedRef, Ref } from 'vue'
2
+ import type { FlatTranslationRow } from '../../types/lang'
3
+ import { savedText } from '../../utils/keySaves'
4
+ import {
5
+ EMPTY_PASS,
6
+ advancePass,
7
+ completeKey,
8
+ currentKey,
9
+ goToIndex,
10
+ pendingAfter,
11
+ pendingKeys,
12
+ previousKey,
13
+ skipCurrent,
14
+ startPass,
15
+ type KeyIsOpen,
16
+ type QueueChange,
17
+ type QueuePass,
18
+ type QueueStep,
19
+ } from '../../utils/queuePass'
20
+ import type { QueueDraftStore } from './useQueueDrafts'
21
+ import type { LanguageQueue } from './useLanguageQueue'
22
+
23
+ export interface QueueEntry {
24
+ row: FlatTranslationRow
25
+ index: number
26
+ isSkipped: boolean
27
+ }
28
+
29
+ type RowsByKey = ComputedRef<Map<string, FlatTranslationRow>>
30
+
31
+ /** What the pass shows: the key shown, the keys left and the next ones. */
32
+ function passView(
33
+ pass: Ref<QueuePass>,
34
+ rowsByKey: RowsByKey,
35
+ isOpen: KeyIsOpen,
36
+ upNextSize: number,
37
+ ) {
38
+ const pending = computed(() => pendingKeys(pass.value, isOpen))
39
+ const current = computed(() => {
40
+ const key = currentKey(pass.value)
41
+ return key === undefined ? undefined : rowsByKey.value.get(key)
42
+ })
43
+ const statusOf = (key: string) => pass.value.status[key]
44
+ const upNext = computed<QueueEntry[]>(() =>
45
+ pendingAfter(pass.value, pass.value.cursor, isOpen)
46
+ .slice(0, upNextSize)
47
+ .flatMap((index) => {
48
+ const row = rowsByKey.value.get(pass.value.order[index])
49
+ if (!row) return []
50
+ return [{ row, index, isSkipped: statusOf(row.key) === 'skipped' }]
51
+ }),
52
+ )
53
+ return {
54
+ pending,
55
+ current,
56
+ upNext,
57
+ currentStatus: computed(() =>
58
+ current.value ? statusOf(current.value.key) : undefined,
59
+ ),
60
+ skippedCount: computed(
61
+ () => pending.value.filter((key) => statusOf(key) === 'skipped').length,
62
+ ),
63
+ progress: computed(() =>
64
+ coveragePercent(
65
+ pass.value.order.length - pending.value.length,
66
+ pass.value.order.length,
67
+ ),
68
+ ),
69
+ }
70
+ }
71
+
72
+ /** The moves of a pass, each one applied to `pass`. */
73
+ function passMoves(pass: Ref<QueuePass>, isOpen: KeyIsOpen) {
74
+ const apply = (change: QueueChange): QueueStep | undefined => {
75
+ pass.value = change.pass
76
+ return change.step
77
+ }
78
+ return {
79
+ skip: () => apply(skipCurrent(pass.value, isOpen)),
80
+ /** Marks a key saved; moves on when it is the one shown, unless `shouldAdvance` is false. */
81
+ complete: (key: string, shouldAdvance = true) =>
82
+ apply(completeKey(pass.value, key, shouldAdvance, isOpen)),
83
+ previous: () => apply({ pass: previousKey(pass.value) }),
84
+ goTo: (index: number) => apply({ pass: goToIndex(pass.value, index) }),
85
+ advance: () => apply(advancePass(pass.value, isOpen)),
86
+ }
87
+ }
88
+
89
+ function isStaleDraft(
90
+ drafts: QueueDraftStore,
91
+ locale: Ref<string>,
92
+ row: FlatTranslationRow,
93
+ ): boolean {
94
+ const start = drafts.startOf(row.key)
95
+ return start !== undefined && start !== savedText(row.values[locale.value])
96
+ }
97
+
98
+ /**
99
+ * One pass through the queue: the keys it holds are fixed when it starts (a
100
+ * saved key stays in it, so Previous goes back to it), each one done, skipped
101
+ * or still to do. Skipped keys come back once the others are done, and the
102
+ * pass only ends when none is left. Drafts are kept per key until saved.
103
+ */
104
+ export const useQueueSession = (upNextSize: number) => {
105
+ const language = useLanguageQueue()
106
+ const { flat } = useWorkspaceFlat()
107
+ const drafts = useQueueDrafts(language.locale)
108
+ const pass = ref<QueuePass>(EMPTY_PASS)
109
+ const rowsByKey = computed(
110
+ () => new Map(flat.value.rows.map((row) => [row.key, row])),
111
+ )
112
+ const isOpen = (key: string) => {
113
+ const row = rowsByKey.value.get(key)
114
+ return !!row && (language.matchesMode(row) || drafts.has(key))
115
+ }
116
+ const view = passView(pass, rowsByKey, isOpen, upNextSize)
117
+ const moves = passMoves(pass, isOpen)
118
+ /** Starts a new pass, on `key` when it is in it, with the keys holding a draft. */
119
+ const start = (key = '') => {
120
+ pass.value = startPass(
121
+ language.passOf((row) => drafts.has(row.key)),
122
+ key,
123
+ isOpen,
124
+ )
125
+ }
126
+ useSessionWatchers(language, view.current, pass, {
127
+ start,
128
+ advance: moves.advance,
129
+ })
130
+ return {
131
+ ...language,
132
+ ...view,
133
+ ...moves,
134
+ drafts,
135
+ isStale: (row: FlatTranslationRow) =>
136
+ isStaleDraft(drafts, language.locale, row),
137
+ isFinished: computed(() => pass.value.isFinished),
138
+ canGoBack: computed(() => pass.value.history.length > 0),
139
+ start,
140
+ }
141
+ }
142
+
143
+ /** What restarts or moves the pass on its own. */
144
+ interface SessionSteps {
145
+ start: (key?: string) => void
146
+ advance: () => QueueStep | undefined
147
+ }
148
+
149
+ /**
150
+ * Starts a new pass whenever the workspace, the language, the mode or the
151
+ * namespace changes (on the key in the URL), and moves on when the key shown
152
+ * leaves the translations (deleted elsewhere).
153
+ */
154
+ function useSessionWatchers(
155
+ language: LanguageQueue,
156
+ current: ComputedRef<FlatTranslationRow | undefined>,
157
+ pass: Ref<QueuePass>,
158
+ steps: SessionSteps,
159
+ ) {
160
+ const { isLoaded } = useWorkspaceFlat()
161
+ const { selectedWorkspace } = useI18nWorkspaces()
162
+ const { route } = useWorkspaceRoute()
163
+ watch(
164
+ [
165
+ selectedWorkspace,
166
+ language.locale,
167
+ language.mode,
168
+ language.namespace,
169
+ isLoaded,
170
+ ],
171
+ () => {
172
+ if (isLoaded.value) steps.start(route.query.key)
173
+ },
174
+ { immediate: true },
175
+ )
176
+ watch(current, (row) => {
177
+ if (!row && !pass.value.isFinished && pass.value.order.length)
178
+ steps.advance()
179
+ })
180
+ }
181
+
182
+ export type QueueSession = ReturnType<typeof useQueueSession>
@@ -0,0 +1,152 @@
1
+ /** A text of a served dialog: an i18n key with `$`, else a literal with `{param}` fill-ins. */
2
+ type ServedText = string
3
+
4
+ const KEY_PREFIX = '$'
5
+ const PARAM_PATTERN = /{(\w+)}/g
6
+
7
+ interface ServedImpact {
8
+ icon: string
9
+ label: ServedText
10
+ count?: number | string
11
+ }
12
+
13
+ interface ServedDownload {
14
+ type: 'download'
15
+ url: string
16
+ filename?: string
17
+ }
18
+
19
+ interface ServedSecondaryAction {
20
+ label: ServedText
21
+ icon?: string
22
+ target: ServedDownload
23
+ }
24
+
25
+ /** A confirmation dialog as a `confirm: { from }` route serves it (interface-dms `ConfirmDialogSerialized`). */
26
+ export interface ServedDialog {
27
+ title: ServedText
28
+ description?: ServedText
29
+ params?: Record<string, unknown>
30
+ color?: 'primary' | 'warning' | 'error' | 'neutral'
31
+ icon?: string
32
+ confirmLabel?: ServedText
33
+ cancelLabel?: ServedText
34
+ confirmText?: string
35
+ impact?: ServedImpact[]
36
+ secondaryAction?: ServedSecondaryAction
37
+ }
38
+
39
+ type ConfirmRun = () => Promise<unknown>
40
+
41
+ type Params = Record<string, unknown>
42
+
43
+ /** What resolving and running a served dialog needs from the DMS. */
44
+ interface ServedContext {
45
+ t: ReturnType<typeof useI18n>['t']
46
+ authFetch: ReturnType<typeof useAuthFetch>['$authFetch']
47
+ }
48
+
49
+ function fillLiteral(text: string, params: Params): string {
50
+ return text.replace(PARAM_PATTERN, (match, name: string) =>
51
+ params[name] === undefined ? match : String(params[name]),
52
+ )
53
+ }
54
+
55
+ function resolveText(
56
+ { t }: ServedContext,
57
+ value: ServedText,
58
+ params: Params,
59
+ ): string {
60
+ if (!value.startsWith(KEY_PREFIX)) return fillLiteral(value, params)
61
+ const key = value.slice(KEY_PREFIX.length)
62
+ const { count } = params
63
+ return typeof count === 'number' ? t(key, params, count) : t(key, params)
64
+ }
65
+
66
+ /** Fetches a served download with the user's session and saves it. */
67
+ async function download(
68
+ context: ServedContext,
69
+ target: ServedDownload,
70
+ params: Params,
71
+ ): Promise<void> {
72
+ const url = interpolateUrl(target.url, params)
73
+ const response = await context.authFetch.raw<Blob, 'blob'>(url, {
74
+ responseType: 'blob',
75
+ })
76
+ const objectUrl = URL.createObjectURL(response._data ?? new Blob([]))
77
+ try {
78
+ downloadFile(objectUrl, fillLiteral(target.filename ?? '', params))
79
+ } finally {
80
+ URL.revokeObjectURL(objectUrl)
81
+ }
82
+ }
83
+
84
+ function secondaryOf(
85
+ context: ServedContext,
86
+ dialog: ServedDialog,
87
+ params: Params,
88
+ ) {
89
+ const action = dialog.secondaryAction
90
+ if (!action) return undefined
91
+ return {
92
+ label: resolveText(context, action.label, params),
93
+ icon: action.icon,
94
+ run: () => download(context, action.target, params),
95
+ }
96
+ }
97
+
98
+ /** A served dialog with its texts resolved, ready for `useConfirm()`. */
99
+ function confirmOptionsOf(context: ServedContext, dialog: ServedDialog) {
100
+ const params = dialog.params ?? {}
101
+ const text = (value?: ServedText) =>
102
+ value === undefined ? undefined : resolveText(context, value, params)
103
+ return {
104
+ title: text(dialog.title) ?? '',
105
+ description: text(dialog.description),
106
+ color: dialog.color,
107
+ icon: dialog.icon,
108
+ confirmLabel: text(dialog.confirmLabel),
109
+ cancelLabel: text(dialog.cancelLabel),
110
+ confirmText: dialog.confirmText,
111
+ impact: dialog.impact?.map((entry) => ({
112
+ ...entry,
113
+ label: text(entry.label) ?? '',
114
+ })),
115
+ secondaryAction: secondaryOf(context, dialog, params),
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Asks with the dialog the server words for an action (the one a table row
121
+ * action asks with, through `confirm: { from }`), so the action asks the
122
+ * same question wherever it starts: its texts, its impact, the code to type
123
+ * and its download. Resolves whether the user confirmed and `run` succeeded.
124
+ */
125
+ export function useServedConfirm() {
126
+ const context: ServedContext = {
127
+ t: useI18n().t,
128
+ authFetch: useAuthFetch().$authFetch,
129
+ }
130
+ const { confirm } = useConfirm()
131
+ const { reportFailure } = useMutationToast()
132
+
133
+ async function loadDialog(url: string): Promise<ServedDialog | null> {
134
+ try {
135
+ return await context.authFetch<ServedDialog>(url)
136
+ } catch (error) {
137
+ reportFailure(error)
138
+ return null
139
+ }
140
+ }
141
+
142
+ return async (url: string, run: ConfirmRun): Promise<boolean> => {
143
+ const dialog = await loadDialog(url)
144
+ if (!dialog) return false
145
+ return confirm({
146
+ ...confirmOptionsOf(context, dialog),
147
+ onConfirm: async () => {
148
+ await run()
149
+ },
150
+ })
151
+ }
152
+ }