@swoff/cli 0.4.3 → 0.4.5
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/dist/lib/commands/generate-guide.js +16 -16
- package/dist/lib/commands/generate-guide.js.map +1 -1
- package/dist/lib/commands/init.js +127 -17
- package/dist/lib/commands/init.js.map +1 -1
- package/dist/lib/config/validator.js +1 -1
- package/dist/lib/config/validator.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector-bundle.js +2 -1
- package/dist/lib/generators/file-generators/client-injector-bundle.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector.js +2 -1
- package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/context.js +1 -1
- package/dist/lib/generators/file-generators/context.js.map +1 -1
- package/dist/lib/generators/file-generators/generate-framework-adapters.js +30 -23
- package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -1
- package/dist/lib/generators/sw-sections/background-precache.js +8 -1
- package/dist/lib/generators/sw-sections/background-precache.js.map +1 -1
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js +2 -5
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js.map +1 -1
- package/dist/lib/generators/sw-sections/fetch-handler.js +29 -7
- package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
- package/dist/lib/generators/swoff-files-generator.js +2 -1
- package/dist/lib/generators/swoff-files-generator.js.map +1 -1
- package/dist/lib/shared/config-types.js +1 -0
- package/dist/lib/shared/config-types.js.map +1 -1
- package/dist/lib/utils/detect-framework.js +4 -1
- package/dist/lib/utils/detect-framework.js.map +1 -1
- package/dist/runtime/client-injector-bundle.js +2 -2
- package/dist/runtime/client-injector-bundle.js.map +1 -1
- package/dist/runtime/client-injector.js +3 -2
- package/dist/runtime/client-injector.js.map +1 -1
- package/dist/runtime/gql-wrapper.js +3 -1
- package/dist/runtime/gql-wrapper.js.map +1 -1
- package/dist/runtime/swoff-api-bundle.js +2 -1
- package/dist/runtime/swoff-api-bundle.js.map +1 -1
- package/package.json +1 -1
- package/templates/react/{useOfflineAnalytics.jsx → useSwoffAnalytics.jsx} +3 -3
- package/templates/react/{useOfflineAnalytics.tsx → useSwoffAnalytics.tsx} +3 -3
- package/templates/react/{useAuth.jsx → useSwoffAuth.jsx} +2 -2
- package/templates/react/{useAuth.tsx → useSwoffAuth.tsx} +2 -2
- package/templates/react/{useCachedFetch.jsx → useSwoffFetch.jsx} +6 -6
- package/templates/react/{useCachedFetch.tsx → useSwoffFetch.tsx} +7 -7
- package/templates/react/{useMutation.jsx → useSwoffMutation.jsx} +3 -3
- package/templates/react/{useMutation.tsx → useSwoffMutation.tsx} +3 -3
- package/templates/react/{useMutationState.jsx → useSwoffMutationState.jsx} +1 -1
- package/templates/react/{useMutationState.tsx → useSwoffMutationState.tsx} +2 -2
- package/templates/react/{useNetworkStatus.jsx → useSwoffNetwork.jsx} +2 -2
- package/templates/react/{useNetworkStatus.tsx → useSwoffNetwork.tsx} +2 -2
- package/templates/react/{usePrecacheProgress.jsx → useSwoffPrecache.jsx} +4 -7
- package/templates/react/{usePrecacheProgress.tsx → useSwoffPrecache.tsx} +4 -7
- package/templates/react/{usePrefetch.jsx → useSwoffPrefetch.jsx} +2 -2
- package/templates/react/{usePrefetch.tsx → useSwoffPrefetch.tsx} +2 -2
- package/templates/react/{usePushSubscription.jsx → useSwoffPush.jsx} +1 -1
- package/templates/react/{usePushSubscription.tsx → useSwoffPush.tsx} +1 -1
- package/templates/react/{usePwaInstall.jsx → useSwoffPwa.jsx} +1 -1
- package/templates/react/{usePwaInstall.tsx → useSwoffPwa.tsx} +1 -1
- package/templates/react/{useMutationQueue.jsx → useSwoffQueue.jsx} +2 -2
- package/templates/react/{useMutationQueue.tsx → useSwoffQueue.tsx} +2 -2
- package/templates/react/{useStorageEstimate.jsx → useSwoffStorage.jsx} +2 -2
- package/templates/react/{useStorageEstimate.tsx → useSwoffStorage.tsx} +2 -2
- package/templates/react/{useBackgroundSync.jsx → useSwoffSync.jsx} +1 -1
- package/templates/react/{useBackgroundSync.tsx → useSwoffSync.tsx} +1 -1
- package/templates/svelte/useSwoffAnalytics.js +31 -0
- package/templates/svelte/useSwoffAnalytics.ts +37 -0
- package/templates/svelte/useSwoffAuth.js +95 -0
- package/templates/svelte/useSwoffAuth.ts +96 -0
- package/templates/svelte/useSwoffFetch.js +97 -0
- package/templates/svelte/useSwoffFetch.ts +112 -0
- package/templates/svelte/useSwoffMutation.js +110 -0
- package/templates/svelte/useSwoffMutation.ts +141 -0
- package/templates/svelte/useSwoffMutationState.js +27 -0
- package/templates/svelte/useSwoffMutationState.ts +28 -0
- package/templates/svelte/useSwoffNetwork.js +75 -0
- package/templates/svelte/useSwoffNetwork.ts +75 -0
- package/templates/svelte/useSwoffPrecache.js +19 -0
- package/templates/svelte/useSwoffPrecache.ts +19 -0
- package/templates/svelte/useSwoffPrefetch.js +19 -0
- package/templates/svelte/useSwoffPrefetch.ts +20 -0
- package/templates/svelte/useSwoffPush.js +82 -0
- package/templates/svelte/useSwoffPush.ts +82 -0
- package/templates/svelte/useSwoffPwa.js +27 -0
- package/templates/svelte/useSwoffPwa.ts +27 -0
- package/templates/svelte/useSwoffQueue.js +57 -0
- package/templates/svelte/useSwoffQueue.ts +67 -0
- package/templates/svelte/useSwoffReset.js +21 -0
- package/templates/svelte/useSwoffReset.ts +32 -0
- package/templates/svelte/useSwoffStorage.js +47 -0
- package/templates/svelte/useSwoffStorage.ts +47 -0
- package/templates/svelte/useSwoffSync.js +31 -0
- package/templates/svelte/useSwoffSync.ts +31 -0
- package/templates/vue/useSwoffAnalytics.js +46 -0
- package/templates/vue/useSwoffAnalytics.ts +54 -0
- package/templates/vue/useSwoffAuth.js +106 -0
- package/templates/vue/useSwoffAuth.ts +118 -0
- package/templates/vue/useSwoffFetch.js +127 -0
- package/templates/vue/useSwoffFetch.ts +143 -0
- package/templates/vue/useSwoffMutation.js +124 -0
- package/templates/vue/useSwoffMutation.ts +155 -0
- package/templates/vue/useSwoffMutationState.js +35 -0
- package/templates/vue/useSwoffMutationState.ts +36 -0
- package/templates/vue/useSwoffNetwork.js +86 -0
- package/templates/vue/useSwoffNetwork.ts +89 -0
- package/templates/vue/useSwoffPrecache.js +19 -0
- package/templates/vue/useSwoffPrecache.ts +19 -0
- package/templates/vue/useSwoffPrefetch.js +33 -0
- package/templates/vue/useSwoffPrefetch.ts +41 -0
- package/templates/vue/useSwoffPush.js +81 -0
- package/templates/vue/useSwoffPush.ts +81 -0
- package/templates/vue/useSwoffPwa.js +27 -0
- package/templates/vue/useSwoffPwa.ts +27 -0
- package/templates/vue/useSwoffQueue.js +70 -0
- package/templates/vue/useSwoffQueue.ts +82 -0
- package/templates/vue/useSwoffReset.js +32 -0
- package/templates/vue/useSwoffReset.ts +39 -0
- package/templates/vue/useSwoffStorage.js +56 -0
- package/templates/vue/useSwoffStorage.ts +57 -0
- package/templates/vue/useSwoffSync.js +30 -0
- package/templates/vue/useSwoffSync.ts +30 -0
|
@@ -10,7 +10,7 @@ export interface OfflineFallbackEvent {
|
|
|
10
10
|
* Tracks offline navigation fallback events dispatched by the Service Worker.
|
|
11
11
|
*
|
|
12
12
|
* Usage:
|
|
13
|
-
* const { lastEvent, events } =
|
|
13
|
+
* const { lastEvent, events } = useSwoffAnalytics();
|
|
14
14
|
*
|
|
15
15
|
* // Render an offline indicator when a fallback was served:
|
|
16
16
|
* if (lastEvent) {
|
|
@@ -18,11 +18,11 @@ export interface OfflineFallbackEvent {
|
|
|
18
18
|
* }
|
|
19
19
|
*
|
|
20
20
|
* // Or pass a callback:
|
|
21
|
-
*
|
|
21
|
+
* useSwoffAnalytics((event) => console.log("Fallback:", event));
|
|
22
22
|
*
|
|
23
23
|
* @returns {{ lastEvent: OfflineFallbackEvent | null, events: OfflineFallbackEvent[], clear: () => void }}
|
|
24
24
|
*/
|
|
25
|
-
export function
|
|
25
|
+
export function useSwoffAnalytics(callback?: (event: OfflineFallbackEvent) => void) {
|
|
26
26
|
const [events, setEvents] = useState<OfflineFallbackEvent[]>([]);
|
|
27
27
|
const [lastEvent, setLastEvent] = useState<OfflineFallbackEvent | null>(null);
|
|
28
28
|
|
|
@@ -6,7 +6,7 @@ import { setAuth, clearAuth, ensureValidAuth } from "../auth/store.js";
|
|
|
6
6
|
* Reactive auth state with actions: setAuth, clearAuth, ensureValid.
|
|
7
7
|
*
|
|
8
8
|
* Usage:
|
|
9
|
-
* const { authenticated, auth, online, isLoading, error, setAuth, clearAuth, ensureValid } =
|
|
9
|
+
* const { authenticated, auth, online, isLoading, error, setAuth, clearAuth, ensureValid } = useSwoffAuth();
|
|
10
10
|
*
|
|
11
11
|
* // Login — call setAuth with the response from your login endpoint:
|
|
12
12
|
* const res = await fetch("/api/login", { method: "POST", body });
|
|
@@ -22,7 +22,7 @@ import { setAuth, clearAuth, ensureValidAuth } from "../auth/store.js";
|
|
|
22
22
|
* // Silent session restore:
|
|
23
23
|
* const authData = await ensureValid();
|
|
24
24
|
*/
|
|
25
|
-
export function
|
|
25
|
+
export function useSwoffAuth() {
|
|
26
26
|
const [state, setState] = useState(() => ({
|
|
27
27
|
authenticated: false,
|
|
28
28
|
auth: null,
|
|
@@ -10,7 +10,7 @@ import type { AuthData } from "../auth/adapter.ts";
|
|
|
10
10
|
* setAuth(), or the SW's AUTH_CLEARED broadcast).
|
|
11
11
|
*
|
|
12
12
|
* Usage:
|
|
13
|
-
* const { authenticated, auth, online, isLoading, error, setAuth, clearAuth, ensureValid } =
|
|
13
|
+
* const { authenticated, auth, online, isLoading, error, setAuth, clearAuth, ensureValid } = useSwoffAuth();
|
|
14
14
|
*
|
|
15
15
|
* // Login:
|
|
16
16
|
* const res = await fetch("/api/login", { method: "POST", body });
|
|
@@ -28,7 +28,7 @@ import type { AuthData } from "../auth/adapter.ts";
|
|
|
28
28
|
*
|
|
29
29
|
* @returns {{ authenticated, auth, online, isLoading, error, setAuth, clearAuth, ensureValid }}
|
|
30
30
|
*/
|
|
31
|
-
export function
|
|
31
|
+
export function useSwoffAuth() {
|
|
32
32
|
const [state, setState] = useState(() => ({
|
|
33
33
|
authenticated: false,
|
|
34
34
|
auth: null as AuthData | null,
|
|
@@ -6,27 +6,27 @@ import { generateTags } from "../cache/tags.js";
|
|
|
6
6
|
* Fetch and cache data with automatic invalidation, revalidation, and offline support.
|
|
7
7
|
*
|
|
8
8
|
* Usage:
|
|
9
|
-
* const { data, error, loading, refetch } =
|
|
9
|
+
* const { data, error, loading, refetch } = useSwoffFetch("/api/todos");
|
|
10
10
|
*
|
|
11
11
|
* // Select/transform data
|
|
12
|
-
* const { data: names } =
|
|
12
|
+
* const { data: names } = useSwoffFetch("/api/users", {
|
|
13
13
|
* select: (users) => users.map(u => u.name),
|
|
14
14
|
* });
|
|
15
15
|
*
|
|
16
16
|
* // Keep previous data while fetching next page
|
|
17
|
-
* const { data } =
|
|
17
|
+
* const { data } = useSwoffFetch(`/api/items?page=${page}`, {
|
|
18
18
|
* keepPreviousData: true,
|
|
19
19
|
* });
|
|
20
20
|
*
|
|
21
21
|
* // Retry on failure
|
|
22
|
-
* const { data } =
|
|
22
|
+
* const { data } = useSwoffFetch("/api/todos", { retry: 3 });
|
|
23
23
|
*
|
|
24
24
|
* // Placeholder data while loading
|
|
25
|
-
* const { data } =
|
|
25
|
+
* const { data } = useSwoffFetch("/api/todos", {
|
|
26
26
|
* placeholderData: { items: [] },
|
|
27
27
|
* });
|
|
28
28
|
*/
|
|
29
|
-
export function
|
|
29
|
+
export function useSwoffFetch(url, options = {}) {
|
|
30
30
|
const {
|
|
31
31
|
select,
|
|
32
32
|
retry: retryOpt,
|
|
@@ -7,35 +7,35 @@ import type { FetchWithCacheOptions } from "../fetch/core.ts";
|
|
|
7
7
|
* Fetch and cache data with automatic invalidation, revalidation, and offline support.
|
|
8
8
|
*
|
|
9
9
|
* Usage:
|
|
10
|
-
* const { data, error, loading, refetch } =
|
|
10
|
+
* const { data, error, loading, refetch } = useSwoffFetch<MyType>("/api/todos");
|
|
11
11
|
*
|
|
12
12
|
* // Select/transform data — skips re-render if selected value hasn't changed
|
|
13
|
-
* const { data: names } =
|
|
13
|
+
* const { data: names } = useSwoffFetch("/api/users", {
|
|
14
14
|
* select: (users) => users.map(u => u.name),
|
|
15
15
|
* });
|
|
16
16
|
*
|
|
17
17
|
* // Keep previous data while fetching the next page
|
|
18
|
-
* const { data } =
|
|
18
|
+
* const { data } = useSwoffFetch(`/api/items?page=${page}`, {
|
|
19
19
|
* keepPreviousData: true,
|
|
20
20
|
* });
|
|
21
21
|
*
|
|
22
22
|
* // Retry on failure (default 0 retries)
|
|
23
|
-
* const { data } =
|
|
23
|
+
* const { data } = useSwoffFetch("/api/todos", { retry: 3 });
|
|
24
24
|
*
|
|
25
25
|
* // Placeholder data while loading for the first time
|
|
26
|
-
* const { data } =
|
|
26
|
+
* const { data } = useSwoffFetch("/api/todos", {
|
|
27
27
|
* placeholderData: { items: [] },
|
|
28
28
|
* });
|
|
29
29
|
*
|
|
30
30
|
* // Callbacks on success/error
|
|
31
|
-
* const { data, refetch } =
|
|
31
|
+
* const { data, refetch } = useSwoffFetch("/api/todos", {
|
|
32
32
|
* onSuccess: (data) => console.log("Loaded", data),
|
|
33
33
|
* });
|
|
34
34
|
*
|
|
35
35
|
* @param url - URL to fetch. Pass null to skip fetching.
|
|
36
36
|
* @param options - Fetch options plus: select, keepPreviousData, retry, placeholderData, onSuccess, onError
|
|
37
37
|
*/
|
|
38
|
-
export function
|
|
38
|
+
export function useSwoffFetch<T = unknown, R = T>(
|
|
39
39
|
url: string | null,
|
|
40
40
|
options: FetchWithCacheOptions & {
|
|
41
41
|
enabled?: boolean;
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
* optimistic updates, and dual-level callbacks.
|
|
12
12
|
*
|
|
13
13
|
* Usage:
|
|
14
|
-
* const { mutate, isLoading, error, data } =
|
|
14
|
+
* const { mutate, isLoading, error, data } = useSwoffMutation("/api/todos", {
|
|
15
15
|
* method: "POST",
|
|
16
16
|
* onSuccess: (data) => navigate(`/item/${data.id}`),
|
|
17
17
|
* });
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
* });
|
|
24
24
|
*
|
|
25
25
|
* // Optimistic update with rollback:
|
|
26
|
-
* const { mutate } =
|
|
26
|
+
* const { mutate } = useSwoffMutation("/api/todos", {
|
|
27
27
|
* method: "POST",
|
|
28
28
|
* onMutate: () => { /* set optimistic state *\\/ },
|
|
29
29
|
* onError: () => { /* rollback *\\/ },
|
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
* if (result.status === "queued") navigate("/items");
|
|
36
36
|
* if (result.status === "error") setError(result.error.message);
|
|
37
37
|
*/
|
|
38
|
-
export function
|
|
38
|
+
export function useSwoffMutation(url, options = {}) {
|
|
39
39
|
const [lastResult, setLastResult] = useState(null);
|
|
40
40
|
const [isLoading, setIsLoading] = useState(false);
|
|
41
41
|
const [mutationId, setMutationId] = useState(null);
|
|
@@ -36,7 +36,7 @@ export type MutateResult<T> =
|
|
|
36
36
|
* optimistic updates, and dual-level callbacks.
|
|
37
37
|
*
|
|
38
38
|
* Usage:
|
|
39
|
-
* const { mutate, isLoading, error, data } =
|
|
39
|
+
* const { mutate, isLoading, error, data } = useSwoffMutation<CreateTodoResponse>(
|
|
40
40
|
* "/api/todos",
|
|
41
41
|
* { method: "POST", onSuccess: (data) => navigate(`/item/${data.id}`) },
|
|
42
42
|
* );
|
|
@@ -48,7 +48,7 @@ export type MutateResult<T> =
|
|
|
48
48
|
* });
|
|
49
49
|
*
|
|
50
50
|
* // Optimistic update with rollback:
|
|
51
|
-
* const { mutate } =
|
|
51
|
+
* const { mutate } = useSwoffMutation("/api/todos", {
|
|
52
52
|
* method: "POST",
|
|
53
53
|
* onMutate: () => { /* set optimistic state *\\/ },
|
|
54
54
|
* onError: () => { /* rollback *\\/ },
|
|
@@ -60,7 +60,7 @@ export type MutateResult<T> =
|
|
|
60
60
|
* if (result.status === "queued") navigate("/items");
|
|
61
61
|
* if (result.status === "error") setError(result.error.message);
|
|
62
62
|
*/
|
|
63
|
-
export function
|
|
63
|
+
export function useSwoffMutation<TData = unknown>(
|
|
64
64
|
url: string,
|
|
65
65
|
options: UseMutationOptions<TData> = {},
|
|
66
66
|
) {
|
|
@@ -5,7 +5,7 @@ function subscribeToMutations(cb) {
|
|
|
5
5
|
return onMutationStateChange(() => cb());
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export function
|
|
8
|
+
export function useSwoffMutationState(id) {
|
|
9
9
|
return useSyncExternalStore(subscribeToMutations, () =>
|
|
10
10
|
id ? (getMutationState(id) ?? null) : null,
|
|
11
11
|
);
|
|
@@ -11,10 +11,10 @@ function subscribeToMutations(cb: () => void) {
|
|
|
11
11
|
* Useful for showing per-mutation loading spinners, error states, etc.
|
|
12
12
|
*
|
|
13
13
|
* Usage:
|
|
14
|
-
* const mutation =
|
|
14
|
+
* const mutation = useSwoffMutationState(mutationId);
|
|
15
15
|
* if (mutation?.status === "error") { ... }
|
|
16
16
|
*/
|
|
17
|
-
export function
|
|
17
|
+
export function useSwoffMutationState(id: string | null): MutationState | null {
|
|
18
18
|
return useSyncExternalStore(subscribeToMutations, () =>
|
|
19
19
|
id ? (getMutationState(id) ?? null) : null,
|
|
20
20
|
);
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
* Reactive network information: online status, connection type, and bandwidth.
|
|
10
10
|
*
|
|
11
11
|
* Usage:
|
|
12
|
-
* const { online, wasOffline, lastChangedAt, effectiveType, downlink } =
|
|
12
|
+
* const { online, wasOffline, lastChangedAt, effectiveType, downlink } = useSwoffNetwork();
|
|
13
13
|
*
|
|
14
14
|
* // Show offline indicator:
|
|
15
15
|
* if (!online) return <OfflineBanner />;
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
* // Warn on slow connection:
|
|
18
18
|
* if (online && effectiveType === "2g") return <SlowConnectionWarning />;
|
|
19
19
|
*/
|
|
20
|
-
export function
|
|
20
|
+
export function useSwoffNetwork() {
|
|
21
21
|
const wasOfflineRef = useRef(false);
|
|
22
22
|
const [isRetrying, setIsRetrying] = useState(false);
|
|
23
23
|
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
* Reactive network information: online status, connection type, and bandwidth.
|
|
10
10
|
*
|
|
11
11
|
* Usage:
|
|
12
|
-
* const { online, wasOffline, lastChangedAt, effectiveType, downlink } =
|
|
12
|
+
* const { online, wasOffline, lastChangedAt, effectiveType, downlink } = useSwoffNetwork();
|
|
13
13
|
*
|
|
14
14
|
* // Show offline indicator:
|
|
15
15
|
* if (!online) return <OfflineBanner />;
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
*
|
|
20
20
|
* @returns {{ online: boolean, wasOffline: boolean, lastChangedAt: number | null, effectiveType: string | null, downlink: number | null, isRetrying: boolean, retry: () => Promise<void> }}
|
|
21
21
|
*/
|
|
22
|
-
export function
|
|
22
|
+
export function useSwoffNetwork() {
|
|
23
23
|
const wasOfflineRef = useRef(false);
|
|
24
24
|
const [isRetrying, setIsRetrying] = useState(false);
|
|
25
25
|
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
2
|
|
|
3
|
-
export function
|
|
4
|
-
const [
|
|
5
|
-
status: "idle",
|
|
6
|
-
progress: 0,
|
|
7
|
-
}));
|
|
3
|
+
export function useSwoffPrecache() {
|
|
4
|
+
const [progress, setProgress] = useState(0);
|
|
8
5
|
|
|
9
6
|
useEffect(() => {
|
|
10
7
|
const onProgress = (e) =>
|
|
11
|
-
|
|
8
|
+
setProgress(e.detail.percent);
|
|
12
9
|
|
|
13
10
|
window.addEventListener("sw-progress", onProgress);
|
|
14
11
|
return () => {
|
|
@@ -16,5 +13,5 @@ export function usePrecacheProgress() {
|
|
|
16
13
|
};
|
|
17
14
|
}, []);
|
|
18
15
|
|
|
19
|
-
return
|
|
16
|
+
return { progress };
|
|
20
17
|
}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
2
|
|
|
3
|
-
export function
|
|
4
|
-
const [
|
|
5
|
-
status: "idle" as "idle" | "installing",
|
|
6
|
-
progress: 0,
|
|
7
|
-
}));
|
|
3
|
+
export function useSwoffPrecache() {
|
|
4
|
+
const [progress, setProgress] = useState(0);
|
|
8
5
|
|
|
9
6
|
useEffect(() => {
|
|
10
7
|
const onProgress = (e: WindowEventMap["sw-progress"]) =>
|
|
11
|
-
|
|
8
|
+
setProgress(e.detail.percent);
|
|
12
9
|
|
|
13
10
|
window.addEventListener("sw-progress", onProgress);
|
|
14
11
|
return () => {
|
|
@@ -16,5 +13,5 @@ export function usePrecacheProgress() {
|
|
|
16
13
|
};
|
|
17
14
|
}, []);
|
|
18
15
|
|
|
19
|
-
return
|
|
16
|
+
return { progress };
|
|
20
17
|
}
|
|
@@ -5,7 +5,7 @@ import { prefetchCache } from "../fetch/core.js";
|
|
|
5
5
|
* Hook that returns a stable prefetch callback, observable prefetch list, and clear function.
|
|
6
6
|
*
|
|
7
7
|
* Usage:
|
|
8
|
-
* const { prefetch, prefetchList, clear } =
|
|
8
|
+
* const { prefetch, prefetchList, clear } = useSwoffPrefetch();
|
|
9
9
|
*
|
|
10
10
|
* // Prefetch when user hovers a link:
|
|
11
11
|
* <Link onMouseEnter={() => prefetch("/api/todos")} to="/todos" />
|
|
@@ -16,7 +16,7 @@ import { prefetchCache } from "../fetch/core.js";
|
|
|
16
16
|
* // Clear prefetched data:
|
|
17
17
|
* <button onClick={clear}>Clear prefetch</button>
|
|
18
18
|
*/
|
|
19
|
-
export function
|
|
19
|
+
export function useSwoffPrefetch() {
|
|
20
20
|
const [prefetchList, setPrefetchList] = useState([]);
|
|
21
21
|
|
|
22
22
|
const prefetch = useCallback((url, options) => {
|
|
@@ -6,7 +6,7 @@ import type { FetchWithCacheOptions } from "../fetch/core.ts";
|
|
|
6
6
|
* Hook that returns a stable prefetch callback, observable prefetch list, and clear function.
|
|
7
7
|
*
|
|
8
8
|
* Usage:
|
|
9
|
-
* const { prefetch, prefetchList, clear } =
|
|
9
|
+
* const { prefetch, prefetchList, clear } = useSwoffPrefetch();
|
|
10
10
|
*
|
|
11
11
|
* // Prefetch when user hovers a link:
|
|
12
12
|
* <Link onMouseEnter={() => prefetch("/api/todos")} to="/todos" />
|
|
@@ -19,7 +19,7 @@ import type { FetchWithCacheOptions } from "../fetch/core.ts";
|
|
|
19
19
|
*
|
|
20
20
|
* @returns {{ prefetch: (url: string, options?: FetchWithCacheOptions) => void, prefetchList: string[], clear: () => void }}
|
|
21
21
|
*/
|
|
22
|
-
export function
|
|
22
|
+
export function useSwoffPrefetch(): {
|
|
23
23
|
prefetch: (url: string, options?: FetchWithCacheOptions) => void;
|
|
24
24
|
prefetchList: string[];
|
|
25
25
|
clear: () => void;
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
getPushSubscription,
|
|
7
7
|
} from "../realtime/notifications.ts";
|
|
8
8
|
|
|
9
|
-
export function
|
|
9
|
+
export function useSwoffPush() {
|
|
10
10
|
const [state, setState] = useState(() => ({
|
|
11
11
|
subscribed: false,
|
|
12
12
|
subscription: null as PushSubscription | null,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState, useEffect, useCallback } from "react";
|
|
2
2
|
import { isInstallable, promptInstall } from "../pwa/prompt.js";
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function useSwoffPwa() {
|
|
5
5
|
const [canInstall, setCanInstall] = useState(false);
|
|
6
6
|
|
|
7
7
|
useEffect(() => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState, useEffect, useCallback } from "react";
|
|
2
2
|
import { isInstallable, promptInstall } from "../pwa/prompt.ts";
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function useSwoffPwa() {
|
|
5
5
|
const [canInstall, setCanInstall] = useState(false);
|
|
6
6
|
|
|
7
7
|
useEffect(() => {
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
* Reactive mutation queue state: pending count, items, last sync info, and processing flag.
|
|
10
10
|
*
|
|
11
11
|
* Usage:
|
|
12
|
-
* const { pending, items, lastSync, isProcessing, retryAll } =
|
|
12
|
+
* const { pending, items, lastSync, isProcessing, retryAll } = useSwoffQueue();
|
|
13
13
|
*
|
|
14
14
|
* // Show pending badge:
|
|
15
15
|
* {pending > 0 && <Badge>{pending}</Badge>}
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
* {isProcessing ? "Syncing..." : "Retry All"}
|
|
20
20
|
* </button>
|
|
21
21
|
*/
|
|
22
|
-
export function
|
|
22
|
+
export function useSwoffQueue() {
|
|
23
23
|
const [state, setState] = useState({
|
|
24
24
|
pending: 0,
|
|
25
25
|
items: [],
|
|
@@ -17,7 +17,7 @@ export interface MutationQueueState {
|
|
|
17
17
|
* Reactive mutation queue state: pending count, items, last sync info, and processing flag.
|
|
18
18
|
*
|
|
19
19
|
* Usage:
|
|
20
|
-
* const { pending, items, lastSync, isProcessing, retryAll } =
|
|
20
|
+
* const { pending, items, lastSync, isProcessing, retryAll } = useSwoffQueue();
|
|
21
21
|
*
|
|
22
22
|
* // Show pending badge:
|
|
23
23
|
* {pending > 0 && <Badge>{pending}</Badge>}
|
|
@@ -29,7 +29,7 @@ export interface MutationQueueState {
|
|
|
29
29
|
*
|
|
30
30
|
* @returns {{ pending, items, lastSync, isProcessing, retryAll }}
|
|
31
31
|
*/
|
|
32
|
-
export function
|
|
32
|
+
export function useSwoffQueue(): MutationQueueState & {
|
|
33
33
|
retryAll: () => Promise<void>;
|
|
34
34
|
} {
|
|
35
35
|
const [state, setState] = useState<MutationQueueState>({
|
|
@@ -5,13 +5,13 @@ import { getStorageEstimate, formatBytes } from "../storage";
|
|
|
5
5
|
* Reactive storage estimate. Shows available quota and usage.
|
|
6
6
|
*
|
|
7
7
|
* Usage:
|
|
8
|
-
* const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } =
|
|
8
|
+
* const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } = useSwoffStorage();
|
|
9
9
|
*
|
|
10
10
|
* if (!loading && percentUsed > 80) return <StorageWarning />;
|
|
11
11
|
*
|
|
12
12
|
* @param autoRefresh - Re-check on visibility change (default true)
|
|
13
13
|
*/
|
|
14
|
-
export function
|
|
14
|
+
export function useSwoffStorage(autoRefresh = true) {
|
|
15
15
|
const [state, setState] = useState({
|
|
16
16
|
usage: 0,
|
|
17
17
|
quota: 0,
|
|
@@ -15,14 +15,14 @@ interface StorageEstimateState {
|
|
|
15
15
|
* Reactive storage estimate. Shows available quota and usage.
|
|
16
16
|
*
|
|
17
17
|
* Usage:
|
|
18
|
-
* const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } =
|
|
18
|
+
* const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } = useSwoffStorage();
|
|
19
19
|
*
|
|
20
20
|
* if (!loading && percentUsed > 80) return <StorageWarning />;
|
|
21
21
|
*
|
|
22
22
|
* @param autoRefresh - Re-check on visibility change (default true)
|
|
23
23
|
* @returns {{ usage: number, quota: number, percentUsed: number, formattedUsage: string, formattedQuota: string, loading: boolean, error: string | null }}
|
|
24
24
|
*/
|
|
25
|
-
export function
|
|
25
|
+
export function useSwoffStorage(autoRefresh = true): StorageEstimateState {
|
|
26
26
|
const [state, setState] = useState<StorageEstimateState>({
|
|
27
27
|
usage: 0,
|
|
28
28
|
quota: 0,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState, useEffect, useCallback } from "react";
|
|
2
2
|
import { retrySync } from "../mutation/sync.js";
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function useSwoffSync() {
|
|
5
5
|
const [state, setState] = useState(() => ({
|
|
6
6
|
supported:
|
|
7
7
|
typeof window !== "undefined" && typeof navigator !== "undefined"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState, useEffect, useCallback } from "react";
|
|
2
2
|
import { retrySync } from "../mutation/sync.ts";
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function useSwoffSync() {
|
|
5
5
|
const [state, setState] = useState(() => ({
|
|
6
6
|
supported:
|
|
7
7
|
typeof window !== "undefined" && typeof navigator !== "undefined"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
|
|
4
|
+
export function useSwoffAnalytics(callback) {
|
|
5
|
+
const events = writable([]);
|
|
6
|
+
const lastEvent = writable(null);
|
|
7
|
+
|
|
8
|
+
function handler(e) {
|
|
9
|
+
if (e.data?.type === "OFFLINE_FALLBACK_ACTIVATED" && e.data?.detail) {
|
|
10
|
+
const detail = e.data.detail;
|
|
11
|
+
events.update((prev) => [...prev, detail]);
|
|
12
|
+
lastEvent.set(detail);
|
|
13
|
+
callback?.(detail);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
onMount(() => {
|
|
18
|
+
if (typeof navigator === "undefined" || !navigator.serviceWorker) return;
|
|
19
|
+
navigator.serviceWorker.addEventListener("message", handler);
|
|
20
|
+
return () => {
|
|
21
|
+
navigator.serviceWorker.removeEventListener("message", handler);
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function clear() {
|
|
26
|
+
events.set([]);
|
|
27
|
+
lastEvent.set(null);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return { lastEvent, events, clear };
|
|
31
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
|
|
4
|
+
export interface OfflineFallbackEvent {
|
|
5
|
+
route: string;
|
|
6
|
+
fallbackLevel: "route-fallback" | "offline-page" | "spa-shell" | "inline-503";
|
|
7
|
+
timestamp: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function useSwoffAnalytics(callback?: (event: OfflineFallbackEvent) => void) {
|
|
11
|
+
const events = writable<OfflineFallbackEvent[]>([]);
|
|
12
|
+
const lastEvent = writable<OfflineFallbackEvent | null>(null);
|
|
13
|
+
|
|
14
|
+
function handler(e: MessageEvent) {
|
|
15
|
+
if (e.data?.type === "OFFLINE_FALLBACK_ACTIVATED" && e.data?.detail) {
|
|
16
|
+
const detail = e.data.detail as OfflineFallbackEvent;
|
|
17
|
+
events.update((prev) => [...prev, detail]);
|
|
18
|
+
lastEvent.set(detail);
|
|
19
|
+
callback?.(detail);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
onMount(() => {
|
|
24
|
+
if (typeof navigator === "undefined" || !navigator.serviceWorker) return;
|
|
25
|
+
navigator.serviceWorker.addEventListener("message", handler);
|
|
26
|
+
return () => {
|
|
27
|
+
navigator.serviceWorker.removeEventListener("message", handler);
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function clear() {
|
|
32
|
+
events.set([]);
|
|
33
|
+
lastEvent.set(null);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return { lastEvent, events, clear };
|
|
37
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { getAuthState } from "../auth/state.js";
|
|
4
|
+
import { setAuth, clearAuth, ensureValidAuth } from "../auth/store.js";
|
|
5
|
+
|
|
6
|
+
export function useSwoffAuth() {
|
|
7
|
+
const authenticated = writable(false);
|
|
8
|
+
const auth = writable(null);
|
|
9
|
+
const online = writable(
|
|
10
|
+
typeof navigator !== "undefined" ? navigator.onLine : true,
|
|
11
|
+
);
|
|
12
|
+
const isLoading = writable(false);
|
|
13
|
+
const error = writable(null);
|
|
14
|
+
|
|
15
|
+
async function refreshState() {
|
|
16
|
+
try {
|
|
17
|
+
const authState = await getAuthState();
|
|
18
|
+
authenticated.set(authState.authenticated);
|
|
19
|
+
auth.set(authState.auth);
|
|
20
|
+
error.set(null);
|
|
21
|
+
} catch {
|
|
22
|
+
// ignore — auth not initialized
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
onMount(() => {
|
|
27
|
+
refreshState();
|
|
28
|
+
|
|
29
|
+
function onOnline() { online.set(true); }
|
|
30
|
+
function onOffline() { online.set(false); }
|
|
31
|
+
function onAuthChange() { refreshState(); }
|
|
32
|
+
|
|
33
|
+
window.addEventListener("online", onOnline);
|
|
34
|
+
window.addEventListener("offline", onOffline);
|
|
35
|
+
window.addEventListener("sw-auth-state-change", onAuthChange);
|
|
36
|
+
|
|
37
|
+
return () => {
|
|
38
|
+
window.removeEventListener("online", onOnline);
|
|
39
|
+
window.removeEventListener("offline", onOffline);
|
|
40
|
+
window.removeEventListener("sw-auth-state-change", onAuthChange);
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
async function doSetAuth(authData) {
|
|
45
|
+
isLoading.set(true);
|
|
46
|
+
error.set(null);
|
|
47
|
+
try {
|
|
48
|
+
await setAuth(authData);
|
|
49
|
+
await refreshState();
|
|
50
|
+
} catch (err) {
|
|
51
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
52
|
+
} finally {
|
|
53
|
+
isLoading.set(false);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function doClearAuth() {
|
|
58
|
+
isLoading.set(true);
|
|
59
|
+
error.set(null);
|
|
60
|
+
try {
|
|
61
|
+
await clearAuth();
|
|
62
|
+
await refreshState();
|
|
63
|
+
} catch (err) {
|
|
64
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
65
|
+
} finally {
|
|
66
|
+
isLoading.set(false);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function doEnsureValid() {
|
|
71
|
+
isLoading.set(true);
|
|
72
|
+
error.set(null);
|
|
73
|
+
try {
|
|
74
|
+
const result = await ensureValidAuth();
|
|
75
|
+
await refreshState();
|
|
76
|
+
return result;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
error.set(err instanceof Error ? err : new Error(String(err)));
|
|
79
|
+
return null;
|
|
80
|
+
} finally {
|
|
81
|
+
isLoading.set(false);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
authenticated,
|
|
87
|
+
auth,
|
|
88
|
+
online,
|
|
89
|
+
isLoading,
|
|
90
|
+
error,
|
|
91
|
+
setAuth: doSetAuth,
|
|
92
|
+
clearAuth: doClearAuth,
|
|
93
|
+
ensureValid: doEnsureValid,
|
|
94
|
+
};
|
|
95
|
+
}
|