@antelopejs/dms-lang 0.1.0 → 0.2.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.
@@ -1,152 +0,0 @@
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
- }