shared-lib-web 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -528
- package/dist/components/app-brand.d.ts.map +1 -1
- package/dist/components/app-brand.js +9 -5
- package/dist/components/app-brand.js.map +1 -1
- package/dist/components/coming-soon.d.ts.map +1 -1
- package/dist/components/coming-soon.js +3 -2
- package/dist/components/coming-soon.js.map +1 -1
- package/dist/components/language-switcher.d.ts +5 -1
- package/dist/components/language-switcher.d.ts.map +1 -1
- package/dist/components/language-switcher.js +20 -7
- package/dist/components/language-switcher.js.map +1 -1
- package/dist/components/otp-modal.d.ts +5 -0
- package/dist/components/otp-modal.d.ts.map +1 -0
- package/dist/components/otp-modal.js +77 -0
- package/dist/components/otp-modal.js.map +1 -0
- package/dist/components/settings-menu.d.ts +4 -0
- package/dist/components/settings-menu.d.ts.map +1 -1
- package/dist/components/settings-menu.js +15 -2
- package/dist/components/settings-menu.js.map +1 -1
- package/dist/context/use-ui.d.ts.map +1 -1
- package/dist/context/use-ui.js +4 -3
- package/dist/context/use-ui.js.map +1 -1
- package/dist/i18n/browser-language.d.ts +9 -0
- package/dist/i18n/browser-language.d.ts.map +1 -0
- package/dist/i18n/browser-language.js +89 -0
- package/dist/i18n/browser-language.js.map +1 -0
- package/dist/i18n/default-messages.d.ts.map +1 -1
- package/dist/i18n/default-messages.js +15 -0
- package/dist/i18n/default-messages.js.map +1 -1
- package/dist/i18n/locale-groups.d.ts +18 -0
- package/dist/i18n/locale-groups.d.ts.map +1 -0
- package/dist/i18n/locale-groups.js +74 -0
- package/dist/i18n/locale-groups.js.map +1 -0
- package/dist/i18n/locale-service.d.ts +22 -4
- package/dist/i18n/locale-service.d.ts.map +1 -1
- package/dist/i18n/locale-service.js +95 -27
- package/dist/i18n/locale-service.js.map +1 -1
- package/dist/i18n/locale-signal.d.ts +18 -0
- package/dist/i18n/locale-signal.d.ts.map +1 -0
- package/dist/i18n/locale-signal.js +142 -0
- package/dist/i18n/locale-signal.js.map +1 -0
- package/dist/i18n/translator.d.ts +8 -1
- package/dist/i18n/translator.d.ts.map +1 -1
- package/dist/i18n/translator.js +36 -12
- package/dist/i18n/translator.js.map +1 -1
- package/dist/index.d.ts +24 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -8
- package/dist/index.js.map +1 -1
- package/dist/interfaces/app-types.d.ts +6 -0
- package/dist/interfaces/app-types.d.ts.map +1 -1
- package/dist/interfaces/ui-config.d.ts +184 -15
- package/dist/interfaces/ui-config.d.ts.map +1 -1
- package/dist/interfaces/ui-context.d.ts +1 -1
- package/dist/interfaces/ui-context.d.ts.map +1 -1
- package/dist/layouts/auth-layout.js +1 -1
- package/dist/layouts/auth-layout.js.map +1 -1
- package/dist/providers/provider-effects.d.ts +2 -2
- package/dist/providers/provider-effects.d.ts.map +1 -1
- package/dist/providers/provider-effects.js +4 -1
- package/dist/providers/provider-effects.js.map +1 -1
- package/dist/providers/register-config.d.ts.map +1 -1
- package/dist/providers/register-config.js +2 -0
- package/dist/providers/register-config.js.map +1 -1
- package/dist/providers/shared-web-ui-provider.d.ts +7 -19
- package/dist/providers/shared-web-ui-provider.d.ts.map +1 -1
- package/dist/providers/shared-web-ui-provider.js +21 -22
- package/dist/providers/shared-web-ui-provider.js.map +1 -1
- package/dist/query/query-param.d.ts +4 -1
- package/dist/query/query-param.d.ts.map +1 -1
- package/dist/query/query-param.js +6 -3
- package/dist/query/query-param.js.map +1 -1
- package/dist/state/api-loader.d.ts +19 -0
- package/dist/state/api-loader.d.ts.map +1 -0
- package/dist/state/api-loader.js +187 -0
- package/dist/state/api-loader.js.map +1 -0
- package/dist/state/auth-service.d.ts +9 -0
- package/dist/state/auth-service.d.ts.map +1 -1
- package/dist/state/auth-service.js +70 -3
- package/dist/state/auth-service.js.map +1 -1
- package/dist/state/branch-service.d.ts +1 -1
- package/dist/state/branch-service.js +3 -3
- package/dist/state/branch-service.js.map +1 -1
- package/dist/state/company-service.d.ts +16 -0
- package/dist/state/company-service.d.ts.map +1 -0
- package/dist/state/company-service.js +97 -0
- package/dist/state/company-service.js.map +1 -0
- package/dist/state/language-service.d.ts +5 -2
- package/dist/state/language-service.d.ts.map +1 -1
- package/dist/state/language-service.js +12 -7
- package/dist/state/language-service.js.map +1 -1
- package/dist/state/layout-choice-service.d.ts +10 -0
- package/dist/state/layout-choice-service.d.ts.map +1 -0
- package/dist/state/layout-choice-service.js +34 -0
- package/dist/state/layout-choice-service.js.map +1 -0
- package/dist/state/menu-service.d.ts +1 -1
- package/dist/state/menu-service.js +2 -2
- package/dist/state/menu-service.js.map +1 -1
- package/dist/state/otp-service.d.ts +69 -0
- package/dist/state/otp-service.d.ts.map +1 -0
- package/dist/state/otp-service.js +212 -0
- package/dist/state/otp-service.js.map +1 -0
- package/dist/state/preference-sync.d.ts +1 -1
- package/dist/state/preference-sync.d.ts.map +1 -1
- package/dist/state/preference-sync.js +11 -3
- package/dist/state/preference-sync.js.map +1 -1
- package/dist/state/signals.d.ts +9 -1
- package/dist/state/signals.d.ts.map +1 -1
- package/dist/state/signals.js +28 -4
- package/dist/state/signals.js.map +1 -1
- package/dist/theme/custom-themes.d.ts +28 -0
- package/dist/theme/custom-themes.d.ts.map +1 -0
- package/dist/theme/custom-themes.js +218 -0
- package/dist/theme/custom-themes.js.map +1 -0
- package/dist/theme/dark-themes.d.ts +9 -1
- package/dist/theme/dark-themes.d.ts.map +1 -1
- package/dist/theme/dark-themes.js +74 -2
- package/dist/theme/dark-themes.js.map +1 -1
- package/dist/theme/logo.d.ts +6 -3
- package/dist/theme/logo.d.ts.map +1 -1
- package/dist/theme/logo.js +17 -3
- package/dist/theme/logo.js.map +1 -1
- package/dist/theme/theme-service.d.ts +14 -3
- package/dist/theme/theme-service.d.ts.map +1 -1
- package/dist/theme/theme-service.js +64 -10
- package/dist/theme/theme-service.js.map +1 -1
- package/dist/types/field-wrapper.d.ts +2 -0
- package/dist/types/field-wrapper.d.ts.map +1 -1
- package/dist/types/field-wrapper.js +3 -2
- package/dist/types/field-wrapper.js.map +1 -1
- package/dist/types/type.otp.d.ts +2 -0
- package/dist/types/type.otp.d.ts.map +1 -1
- package/dist/types/type.otp.js +2 -2
- package/dist/types/type.otp.js.map +1 -1
- package/dist/utils/app-http.d.ts +41 -1
- package/dist/utils/app-http.d.ts.map +1 -1
- package/dist/utils/app-http.js +188 -12
- package/dist/utils/app-http.js.map +1 -1
- package/dist/utils/content-util.d.ts +6 -0
- package/dist/utils/content-util.d.ts.map +1 -0
- package/dist/utils/content-util.js +45 -0
- package/dist/utils/content-util.js.map +1 -0
- package/dist/utils/error-util.d.ts +3 -0
- package/dist/utils/error-util.d.ts.map +1 -0
- package/dist/utils/error-util.js +58 -0
- package/dist/utils/error-util.js.map +1 -0
- package/dist/utils/language-util.d.ts +3 -3
- package/dist/utils/language-util.d.ts.map +1 -1
- package/dist/utils/language-util.js +7 -7
- package/dist/utils/language-util.js.map +1 -1
- package/dist/utils/menu-util.js +1 -1
- package/dist/utils/menu-util.js.map +1 -1
- package/dist/utils/signal-source.d.ts +4 -0
- package/dist/utils/signal-source.d.ts.map +1 -0
- package/dist/utils/signal-source.js +13 -0
- package/dist/utils/signal-source.js.map +1 -0
- package/dist/utils/storage-keys.d.ts +5 -0
- package/dist/utils/storage-keys.d.ts.map +1 -1
- package/dist/utils/storage-keys.js +5 -0
- package/dist/utils/storage-keys.js.map +1 -1
- package/package.json +3 -3
- package/src/components/app-brand.tsx +9 -5
- package/src/components/coming-soon.tsx +3 -2
- package/src/components/language-switcher.tsx +26 -7
- package/src/components/otp-modal.tsx +154 -0
- package/src/components/settings-menu.tsx +28 -2
- package/src/context/use-ui.ts +4 -3
- package/src/i18n/browser-language.ts +98 -0
- package/src/i18n/default-messages.ts +15 -0
- package/src/i18n/locale-groups.ts +87 -0
- package/src/i18n/locale-service.ts +99 -27
- package/src/i18n/locale-signal.ts +173 -0
- package/src/i18n/translator.ts +38 -12
- package/src/index.ts +102 -4
- package/src/interfaces/app-types.ts +6 -0
- package/src/interfaces/ui-config.ts +191 -15
- package/src/interfaces/ui-context.ts +1 -1
- package/src/layouts/auth-layout.tsx +1 -1
- package/src/providers/provider-effects.ts +5 -2
- package/src/providers/register-config.ts +2 -0
- package/src/providers/shared-web-ui-provider.tsx +26 -43
- package/src/query/query-param.ts +6 -2
- package/src/state/api-loader.ts +205 -0
- package/src/state/auth-service.ts +84 -3
- package/src/state/branch-service.ts +3 -3
- package/src/state/company-service.ts +107 -0
- package/src/state/language-service.ts +12 -7
- package/src/state/layout-choice-service.ts +40 -0
- package/src/state/menu-service.ts +2 -2
- package/src/state/otp-service.ts +283 -0
- package/src/state/preference-sync.ts +11 -3
- package/src/state/signals.ts +36 -4
- package/src/theme/custom-themes.ts +236 -0
- package/src/theme/dark-themes.ts +80 -3
- package/src/theme/logo.ts +19 -4
- package/src/theme/org-themes.json +70 -0
- package/src/theme/theme-service.ts +70 -10
- package/src/types/field-wrapper.tsx +5 -2
- package/src/types/type.otp.tsx +11 -2
- package/src/utils/app-http.ts +205 -12
- package/src/utils/content-util.ts +49 -0
- package/src/utils/error-util.ts +64 -0
- package/src/utils/language-util.ts +7 -7
- package/src/utils/menu-util.ts +1 -1
- package/src/utils/signal-source.ts +15 -0
- package/src/utils/storage-keys.ts +6 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// otp-service.ts
|
|
2
|
+
// State and HTTP calls of the one-time code dialog (components/otp-modal.tsx). The URLs come from config.http.otp:
|
|
3
|
+
//
|
|
4
|
+
// // config.ts
|
|
5
|
+
// http: { baseUrl, otp: { sendUrl: `${MsUrl.sor}/otp-send`, verifyUrl: `${MsUrl.sor}/otp-verify`, resendSeconds: 180 } }
|
|
6
|
+
//
|
|
7
|
+
// // page: render <OtpModal /> once, then open it (the code is sent on open)
|
|
8
|
+
// openOtpModal({ userId: email, title: t("profile.verifyEmail"), message: t("profile.verifyEmailText"),
|
|
9
|
+
// onVerified: handleEmailVerified });
|
|
10
|
+
// export function handleEmailVerified(result: OtpVerifiedResult): void { ... result.oid, result.uid, result.otp ... }
|
|
11
|
+
//
|
|
12
|
+
// Send: POST sendUrl { userid } -> { data: { message, oid, uid } }
|
|
13
|
+
// Verify: POST verifyUrl { otp, oid, uid } -> { data: { message, oid, uid } }
|
|
14
|
+
// The backend message is shown as a toast; a failed call shows the backend error (readErrorMessage).
|
|
15
|
+
|
|
16
|
+
import { signal } from "@preact/signals-react";
|
|
17
|
+
import { ConstValue } from "dff-util";
|
|
18
|
+
import { translate } from "../i18n/translator.ts";
|
|
19
|
+
import type { OtpConfig } from "../interfaces/ui-config.ts";
|
|
20
|
+
import { readAppConfig } from "../utils/app-config.ts";
|
|
21
|
+
import { AppHttp } from "../utils/app-http.ts";
|
|
22
|
+
import { readErrorMessage } from "../utils/error-util.ts";
|
|
23
|
+
import { ShowToast } from "./toast-service.ts";
|
|
24
|
+
|
|
25
|
+
/** Default number of characters of the code. */
|
|
26
|
+
export const DEFAULT_OTP_LENGTH = 6;
|
|
27
|
+
|
|
28
|
+
/** Default seconds before "Resend code" works again. */
|
|
29
|
+
export const DEFAULT_OTP_RESEND_SECONDS = 180;
|
|
30
|
+
|
|
31
|
+
const OTP_CONFIG_MISSING = "shared-lib-web: config.http.otp (sendUrl, verifyUrl) is missing.";
|
|
32
|
+
|
|
33
|
+
/** oid + uid of the code that was sent: the verify call needs them. */
|
|
34
|
+
export interface OtpSession {
|
|
35
|
+
oid: string;
|
|
36
|
+
uid: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What onVerified receives. */
|
|
40
|
+
export interface OtpVerifiedResult extends OtpSession {
|
|
41
|
+
otp: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The form of the dialog. */
|
|
45
|
+
export interface OtpFormType {
|
|
46
|
+
otp: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What openOtpModal needs. */
|
|
50
|
+
export interface OtpOpenOptions {
|
|
51
|
+
/** Sent as "userid" and shown in the dialog (email, mobile or user id). */
|
|
52
|
+
userId: string;
|
|
53
|
+
/** Heading (already translated). Default t("ui.otpTitle"). */
|
|
54
|
+
title?: string;
|
|
55
|
+
/** Text under the user id (already translated). Default t("ui.otpText"). */
|
|
56
|
+
message?: string;
|
|
57
|
+
/** Called after a successful verify (the dialog is closed by then). */
|
|
58
|
+
onVerified?: (result: OtpVerifiedResult) => void;
|
|
59
|
+
/** Called after Cancel, ✕, Escape or a backdrop click. */
|
|
60
|
+
onCancel?: () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What the dialog shows. */
|
|
64
|
+
export interface OtpModalState {
|
|
65
|
+
isOpen: boolean;
|
|
66
|
+
title: string;
|
|
67
|
+
message: string;
|
|
68
|
+
userId: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The reply fields of the send / verify services. */
|
|
72
|
+
interface OtpReply {
|
|
73
|
+
message?: string;
|
|
74
|
+
oid?: string;
|
|
75
|
+
uid?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type OtpCallbacks = Pick<OtpOpenOptions, "onVerified" | "onCancel">;
|
|
79
|
+
|
|
80
|
+
const CLOSED_OTP_MODAL: OtpModalState = {
|
|
81
|
+
isOpen: false,
|
|
82
|
+
title: ConstValue.EMPTY,
|
|
83
|
+
message: ConstValue.EMPTY,
|
|
84
|
+
userId: ConstValue.EMPTY,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** The dialog: open, title and user id. */
|
|
88
|
+
export const otpModalSignal = signal<OtpModalState>(CLOSED_OTP_MODAL);
|
|
89
|
+
|
|
90
|
+
/** oid + uid of the sent code; null = no code sent yet. */
|
|
91
|
+
export const otpSessionSignal = signal<OtpSession | null>(null);
|
|
92
|
+
|
|
93
|
+
/** true while the send call runs. */
|
|
94
|
+
export const otpIsSendingSignal = signal(false);
|
|
95
|
+
|
|
96
|
+
/** true while the verify call runs. */
|
|
97
|
+
export const otpIsVerifyingSignal = signal(false);
|
|
98
|
+
|
|
99
|
+
/** Seconds left before "Resend code" works again (0 = now). */
|
|
100
|
+
export const otpResendSecondsSignal = signal(0);
|
|
101
|
+
|
|
102
|
+
let otpCallbacks: OtpCallbacks = {};
|
|
103
|
+
|
|
104
|
+
let resendTimer: ReturnType<typeof setInterval> | null = null;
|
|
105
|
+
|
|
106
|
+
/** config.http.otp, or an error when it is not set. */
|
|
107
|
+
export function readOtpConfig(): OtpConfig {
|
|
108
|
+
const config = readAppConfig();
|
|
109
|
+
const otpConfig = config?.http?.otp;
|
|
110
|
+
if (!otpConfig) {
|
|
111
|
+
throw new Error(OTP_CONFIG_MISSING);
|
|
112
|
+
}
|
|
113
|
+
return otpConfig;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Number of characters of the code: config.http.otp.length, default 6. */
|
|
117
|
+
export function readOtpLength(): number {
|
|
118
|
+
const config = readAppConfig();
|
|
119
|
+
return config?.http?.otp?.length ?? DEFAULT_OTP_LENGTH;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** true for a plain object. */
|
|
123
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
124
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The reply fields from { data: { message, oid, uid } } or { message, oid, uid }. */
|
|
128
|
+
function readOtpReply(reply: unknown): OtpReply {
|
|
129
|
+
if (!isRecord(reply)) {
|
|
130
|
+
return {};
|
|
131
|
+
}
|
|
132
|
+
const source = isRecord(reply.data) ? reply.data : reply;
|
|
133
|
+
return source as OtpReply;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 125 -> "2:05". */
|
|
137
|
+
export function formatOtpCountdown(seconds: number): string {
|
|
138
|
+
const minutes = Math.floor(seconds / 60);
|
|
139
|
+
const rest = seconds % 60;
|
|
140
|
+
const restText = String(rest);
|
|
141
|
+
const paddedRest = restText.padStart(2, "0");
|
|
142
|
+
return `${minutes}:${paddedRest}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Stops the resend countdown (0 = resend works). */
|
|
146
|
+
export function stopOtpResendCountdown(): void {
|
|
147
|
+
if (resendTimer) {
|
|
148
|
+
clearInterval(resendTimer);
|
|
149
|
+
resendTimer = null;
|
|
150
|
+
}
|
|
151
|
+
otpResendSecondsSignal.value = 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** One second of the resend countdown. */
|
|
155
|
+
function tickOtpResendCountdown(): void {
|
|
156
|
+
const nextSeconds = otpResendSecondsSignal.value - 1;
|
|
157
|
+
if (nextSeconds <= 0) {
|
|
158
|
+
stopOtpResendCountdown();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
otpResendSecondsSignal.value = nextSeconds;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Starts the resend countdown (one timer for the whole page). */
|
|
165
|
+
function startOtpResendCountdown(seconds: number): void {
|
|
166
|
+
stopOtpResendCountdown();
|
|
167
|
+
if (seconds <= 0) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
otpResendSecondsSignal.value = seconds;
|
|
171
|
+
resendTimer = setInterval(tickOtpResendCountdown, 1000);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The backend message, else the translated fallback. */
|
|
175
|
+
function readReplyMessage(otpReply: OtpReply, fallbackKey: string): string {
|
|
176
|
+
const fallback = translate(fallbackKey);
|
|
177
|
+
return otpReply.message || fallback;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Shows the backend error of a failed call, else the translated fallback. */
|
|
181
|
+
function showOtpError(error: unknown, fallbackKey: string): void {
|
|
182
|
+
const fallback = translate(fallbackKey);
|
|
183
|
+
const message = readErrorMessage(error, fallback);
|
|
184
|
+
ShowToast(message, "error");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** POST sendUrl { userid }: keeps oid + uid and starts the resend countdown. Returns true when sent. */
|
|
188
|
+
export async function otpSendCall(): Promise<boolean> {
|
|
189
|
+
const userId = otpModalSignal.value.userId;
|
|
190
|
+
if (otpIsSendingSignal.value || userId.length === 0) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
otpIsSendingSignal.value = true;
|
|
194
|
+
try {
|
|
195
|
+
const otpConfig = readOtpConfig();
|
|
196
|
+
const body = { userid: userId };
|
|
197
|
+
const reply = await AppHttp.Post(otpConfig.sendUrl, body);
|
|
198
|
+
const isStillOpen = otpModalSignal.value.isOpen && otpModalSignal.value.userId === userId;
|
|
199
|
+
if (!isStillOpen) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
const otpReply = readOtpReply(reply);
|
|
203
|
+
otpSessionSignal.value = { oid: otpReply.oid ?? ConstValue.EMPTY, uid: otpReply.uid ?? ConstValue.EMPTY };
|
|
204
|
+
const message = readReplyMessage(otpReply, "ui.otpSent");
|
|
205
|
+
ShowToast(message, "success");
|
|
206
|
+
const resendSeconds = otpConfig.resendSeconds ?? DEFAULT_OTP_RESEND_SECONDS;
|
|
207
|
+
startOtpResendCountdown(resendSeconds);
|
|
208
|
+
return true;
|
|
209
|
+
} catch (error: unknown) {
|
|
210
|
+
showOtpError(error, "ui.otpSendFailed");
|
|
211
|
+
return false;
|
|
212
|
+
} finally {
|
|
213
|
+
otpIsSendingSignal.value = false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** "Resend code" button: sends a new code once the countdown is over. */
|
|
218
|
+
export function handleOtpResend(): void {
|
|
219
|
+
if (otpResendSecondsSignal.value > 0) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
void otpSendCall();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** POST verifyUrl { otp, oid, uid }: closes the dialog and calls onVerified. For form.handleSubmit. */
|
|
226
|
+
export async function handleOtpVerify(values: OtpFormType): Promise<void> {
|
|
227
|
+
const session = otpSessionSignal.value;
|
|
228
|
+
if (otpIsVerifyingSignal.value) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (!session) {
|
|
232
|
+
const message = translate("ui.otpSessionMissing");
|
|
233
|
+
ShowToast(message, "error");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
otpIsVerifyingSignal.value = true;
|
|
237
|
+
try {
|
|
238
|
+
const otpConfig = readOtpConfig();
|
|
239
|
+
const body = { otp: values.otp, oid: session.oid, uid: session.uid };
|
|
240
|
+
const reply = await AppHttp.Post(otpConfig.verifyUrl, body);
|
|
241
|
+
const otpReply = readOtpReply(reply);
|
|
242
|
+
const message = readReplyMessage(otpReply, "ui.otpVerified");
|
|
243
|
+
ShowToast(message, "success");
|
|
244
|
+
const result: OtpVerifiedResult = {
|
|
245
|
+
oid: otpReply.oid || session.oid,
|
|
246
|
+
uid: otpReply.uid || session.uid,
|
|
247
|
+
otp: values.otp,
|
|
248
|
+
};
|
|
249
|
+
const onVerified = otpCallbacks.onVerified;
|
|
250
|
+
closeOtpModal();
|
|
251
|
+
onVerified?.(result);
|
|
252
|
+
} catch (error: unknown) {
|
|
253
|
+
showOtpError(error, "ui.otpInvalid");
|
|
254
|
+
} finally {
|
|
255
|
+
otpIsVerifyingSignal.value = false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Opens the dialog for a user id and sends the code. */
|
|
260
|
+
export function openOtpModal(options: OtpOpenOptions): void {
|
|
261
|
+
stopOtpResendCountdown();
|
|
262
|
+
otpCallbacks = { onVerified: options.onVerified, onCancel: options.onCancel };
|
|
263
|
+
otpSessionSignal.value = null;
|
|
264
|
+
const title = options.title ?? translate("ui.otpTitle");
|
|
265
|
+
const message = options.message ?? translate("ui.otpText");
|
|
266
|
+
otpModalSignal.value = { isOpen: true, title: title, message: message, userId: options.userId };
|
|
267
|
+
void otpSendCall();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Closes the dialog without calling onCancel (also used after a successful verify). */
|
|
271
|
+
export function closeOtpModal(): void {
|
|
272
|
+
stopOtpResendCountdown();
|
|
273
|
+
otpCallbacks = {};
|
|
274
|
+
otpSessionSignal.value = null;
|
|
275
|
+
otpModalSignal.value = CLOSED_OTP_MODAL;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Cancel, ✕, Escape or a backdrop click: closes the dialog and calls onCancel. */
|
|
279
|
+
export function cancelOtpModal(): void {
|
|
280
|
+
const onCancel = otpCallbacks.onCancel;
|
|
281
|
+
closeOtpModal();
|
|
282
|
+
onCancel?.();
|
|
283
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// preference-sync.ts
|
|
2
|
-
// Keeps the theme, language
|
|
2
|
+
// Keeps the theme, language, menu mode and layout choice the same in every copy of the library on the page — for microfrontends,
|
|
3
3
|
// where each MFE bundles its own shared-lib-web — and in other tabs.
|
|
4
4
|
//
|
|
5
5
|
// AppStorage.setData(THEME | LANG, ...) -> fires window event PREFERENCE_EVENT (same tab, storage-keys.ts)
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
import { applyLanguage } from "../i18n/locale-service.ts";
|
|
10
10
|
import { isKnownTheme } from "../theme/theme-service.ts";
|
|
11
11
|
import { AppStorage, readPrefixedKey } from "../utils/app-storage.ts";
|
|
12
|
-
import { LANG, MENU_MODE, PREFERENCE_EVENT, THEME } from "../utils/storage-keys.ts";
|
|
12
|
+
import { LANG, LAYOUT_CHOICE, MENU_MODE, PREFERENCE_EVENT, THEME } from "../utils/storage-keys.ts";
|
|
13
|
+
import { applySavedLayoutChoice } from "./layout-choice-service.ts";
|
|
13
14
|
import { localeSignal, navigationModeSignal, themeSignal, uiConfigSignal } from "./signals.ts";
|
|
14
15
|
|
|
15
16
|
let isPreferenceSyncStarted = false;
|
|
@@ -45,7 +46,7 @@ export function applySavedMenuMode(): void {
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
/** Applies the preference named by the key ("THEME", "LANG" or "
|
|
49
|
+
/** Applies the preference named by the key ("THEME", "LANG", "MENU_MODE" or "LAYOUT_CHOICE"). */
|
|
49
50
|
export function applySavedPreference(key: string): void {
|
|
50
51
|
if (key === THEME) {
|
|
51
52
|
applySavedTheme();
|
|
@@ -56,6 +57,9 @@ export function applySavedPreference(key: string): void {
|
|
|
56
57
|
if (key === MENU_MODE) {
|
|
57
58
|
applySavedMenuMode();
|
|
58
59
|
}
|
|
60
|
+
if (key === LAYOUT_CHOICE) {
|
|
61
|
+
applySavedLayoutChoice();
|
|
62
|
+
}
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
/** Same tab: another library copy (or this one) saved a preference. */
|
|
@@ -70,6 +74,7 @@ export function handleStorageEvent(event: StorageEvent): void {
|
|
|
70
74
|
const themeKey = readPrefixedKey(THEME);
|
|
71
75
|
const languageKey = readPrefixedKey(LANG);
|
|
72
76
|
const menuModeKey = readPrefixedKey(MENU_MODE);
|
|
77
|
+
const layoutChoiceKey = readPrefixedKey(LAYOUT_CHOICE);
|
|
73
78
|
if (event.key === themeKey) {
|
|
74
79
|
applySavedTheme();
|
|
75
80
|
}
|
|
@@ -79,6 +84,9 @@ export function handleStorageEvent(event: StorageEvent): void {
|
|
|
79
84
|
if (event.key === menuModeKey) {
|
|
80
85
|
applySavedMenuMode();
|
|
81
86
|
}
|
|
87
|
+
if (event.key === layoutChoiceKey) {
|
|
88
|
+
applySavedLayoutChoice();
|
|
89
|
+
}
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
/** Starts listening once (called by SharedWebUiProvider). */
|
package/src/state/signals.ts
CHANGED
|
@@ -15,6 +15,9 @@ import { ConstValue } from "dff-util";
|
|
|
15
15
|
import { toPagePath } from "../query/page-path.ts";
|
|
16
16
|
import { resolveDirection } from "../i18n/direction.ts";
|
|
17
17
|
import type {
|
|
18
|
+
AppType,
|
|
19
|
+
CompanyDetails,
|
|
20
|
+
CustomTheme,
|
|
18
21
|
Direction,
|
|
19
22
|
LanguageOption,
|
|
20
23
|
LocaleMessages,
|
|
@@ -24,17 +27,26 @@ import type {
|
|
|
24
27
|
UiConfig,
|
|
25
28
|
} from "../interfaces/ui-config.ts";
|
|
26
29
|
import type { ToastItem } from "../interfaces/toast.ts";
|
|
27
|
-
import {
|
|
30
|
+
import { buildDarkThemeNames } from "../theme/dark-themes.ts";
|
|
28
31
|
|
|
29
32
|
/** The config given to <SharedWebUiProvider>. null until the provider renders. */
|
|
30
33
|
export const uiConfigSignal = signal<UiConfig | null>(null);
|
|
31
34
|
|
|
35
|
+
/** What the app is: <SharedWebUiProvider type="spa" | "shell" | "mfe"> (default "spa"). */
|
|
36
|
+
export const appTypeSignal = signal<AppType>("spa");
|
|
37
|
+
|
|
32
38
|
/** Current language code. */
|
|
33
39
|
export const localeSignal = signal<string>(ConstValue.EMPTY);
|
|
34
40
|
|
|
35
41
|
/** Current DaisyUI theme. */
|
|
36
42
|
export const themeSignal = signal<Theme>(ConstValue.LIGHT);
|
|
37
43
|
|
|
44
|
+
/** The custom themes (config.customThemes or setCustomThemes), listed first in the theme switcher. */
|
|
45
|
+
export const customThemesSignal = signal<CustomTheme[]>([]);
|
|
46
|
+
|
|
47
|
+
/** The company details (config.loadCompanyDetails or setCompanyDetails); null = not here yet (config values are used). */
|
|
48
|
+
export const companyDetailsSignal = signal<CompanyDetails | null>(null);
|
|
49
|
+
|
|
38
50
|
/** Messages of the current language. */
|
|
39
51
|
export const translationSignal = signal<LocaleMessages>({});
|
|
40
52
|
|
|
@@ -83,10 +95,29 @@ export function toPageFromPath(pathname: string): string {
|
|
|
83
95
|
return pathname.slice(0, end);
|
|
84
96
|
}
|
|
85
97
|
|
|
98
|
+
/** true when pages live in the path: layout.pageMode "path", or no pageMode and the app type "shell". */
|
|
99
|
+
export function readIsPathPageMode(config: UiConfig | null): boolean {
|
|
100
|
+
const pageMode = config?.layout?.pageMode;
|
|
101
|
+
if (pageMode) {
|
|
102
|
+
return pageMode === "path";
|
|
103
|
+
}
|
|
104
|
+
return appTypeSignal.value === "shell";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** config.homePath (not used by an "mfe": the shell owns the home page), else "/". */
|
|
108
|
+
function readHomePage(config: UiConfig | null): string {
|
|
109
|
+
const isMfe = appTypeSignal.value === "mfe";
|
|
110
|
+
if (isMfe) {
|
|
111
|
+
return DEFAULT_HOME_PAGE;
|
|
112
|
+
}
|
|
113
|
+
return config?.homePath ?? DEFAULT_HOME_PAGE;
|
|
114
|
+
}
|
|
115
|
+
|
|
86
116
|
function readCurrentPage(): string {
|
|
87
117
|
const config = uiConfigSignal.value;
|
|
88
|
-
const homePage = config
|
|
89
|
-
|
|
118
|
+
const homePage = readHomePage(config);
|
|
119
|
+
const isPathMode = readIsPathPageMode(config);
|
|
120
|
+
if (isPathMode) {
|
|
90
121
|
const pathname = pathnameSignal.value;
|
|
91
122
|
const pathPage = toPageFromPath(pathname);
|
|
92
123
|
return pathPage || homePage;
|
|
@@ -174,7 +205,8 @@ export const directionSignal = computed(readDirection);
|
|
|
174
205
|
function readIsDarkTheme(): boolean {
|
|
175
206
|
const config = uiConfigSignal.value;
|
|
176
207
|
const theme = themeSignal.value;
|
|
177
|
-
const
|
|
208
|
+
const customThemes = customThemesSignal.value;
|
|
209
|
+
const darkThemes = buildDarkThemeNames(config?.darkThemes, customThemes);
|
|
178
210
|
const isDark = darkThemes.includes(theme);
|
|
179
211
|
return isDark;
|
|
180
212
|
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// custom-themes.ts
|
|
2
|
+
// Custom DaisyUI themes from plain data (a JSON file or an API reply) — no CSS file, no rebuild.
|
|
3
|
+
// The themes are an object keyed by theme name ("name" inside is optional, the key is the name):
|
|
4
|
+
//
|
|
5
|
+
// { "org-light": { "isDark": false, "name": "org-light", "variables": { "color-primary": "oklch(55.8% 0.154 249.1)", ... } },
|
|
6
|
+
// "org-dark": { "isDark": true, "name": "org-dark", "variables": { ... } } }
|
|
7
|
+
//
|
|
8
|
+
// A plain list [{ "name": "org-light", ... }] is accepted too.
|
|
9
|
+
//
|
|
10
|
+
// setCustomThemes(list) writes ONE <style> element:
|
|
11
|
+
// [data-theme="org-light"] { color-scheme: light; --color-primary: oklch(55.8% 0.154 249.1); ... }
|
|
12
|
+
// DaisyUI components read these CSS variables, so a custom theme works like a built-in one, and the theme switcher
|
|
13
|
+
// lists it first. Pass them in config.customThemes, load them lazily with config.loadThemes (like loadLocale),
|
|
14
|
+
// or set them any time later with setCustomThemes(json).
|
|
15
|
+
// The built-in DaisyUI themes still come from your CSS: @plugin "daisyui" { themes: all; }.
|
|
16
|
+
|
|
17
|
+
import type { CustomTheme, CustomThemeMap, CustomThemes, CustomThemesModule, UiConfig } from "../interfaces/ui-config.ts";
|
|
18
|
+
import { customThemesSignal, uiConfigSignal } from "../state/signals.ts";
|
|
19
|
+
import { isSignalSource } from "../utils/signal-source.ts";
|
|
20
|
+
import { applySavedThemeWhenKnown, leaveUnknownTheme } from "./theme-service.ts";
|
|
21
|
+
|
|
22
|
+
const STYLE_ELEMENT_ID = "shared-lib-web-custom-themes";
|
|
23
|
+
/** Theme names: lowercase letters, digits and "-" (they go into a CSS selector). */
|
|
24
|
+
const SAFE_NAME = /^[a-z0-9][a-z0-9-]*$/;
|
|
25
|
+
/** Variable names, with or without the leading "--". */
|
|
26
|
+
const SAFE_VARIABLE = /^(--)?[a-z0-9][a-z0-9-]*$/i;
|
|
27
|
+
/** Characters that could end the CSS rule (values come from JSON, maybe from a server). */
|
|
28
|
+
const UNSAFE_VALUE = /[;{}<>"\\]/;
|
|
29
|
+
const VARIABLE_PREFIX = "--";
|
|
30
|
+
|
|
31
|
+
/** "color-primary" -> "--color-primary" ("--color-primary" stays). */
|
|
32
|
+
function toVariableName(key: string): string {
|
|
33
|
+
const hasPrefix = key.startsWith(VARIABLE_PREFIX);
|
|
34
|
+
return hasPrefix ? key : `${VARIABLE_PREFIX}${key}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The CSS lines of the variables; unsafe names or values are skipped. */
|
|
38
|
+
function buildVariableLines(variables: Record<string, string>): string[] {
|
|
39
|
+
const lines: string[] = [];
|
|
40
|
+
const entries = Object.entries(variables);
|
|
41
|
+
for (const [key, value] of entries) {
|
|
42
|
+
const valueText = String(value);
|
|
43
|
+
const isSafeKey = SAFE_VARIABLE.test(key);
|
|
44
|
+
const isUnsafeValue = UNSAFE_VALUE.test(valueText);
|
|
45
|
+
if (!isSafeKey || isUnsafeValue) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const variableName = toVariableName(key);
|
|
49
|
+
lines.push(` ${variableName}: ${valueText};`);
|
|
50
|
+
}
|
|
51
|
+
return lines;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** true when the theme has a usable name. */
|
|
55
|
+
export function isValidCustomTheme(theme: CustomTheme): boolean {
|
|
56
|
+
const hasName = typeof theme?.name === "string";
|
|
57
|
+
return hasName && SAFE_NAME.test(theme.name);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The CSS rule of one custom theme: [data-theme="name"] { color-scheme: ...; --color-primary: ...; } */
|
|
61
|
+
export function buildCustomThemeCss(theme: CustomTheme): string {
|
|
62
|
+
const isValid = isValidCustomTheme(theme);
|
|
63
|
+
if (!isValid) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
const colorScheme = theme.isDark ? "dark" : "light";
|
|
67
|
+
const variableLines = buildVariableLines(theme.variables ?? {});
|
|
68
|
+
const bodyLines = [` color-scheme: ${colorScheme};`, ...variableLines];
|
|
69
|
+
const bodyText = bodyLines.join("\n");
|
|
70
|
+
return `[data-theme="${theme.name}"] {\n${bodyText}\n}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Writes the CSS into the one <style> element of the custom themes (created on first use). */
|
|
74
|
+
function writeThemeStyle(cssText: string): void {
|
|
75
|
+
if (typeof document === "undefined") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
let styleElement = document.getElementById(STYLE_ELEMENT_ID);
|
|
79
|
+
if (!styleElement) {
|
|
80
|
+
styleElement = document.createElement("style");
|
|
81
|
+
styleElement.id = STYLE_ELEMENT_ID;
|
|
82
|
+
document.head.append(styleElement);
|
|
83
|
+
}
|
|
84
|
+
styleElement.textContent = cssText;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** true for a plain object (not null, not a list). */
|
|
88
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
89
|
+
const isObject = typeof value === "object" && value !== null;
|
|
90
|
+
return isObject && !Array.isArray(value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The themes of an object keyed by theme name; an entry without "name" gets its key as name. */
|
|
94
|
+
function readThemeMapEntries(themeMap: CustomThemeMap): CustomTheme[] {
|
|
95
|
+
const themeList: CustomTheme[] = [];
|
|
96
|
+
const entries = Object.entries(themeMap);
|
|
97
|
+
for (const [key, entry] of entries) {
|
|
98
|
+
if (!isPlainObject(entry)) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const hasName = typeof entry.name === "string";
|
|
102
|
+
const name = hasName ? entry.name : key;
|
|
103
|
+
themeList.push({ ...entry, name } as CustomTheme);
|
|
104
|
+
}
|
|
105
|
+
return themeList;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The custom themes as a list: an object keyed by theme name ({ "org-light": {...}, "org-dark": {...} }) is turned
|
|
110
|
+
* into a list (in key order); a list stays as it is; anything else is an empty list.
|
|
111
|
+
*/
|
|
112
|
+
export function toCustomThemeList(themes?: CustomThemes | null): CustomTheme[] {
|
|
113
|
+
if (Array.isArray(themes)) {
|
|
114
|
+
return themes;
|
|
115
|
+
}
|
|
116
|
+
if (isPlainObject(themes)) {
|
|
117
|
+
return readThemeMapEntries(themes as CustomThemeMap);
|
|
118
|
+
}
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sets the custom themes (replaces the earlier ones): writes their CSS, adds them to the theme switcher and,
|
|
124
|
+
* when the saved theme is one of them, switches to it. Accepts the parsed JSON as it is — the object keyed by theme
|
|
125
|
+
* name, or a list; invalid entries are skipped.
|
|
126
|
+
*/
|
|
127
|
+
export function setCustomThemes(themes?: CustomThemes | null): void {
|
|
128
|
+
const validThemes: CustomTheme[] = [];
|
|
129
|
+
const givenThemes = toCustomThemeList(themes);
|
|
130
|
+
for (const theme of givenThemes) {
|
|
131
|
+
const isValid = isValidCustomTheme(theme);
|
|
132
|
+
if (isValid) {
|
|
133
|
+
validThemes.push(theme);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const cssParts = validThemes.map(buildCustomThemeCss);
|
|
137
|
+
const cssText = cssParts.join("\n\n");
|
|
138
|
+
writeThemeStyle(cssText);
|
|
139
|
+
customThemesSignal.value = validThemes;
|
|
140
|
+
applySavedThemeWhenKnown();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** true when the value is a JSON module ({ default: themes }), not a theme map with a theme called "default". */
|
|
144
|
+
function isThemesJsonModule(themesModule: CustomThemesModule): themesModule is { default: CustomThemes } {
|
|
145
|
+
if (!isPlainObject(themesModule)) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
const defaultValue = themesModule.default;
|
|
149
|
+
if (Array.isArray(defaultValue)) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
return isPlainObject(defaultValue) && !("variables" in defaultValue);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The themes inside a themes module as a list: the object keyed by name or the list, or the default export of a JSON module. */
|
|
156
|
+
export function unwrapThemesModule(themesModule: CustomThemesModule): CustomTheme[] {
|
|
157
|
+
const isJsonModule = isThemesJsonModule(themesModule);
|
|
158
|
+
if (isJsonModule) {
|
|
159
|
+
return toCustomThemeList(themesModule.default);
|
|
160
|
+
}
|
|
161
|
+
return toCustomThemeList(themesModule as CustomThemes);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** baseThemes + extraThemes; a theme with the same name in extraThemes replaces the one in baseThemes. */
|
|
165
|
+
export function mergeCustomThemes(baseThemes: CustomTheme[], extraThemes: CustomTheme[]): CustomTheme[] {
|
|
166
|
+
const mergedThemes: CustomTheme[] = [];
|
|
167
|
+
const extraNames: string[] = [];
|
|
168
|
+
for (const extraTheme of extraThemes) {
|
|
169
|
+
extraNames.push(extraTheme?.name);
|
|
170
|
+
}
|
|
171
|
+
for (const baseTheme of baseThemes) {
|
|
172
|
+
const isReplaced = extraNames.includes(baseTheme.name);
|
|
173
|
+
if (!isReplaced) {
|
|
174
|
+
mergedThemes.push(baseTheme);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
mergedThemes.push(...extraThemes);
|
|
178
|
+
return mergedThemes;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Configs whose loadThemes already ran (StrictMode mounts twice). */
|
|
182
|
+
const loadedConfigs = new WeakSet<UiConfig>();
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Runs config.loadThemes once per config and adds the result to config.customThemes.
|
|
186
|
+
* When it fails, or the saved theme is not in the result, the default theme is used.
|
|
187
|
+
*/
|
|
188
|
+
export async function loadConfigThemes(config: UiConfig): Promise<void> {
|
|
189
|
+
const loadThemes = config.loadThemes;
|
|
190
|
+
const isLoaded = loadedConfigs.has(config);
|
|
191
|
+
if (typeof loadThemes !== "function" || isLoaded) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
loadedConfigs.add(config);
|
|
195
|
+
try {
|
|
196
|
+
const themesModule = await loadThemes();
|
|
197
|
+
const loadedThemes = unwrapThemesModule(themesModule);
|
|
198
|
+
const isConfigStillActive = uiConfigSignal.peek() === config;
|
|
199
|
+
if (isConfigStillActive) {
|
|
200
|
+
const baseThemes = toCustomThemeList(config.customThemes);
|
|
201
|
+
const mergedThemes = mergeCustomThemes(baseThemes, loadedThemes);
|
|
202
|
+
setCustomThemes(mergedThemes);
|
|
203
|
+
}
|
|
204
|
+
} catch (error) {
|
|
205
|
+
console.error("shared-lib-web: could not load the custom themes (config.loadThemes).", error);
|
|
206
|
+
}
|
|
207
|
+
leaveUnknownTheme();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** One value of a loadThemes signal: null = not here yet; a list (or JSON module) replaces the loaded themes. */
|
|
211
|
+
function applyThemesFromSignal(config: UiConfig, themesModule: CustomThemesModule | null): void {
|
|
212
|
+
const isConfigActive = uiConfigSignal.peek() === config;
|
|
213
|
+
if (!themesModule || !isConfigActive) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const loadedThemes = unwrapThemesModule(themesModule);
|
|
217
|
+
const baseThemes = toCustomThemeList(config.customThemes);
|
|
218
|
+
const mergedThemes = mergeCustomThemes(baseThemes, loadedThemes);
|
|
219
|
+
setCustomThemes(mergedThemes);
|
|
220
|
+
leaveUnknownTheme();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Follows config.loadThemes when it is a signal. Returns the stop function (nothing to stop otherwise). */
|
|
224
|
+
export function followConfigThemes(config: UiConfig): () => void {
|
|
225
|
+
const themesSource = config.loadThemes;
|
|
226
|
+
if (!isSignalSource(themesSource)) {
|
|
227
|
+
return stopNothing;
|
|
228
|
+
}
|
|
229
|
+
const applyThemes = applyThemesFromSignal.bind(null, config);
|
|
230
|
+
return themesSource.subscribe(applyThemes as (value: unknown) => void);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Nothing to clean up. */
|
|
234
|
+
function stopNothing(): void {
|
|
235
|
+
return;
|
|
236
|
+
}
|