@molecule/app-angular 3.3.1 → 3.6.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.
- package/README.md +4049 -1255
- package/dist/chat.d.ts.map +1 -1
- package/dist/chat.js +3 -2
- package/dist/chat.js.map +1 -1
- package/dist/hooks/external-store.d.ts +32 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +62 -0
- package/dist/hooks/external-store.js.map +1 -0
- package/dist/hooks/index.d.ts +24 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +24 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-ai-models.d.ts +53 -0
- package/dist/hooks/use-ai-models.d.ts.map +1 -0
- package/dist/hooks/use-ai-models.js +167 -0
- package/dist/hooks/use-ai-models.js.map +1 -0
- package/dist/hooks/use-async.d.ts +129 -0
- package/dist/hooks/use-async.d.ts.map +1 -0
- package/dist/hooks/use-async.js +194 -0
- package/dist/hooks/use-async.js.map +1 -0
- package/dist/hooks/use-auth-flows.d.ts +226 -0
- package/dist/hooks/use-auth-flows.d.ts.map +1 -0
- package/dist/hooks/use-auth-flows.js +481 -0
- package/dist/hooks/use-auth-flows.js.map +1 -0
- package/dist/hooks/use-auth.d.ts +85 -0
- package/dist/hooks/use-auth.d.ts.map +1 -0
- package/dist/hooks/use-auth.js +104 -0
- package/dist/hooks/use-auth.js.map +1 -0
- package/dist/hooks/use-forms.d.ts +174 -0
- package/dist/hooks/use-forms.d.ts.map +1 -0
- package/dist/hooks/use-forms.js +221 -0
- package/dist/hooks/use-forms.js.map +1 -0
- package/dist/hooks/use-http.d.ts +163 -0
- package/dist/hooks/use-http.d.ts.map +1 -0
- package/dist/hooks/use-http.js +198 -0
- package/dist/hooks/use-http.js.map +1 -0
- package/dist/hooks/use-ide.d.ts +208 -0
- package/dist/hooks/use-ide.d.ts.map +1 -0
- package/dist/hooks/use-ide.js +217 -0
- package/dist/hooks/use-ide.js.map +1 -0
- package/dist/hooks/use-logger.d.ts +40 -0
- package/dist/hooks/use-logger.d.ts.map +1 -0
- package/dist/hooks/use-logger.js +56 -0
- package/dist/hooks/use-logger.js.map +1 -0
- package/dist/hooks/use-native.d.ts +218 -0
- package/dist/hooks/use-native.d.ts.map +1 -0
- package/dist/hooks/use-native.js +173 -0
- package/dist/hooks/use-native.js.map +1 -0
- package/dist/hooks/use-profile-form.d.ts +113 -0
- package/dist/hooks/use-profile-form.d.ts.map +1 -0
- package/dist/hooks/use-profile-form.js +253 -0
- package/dist/hooks/use-profile-form.js.map +1 -0
- package/dist/hooks/use-router.d.ts +219 -0
- package/dist/hooks/use-router.d.ts.map +1 -0
- package/dist/hooks/use-router.js +266 -0
- package/dist/hooks/use-router.js.map +1 -0
- package/dist/hooks/use-storage.d.ts +142 -0
- package/dist/hooks/use-storage.d.ts.map +1 -0
- package/dist/hooks/use-storage.js +299 -0
- package/dist/hooks/use-storage.js.map +1 -0
- package/dist/hooks/use-store.d.ts +81 -0
- package/dist/hooks/use-store.d.ts.map +1 -0
- package/dist/hooks/use-store.js +143 -0
- package/dist/hooks/use-store.js.map +1 -0
- package/dist/hooks/use-theme.d.ts +59 -0
- package/dist/hooks/use-theme.d.ts.map +1 -0
- package/dist/hooks/use-theme.js +117 -0
- package/dist/hooks/use-theme.js.map +1 -0
- package/dist/hooks/use-translation.d.ts +91 -0
- package/dist/hooks/use-translation.d.ts.map +1 -0
- package/dist/hooks/use-translation.js +152 -0
- package/dist/hooks/use-translation.js.map +1 -0
- package/dist/hooks/use-verify-payment-return.d.ts +86 -0
- package/dist/hooks/use-verify-payment-return.d.ts.map +1 -0
- package/dist/hooks/use-verify-payment-return.js +167 -0
- package/dist/hooks/use-verify-payment-return.js.map +1 -0
- package/dist/hooks/use-version.d.ts +49 -0
- package/dist/hooks/use-version.d.ts.map +1 -0
- package/dist/hooks/use-version.js +70 -0
- package/dist/hooks/use-version.js.map +1 -0
- package/dist/i18n-reactive.d.ts +2 -5
- package/dist/i18n-reactive.d.ts.map +1 -1
- package/dist/i18n-reactive.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/inject.d.ts +24 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/inject.js +33 -0
- package/dist/inject.js.map +1 -0
- package/dist/link.d.ts +63 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +82 -0
- package/dist/link.js.map +1 -0
- package/dist/maybe-signal.d.ts +26 -0
- package/dist/maybe-signal.d.ts.map +1 -0
- package/dist/maybe-signal.js +20 -0
- package/dist/maybe-signal.js.map +1 -0
- package/dist/providers.d.ts +24 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +44 -3
- package/dist/providers.js.map +1 -1
- package/dist/session-ready.d.ts +41 -0
- package/dist/session-ready.d.ts.map +1 -0
- package/dist/session-ready.js +79 -0
- package/dist/session-ready.js.map +1 -0
- package/dist/tokens.d.ts +15 -0
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +12 -0
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +14 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for routing, mirroring `useRouter`, `useRouterInstance`,
|
|
3
|
+
* `useLocation`, `useParams`, `useQuery`, `useNavigate`, `useIsActive`,
|
|
4
|
+
* `useListRoute` and `useConsumedSearchParam` from `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import type { ListRouteChange, ListRouteOptions, ListRouteResult, NavigateOptions, QueryParams, RouteLocation, RouteParams, Router } from '@molecule/app-routing';
|
|
10
|
+
import { type MaybeSignal } from '../maybe-signal.js';
|
|
11
|
+
/**
|
|
12
|
+
* The router registered by `provideMolecule({ router })` or `provideRouter`.
|
|
13
|
+
* Call it in an injection context.
|
|
14
|
+
*
|
|
15
|
+
* @returns The router instance.
|
|
16
|
+
* @throws {Error} When no router is registered.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useRouterInstance(): Router;
|
|
19
|
+
/**
|
|
20
|
+
* What {@link useRouter} returns.
|
|
21
|
+
*/
|
|
22
|
+
export interface UseRouterResult {
|
|
23
|
+
/** The current route location. */
|
|
24
|
+
readonly location: Signal<RouteLocation>;
|
|
25
|
+
/** The current route's path parameters. */
|
|
26
|
+
readonly params: Signal<RouteParams>;
|
|
27
|
+
/** The current query parameters. */
|
|
28
|
+
readonly query: Signal<QueryParams>;
|
|
29
|
+
/** Navigates to a path. */
|
|
30
|
+
navigate: (path: string, options?: NavigateOptions) => void;
|
|
31
|
+
/** Navigates to a named route. */
|
|
32
|
+
navigateTo: (name: string, params?: RouteParams, query?: QueryParams, options?: NavigateOptions) => void;
|
|
33
|
+
/** Goes back one history entry. */
|
|
34
|
+
back: () => void;
|
|
35
|
+
/** Goes forward one history entry. */
|
|
36
|
+
forward: () => void;
|
|
37
|
+
/** Whether a path matches the current route. */
|
|
38
|
+
isActive: (path: string, exact?: boolean) => boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Routing state and actions. Call it in an injection context (a component
|
|
42
|
+
* field initializer or constructor).
|
|
43
|
+
*
|
|
44
|
+
* @returns The location, params and query as signals, and the navigation actions.
|
|
45
|
+
*/
|
|
46
|
+
export declare function useRouter(): UseRouterResult;
|
|
47
|
+
/**
|
|
48
|
+
* The current location alone. Call it in an injection context.
|
|
49
|
+
*
|
|
50
|
+
* @returns A signal of the current route location.
|
|
51
|
+
*/
|
|
52
|
+
export declare function useLocation(): Signal<RouteLocation>;
|
|
53
|
+
/**
|
|
54
|
+
* The current path parameters alone. Call it in an injection context.
|
|
55
|
+
*
|
|
56
|
+
* @returns A signal of the current path parameters.
|
|
57
|
+
*/
|
|
58
|
+
export declare function useParams<T extends Record<string, string> = RouteParams>(): Signal<T>;
|
|
59
|
+
/**
|
|
60
|
+
* The current query parameters alone. Call it in an injection context.
|
|
61
|
+
*
|
|
62
|
+
* @returns A signal of the current query parameters.
|
|
63
|
+
*/
|
|
64
|
+
export declare function useQuery<T extends QueryParams = QueryParams>(): Signal<T>;
|
|
65
|
+
/**
|
|
66
|
+
* A navigate function alone. Call it in an injection context.
|
|
67
|
+
*
|
|
68
|
+
* @returns The navigate function.
|
|
69
|
+
*/
|
|
70
|
+
export declare function useNavigate(): (path: string, options?: NavigateOptions) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Whether a path is active, re-evaluated on every route change. Call it in an
|
|
73
|
+
* injection context.
|
|
74
|
+
*
|
|
75
|
+
* The arguments may be signals: the active flag then follows a changed path or
|
|
76
|
+
* `exact` on the same component, the way React re-renders `useIsActive` with
|
|
77
|
+
* new arguments. A signal holding `undefined` for `exact` matches an omitted
|
|
78
|
+
* argument (`false`).
|
|
79
|
+
*
|
|
80
|
+
* @param path - The path to check (or a signal of it).
|
|
81
|
+
* @param exact - Whether to match exactly (default: `false`, or a signal of
|
|
82
|
+
* it).
|
|
83
|
+
* @returns A signal of whether the path is active.
|
|
84
|
+
*/
|
|
85
|
+
export declare function useIsActive(path: MaybeSignal<string>, exact?: MaybeSignal<boolean | undefined>): Signal<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* What {@link useListRoute} returns: each list state field as a signal (the
|
|
88
|
+
* same fields React's result carries as plain values) and the list's link
|
|
89
|
+
* builder and navigation actions.
|
|
90
|
+
*
|
|
91
|
+
* @template Filter - The filter names.
|
|
92
|
+
* @template Selection - The selection names.
|
|
93
|
+
*/
|
|
94
|
+
export interface UseListRouteResult<Filter extends string = string, Selection extends string = string> {
|
|
95
|
+
/** The current 1-indexed page (malformed and non-positive URL values read as page one). */
|
|
96
|
+
readonly page: Signal<number>;
|
|
97
|
+
/** The configured items per page. */
|
|
98
|
+
readonly pageSize: Signal<number>;
|
|
99
|
+
/** The zero-indexed item offset for an API request. */
|
|
100
|
+
readonly offset: Signal<number>;
|
|
101
|
+
/** The current URL filter values, including values supplied by path parameters. */
|
|
102
|
+
readonly filters: Signal<Record<Filter, string | undefined>>;
|
|
103
|
+
/** The matching view, or the configured fallback. */
|
|
104
|
+
readonly selection: Signal<Selection | undefined>;
|
|
105
|
+
/** The decoded current path parameters, also usable as API-only filters. */
|
|
106
|
+
readonly params: Signal<RouteParams>;
|
|
107
|
+
/** Whether the current view may load; a protected view waits for auth. */
|
|
108
|
+
readonly canLoad: Signal<boolean>;
|
|
109
|
+
/** Readiness for the current view; a public view always stays ready. */
|
|
110
|
+
readonly status: Signal<ListRouteResult<Filter, Selection>['status']>;
|
|
111
|
+
/** The sign-in destination while a signed-out protected view is being redirected. */
|
|
112
|
+
readonly redirectTo: Signal<string | undefined>;
|
|
113
|
+
/**
|
|
114
|
+
* Builds a real link target, preserving the current pathname, unrelated query
|
|
115
|
+
* values and hash.
|
|
116
|
+
*
|
|
117
|
+
* @param change - The page, filter, selection and path-parameter changes.
|
|
118
|
+
* @returns The link target for the change.
|
|
119
|
+
*/
|
|
120
|
+
readonly href: (change?: ListRouteChange<Filter, Selection>) => string;
|
|
121
|
+
/**
|
|
122
|
+
* Navigates atomically; location state is preserved unless options replace it.
|
|
123
|
+
*
|
|
124
|
+
* @param change - The page, filter, selection and path-parameter changes.
|
|
125
|
+
* @param options - Navigation options.
|
|
126
|
+
*/
|
|
127
|
+
readonly navigate: (change: ListRouteChange<Filter, Selection>, options?: NavigateOptions) => void;
|
|
128
|
+
/**
|
|
129
|
+
* Changes only the page, retaining the current filters and view.
|
|
130
|
+
*
|
|
131
|
+
* @param page - The target page, starting at one.
|
|
132
|
+
* @param options - Navigation options.
|
|
133
|
+
*/
|
|
134
|
+
readonly setPage: (page: number, options?: NavigateOptions) => void;
|
|
135
|
+
/**
|
|
136
|
+
* Patches URL filters and returns to page one.
|
|
137
|
+
*
|
|
138
|
+
* @param filters - The filter values to write (`undefined` removes one).
|
|
139
|
+
* @param options - Navigation options.
|
|
140
|
+
*/
|
|
141
|
+
readonly setFilters: (filters: Partial<Record<Filter, string | undefined>>, options?: NavigateOptions) => void;
|
|
142
|
+
/**
|
|
143
|
+
* Selects a configured view at page one, preserving current route parameters.
|
|
144
|
+
*
|
|
145
|
+
* @param selection - The view to select.
|
|
146
|
+
* @param options - Navigation options.
|
|
147
|
+
*/
|
|
148
|
+
readonly select: (selection: Selection, options?: NavigateOptions) => void;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* URL-backed pagination, filters and selected views, through whichever routing
|
|
152
|
+
* bond the app wired: the page, the filters and the view live in the URL only,
|
|
153
|
+
* so a reload, a shared link and Back all show the same list. Built on
|
|
154
|
+
* `resolveListRoute` from `@molecule/app-routing`, so every framework binding
|
|
155
|
+
* returns the same result. Call it in an injection context (a component field
|
|
156
|
+
* initializer or constructor).
|
|
157
|
+
*
|
|
158
|
+
* The options may be a signal: an auth status going from `loading` to
|
|
159
|
+
* `unauthenticated` reaches the list on the same component, the way React
|
|
160
|
+
* re-renders `useListRoute` with new options.
|
|
161
|
+
*
|
|
162
|
+
* A signed-out visit to a view with `requiresAuth` replaces the entry with
|
|
163
|
+
* `options.auth.redirectTo`, carrying `state.returnTo`; until auth is confirmed
|
|
164
|
+
* `canLoad` is `false`.
|
|
165
|
+
*
|
|
166
|
+
* @param options - Filter sources, view paths, pagination and optional auth
|
|
167
|
+
* readiness (or a signal of them).
|
|
168
|
+
* @returns The list state (`page`, `offset`, `filters`, `selection`, `canLoad`)
|
|
169
|
+
* as signals, and the list's link builder and navigation actions.
|
|
170
|
+
* @throws {Error} When `pageSize` is not a positive safe integer, or a protected
|
|
171
|
+
* view has no `auth`.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* @Component({
|
|
176
|
+
* selector: 'app-articles',
|
|
177
|
+
* template: `
|
|
178
|
+
* <a [href]="list.href({ page: list.page() + 1 })">{{ i18n.t('list.nextPage') }}</a>
|
|
179
|
+
* `,
|
|
180
|
+
* })
|
|
181
|
+
* export class ArticlesComponent {
|
|
182
|
+
* readonly i18n = useTranslation()
|
|
183
|
+
* // Load items from list.offset(); the page and filters come from the URL.
|
|
184
|
+
* readonly list = useListRoute({ pageSize: 10, filters: { tag: {} } })
|
|
185
|
+
* }
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function useListRoute<Filter extends string = string, Selection extends string = string>(options?: MaybeSignal<ListRouteOptions<Filter, Selection>>): UseListRouteResult<Filter, Selection>;
|
|
189
|
+
/**
|
|
190
|
+
* Takes a one-time credential (a reset `token`, an OAuth `code`) out of the URL
|
|
191
|
+
* and keeps it in memory, through whichever routing bond the app wired. The
|
|
192
|
+
* parameter is removed with a replace navigation that keeps the path, the hash,
|
|
193
|
+
* the other query values and the location state; the value survives the
|
|
194
|
+
* component's lifetime, and Back to an entry restores the value consumed there.
|
|
195
|
+
* Call it in an injection context (a component field initializer or
|
|
196
|
+
* constructor).
|
|
197
|
+
*
|
|
198
|
+
* @param name - The query key to consume, for example `token` or `code`.
|
|
199
|
+
* @returns The consumed value (`''` before the parameter was ever present) and
|
|
200
|
+
* a setter (a form clears it after a successful submit).
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* @Component({
|
|
205
|
+
* selector: 'app-reset',
|
|
206
|
+
* template: `<input [placeholder]="i18n.t('auth.resetPassword.token')" />`,
|
|
207
|
+
* })
|
|
208
|
+
* export class ResetComponent {
|
|
209
|
+
* readonly i18n = useTranslation()
|
|
210
|
+
* readonly token = useConsumedSearchParam('token')
|
|
211
|
+
* submit(password: string): void {
|
|
212
|
+
* // token[0] holds the value after the address no longer contains it;
|
|
213
|
+
* // token[1]('') clears it after a successful submit.
|
|
214
|
+
* }
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export declare function useConsumedSearchParam(name: string): readonly [string, (value: string) => void];
|
|
219
|
+
//# sourceMappingURL=use-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-router.d.ts","sourceRoot":"","sources":["../../src/hooks/use-router.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAML,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,EACb,WAAW,EACX,MAAM,EACP,MAAM,uBAAuB,CAAA;AAQ9B,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,oBAAoB,CAAA;AAGtE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAM1C;AAiCD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,kCAAkC;IAClC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,aAAa,CAAC,CAAA;IACxC,2CAA2C;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,CAAA;IACpC,oCAAoC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC,CAAA;IACnC,2BAA2B;IAC3B,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,KAAK,IAAI,CAAA;IAC3D,kCAAkC;IAClC,UAAU,EAAE,CACV,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,WAAW,EACpB,KAAK,CAAC,EAAE,WAAW,EACnB,OAAO,CAAC,EAAE,eAAe,KACtB,IAAI,CAAA;IACT,mCAAmC;IACnC,IAAI,EAAE,MAAM,IAAI,CAAA;IAChB,sCAAsC;IACtC,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,gDAAgD;IAChD,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,OAAO,CAAA;CACrD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AAC,gBAAgB,CAAC,MAAM,EAAE,SAAS,CAAC,CAAM,GAC7D,kBAAkB,CAAC,MAAM,EAAE,SAAS,CAAC,CAwCvC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC,CAuB/F"}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for routing, mirroring `useRouter`, `useRouterInstance`,
|
|
3
|
+
* `useLocation`, `useParams`, `useQuery`, `useNavigate`, `useIsActive`,
|
|
4
|
+
* `useListRoute` and `useConsumedSearchParam` from `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { afterNextRender, computed, DestroyRef, effect, inject, signal, untracked, } from '@angular/core';
|
|
9
|
+
import { createConsumedSearchParam, listRouteRedirect, resolveListRoute, } from '@molecule/app-routing';
|
|
10
|
+
import { injectProvider } from '../inject.js';
|
|
11
|
+
import { readMaybeSignal } from '../maybe-signal.js';
|
|
12
|
+
import { ROUTER } from '../tokens.js';
|
|
13
|
+
/**
|
|
14
|
+
* The router registered by `provideMolecule({ router })` or `provideRouter`.
|
|
15
|
+
* Call it in an injection context.
|
|
16
|
+
*
|
|
17
|
+
* @returns The router instance.
|
|
18
|
+
* @throws {Error} When no router is registered.
|
|
19
|
+
*/
|
|
20
|
+
export function useRouterInstance() {
|
|
21
|
+
return injectProvider(ROUTER, 'useRouterOutsideProvider', 'useRouterInstance must be used within a RouterProvider');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Subscribes to the router once and mirrors location, params and query into
|
|
25
|
+
* signals.
|
|
26
|
+
*
|
|
27
|
+
* @param router - The router to follow.
|
|
28
|
+
* @returns The three location signals.
|
|
29
|
+
*/
|
|
30
|
+
function followRouter(router) {
|
|
31
|
+
const location = signal(router.getLocation(), /* @ts-ignore */
|
|
32
|
+
...(ngDevMode ? [{ debugName: "location" }] : /* istanbul ignore next */ []));
|
|
33
|
+
const params = signal(router.getParams(), /* @ts-ignore */
|
|
34
|
+
...(ngDevMode ? [{ debugName: "params" }] : /* istanbul ignore next */ []));
|
|
35
|
+
const query = signal(router.getQuery(), /* @ts-ignore */
|
|
36
|
+
...(ngDevMode ? [{ debugName: "query" }] : /* istanbul ignore next */ []));
|
|
37
|
+
const stop = router.subscribe(() => {
|
|
38
|
+
location.set(router.getLocation());
|
|
39
|
+
params.set(router.getParams());
|
|
40
|
+
query.set(router.getQuery());
|
|
41
|
+
});
|
|
42
|
+
inject(DestroyRef).onDestroy(stop);
|
|
43
|
+
return {
|
|
44
|
+
location: location.asReadonly(),
|
|
45
|
+
params: params.asReadonly(),
|
|
46
|
+
query: query.asReadonly(),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Routing state and actions. Call it in an injection context (a component
|
|
51
|
+
* field initializer or constructor).
|
|
52
|
+
*
|
|
53
|
+
* @returns The location, params and query as signals, and the navigation actions.
|
|
54
|
+
*/
|
|
55
|
+
export function useRouter() {
|
|
56
|
+
const router = useRouterInstance();
|
|
57
|
+
const { location, params, query } = followRouter(router);
|
|
58
|
+
const navigate = (path, options) => {
|
|
59
|
+
router.navigate(path, options);
|
|
60
|
+
};
|
|
61
|
+
const navigateTo = (name, routeParams, query, options) => {
|
|
62
|
+
router.navigateTo(name, routeParams, query, options);
|
|
63
|
+
};
|
|
64
|
+
const back = () => {
|
|
65
|
+
router.back();
|
|
66
|
+
};
|
|
67
|
+
const forward = () => {
|
|
68
|
+
router.forward();
|
|
69
|
+
};
|
|
70
|
+
const isActive = (path, exact) => router.isActive(path, exact);
|
|
71
|
+
return { location, params, query, navigate, navigateTo, back, forward, isActive };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The current location alone. Call it in an injection context.
|
|
75
|
+
*
|
|
76
|
+
* @returns A signal of the current route location.
|
|
77
|
+
*/
|
|
78
|
+
export function useLocation() {
|
|
79
|
+
return followRouter(useRouterInstance()).location;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The current path parameters alone. Call it in an injection context.
|
|
83
|
+
*
|
|
84
|
+
* @returns A signal of the current path parameters.
|
|
85
|
+
*/
|
|
86
|
+
export function useParams() {
|
|
87
|
+
return followRouter(useRouterInstance()).params;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The current query parameters alone. Call it in an injection context.
|
|
91
|
+
*
|
|
92
|
+
* @returns A signal of the current query parameters.
|
|
93
|
+
*/
|
|
94
|
+
export function useQuery() {
|
|
95
|
+
return followRouter(useRouterInstance()).query;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A navigate function alone. Call it in an injection context.
|
|
99
|
+
*
|
|
100
|
+
* @returns The navigate function.
|
|
101
|
+
*/
|
|
102
|
+
export function useNavigate() {
|
|
103
|
+
const router = useRouterInstance();
|
|
104
|
+
return (path, options) => {
|
|
105
|
+
router.navigate(path, options);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Whether a path is active, re-evaluated on every route change. Call it in an
|
|
110
|
+
* injection context.
|
|
111
|
+
*
|
|
112
|
+
* The arguments may be signals: the active flag then follows a changed path or
|
|
113
|
+
* `exact` on the same component, the way React re-renders `useIsActive` with
|
|
114
|
+
* new arguments. A signal holding `undefined` for `exact` matches an omitted
|
|
115
|
+
* argument (`false`).
|
|
116
|
+
*
|
|
117
|
+
* @param path - The path to check (or a signal of it).
|
|
118
|
+
* @param exact - Whether to match exactly (default: `false`, or a signal of
|
|
119
|
+
* it).
|
|
120
|
+
* @returns A signal of whether the path is active.
|
|
121
|
+
*/
|
|
122
|
+
export function useIsActive(path, exact = false) {
|
|
123
|
+
const router = useRouterInstance();
|
|
124
|
+
const readExact = () => readMaybeSignal(exact) ?? false;
|
|
125
|
+
const active = signal(router.isActive(readMaybeSignal(path), readExact()), /* @ts-ignore */
|
|
126
|
+
...(ngDevMode ? [{ debugName: "active" }] : /* istanbul ignore next */ []));
|
|
127
|
+
const stop = router.subscribe(() => active.set(router.isActive(readMaybeSignal(path), readExact())));
|
|
128
|
+
// React recomputes the same expression when a re-render passes new
|
|
129
|
+
// arguments; a signal argument changes it on this component instead.
|
|
130
|
+
effect(() => {
|
|
131
|
+
const nextPath = readMaybeSignal(path);
|
|
132
|
+
const nextExact = readMaybeSignal(exact);
|
|
133
|
+
untracked(() => active.set(router.isActive(nextPath, nextExact ?? false)));
|
|
134
|
+
});
|
|
135
|
+
inject(DestroyRef).onDestroy(stop);
|
|
136
|
+
return active.asReadonly();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* URL-backed pagination, filters and selected views, through whichever routing
|
|
140
|
+
* bond the app wired: the page, the filters and the view live in the URL only,
|
|
141
|
+
* so a reload, a shared link and Back all show the same list. Built on
|
|
142
|
+
* `resolveListRoute` from `@molecule/app-routing`, so every framework binding
|
|
143
|
+
* returns the same result. Call it in an injection context (a component field
|
|
144
|
+
* initializer or constructor).
|
|
145
|
+
*
|
|
146
|
+
* The options may be a signal: an auth status going from `loading` to
|
|
147
|
+
* `unauthenticated` reaches the list on the same component, the way React
|
|
148
|
+
* re-renders `useListRoute` with new options.
|
|
149
|
+
*
|
|
150
|
+
* A signed-out visit to a view with `requiresAuth` replaces the entry with
|
|
151
|
+
* `options.auth.redirectTo`, carrying `state.returnTo`; until auth is confirmed
|
|
152
|
+
* `canLoad` is `false`.
|
|
153
|
+
*
|
|
154
|
+
* @param options - Filter sources, view paths, pagination and optional auth
|
|
155
|
+
* readiness (or a signal of them).
|
|
156
|
+
* @returns The list state (`page`, `offset`, `filters`, `selection`, `canLoad`)
|
|
157
|
+
* as signals, and the list's link builder and navigation actions.
|
|
158
|
+
* @throws {Error} When `pageSize` is not a positive safe integer, or a protected
|
|
159
|
+
* view has no `auth`.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* @Component({
|
|
164
|
+
* selector: 'app-articles',
|
|
165
|
+
* template: `
|
|
166
|
+
* <a [href]="list.href({ page: list.page() + 1 })">{{ i18n.t('list.nextPage') }}</a>
|
|
167
|
+
* `,
|
|
168
|
+
* })
|
|
169
|
+
* export class ArticlesComponent {
|
|
170
|
+
* readonly i18n = useTranslation()
|
|
171
|
+
* // Load items from list.offset(); the page and filters come from the URL.
|
|
172
|
+
* readonly list = useListRoute({ pageSize: 10, filters: { tag: {} } })
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export function useListRoute(options = {}) {
|
|
177
|
+
const { location, params, navigate } = useRouter();
|
|
178
|
+
// The options may be a signal; a plain object never changes on this component.
|
|
179
|
+
const readOptions = () => readMaybeSignal(options);
|
|
180
|
+
// Re-resolved from the current location alone, exactly as React re-renders:
|
|
181
|
+
// a navigation (or an options change) recomputes every field.
|
|
182
|
+
const list = computed(() => resolveListRoute({ location: location(), routeParams: params() }, readOptions(), navigate), /* @ts-ignore */
|
|
183
|
+
...(ngDevMode ? [{ debugName: "list" }] : /* istanbul ignore next */ []));
|
|
184
|
+
// Resolve once at the call site: an invalid pageSize or a protected selection
|
|
185
|
+
// without auth throws here, the way React's render throws (a lazy computed
|
|
186
|
+
// would defer the throw to the first template read).
|
|
187
|
+
list();
|
|
188
|
+
// React performs the redirect after the render that reported it; this effect
|
|
189
|
+
// runs it at the same point, and re-runs while the redirect target moves.
|
|
190
|
+
effect(() => {
|
|
191
|
+
const redirect = listRouteRedirect(list(), location());
|
|
192
|
+
if (redirect === undefined)
|
|
193
|
+
return;
|
|
194
|
+
navigate(redirect.to, redirect.options);
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
page: computed(() => list().page),
|
|
198
|
+
pageSize: computed(() => list().pageSize),
|
|
199
|
+
offset: computed(() => list().offset),
|
|
200
|
+
filters: computed(() => list().filters),
|
|
201
|
+
selection: computed(() => list().selection),
|
|
202
|
+
params: computed(() => list().params),
|
|
203
|
+
canLoad: computed(() => list().canLoad),
|
|
204
|
+
status: computed(() => list().status),
|
|
205
|
+
redirectTo: computed(() => list().redirectTo),
|
|
206
|
+
href: (change) => list().href(change),
|
|
207
|
+
navigate: (change, navOptions) => list().navigate(change, navOptions),
|
|
208
|
+
setPage: (page, navOptions) => list().setPage(page, navOptions),
|
|
209
|
+
setFilters: (filters, navOptions) => list().setFilters(filters, navOptions),
|
|
210
|
+
select: (selection, navOptions) => list().select(selection, navOptions),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Takes a one-time credential (a reset `token`, an OAuth `code`) out of the URL
|
|
215
|
+
* and keeps it in memory, through whichever routing bond the app wired. The
|
|
216
|
+
* parameter is removed with a replace navigation that keeps the path, the hash,
|
|
217
|
+
* the other query values and the location state; the value survives the
|
|
218
|
+
* component's lifetime, and Back to an entry restores the value consumed there.
|
|
219
|
+
* Call it in an injection context (a component field initializer or
|
|
220
|
+
* constructor).
|
|
221
|
+
*
|
|
222
|
+
* @param name - The query key to consume, for example `token` or `code`.
|
|
223
|
+
* @returns The consumed value (`''` before the parameter was ever present) and
|
|
224
|
+
* a setter (a form clears it after a successful submit).
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* @Component({
|
|
229
|
+
* selector: 'app-reset',
|
|
230
|
+
* template: `<input [placeholder]="i18n.t('auth.resetPassword.token')" />`,
|
|
231
|
+
* })
|
|
232
|
+
* export class ResetComponent {
|
|
233
|
+
* readonly i18n = useTranslation()
|
|
234
|
+
* readonly token = useConsumedSearchParam('token')
|
|
235
|
+
* submit(password: string): void {
|
|
236
|
+
* // token[0] holds the value after the address no longer contains it;
|
|
237
|
+
* // token[1]('') clears it after a successful submit.
|
|
238
|
+
* }
|
|
239
|
+
* }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export function useConsumedSearchParam(name) {
|
|
243
|
+
const router = useRouterInstance();
|
|
244
|
+
const param = createConsumedSearchParam(router, name);
|
|
245
|
+
// The tuple is the mounted result for this component's whole lifetime and its
|
|
246
|
+
// state member is updated in place, exactly like `usePromise`/`useAsyncState`
|
|
247
|
+
// (see use-async.ts): React re-renders a new value into the same tuple, this
|
|
248
|
+
// port writes the new value into the same tuple's member.
|
|
249
|
+
const tuple = [param.get(), (value) => param.set(value)];
|
|
250
|
+
const stopFollowing = param.subscribe((value) => {
|
|
251
|
+
tuple[0] = value;
|
|
252
|
+
});
|
|
253
|
+
inject(DestroyRef).onDestroy(stopFollowing);
|
|
254
|
+
// Like React's effect, the parameter is consumed once the component has
|
|
255
|
+
// rendered (and never during server rendering).
|
|
256
|
+
let disconnect = null;
|
|
257
|
+
afterNextRender(() => {
|
|
258
|
+
disconnect = param.connect();
|
|
259
|
+
});
|
|
260
|
+
inject(DestroyRef).onDestroy(() => {
|
|
261
|
+
disconnect?.();
|
|
262
|
+
disconnect = null;
|
|
263
|
+
});
|
|
264
|
+
return tuple;
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=use-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-router.js","sourceRoot":"","sources":["../../src/hooks/use-router.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,eAAe,EACf,QAAQ,EACR,UAAU,EACV,MAAM,EACN,MAAM,EAEN,MAAM,EACN,SAAS,GACV,MAAM,eAAe,CAAA;AAYtB,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAoB,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OA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IAC1C,eAAe,CAAC,GAAG,EAAE;QACnB,UAAU,GAAG,KAAK,CAAC,OAAO,EAAE,CAAA;IAC9B,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE;QAChC,UAAU,EAAE,EAAE,CAAA;QACd,UAAU,GAAG,IAAI,CAAA;IACnB,CAAC,CAAC,CAAA;IACF,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for storage, mirroring `useStorageProvider`,
|
|
3
|
+
* `useStorageValue`, `useStoragePreference` and `useStorage` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import { type StorageProvider } from '@molecule/app-storage';
|
|
10
|
+
import { type MaybeSignal } from '../maybe-signal.js';
|
|
11
|
+
/**
|
|
12
|
+
* The storage provider registered by `provideMolecule({ storage })` or
|
|
13
|
+
* `provideStorage`. Call it in an injection context.
|
|
14
|
+
*
|
|
15
|
+
* @returns The storage provider.
|
|
16
|
+
* @throws {Error} When no storage provider is registered.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useStorageProvider(): StorageProvider;
|
|
19
|
+
/**
|
|
20
|
+
* Options for {@link useStorageValue}.
|
|
21
|
+
*
|
|
22
|
+
* @template T - The value's shape.
|
|
23
|
+
*/
|
|
24
|
+
export interface UseStorageValueOptions<T> {
|
|
25
|
+
/** Default value if the key doesn't exist. */
|
|
26
|
+
defaultValue?: T;
|
|
27
|
+
/** Whether to sync across tabs/windows (if supported by storage provider). */
|
|
28
|
+
sync?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Result of {@link useStorageValue}: every state field as a signal and the
|
|
32
|
+
* mutators as plain functions.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The value's shape.
|
|
35
|
+
*/
|
|
36
|
+
export interface UseStorageValueResult<T> {
|
|
37
|
+
/** The stored value, the default while absent, `undefined` while unknown. */
|
|
38
|
+
readonly value: Signal<T | undefined>;
|
|
39
|
+
/** Writes the value through the provider and publishes it. */
|
|
40
|
+
setValue: (value: T) => Promise<void>;
|
|
41
|
+
/** Removes the key; the value falls back to the default. */
|
|
42
|
+
removeValue: () => Promise<void>;
|
|
43
|
+
/** Whether the first read for the key is still in flight. */
|
|
44
|
+
readonly loading: Signal<boolean>;
|
|
45
|
+
/** The read or write failure, cleared by the next successful operation. */
|
|
46
|
+
readonly error: Signal<Error | null>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Manages one storage value. Call it in an injection context (a component
|
|
50
|
+
* field initializer or constructor).
|
|
51
|
+
*
|
|
52
|
+
* A write bumps a version counter, so a read that started before it never
|
|
53
|
+
* replaces the newer value; a read that started before a write still ends
|
|
54
|
+
* loading. `sync` subscribes through the PROVIDER's own change subscription, so
|
|
55
|
+
* a non-browser backend propagates the same way instead of being limited to
|
|
56
|
+
* the browser `storage` event.
|
|
57
|
+
*
|
|
58
|
+
* The arguments may be signals: a new key then starts over on the same
|
|
59
|
+
* component (the value falls back to the default, loading turns true and the
|
|
60
|
+
* new key is read and subscribed), the way React re-renders `useStorageValue`
|
|
61
|
+
* with a new key. The value, its pending write and any error survive the
|
|
62
|
+
* change, as React's state does.
|
|
63
|
+
*
|
|
64
|
+
* @param key - Storage key (or a signal of it).
|
|
65
|
+
* @param options - Hook options (`defaultValue`, `sync`), or a signal of them.
|
|
66
|
+
* @returns The value, its mutators, and the loading/error state as signals.
|
|
67
|
+
*/
|
|
68
|
+
export declare function useStorageValue<T>(key: MaybeSignal<string>, options?: MaybeSignal<UseStorageValueOptions<T>>): UseStorageValueResult<T>;
|
|
69
|
+
/**
|
|
70
|
+
* Options for {@link useStoragePreference}.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The value's shape.
|
|
73
|
+
*/
|
|
74
|
+
export interface UseStoragePreferenceOptions<T> {
|
|
75
|
+
/**
|
|
76
|
+
* Value reported until the provider supplies one. It is NEVER written to
|
|
77
|
+
* storage: a default is a display fallback, not a decision the user made.
|
|
78
|
+
*/
|
|
79
|
+
defaultValue?: T;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Result of {@link useStoragePreference}.
|
|
83
|
+
*
|
|
84
|
+
* @template T - The value's shape.
|
|
85
|
+
*/
|
|
86
|
+
export interface UseStoragePreferenceResult<T> {
|
|
87
|
+
/** The persisted value, or `defaultValue` while none is known. */
|
|
88
|
+
readonly value: Signal<T | undefined>;
|
|
89
|
+
/**
|
|
90
|
+
* Writes the value through the preference store: subscribers update
|
|
91
|
+
* immediately and persistence is best-effort.
|
|
92
|
+
*/
|
|
93
|
+
setValue: (next: T | ((previous: T | undefined) => T)) => void;
|
|
94
|
+
/** Removes the key through the preference store. */
|
|
95
|
+
removeValue: () => void;
|
|
96
|
+
/**
|
|
97
|
+
* `true` once the store has a definite answer — the synchronous snapshot on
|
|
98
|
+
* a synchronous provider, or the hydration result on an asynchronous one.
|
|
99
|
+
*/
|
|
100
|
+
readonly ready: Signal<boolean>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Subscribes to ONE persisted preference through `getPreferenceStore()`. Call
|
|
104
|
+
* it in an injection context (a component field initializer or constructor).
|
|
105
|
+
*
|
|
106
|
+
* This is the component-side half of `getPreferenceStore()`: a value read at
|
|
107
|
+
* injection time is available immediately when the bonded provider can answer
|
|
108
|
+
* synchronously, and an asynchronous provider's hydration CONVERGES onto the
|
|
109
|
+
* same component without a remount. Ordering is the store's: a read that
|
|
110
|
+
* started before a newer write, remove or external change is discarded, so a
|
|
111
|
+
* slow hydration never overwrites an edit the user already made.
|
|
112
|
+
*
|
|
113
|
+
* Pass `null` for `key` while the real key is unknown (no project selected
|
|
114
|
+
* yet): the hook then behaves as local state around `defaultValue` and
|
|
115
|
+
* persists nothing.
|
|
116
|
+
*
|
|
117
|
+
* The arguments may be signals: a new key then re-reads and re-subscribes that
|
|
118
|
+
* key on the same component (its old subscription is dropped, hydration
|
|
119
|
+
* converges it), the way React re-renders `useStoragePreference` with a new
|
|
120
|
+
* key. The shown value survives the change, as React's state does.
|
|
121
|
+
*
|
|
122
|
+
* @param key - The storage key, or `null` when there is nothing to persist (or
|
|
123
|
+
* a signal of it).
|
|
124
|
+
* @param options - `defaultValue`, reported until the provider supplies one
|
|
125
|
+
* (or a signal of them).
|
|
126
|
+
* @returns The current value, its mutators, and whether it is definite yet.
|
|
127
|
+
*/
|
|
128
|
+
export declare function useStoragePreference<T = unknown>(key: MaybeSignal<string | null | undefined>, options?: MaybeSignal<UseStoragePreferenceOptions<T>>): UseStoragePreferenceResult<T>;
|
|
129
|
+
/**
|
|
130
|
+
* The registered provider's operations as stable functions, no reactive state.
|
|
131
|
+
* Call it in an injection context.
|
|
132
|
+
*
|
|
133
|
+
* @returns The get/set/remove/clear/keys functions.
|
|
134
|
+
*/
|
|
135
|
+
export declare function useStorage(): {
|
|
136
|
+
get: <T>(key: string) => Promise<T | null>;
|
|
137
|
+
set: <T>(key: string, value: T) => Promise<void>;
|
|
138
|
+
remove: (key: string) => Promise<void>;
|
|
139
|
+
clear: () => Promise<void>;
|
|
140
|
+
keys: () => Promise<string[]>;
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=use-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-storage.d.ts","sourceRoot":"","sources":["../../src/hooks/use-storage.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAA8B,KAAK,MAAM,EAAqB,MAAM,eAAe,CAAA;AAE1F,OAAO,EAAsB,KAAK,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAGhF,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,oBAAoB,CAAA;AAGtE;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,CAMpD;AAED;;;;GAIG;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