tiun 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.
- checksums.yaml +4 -4
- data/docs/components/images.md +34 -4
- data/docs/components/images.ru.md +33 -4
- data/docs/components/login.md +2 -0
- data/docs/components/login.ru.md +2 -0
- data/docs/components/profile.md +3 -1
- data/docs/components/profile.ru.md +3 -1
- data/docs/components/schema-records.md +2 -0
- data/docs/components/schema-records.ru.md +2 -0
- data/docs/components/signup.md +3 -1
- data/docs/components/signup.ru.md +3 -1
- data/docs/components/theme.md +3 -1
- data/docs/components/theme.ru.md +3 -1
- data/docs/reference.ru.md +6 -6
- data/lib/config/locale/ru.yml +0 -3
- data/lib/tiun/auth/token_identity.rb +9 -0
- data/lib/tiun/controller/auth/identity.rb +78 -19
- data/lib/tiun/controller/auth/registration.rb +23 -0
- data/lib/tiun/controller/auth.rb +4 -4
- data/lib/tiun/controller/base/representation.rb +12 -1
- data/lib/tiun/mailer/confirmation.rb +10 -2
- data/lib/tiun/model/account.rb +79 -3
- data/lib/tiun/model/auth.rb +2 -0
- data/lib/tiun/model/me.rb +83 -2
- data/lib/tiun/model/settings.rb +7 -0
- data/lib/tiun/model/token.rb +2 -2
- data/lib/tiun/proceed/schemas/constants.rb +1 -0
- data/lib/tiun/proceed/schemas/spans.rb +1 -0
- data/lib/tiun/routes/mounts.rb +7 -4
- data/lib/tiun/service/email_confirmation.rb +84 -12
- data/lib/tiun/version.rb +1 -1
- data/package-lock.json +2 -2
- data/package.json +1 -1
- data/src/components/Component.tsx +4 -1
- data/src/components/Image.tsx +36 -25
- data/src/components/Index.tsx +193 -24
- data/src/components/Loader.tsx +11 -9
- data/src/components/Lorem.tsx +7 -1
- data/src/components/Modal.tsx +8 -1
- data/src/components/Paginator.tsx +9 -3
- data/src/components/Records.tsx +45 -18
- data/src/components/calendar-browser.scss +14 -4
- data/src/components/form/Login.tsx +12 -6
- data/src/components/form/auth.scss +44 -130
- data/src/components/form/forms.scss +9 -8
- data/src/components/form/types.ts +2 -0
- data/src/components/image.scss +10 -7
- data/src/components/records.scss +4 -0
- data/src/components/theme-palettes/dark.scss +1 -0
- data/src/concern/application.ts +3 -2
- data/src/concern/spansViewProps.tsx +14 -1
- data/src/concern/useRecordsResource.ts +2 -0
- data/src/decorators/withField.tsx +10 -7
- data/src/decorators/withForm.tsx +12 -6
- data/src/schema/Schema.ts +10 -0
- data/src/services/calendar/daylight.ts +31 -4
- data/src/services/methods.ts +1 -1
- data/src/services/records/useRecordPages.ts +23 -3
- data/src/services/session/renewal.ts +69 -6
- data/src/services/useAppearance.ts +22 -11
- data/src/services/useSwitch.ts +23 -17
- data/src/theme/configuration.ts +21 -7
- metadata +2 -2
|
@@ -235,7 +235,13 @@ export async function resolveDaylight(
|
|
|
235
235
|
return daylightState(response, now);
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
/**
|
|
238
|
+
/**
|
|
239
|
+
* Keeps the automatic theme in step with sunrise and sunset.
|
|
240
|
+
* - Refreshes on return to a visible tab when the scheduled update is due.
|
|
241
|
+
* - Cancels pending work when the automatic theme is turned off.
|
|
242
|
+
* @param options DaylightOptions with theme callbacks, browser services, and optional endpoints.
|
|
243
|
+
* @returns A function that stops the automatic theme updates.
|
|
244
|
+
*/
|
|
239
245
|
export function startAutomaticTheme({
|
|
240
246
|
onTheme,
|
|
241
247
|
onError,
|
|
@@ -250,8 +256,24 @@ export function startAutomaticTheme({
|
|
|
250
256
|
let locatedAt = 0,
|
|
251
257
|
location: DaylightCoordinates | undefined,
|
|
252
258
|
pending = false,
|
|
259
|
+
refreshAt = 0,
|
|
253
260
|
timer: ReturnType<typeof setTimeout> | undefined;
|
|
254
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Schedules the next automatic theme calculation.
|
|
264
|
+
* @param delay Number of milliseconds before the next calculation.
|
|
265
|
+
* @returns No value.
|
|
266
|
+
*/
|
|
267
|
+
function scheduleRefresh(delay: number): void {
|
|
268
|
+
refreshAt = now() + delay;
|
|
269
|
+
timer = setTimeout(refresh, delay);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Calculates the theme from the current location and schedules the next update.
|
|
274
|
+
* Takes no arguments.
|
|
275
|
+
* @returns A promise that settles when the calculation or error handling finishes.
|
|
276
|
+
*/
|
|
255
277
|
async function refresh() {
|
|
256
278
|
if (pending || controller.signal.aborted) {
|
|
257
279
|
return;
|
|
@@ -272,19 +294,24 @@ export function startAutomaticTheme({
|
|
|
272
294
|
const midnight =
|
|
273
295
|
Date.parse(`${localDate(now(), location.longitude)}T00:00:00Z`) + 86400000 - location.longitude * 240000,
|
|
274
296
|
next = Math.min(state.next ?? Infinity, midnight, now() + 21600000);
|
|
275
|
-
|
|
297
|
+
scheduleRefresh(Math.max(1000, next - now() + 50));
|
|
276
298
|
} catch (error) {
|
|
277
299
|
if (!controller.signal.aborted) {
|
|
278
300
|
onError(error);
|
|
279
|
-
|
|
301
|
+
scheduleRefresh(300000);
|
|
280
302
|
}
|
|
281
303
|
} finally {
|
|
282
304
|
pending = false;
|
|
283
305
|
}
|
|
284
306
|
}
|
|
285
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Runs an overdue theme calculation when the page is visible.
|
|
310
|
+
* Takes no arguments.
|
|
311
|
+
* @returns No value.
|
|
312
|
+
*/
|
|
286
313
|
const visible = () => {
|
|
287
|
-
if (!view.document.hidden) {
|
|
314
|
+
if (!view.document.hidden && now() >= refreshAt) {
|
|
288
315
|
void refresh();
|
|
289
316
|
}
|
|
290
317
|
};
|
data/src/services/methods.ts
CHANGED
|
@@ -35,12 +35,14 @@ type PageReceiver<T> = (items: readonly T[], replace: boolean) => void;
|
|
|
35
35
|
|
|
36
36
|
interface PageDelivery<T> {
|
|
37
37
|
readonly onPage?: PageReceiver<T> | undefined;
|
|
38
|
+
readonly keepPreviousOnReload?: boolean;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
const EMPTY: readonly never[] = [];
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* Loads and validates paginated reads through the shared Loader operation.
|
|
45
|
+
* - Keeps prior records during a replacement read when enabled for the same endpoint.
|
|
44
46
|
* @param options Endpoint or page loader, initial records, pagination and optional page receiver.
|
|
45
47
|
* @returns Loaded records, pagination state and request callbacks.
|
|
46
48
|
*/
|
|
@@ -53,6 +55,7 @@ export function useRecordPages<T>({
|
|
|
53
55
|
source,
|
|
54
56
|
eagerLoad = true,
|
|
55
57
|
enabled = true,
|
|
58
|
+
keepPreviousOnReload = false,
|
|
56
59
|
onPage,
|
|
57
60
|
}: RecordPagesOptions<T> & PageDelivery<T>) {
|
|
58
61
|
if (!Number.isSafeInteger(pageSize) || pageSize < 1) throw new Error("Invalid pageSize");
|
|
@@ -118,6 +121,8 @@ export function useRecordPages<T>({
|
|
|
118
121
|
current.current = { loadedCount, range };
|
|
119
122
|
const loader = useRef(activeLoader);
|
|
120
123
|
loader.current = activeLoader;
|
|
124
|
+
const endpoint = enabled ? source?.url : undefined;
|
|
125
|
+
const previousEndpoint = useRef<string | undefined>(undefined);
|
|
121
126
|
|
|
122
127
|
/**
|
|
123
128
|
* Requests a range and publishes it only while its loading operation is current.
|
|
@@ -205,15 +210,30 @@ export function useRecordPages<T>({
|
|
|
205
210
|
);
|
|
206
211
|
|
|
207
212
|
useEffect(() => {
|
|
213
|
+
const replacing = enabled && eagerLoad && !initialContentRange && Boolean(loader.current);
|
|
214
|
+
const keepItems =
|
|
215
|
+
keepPreviousOnReload && replacing && endpoint !== undefined && endpoint === previousEndpoint.current;
|
|
216
|
+
previousEndpoint.current = endpoint;
|
|
208
217
|
removedBeforeOffset.current = 0;
|
|
209
|
-
publish(initialItems, true);
|
|
218
|
+
if (!keepItems) publish(initialItems, true);
|
|
210
219
|
setRange(initialContentRange ? parseContentRange(initialContentRange) : undefined);
|
|
211
220
|
setError("");
|
|
212
|
-
if (
|
|
221
|
+
if (replacing) {
|
|
213
222
|
void request(0, pageSize, true);
|
|
214
223
|
}
|
|
215
224
|
return cancel;
|
|
216
|
-
}, [
|
|
225
|
+
}, [
|
|
226
|
+
endpoint,
|
|
227
|
+
initialItems,
|
|
228
|
+
initialContentRange,
|
|
229
|
+
pageSize,
|
|
230
|
+
request,
|
|
231
|
+
eagerLoad,
|
|
232
|
+
enabled,
|
|
233
|
+
keepPreviousOnReload,
|
|
234
|
+
publish,
|
|
235
|
+
cancel,
|
|
236
|
+
]);
|
|
217
237
|
|
|
218
238
|
const fetchNext = useCallback(() => {
|
|
219
239
|
const previous = current.current.range;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AxiosError, InternalAxiosRequestConfig } from "axios";
|
|
2
2
|
import axios, { AxiosHeaders } from "axios";
|
|
3
3
|
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
4
|
+
import { object } from "../../config/application.js";
|
|
4
5
|
|
|
5
6
|
export interface SessionToken {
|
|
6
7
|
code?: string;
|
|
@@ -11,6 +12,7 @@ export interface SessionUser {
|
|
|
11
12
|
id?: string | number;
|
|
12
13
|
uid?: string | number;
|
|
13
14
|
permissions?: readonly string[];
|
|
15
|
+
appearance?: Readonly<Record<string, unknown>>;
|
|
14
16
|
session_token?: SessionToken | null;
|
|
15
17
|
refresh_token?: SessionToken | null;
|
|
16
18
|
}
|
|
@@ -35,11 +37,57 @@ export interface SessionRenewal {
|
|
|
35
37
|
|
|
36
38
|
type Request = InternalAxiosRequestConfig & { _tiunSessionRetried?: boolean };
|
|
37
39
|
|
|
38
|
-
const MAX_TIMEOUT = 2 ** 31 - 1
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
const MAX_TIMEOUT = 2 ** 31 - 1;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Reads the account key used to associate credentials with the current user.
|
|
44
|
+
* @param user Session identity or null for a guest.
|
|
45
|
+
* @returns The account key or null for a missing identity.
|
|
46
|
+
*/
|
|
47
|
+
export function ownerOf(user: SessionUser | null): string | null {
|
|
48
|
+
const owner = user?.id ?? user?.uid;
|
|
49
|
+
return typeof owner === "string" || typeof owner === "number" ? String(owner) : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Reads the browser session from its configured user resource.
|
|
54
|
+
* - Relies on the browser cookie and skips the API Authorization header.
|
|
55
|
+
* - Retains transport credentials only when they belong to the returned user.
|
|
56
|
+
* @param path Same-origin URL of the current user resource.
|
|
57
|
+
* @param credentials Previously issued session tokens, associated with an account.
|
|
58
|
+
* @param signal Signal for an obsolete or unmounted request.
|
|
59
|
+
* @returns The current user, or null when the server denies the browser session.
|
|
60
|
+
*/
|
|
61
|
+
export async function readBrowserUser<User extends SessionUser>(
|
|
62
|
+
path: string,
|
|
63
|
+
credentials: User | null,
|
|
64
|
+
signal: AbortSignal,
|
|
65
|
+
): Promise<User | null> {
|
|
66
|
+
try {
|
|
67
|
+
const { data } = await axios.get<User>(path, {
|
|
68
|
+
headers: { Authorization: false, "Cache-Control": "no-cache" },
|
|
69
|
+
signal,
|
|
70
|
+
});
|
|
71
|
+
if (!object(data) || !ownerOf(data)) {
|
|
72
|
+
throw new TypeError("Invalid current user response");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const { session_token: _session, refresh_token: _refresh, ...profile } = data;
|
|
76
|
+
return {
|
|
77
|
+
...profile,
|
|
78
|
+
...(credentials &&
|
|
79
|
+
ownerOf(credentials) === ownerOf(data) && {
|
|
80
|
+
...(credentials.session_token && { session_token: credentials.session_token }),
|
|
81
|
+
...(credentials.refresh_token && { refresh_token: credentials.refresh_token }),
|
|
82
|
+
}),
|
|
83
|
+
} as User;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (axios.isAxiosError(error) && (error.response?.status === 401 || error.response?.status === 403)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
43
91
|
|
|
44
92
|
function localPath(config: InternalAxiosRequestConfig): string | null {
|
|
45
93
|
const base = new URL(config.baseURL || "/", window.location.origin),
|
|
@@ -137,6 +185,11 @@ export function createSessionRenewal<User extends SessionUser>(
|
|
|
137
185
|
onUpdate(null);
|
|
138
186
|
}
|
|
139
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Renews the active session and shares an in-flight request.
|
|
190
|
+
* - Keeps the current permission list when the server returns the same rights.
|
|
191
|
+
* @returns Updated credentials, or the newer credentials if the session changed in the meantime.
|
|
192
|
+
*/
|
|
140
193
|
async function renew(): Promise<User> {
|
|
141
194
|
if (pending) {
|
|
142
195
|
return pending;
|
|
@@ -160,7 +213,17 @@ export function createSessionRenewal<User extends SessionUser>(
|
|
|
160
213
|
if (!nextToken || (previous.id != null && next.id != null && String(previous.id) !== String(next.id))) {
|
|
161
214
|
throw new Error("Invalid session renewal response");
|
|
162
215
|
}
|
|
163
|
-
credentials = { ...
|
|
216
|
+
credentials = { ...current, ...next };
|
|
217
|
+
if (next.permissions && current.permissions) {
|
|
218
|
+
const currentPermissions = new Set(current.permissions),
|
|
219
|
+
permissions = new Set(next.permissions);
|
|
220
|
+
if (
|
|
221
|
+
permissions.size === currentPermissions.size &&
|
|
222
|
+
[...permissions].every((permission) => currentPermissions.has(permission))
|
|
223
|
+
) {
|
|
224
|
+
credentials.permissions = current.permissions;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
164
227
|
sessionTokens.add(nextToken);
|
|
165
228
|
axios.defaults.headers.common.Authorization = `Session ${nextToken}`;
|
|
166
229
|
onUpdate(credentials);
|
|
@@ -56,6 +56,7 @@ interface DaylightStatus {
|
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Reads and saves the active owner's appearance. Guest selections use cookies.
|
|
59
|
+
* - Uses appearance supplied by the current user response before reading the separate resource.
|
|
59
60
|
* @param props Owner, metadata, resource fields and localized labels.
|
|
60
61
|
* @returns The appearance context and the action to retry a failed operation.
|
|
61
62
|
*/
|
|
@@ -65,6 +66,7 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
65
66
|
client = props.client ?? axios,
|
|
66
67
|
configuration = props.configuration ?? {},
|
|
67
68
|
meta = props.meta ?? {},
|
|
69
|
+
sessionAppearance = props.user?.appearance,
|
|
68
70
|
ownerId = props.user?.id ?? props.user?.uid,
|
|
69
71
|
// eslint-disable-next-line no-ternary, no-eq-null, eqeqeq -- Both null and undefined identify guests.
|
|
70
72
|
ownerKey = ownerId == null ? null : String(ownerId),
|
|
@@ -157,19 +159,26 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
157
159
|
*/
|
|
158
160
|
const isCurrent = (): boolean => !controller.signal.aborted && ownerRef.current.owner === ownerKey;
|
|
159
161
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
162
|
+
let read: Promise<unknown> | undefined;
|
|
163
|
+
if (shouldRead && object(sessionAppearance)) {
|
|
164
|
+
read = Promise.resolve(sessionAppearance);
|
|
165
|
+
} else if (shouldRead && typeof readPath === "string") {
|
|
166
|
+
read = client
|
|
163
167
|
.get<unknown>(readPath, { headers: headers(), signal: controller.signal })
|
|
168
|
+
.then(({ data }) => appearanceResponse(data, config));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (read) {
|
|
172
|
+
// eslint-disable-next-line no-void -- The request reports failures in the handlers below.
|
|
173
|
+
void read
|
|
164
174
|
// eslint-disable-next-line max-statements, @typescript-eslint/prefer-readonly-parameter-types -- The read and guest transfer are one operation over the HTTP response.
|
|
165
|
-
.then(async (
|
|
175
|
+
.then(async (initial): Promise<void> => {
|
|
166
176
|
if (!isCurrent()) {
|
|
167
177
|
return;
|
|
168
178
|
}
|
|
169
179
|
|
|
170
|
-
const guest = readThemeCookie(config.cookies?.guest ?? "tiun_guest_theme")
|
|
171
|
-
|
|
172
|
-
let result = response;
|
|
180
|
+
const guest = readThemeCookie(config.cookies?.guest ?? "tiun_guest_theme");
|
|
181
|
+
let loadedSettings = initial;
|
|
173
182
|
let isPartialUpdate = false;
|
|
174
183
|
|
|
175
184
|
if (typeof guest === "string") {
|
|
@@ -177,7 +186,7 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
177
186
|
if (request) {
|
|
178
187
|
setSavedAppearance(normalizedAppearance(initial, config));
|
|
179
188
|
setPreviewedTheme(guest);
|
|
180
|
-
result = await client.request<unknown>({
|
|
189
|
+
const result = await client.request<unknown>({
|
|
181
190
|
...request,
|
|
182
191
|
headers: headers(),
|
|
183
192
|
signal: controller.signal,
|
|
@@ -185,12 +194,12 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
185
194
|
if (!isCurrent()) {
|
|
186
195
|
return;
|
|
187
196
|
}
|
|
197
|
+
loadedSettings = appearanceResponse(result.data, config);
|
|
188
198
|
clearGuestTheme(guest, config.cookies);
|
|
189
199
|
isPartialUpdate = request.method === "patch";
|
|
190
200
|
}
|
|
191
201
|
}
|
|
192
202
|
|
|
193
|
-
const loadedSettings = appearanceResponse(result.data, config);
|
|
194
203
|
let settings = loadedSettings;
|
|
195
204
|
if (isPartialUpdate && object(initial) && object(loadedSettings)) {
|
|
196
205
|
settings = { ...initial, ...loadedSettings };
|
|
@@ -222,7 +231,7 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
222
231
|
controller.abort();
|
|
223
232
|
appearanceRequestRef.current?.abort();
|
|
224
233
|
};
|
|
225
|
-
}, [ownerKey, ready, readPath, savePath, saveMethod, revision, themeSettingsKey, client]);
|
|
234
|
+
}, [ownerKey, ready, sessionAppearance, readPath, savePath, saveMethod, revision, themeSettingsKey, client]);
|
|
226
235
|
|
|
227
236
|
/**
|
|
228
237
|
* Saves appearance through the shared coordinator, including writes from configured forms.
|
|
@@ -368,7 +377,9 @@ export const useAppearance = (props: AppearanceOptions): AppearanceLifecycle =>
|
|
|
368
377
|
loading,
|
|
369
378
|
owner: ownerKey,
|
|
370
379
|
previewTheme,
|
|
371
|
-
ready:
|
|
380
|
+
ready:
|
|
381
|
+
ready &&
|
|
382
|
+
(!isSignedIn || !hasResource || ((object(sessionAppearance) || Boolean(readPath)) && Boolean(savePath))),
|
|
372
383
|
save,
|
|
373
384
|
savedAppearance,
|
|
374
385
|
saving,
|
data/src/services/useSwitch.ts
CHANGED
|
@@ -9,7 +9,8 @@ import { useForm } from "../components/form/context.js";
|
|
|
9
9
|
import { useI18n } from "../concern/i18n.js";
|
|
10
10
|
import { useTheme } from "../theme/context.js";
|
|
11
11
|
|
|
12
|
-
const defaultWait = 5000
|
|
12
|
+
const defaultWait = 5000,
|
|
13
|
+
singleField = 1;
|
|
13
14
|
|
|
14
15
|
type SwitchPictures = readonly [StockeeAsset, StockeeAsset];
|
|
15
16
|
type SwitchProps = Pick<
|
|
@@ -21,6 +22,7 @@ type SwitchProps = Pick<
|
|
|
21
22
|
| "name"
|
|
22
23
|
| "onSwitch"
|
|
23
24
|
| "pics"
|
|
25
|
+
| "sharedTheme"
|
|
24
26
|
| "sunImage"
|
|
25
27
|
| "title"
|
|
26
28
|
| "value"
|
|
@@ -64,7 +66,8 @@ const switchPictures = ({
|
|
|
64
66
|
|
|
65
67
|
/**
|
|
66
68
|
* Resolves a switch field and connects its changes to its form.
|
|
67
|
-
* -
|
|
69
|
+
* - Previews the shared theme on selection and schedules saving for a single-field theme form.
|
|
70
|
+
* - Generic switches use the configured callback and the form's submit method.
|
|
68
71
|
* - Uses the built-in theme defaults when the switch has no custom choices or pictures.
|
|
69
72
|
* @param props Field name, current value, choices, captions, pictures and form settings.
|
|
70
73
|
* @returns The current position, visible value, accessible title and handler for cycling the switch.
|
|
@@ -73,15 +76,19 @@ export const useSwitch = (
|
|
|
73
76
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types -- The shared form types include asset and callback objects.
|
|
74
77
|
props: SwitchProps,
|
|
75
78
|
): SwitchState => {
|
|
76
|
-
const { submit, update } = useForm(),
|
|
79
|
+
const { meta, submit, update } = useForm(),
|
|
77
80
|
{ t: translate } = useI18n(),
|
|
78
81
|
cancel = useRef<(() => void) | null>(null),
|
|
79
82
|
context = useTheme(),
|
|
80
83
|
isThemeSwitch = props.onSwitch === methods.change_theme.onSwitch,
|
|
84
|
+
sharedTheme = props.sharedTheme === true && (!props.onSwitch || isThemeSwitch),
|
|
81
85
|
previewTheme = context?.previewTheme,
|
|
82
86
|
values = props.values ?? THEMES,
|
|
83
|
-
[first] = values
|
|
84
|
-
|
|
87
|
+
[first] = values;
|
|
88
|
+
let value = props.value ?? first;
|
|
89
|
+
if (sharedTheme && context) {
|
|
90
|
+
value = context.appearance.theme;
|
|
91
|
+
}
|
|
85
92
|
if (typeof value !== "string") {
|
|
86
93
|
throw new TypeError(translate("tiun.errors.application.configuration"));
|
|
87
94
|
}
|
|
@@ -93,10 +100,10 @@ export const useSwitch = (
|
|
|
93
100
|
|
|
94
101
|
useEffect(() => (): void => cancel.current?.(), []);
|
|
95
102
|
useLayoutEffect((): void => {
|
|
96
|
-
if (isThemeSwitch && previewTheme) {
|
|
103
|
+
if (isThemeSwitch && previewTheme && !sharedTheme) {
|
|
97
104
|
methods.change_theme.onSwitch(value, { previewTheme });
|
|
98
105
|
}
|
|
99
|
-
}, [isThemeSwitch, previewTheme, value]);
|
|
106
|
+
}, [isThemeSwitch, previewTheme, sharedTheme, value]);
|
|
100
107
|
|
|
101
108
|
/**
|
|
102
109
|
* Selects the next configured value and schedules the form submission.
|
|
@@ -104,19 +111,18 @@ export const useSwitch = (
|
|
|
104
111
|
*/
|
|
105
112
|
const cycle = (): void => {
|
|
106
113
|
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- Advance one position and wrap to the first choice.
|
|
107
|
-
const next = values.at(position + 1) ?? first
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
if (!isThemeSwitch || !previewTheme) {
|
|
114
|
+
const next = values.at(position + 1) ?? first,
|
|
115
|
+
theme = THEMES.find((mode) => mode === next);
|
|
116
|
+
if (isThemeSwitch && context?.owner === null && typeof theme === "string") {
|
|
117
|
+
// eslint-disable-next-line no-void -- changeTheme owns its asynchronous request state.
|
|
118
|
+
void context.changeTheme(theme);
|
|
119
|
+
} else if (sharedTheme && typeof theme === "string" && !props.onSwitch) {
|
|
120
|
+
context?.previewTheme(theme);
|
|
121
|
+
} else if (sharedTheme || !isThemeSwitch || !previewTheme) {
|
|
116
122
|
props.onSwitch?.(next, context);
|
|
117
123
|
}
|
|
118
124
|
update(props.name, next);
|
|
119
|
-
if (submit) {
|
|
125
|
+
if (submit && (!(isThemeSwitch || sharedTheme) || Object.keys(meta ?? {}).length <= singleField)) {
|
|
120
126
|
cancel.current?.();
|
|
121
127
|
cancel.current = scheduleTimeout((): void => {
|
|
122
128
|
// eslint-disable-next-line no-void -- Form reports the result in its own feedback.
|
data/src/theme/configuration.ts
CHANGED
|
@@ -225,18 +225,32 @@ export function resetAppearance(data: FormData, form: FormMeta, configuration: T
|
|
|
225
225
|
};
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Applies appearance rules and binds its theme field to the shared appearance state.
|
|
230
|
+
* @param form Configured appearance fields.
|
|
231
|
+
* @param configuration Default values and numeric bounds of the appearance settings.
|
|
232
|
+
* @returns Form meta with prepared appearance fields and live translation getters.
|
|
233
|
+
*/
|
|
228
234
|
export function appearanceFields(form: FormMeta, configuration: ThemeConfiguration): FormMeta {
|
|
229
235
|
return Object.fromEntries(
|
|
230
236
|
Object.entries(form).map(([name, field]) => {
|
|
231
|
-
const rule = configuration.fields?.[name]
|
|
232
|
-
|
|
237
|
+
const rule = configuration.fields?.[name],
|
|
238
|
+
sharedTheme =
|
|
239
|
+
typeof configuration.resource === "string" &&
|
|
240
|
+
name === "theme" &&
|
|
241
|
+
field.of === "wye" &&
|
|
242
|
+
(field.values?.every((value) => THEMES.some((theme) => theme === value)) ?? true);
|
|
243
|
+
if (!rule && !sharedTheme) {
|
|
233
244
|
return [name, field];
|
|
234
245
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
...(
|
|
239
|
-
...(rule
|
|
246
|
+
// eslint-disable-next-line one-var -- Prepare overrides only for fields managed by appearance.
|
|
247
|
+
const maximum = rule && maxAppearanceValue(rule),
|
|
248
|
+
overrides = {
|
|
249
|
+
...(sharedTheme && { sharedTheme }),
|
|
250
|
+
...(rule && { default: rule.default }),
|
|
251
|
+
...(typeof rule?.min === "number" && { min: rule.min }),
|
|
252
|
+
...(typeof maximum === "number" && { max: maximum }),
|
|
253
|
+
...(typeof rule?.step === "number" && { step: rule.step }),
|
|
240
254
|
},
|
|
241
255
|
result: FieldMeta = {};
|
|
242
256
|
// Keep Schema's live translation getters when applying numeric constraints.
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tiun
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Malo Skrylevo
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-12-
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: appraisal
|