@molecule/app-svelte 3.3.0 → 3.5.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 +2393 -220
- package/dist/agent-identity.js +0 -1
- package/dist/context.d.ts +101 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +199 -3
- package/dist/hooks/external-store.d.ts +28 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +95 -0
- package/dist/hooks/index.d.ts +37 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +36 -0
- package/dist/hooks/live-store.d.ts +68 -0
- package/dist/hooks/live-store.d.ts.map +1 -0
- package/dist/hooks/live-store.js +76 -0
- package/dist/hooks/useAIModels.d.ts +58 -0
- package/dist/hooks/useAIModels.d.ts.map +1 -0
- package/dist/hooks/useAIModels.js +155 -0
- package/dist/hooks/useAsyncState.d.ts +52 -0
- package/dist/hooks/useAsyncState.d.ts.map +1 -0
- package/dist/hooks/useAsyncState.js +80 -0
- package/dist/hooks/useAuth.d.ts +99 -0
- package/dist/hooks/useAuth.d.ts.map +1 -0
- package/dist/hooks/useAuth.js +88 -0
- package/dist/hooks/useCapacitorApp.d.ts +51 -0
- package/dist/hooks/useCapacitorApp.d.ts.map +1 -0
- package/dist/hooks/useCapacitorApp.js +57 -0
- package/dist/hooks/useChangePassword.d.ts +41 -0
- package/dist/hooks/useChangePassword.d.ts.map +1 -0
- package/dist/hooks/useChangePassword.js +40 -0
- package/dist/hooks/useDevice.d.ts +48 -0
- package/dist/hooks/useDevice.d.ts.map +1 -0
- package/dist/hooks/useDevice.js +37 -0
- package/dist/hooks/useEditor.d.ts +65 -0
- package/dist/hooks/useEditor.d.ts.map +1 -0
- package/dist/hooks/useEditor.js +86 -0
- package/dist/hooks/useForm.d.ts +137 -0
- package/dist/hooks/useForm.d.ts.map +1 -0
- package/dist/hooks/useForm.js +221 -0
- package/dist/hooks/useHttp.d.ts +122 -0
- package/dist/hooks/useHttp.d.ts.map +1 -0
- package/dist/hooks/useHttp.js +220 -0
- package/dist/hooks/useLogger.d.ts +52 -0
- package/dist/hooks/useLogger.d.ts.map +1 -0
- package/dist/hooks/useLogger.js +64 -0
- package/dist/hooks/useLogin.d.ts +47 -0
- package/dist/hooks/useLogin.d.ts.map +1 -0
- package/dist/hooks/useLogin.js +44 -0
- package/dist/hooks/useOAuth.d.ts +96 -0
- package/dist/hooks/useOAuth.d.ts.map +1 -0
- package/dist/hooks/useOAuth.js +363 -0
- package/dist/hooks/useOptimisticValue.d.ts +58 -0
- package/dist/hooks/useOptimisticValue.d.ts.map +1 -0
- package/dist/hooks/useOptimisticValue.js +98 -0
- package/dist/hooks/usePasswordReset.d.ts +50 -0
- package/dist/hooks/usePasswordReset.d.ts.map +1 -0
- package/dist/hooks/usePasswordReset.js +50 -0
- package/dist/hooks/usePlatform.d.ts +45 -0
- package/dist/hooks/usePlatform.d.ts.map +1 -0
- package/dist/hooks/usePlatform.js +35 -0
- package/dist/hooks/usePreview.d.ts +54 -0
- package/dist/hooks/usePreview.d.ts.map +1 -0
- package/dist/hooks/usePreview.js +61 -0
- package/dist/hooks/useProfileForm.d.ts +100 -0
- package/dist/hooks/useProfileForm.d.ts.map +1 -0
- package/dist/hooks/useProfileForm.js +267 -0
- package/dist/hooks/usePromise.d.ts +62 -0
- package/dist/hooks/usePromise.d.ts.map +1 -0
- package/dist/hooks/usePromise.js +105 -0
- package/dist/hooks/usePush.d.ts +68 -0
- package/dist/hooks/usePush.d.ts.map +1 -0
- package/dist/hooks/usePush.js +79 -0
- package/dist/hooks/useRouter.d.ts +169 -0
- package/dist/hooks/useRouter.d.ts.map +1 -0
- package/dist/hooks/useRouter.js +212 -0
- package/dist/hooks/useSignup.d.ts +52 -0
- package/dist/hooks/useSignup.d.ts.map +1 -0
- package/dist/hooks/useSignup.js +42 -0
- package/dist/hooks/useStorage.d.ts +133 -0
- package/dist/hooks/useStorage.d.ts.map +1 -0
- package/dist/hooks/useStorage.js +228 -0
- package/dist/hooks/useStore.d.ts +69 -0
- package/dist/hooks/useStore.d.ts.map +1 -0
- package/dist/hooks/useStore.js +82 -0
- package/dist/hooks/useTheme.d.ts +55 -0
- package/dist/hooks/useTheme.d.ts.map +1 -0
- package/dist/hooks/useTheme.js +102 -0
- package/dist/hooks/useTranslation.d.ts +106 -0
- package/dist/hooks/useTranslation.d.ts.map +1 -0
- package/dist/hooks/useTranslation.js +159 -0
- package/dist/hooks/useVerifyPaymentReturn.d.ts +91 -0
- package/dist/hooks/useVerifyPaymentReturn.d.ts.map +1 -0
- package/dist/hooks/useVerifyPaymentReturn.js +189 -0
- package/dist/hooks/useVersion.d.ts +53 -0
- package/dist/hooks/useVersion.d.ts.map +1 -0
- package/dist/hooks/useVersion.js +70 -0
- package/dist/hooks/useWorkspace.d.ts +47 -0
- package/dist/hooks/useWorkspace.d.ts.map +1 -0
- package/dist/hooks/useWorkspace.js +68 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/session-ready/SessionReady.svelte +20 -0
- package/dist/session-ready/SessionReady.svelte.d.ts +5 -0
- package/dist/session-ready/SessionReady.svelte.d.ts.map +1 -0
- package/dist/session-ready/index.d.ts +7 -0
- package/dist/session-ready/index.d.ts.map +1 -0
- package/dist/session-ready/index.js +6 -0
- package/dist/stores/async-state.js +0 -1
- package/dist/stores/auth.js +0 -1
- package/dist/stores/capacitor-app.js +0 -1
- package/dist/stores/change-password.js +0 -1
- package/dist/stores/chat.d.ts.map +1 -1
- package/dist/stores/chat.js +3 -10
- package/dist/stores/device.js +0 -1
- package/dist/stores/forms.js +0 -1
- package/dist/stores/http.js +0 -1
- package/dist/stores/i18n.d.ts +3 -2
- package/dist/stores/i18n.d.ts.map +1 -1
- package/dist/stores/i18n.js +2 -3
- package/dist/stores/index.js +0 -1
- package/dist/stores/logger.js +0 -1
- package/dist/stores/login.js +0 -1
- package/dist/stores/oauth.js +0 -1
- package/dist/stores/password-reset.js +0 -1
- package/dist/stores/platform.js +0 -1
- package/dist/stores/promise.js +0 -1
- package/dist/stores/push.js +0 -1
- package/dist/stores/router.js +0 -1
- package/dist/stores/signup.js +0 -1
- package/dist/stores/state.js +0 -1
- package/dist/stores/storage.js +0 -1
- package/dist/stores/theme.js +0 -1
- package/dist/stores/version.js +0 -1
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/package.json +13 -8
- package/dist/agent-identity.js.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/stores/async-state.js.map +0 -1
- package/dist/stores/auth.js.map +0 -1
- package/dist/stores/capacitor-app.js.map +0 -1
- package/dist/stores/change-password.js.map +0 -1
- package/dist/stores/chat.js.map +0 -1
- package/dist/stores/device.js.map +0 -1
- package/dist/stores/forms.js.map +0 -1
- package/dist/stores/http.js.map +0 -1
- package/dist/stores/i18n.js.map +0 -1
- package/dist/stores/index.js.map +0 -1
- package/dist/stores/logger.js.map +0 -1
- package/dist/stores/login.js.map +0 -1
- package/dist/stores/oauth.js.map +0 -1
- package/dist/stores/password-reset.js.map +0 -1
- package/dist/stores/platform.js.map +0 -1
- package/dist/stores/promise.js.map +0 -1
- package/dist/stores/push.js.map +0 -1
- package/dist/stores/router.js.map +0 -1
- package/dist/stores/signup.js.map +0 -1
- package/dist/stores/state.js.map +0 -1
- package/dist/stores/storage.js.map +0 -1
- package/dist/stores/theme.js.map +0 -1
- package/dist/stores/version.js.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for a server-backed value that the UI changes at once and saves
|
|
3
|
+
* in order, porting React's `useOptimisticValue`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { onDestroy } from 'svelte';
|
|
8
|
+
import { writable } from 'svelte/store';
|
|
9
|
+
import { createOptimisticValue, shallowEqual } from '@molecule/app-state';
|
|
10
|
+
import { fieldStore } from './live-store.js';
|
|
11
|
+
/** The previous host's handoff, alive until its disarm timer runs. */
|
|
12
|
+
let optimisticHandoff = null;
|
|
13
|
+
/**
|
|
14
|
+
* A server-backed value — a favorite, follow, like or bookmark button, a
|
|
15
|
+
* rating, a quantity, a setting switch — that changes on the first render
|
|
16
|
+
* after a tap and is saved one change at a time, in order. See
|
|
17
|
+
* `createOptimisticValue` in `@molecule/app-state`, under React's hook name.
|
|
18
|
+
*
|
|
19
|
+
* A new server `value` is taken only when it differs (by `equals`) from the
|
|
20
|
+
* one the hook last received, and only while nothing is being saved, so a
|
|
21
|
+
* refetch of older data never flips the button mid-save. The latest `commit`
|
|
22
|
+
* and `onError` are always used. Give the component a `key` per record, so one
|
|
23
|
+
* record's pending change never shows on another. Keep related fields, such as
|
|
24
|
+
* a favorite flag and its count, in the SAME value. Let commit reject on
|
|
25
|
+
* failure: a catch that returns old data hides the failure.
|
|
26
|
+
*
|
|
27
|
+
* @param options - The server's current value and how to save a change.
|
|
28
|
+
* @returns The value to render, whether a save is pending, the newest failure,
|
|
29
|
+
* and `set`/`update`.
|
|
30
|
+
* @example
|
|
31
|
+
* ```svelte
|
|
32
|
+
* <script>
|
|
33
|
+
* import { useOptimisticValue } from '@molecule/app-svelte'
|
|
34
|
+
* const star = useOptimisticValue({
|
|
35
|
+
* value: { starred: repository.starred, stars: repository.stars },
|
|
36
|
+
* commit: async (next) => { /* save and return the server's value * },
|
|
37
|
+
* })
|
|
38
|
+
* </script>
|
|
39
|
+
*
|
|
40
|
+
* <button aria-pressed={$star.value.starred} onclick={() => star.update((on) => ({ ...on, starred: !on.starred }))}>
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export const useOptimisticValue = (options) => {
|
|
44
|
+
// Actions read the latest options, so an adopting mount re-arms them (React
|
|
45
|
+
// publishes them from committed renders).
|
|
46
|
+
const latest = options;
|
|
47
|
+
// A mount in the same turn as the previous host's destroy is React's
|
|
48
|
+
// re-render (an args step), not a new component: adopt its controller so the
|
|
49
|
+
// pending save continues and the value does not flip.
|
|
50
|
+
const adopted = optimisticHandoff;
|
|
51
|
+
optimisticHandoff = null;
|
|
52
|
+
if (adopted)
|
|
53
|
+
adopted.adopted = true;
|
|
54
|
+
const controller = (adopted?.controller ??
|
|
55
|
+
createOptimisticValue({
|
|
56
|
+
value: options.value,
|
|
57
|
+
commit: (next, previous, confirm) => latest.commit(next, previous, confirm),
|
|
58
|
+
equals: (a, b) => (latest.equals ?? shallowEqual)(a, b),
|
|
59
|
+
onError: (error, value) => latest.onError?.(error, value),
|
|
60
|
+
}));
|
|
61
|
+
// The handoff carries the value type-erased (it outlives one generic
|
|
62
|
+
// instantiation); the adopting mount is the same component, so its `T` is
|
|
63
|
+
// the one the controller was created with.
|
|
64
|
+
const received = {
|
|
65
|
+
current: adopted ? adopted.received : options.value,
|
|
66
|
+
};
|
|
67
|
+
const state = writable(controller.getState());
|
|
68
|
+
const unsubscribe = controller.subscribe((next) => state.set(next));
|
|
69
|
+
onDestroy(() => {
|
|
70
|
+
unsubscribe();
|
|
71
|
+
const offer = {
|
|
72
|
+
adopted: false,
|
|
73
|
+
controller: controller,
|
|
74
|
+
received: received.current,
|
|
75
|
+
};
|
|
76
|
+
optimisticHandoff = offer;
|
|
77
|
+
// An unadopted handoff was a real unmount; the timer drops it so the next
|
|
78
|
+
// mount starts clean. A mount in the same turn adopts first.
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
if (optimisticHandoff === offer && !offer.adopted)
|
|
81
|
+
optimisticHandoff = null;
|
|
82
|
+
}, 0);
|
|
83
|
+
});
|
|
84
|
+
// A new server value is taken when it differs from the one last received
|
|
85
|
+
// and nothing is being saved (the controller's `sync` decides that) —
|
|
86
|
+
// React's value effect, which also runs only for a changed value.
|
|
87
|
+
if (!(latest.equals ?? shallowEqual)(received.current, options.value)) {
|
|
88
|
+
received.current = options.value;
|
|
89
|
+
controller.sync(options.value);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
value: fieldStore(state, 'value'),
|
|
93
|
+
pending: fieldStore(state, 'pending'),
|
|
94
|
+
error: fieldStore(state, 'error'),
|
|
95
|
+
set: (next) => controller.set(next),
|
|
96
|
+
update: (change) => controller.update(change),
|
|
97
|
+
};
|
|
98
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for the password-reset flow, porting React's `usePasswordReset`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Readable } from 'svelte/store';
|
|
7
|
+
import type { PasswordResetConfirm, PasswordResetRequest } from '@molecule/app-auth';
|
|
8
|
+
import { type UsePromiseState } from './usePromise.js';
|
|
9
|
+
/** What {@link usePasswordReset} returns: two independent trackers plus a combined reset. */
|
|
10
|
+
export interface UsePasswordResetResult {
|
|
11
|
+
/** The request step's status. */
|
|
12
|
+
requestStatus: Readable<UsePromiseState<void>['status']>;
|
|
13
|
+
/** The request step's rejection. */
|
|
14
|
+
requestError: Readable<UsePromiseState<void>['error']>;
|
|
15
|
+
/** The confirm step's status. */
|
|
16
|
+
confirmStatus: Readable<UsePromiseState<void>['status']>;
|
|
17
|
+
/** The confirm step's rejection. */
|
|
18
|
+
confirmError: Readable<UsePromiseState<void>['error']>;
|
|
19
|
+
/** Sends the reset email (step 1). */
|
|
20
|
+
requestReset: (data: PasswordResetRequest) => Promise<void>;
|
|
21
|
+
/** Sets the new password (step 2). */
|
|
22
|
+
confirmReset: (data: PasswordResetConfirm) => Promise<void>;
|
|
23
|
+
/** Returns BOTH trackers to idle. */
|
|
24
|
+
reset: () => void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The password-reset flow with separate tracking for the request and confirm
|
|
28
|
+
* steps, React's `usePasswordReset` in Svelte's idiom: call it during
|
|
29
|
+
* component init under `setMoleculeContext({ auth })`.
|
|
30
|
+
*
|
|
31
|
+
* @returns Both steps' state as stores and their actions.
|
|
32
|
+
* @example
|
|
33
|
+
* ```svelte
|
|
34
|
+
* <script>
|
|
35
|
+
* import { usePasswordReset } from '@molecule/app-svelte'
|
|
36
|
+
* const { requestStatus, requestError, requestReset, confirmReset } = usePasswordReset()
|
|
37
|
+
* </script>
|
|
38
|
+
*
|
|
39
|
+
* <!-- Step 1: request the reset email -->
|
|
40
|
+
* <button onclick={() => requestReset({ email })} disabled={$requestStatus === 'pending'}>
|
|
41
|
+
* Send reset email
|
|
42
|
+
* </button>
|
|
43
|
+
*
|
|
44
|
+
* <!-- Step 2: set the new password -->
|
|
45
|
+
* <button onclick={() => confirmReset({ token, password })}>Set new password</button>
|
|
46
|
+
* {#if $requestError}<p>{$requestError.message}</p>{/if}
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function usePasswordReset(): UsePasswordResetResult;
|
|
50
|
+
//# sourceMappingURL=usePasswordReset.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePasswordReset.d.ts","sourceRoot":"","sources":["../../src/hooks/usePasswordReset.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAIpF,OAAO,EAAc,KAAK,eAAe,EAAwB,MAAM,iBAAiB,CAAA;AAExF,6FAA6F;AAC7F,MAAM,WAAW,sBAAsB;IACrC,iCAAiC;IACjC,aAAa,EAAE,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxD,oCAAoC;IACpC,YAAY,EAAE,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;IACtD,iCAAiC;IACjC,aAAa,EAAE,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;IACxD,oCAAoC;IACpC,YAAY,EAAE,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;IACtD,sCAAsC;IACtC,YAAY,EAAE,CAAC,IAAI,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3D,sCAAsC;IACtC,YAAY,EAAE,CAAC,IAAI,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3D,qCAAqC;IACrC,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,IAAI,sBAAsB,CAsBzD"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for the password-reset flow, porting React's `usePasswordReset`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { useAuthClient } from '../context.js';
|
|
7
|
+
import { promiseFields } from './live-store.js';
|
|
8
|
+
import { usePromise } from './usePromise.js';
|
|
9
|
+
/**
|
|
10
|
+
* The password-reset flow with separate tracking for the request and confirm
|
|
11
|
+
* steps, React's `usePasswordReset` in Svelte's idiom: call it during
|
|
12
|
+
* component init under `setMoleculeContext({ auth })`.
|
|
13
|
+
*
|
|
14
|
+
* @returns Both steps' state as stores and their actions.
|
|
15
|
+
* @example
|
|
16
|
+
* ```svelte
|
|
17
|
+
* <script>
|
|
18
|
+
* import { usePasswordReset } from '@molecule/app-svelte'
|
|
19
|
+
* const { requestStatus, requestError, requestReset, confirmReset } = usePasswordReset()
|
|
20
|
+
* </script>
|
|
21
|
+
*
|
|
22
|
+
* <!-- Step 1: request the reset email -->
|
|
23
|
+
* <button onclick={() => requestReset({ email })} disabled={$requestStatus === 'pending'}>
|
|
24
|
+
* Send reset email
|
|
25
|
+
* </button>
|
|
26
|
+
*
|
|
27
|
+
* <!-- Step 2: set the new password -->
|
|
28
|
+
* <button onclick={() => confirmReset({ token, password })}>Set new password</button>
|
|
29
|
+
* {#if $requestError}<p>{$requestError.message}</p>{/if}
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function usePasswordReset() {
|
|
33
|
+
const client = useAuthClient();
|
|
34
|
+
const request = usePromise((data) => client.requestPasswordReset(data));
|
|
35
|
+
const confirm = usePromise((data) => client.confirmPasswordReset(data));
|
|
36
|
+
const requestFields = promiseFields(request);
|
|
37
|
+
const confirmFields = promiseFields(confirm);
|
|
38
|
+
return {
|
|
39
|
+
requestStatus: requestFields.status,
|
|
40
|
+
requestError: requestFields.error,
|
|
41
|
+
confirmStatus: confirmFields.status,
|
|
42
|
+
confirmError: confirmFields.error,
|
|
43
|
+
requestReset: requestFields.call,
|
|
44
|
+
confirmReset: confirmFields.call,
|
|
45
|
+
reset: () => {
|
|
46
|
+
requestFields.reset();
|
|
47
|
+
confirmFields.reset();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for platform detection, porting React's `usePlatform`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Platform } from '@molecule/app-platform';
|
|
7
|
+
/**
|
|
8
|
+
* Hook return type.
|
|
9
|
+
*/
|
|
10
|
+
export interface UsePlatformResult {
|
|
11
|
+
/** The detected platform. */
|
|
12
|
+
platform: Platform;
|
|
13
|
+
/** Whether the app runs in a native shell. */
|
|
14
|
+
isNative: boolean;
|
|
15
|
+
/** Whether the platform is a phone or tablet. */
|
|
16
|
+
isMobile: boolean;
|
|
17
|
+
/** Whether the platform is a desktop. */
|
|
18
|
+
isDesktop: boolean;
|
|
19
|
+
/** Whether the platform is the web. */
|
|
20
|
+
isWeb: boolean;
|
|
21
|
+
/** Whether the app runs in development. */
|
|
22
|
+
isDevelopment: boolean;
|
|
23
|
+
/** Whether the app runs in production. */
|
|
24
|
+
isProduction: boolean;
|
|
25
|
+
/** Whether the current platform is one of the candidates. */
|
|
26
|
+
isPlatform: (...platforms: Platform[]) => boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Detected platform info plus the `isPlatform` helper, React's `usePlatform`
|
|
30
|
+
* in Svelte's idiom: called during component init; platform info is static, so
|
|
31
|
+
* the result is a plain object (nothing reactive).
|
|
32
|
+
*
|
|
33
|
+
* Uses module-level functions — platform is a singleton, not context-provided.
|
|
34
|
+
*
|
|
35
|
+
* @returns Platform information and utility methods.
|
|
36
|
+
* @example
|
|
37
|
+
* ```svelte
|
|
38
|
+
* <script>
|
|
39
|
+
* import { usePlatform } from '@molecule/app-svelte'
|
|
40
|
+
* const { isWeb, isMobile, isPlatform } = usePlatform()
|
|
41
|
+
* </script>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function usePlatform(): UsePlatformResult;
|
|
45
|
+
//# sourceMappingURL=usePlatform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePlatform.d.ts","sourceRoot":"","sources":["../../src/hooks/usePlatform.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AAGtD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,6BAA6B;IAC7B,QAAQ,EAAE,QAAQ,CAAA;IAClB,8CAA8C;IAC9C,QAAQ,EAAE,OAAO,CAAA;IACjB,iDAAiD;IACjD,QAAQ,EAAE,OAAO,CAAA;IACjB,yCAAyC;IACzC,SAAS,EAAE,OAAO,CAAA;IAClB,uCAAuC;IACvC,KAAK,EAAE,OAAO,CAAA;IACd,2CAA2C;IAC3C,aAAa,EAAE,OAAO,CAAA;IACtB,0CAA0C;IAC1C,YAAY,EAAE,OAAO,CAAA;IACrB,6DAA6D;IAC7D,UAAU,EAAE,CAAC,GAAG,SAAS,EAAE,QAAQ,EAAE,KAAK,OAAO,CAAA;CAClD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,IAAI,iBAAiB,CAY/C"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for platform detection, porting React's `usePlatform`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { isPlatform, platform } from '@molecule/app-platform';
|
|
7
|
+
/**
|
|
8
|
+
* Detected platform info plus the `isPlatform` helper, React's `usePlatform`
|
|
9
|
+
* in Svelte's idiom: called during component init; platform info is static, so
|
|
10
|
+
* the result is a plain object (nothing reactive).
|
|
11
|
+
*
|
|
12
|
+
* Uses module-level functions — platform is a singleton, not context-provided.
|
|
13
|
+
*
|
|
14
|
+
* @returns Platform information and utility methods.
|
|
15
|
+
* @example
|
|
16
|
+
* ```svelte
|
|
17
|
+
* <script>
|
|
18
|
+
* import { usePlatform } from '@molecule/app-svelte'
|
|
19
|
+
* const { isWeb, isMobile, isPlatform } = usePlatform()
|
|
20
|
+
* </script>
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function usePlatform() {
|
|
24
|
+
const info = platform();
|
|
25
|
+
return {
|
|
26
|
+
platform: info.platform,
|
|
27
|
+
isNative: info.isNative,
|
|
28
|
+
isMobile: info.isMobile,
|
|
29
|
+
isDesktop: info.isDesktop,
|
|
30
|
+
isWeb: info.isWeb,
|
|
31
|
+
isDevelopment: info.isDevelopment,
|
|
32
|
+
isProduction: info.isProduction,
|
|
33
|
+
isPlatform: (...platforms) => isPlatform(...platforms),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for live preview management, porting React's `usePreview`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { Readable } from 'svelte/store';
|
|
7
|
+
import type { DeviceFrame, PreviewState } from '@molecule/app-live-preview';
|
|
8
|
+
/**
|
|
9
|
+
* Hook result for usePreview: the preview state as a readable store and the
|
|
10
|
+
* controls as plain functions.
|
|
11
|
+
*/
|
|
12
|
+
export interface UsePreviewResult {
|
|
13
|
+
/** The preview's full state (url, currentUrl, isLoading, device, error, isConnected, canGoBack, canGoForward, loadNonce). */
|
|
14
|
+
state: Readable<PreviewState>;
|
|
15
|
+
/** Loads a url. */
|
|
16
|
+
setUrl: (url: string) => void;
|
|
17
|
+
/** Reloads the current document. */
|
|
18
|
+
refresh: () => void;
|
|
19
|
+
/** Switches the device frame. */
|
|
20
|
+
setDevice: (device: DeviceFrame) => void;
|
|
21
|
+
/** Opens the current url outside the app. */
|
|
22
|
+
openExternal: () => void;
|
|
23
|
+
/**
|
|
24
|
+
* Records a navigation the running preview reported via its `molecule:navigate`
|
|
25
|
+
* message — updates the displayed current location without reloading the iframe.
|
|
26
|
+
* Pass `isReplace` when the preview REPLACED its current history entry (a
|
|
27
|
+
* `replaceState` redirect/canonicalization) so the forward stack is preserved
|
|
28
|
+
* instead of truncated (a `pushState`, the default, truncates forward).
|
|
29
|
+
*/
|
|
30
|
+
recordNavigation: (url: string, isReplace?: boolean) => void;
|
|
31
|
+
/** Navigates the preview to the previous navigation-history entry (Back). */
|
|
32
|
+
back: () => void;
|
|
33
|
+
/** Navigates the preview to the next navigation-history entry (Forward). */
|
|
34
|
+
forward: () => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Live preview management, React's `usePreview` in Svelte's idiom: call it
|
|
38
|
+
* during component init under `setMoleculeContext({ preview })`.
|
|
39
|
+
*
|
|
40
|
+
* The state is a snapshot store over `provider.subscribe`, cached field-wise
|
|
41
|
+
* the way React's `useSyncExternalStore` snapshot is, so a provider that
|
|
42
|
+
* answers fresh objects does not publish a new snapshot per read.
|
|
43
|
+
*
|
|
44
|
+
* @returns Preview state store and controls.
|
|
45
|
+
* @example
|
|
46
|
+
* ```svelte
|
|
47
|
+
* <script>
|
|
48
|
+
* import { usePreview } from '@molecule/app-svelte'
|
|
49
|
+
* const { state, setUrl, refresh } = usePreview()
|
|
50
|
+
* </script>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function usePreview(): UsePreviewResult;
|
|
54
|
+
//# sourceMappingURL=usePreview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePreview.d.ts","sourceRoot":"","sources":["../../src/hooks/usePreview.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EAAE,WAAW,EAAmB,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAK5F;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6HAA6H;IAC7H,KAAK,EAAE,QAAQ,CAAC,YAAY,CAAC,CAAA;IAC7B,mBAAmB;IACnB,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAC7B,oCAAoC;IACpC,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,iCAAiC;IACjC,SAAS,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,IAAI,CAAA;IACxC,6CAA6C;IAC7C,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB;;;;;;OAMG;IACH,gBAAgB,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,KAAK,IAAI,CAAA;IAC5D,6EAA6E;IAC7E,IAAI,EAAE,MAAM,IAAI,CAAA;IAChB,4EAA4E;IAC5E,OAAO,EAAE,MAAM,IAAI,CAAA;CACpB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,IAAI,gBAAgB,CA0C7C"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for live preview management, porting React's `usePreview`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { usePreviewProvider } from '../context.js';
|
|
7
|
+
import { useSyncExternalStore } from './external-store.js';
|
|
8
|
+
/**
|
|
9
|
+
* Live preview management, React's `usePreview` in Svelte's idiom: call it
|
|
10
|
+
* during component init under `setMoleculeContext({ preview })`.
|
|
11
|
+
*
|
|
12
|
+
* The state is a snapshot store over `provider.subscribe`, cached field-wise
|
|
13
|
+
* the way React's `useSyncExternalStore` snapshot is, so a provider that
|
|
14
|
+
* answers fresh objects does not publish a new snapshot per read.
|
|
15
|
+
*
|
|
16
|
+
* @returns Preview state store and controls.
|
|
17
|
+
* @example
|
|
18
|
+
* ```svelte
|
|
19
|
+
* <script>
|
|
20
|
+
* import { usePreview } from '@molecule/app-svelte'
|
|
21
|
+
* const { state, setUrl, refresh } = usePreview()
|
|
22
|
+
* </script>
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function usePreview() {
|
|
26
|
+
const provider = usePreviewProvider();
|
|
27
|
+
// Cached previous state (React's cachedRef): a snapshot whose nine fields
|
|
28
|
+
// all match the provider's keeps the previous object.
|
|
29
|
+
let cached = null;
|
|
30
|
+
const getSnapshot = () => {
|
|
31
|
+
const next = provider.getState();
|
|
32
|
+
const prev = cached;
|
|
33
|
+
if (prev &&
|
|
34
|
+
prev.url === next.url &&
|
|
35
|
+
prev.currentUrl === next.currentUrl &&
|
|
36
|
+
prev.isLoading === next.isLoading &&
|
|
37
|
+
prev.device === next.device &&
|
|
38
|
+
prev.error === next.error &&
|
|
39
|
+
prev.isConnected === next.isConnected &&
|
|
40
|
+
prev.canGoBack === next.canGoBack &&
|
|
41
|
+
prev.canGoForward === next.canGoForward &&
|
|
42
|
+
prev.loadNonce === next.loadNonce) {
|
|
43
|
+
return prev;
|
|
44
|
+
}
|
|
45
|
+
cached = next;
|
|
46
|
+
return next;
|
|
47
|
+
};
|
|
48
|
+
const state = useSyncExternalStore((onStoreChange) => {
|
|
49
|
+
return provider.subscribe(() => onStoreChange());
|
|
50
|
+
}, getSnapshot);
|
|
51
|
+
return {
|
|
52
|
+
state,
|
|
53
|
+
setUrl: (url) => provider.setUrl(url),
|
|
54
|
+
refresh: () => provider.refresh(),
|
|
55
|
+
setDevice: (device) => provider.setDevice(device),
|
|
56
|
+
openExternal: () => provider.openExternal(),
|
|
57
|
+
recordNavigation: (url, isReplace) => provider.recordNavigation(url, isReplace),
|
|
58
|
+
back: () => provider.back(),
|
|
59
|
+
forward: () => provider.forward(),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for profile editing over the app's bonded form and auth clients,
|
|
3
|
+
* porting React's `useProfileForm`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type Readable } from 'svelte/store';
|
|
8
|
+
import type { FieldRegistration, FormValuesConstraint } from '@molecule/app-forms';
|
|
9
|
+
import { type UseFormOptions, type UseFormResult } from './useForm.js';
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for a profile form; transport stays in the auth bond.
|
|
12
|
+
*
|
|
13
|
+
* `TValues` is the page's own controlled-values type, named once as an
|
|
14
|
+
* `interface` or a type alias — both satisfy the shared form-values constraint.
|
|
15
|
+
*/
|
|
16
|
+
export interface UseProfileFormOptions<TUser, TValues extends FormValuesConstraint<TValues>> extends Omit<UseFormOptions<TValues>, 'defaultValues'> {
|
|
17
|
+
/** Complete controlled-field defaults, including blank write-only fields. */
|
|
18
|
+
defaultValues: TValues;
|
|
19
|
+
/**
|
|
20
|
+
* Optional additional account identity check; changing it discards prior edits.
|
|
21
|
+
* By default ownership follows the auth client's session revision, so editable
|
|
22
|
+
* fields can change during a save. Providers without getSessionRevision must
|
|
23
|
+
* supply this callback with an identity that remains stable across profile edits.
|
|
24
|
+
*/
|
|
25
|
+
getIdentity?: (user: TUser) => string;
|
|
26
|
+
/** Maps the current/saved user into controlled values, e.g. nullable text to ''. */
|
|
27
|
+
toValues: (user: TUser) => TValues;
|
|
28
|
+
/**
|
|
29
|
+
* Maps ONLY changed fields into an API patch. Omit blank optional credentials
|
|
30
|
+
* here; preserve undefined/absent fields rather than turning them into null.
|
|
31
|
+
* The second argument is the complete value snapshot taken at submit time.
|
|
32
|
+
*/
|
|
33
|
+
toPatch: (changes: Partial<TValues>, values: Readonly<TValues>) => Partial<TUser>;
|
|
34
|
+
/**
|
|
35
|
+
* Propagates the server's saved user to other caches, then navigates if needed.
|
|
36
|
+
* Awaited before submission finishes. The auth client already persists and
|
|
37
|
+
* publishes the saved user; never seed caches from the submitted patch.
|
|
38
|
+
*/
|
|
39
|
+
onSaved?: (user: TUser) => void | Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
/** Controlled profile form state and native form bindings. */
|
|
42
|
+
export interface UseProfileFormResult<TUser, TValues extends FormValuesConstraint<TValues>> extends Omit<UseFormResult<TValues>, 'register' | 'handleSubmit' | 'reset'> {
|
|
43
|
+
/** The shared auth user's current profile, as a readable store. */
|
|
44
|
+
user: Readable<TUser | null>;
|
|
45
|
+
/** A user has been loaded; disable submission until this is true. */
|
|
46
|
+
ready: Readable<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* The latest save/propagation failure. Rejected saves retain edits; a failed
|
|
49
|
+
* onSaved callback can be retried by submit without repeating the mutation.
|
|
50
|
+
*/
|
|
51
|
+
error: Readable<unknown>;
|
|
52
|
+
/** Native input/textarea bindings with the field's concrete value type. */
|
|
53
|
+
register: <K extends keyof TValues>(name: K, options?: Parameters<UseFormResult<TValues>['register']>[1]) => Omit<FieldRegistration, 'value'> & {
|
|
54
|
+
value: TValues[K];
|
|
55
|
+
};
|
|
56
|
+
/** Bind to the native form, with a type="submit" button inside it. */
|
|
57
|
+
formProps: {
|
|
58
|
+
onSubmit: (event: {
|
|
59
|
+
preventDefault: () => void;
|
|
60
|
+
}) => void;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Validates and saves one submit-time snapshot. Concurrent submits share the
|
|
64
|
+
* same promise. Resolves undefined on validation, save or propagation failure.
|
|
65
|
+
*/
|
|
66
|
+
submit: () => Promise<TUser | undefined>;
|
|
67
|
+
/** Discards edits and restores the newest loaded/saved profile baseline. */
|
|
68
|
+
reset: () => void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Owns load → edit → submit → publish for the signed-in user's profile,
|
|
72
|
+
* React's `useProfileForm` in Svelte's idiom: call it during component init
|
|
73
|
+
* under `setMoleculeContext({ auth })`.
|
|
74
|
+
*
|
|
75
|
+
* Same-account background updates refresh untouched fields but preserve dirty
|
|
76
|
+
* controls. Session ownership comes from the auth client, independently of
|
|
77
|
+
* mutable profile fields; an explicit getIdentity adds the caller's identity
|
|
78
|
+
* check. Submission snapshots the registered values before asynchronous
|
|
79
|
+
* validation, sends only changed fields through `toPatch`, and uses the auth
|
|
80
|
+
* client's configured updateProfile transport. The saved server response
|
|
81
|
+
* becomes the clean baseline; edits made while saving remain dirty. No request
|
|
82
|
+
* is made for a patch with no fields. A failed request retains the edits and
|
|
83
|
+
* shared user.
|
|
84
|
+
*
|
|
85
|
+
* @param options - Controlled defaults, identity and wire adapters, optional cache/navigation callback.
|
|
86
|
+
* @returns Form state stores, typed control bindings, native submit binding and save error store.
|
|
87
|
+
* @example
|
|
88
|
+
* ```svelte
|
|
89
|
+
* <script>
|
|
90
|
+
* import { useProfileForm } from '@molecule/app-svelte'
|
|
91
|
+
* const profile = useProfileForm({
|
|
92
|
+
* defaultValues: { name: '', bio: '' },
|
|
93
|
+
* toValues: (user) => ({ name: user.name ?? '', bio: user.bio ?? '' }),
|
|
94
|
+
* toPatch: (changes) => changes,
|
|
95
|
+
* })
|
|
96
|
+
* </script>
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function useProfileForm<TUser, TValues extends FormValuesConstraint<TValues>>(options: UseProfileFormOptions<TUser, TValues>): UseProfileFormResult<TUser, TValues>;
|
|
100
|
+
//# sourceMappingURL=useProfileForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProfileForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useProfileForm.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAW,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAA;AAKlF,OAAO,EAAW,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAA;AAgB/E;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB,CACpC,KAAK,EACL,OAAO,SAAS,oBAAoB,CAAC,OAAO,CAAC,CAC7C,SAAQ,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,eAAe,CAAC;IACtD,6EAA6E;IAC7E,aAAa,EAAE,OAAO,CAAA;IACtB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAA;IACrC,oFAAoF;IACpF,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAA;IAClC;;;;OAIG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,KAAK,CAAC,CAAA;IACjF;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAChD;AAED,8DAA8D;AAC9D,MAAM,WAAW,oBAAoB,CACnC,KAAK,EACL,OAAO,SAAS,oBAAoB,CAAC,OAAO,CAAC,CAC7C,SAAQ,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC;IAC3E,mEAAmE;IACnE,IAAI,EAAE,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;IAC5B,qEAAqE;IACrE,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IACxB;;;OAGG;IACH,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IACxB,2EAA2E;IAC3E,QAAQ,EAAE,CAAC,CAAC,SAAS,MAAM,OAAO,EAChC,IAAI,EAAE,CAAC,EACP,OAAO,CAAC,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,KACxD,IAAI,CAAC,iBAAiB,EAAE,OAAO,CAAC,GAAG;QAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;KAAE,CAAA;IAC7D,sEAAsE;IACtE,SAAS,EAAE;QAAE,QAAQ,EAAE,CAAC,KAAK,EAAE;YAAE,cAAc,EAAE,MAAM,IAAI,CAAA;SAAE,KAAK,IAAI,CAAA;KAAE,CAAA;IACxE;;;OAGG;IACH,MAAM,EAAE,MAAM,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAA;IACxC,4EAA4E;IAC5E,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,SAAS,oBAAoB,CAAC,OAAO,CAAC,EACjF,OAAO,EAAE,qBAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,GAC7C,oBAAoB,CAAC,KAAK,EAAE,OAAO,CAAC,CAuOtC"}
|