@pol-studios/hooks 1.0.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.
@@ -0,0 +1,39 @@
1
+ // src/storage/providers.tsx
2
+ import { createContext } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var localStorageContext = createContext({});
5
+ function LocalStorageProvider({
6
+ children,
7
+ value
8
+ }) {
9
+ return /* @__PURE__ */ jsx(localStorageContext.Provider, { value, children });
10
+ }
11
+ var indexDbContext = createContext(
12
+ {}
13
+ );
14
+ function IndexDbProvider({
15
+ children,
16
+ value
17
+ }) {
18
+ return /* @__PURE__ */ jsx(indexDbContext.Provider, { value: { services: value }, children });
19
+ }
20
+
21
+ // src/storage/useLocalStorageService.ts
22
+ import { useContext } from "react";
23
+ function useLocalStorageService() {
24
+ const context = useContext(localStorageContext);
25
+ if (context === null) {
26
+ throw new Error(
27
+ "useLocalStorageService should be used inside of LocalStorageProvider"
28
+ );
29
+ }
30
+ return context;
31
+ }
32
+
33
+ export {
34
+ localStorageContext,
35
+ LocalStorageProvider,
36
+ indexDbContext,
37
+ IndexDbProvider,
38
+ useLocalStorageService
39
+ };
@@ -0,0 +1,37 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
13
+ var __commonJS = (cb, mod) => function __require2() {
14
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+
33
+ export {
34
+ __require,
35
+ __commonJS,
36
+ __toESM
37
+ };
@@ -0,0 +1,130 @@
1
+ // src/state/usePartialState.ts
2
+ import { useState } from "react";
3
+ function usePartialState(initialState) {
4
+ const [state, setState] = useState(initialState);
5
+ const patch = (newProps) => {
6
+ setState((prev) => {
7
+ return { ...prev, ...newProps };
8
+ });
9
+ };
10
+ return { state, patch, setState };
11
+ }
12
+
13
+ // src/state/useQueuedState.ts
14
+ import { useCallback, useRef, useState as useState2 } from "react";
15
+ function useQueuedState(initialState) {
16
+ const [state, setState] = useState2(initialState);
17
+ const queue = useRef([]);
18
+ const processing = useRef(false);
19
+ const processQueue = useCallback(async () => {
20
+ if (processing.current) return;
21
+ processing.current = true;
22
+ while (queue.current.length > 0) {
23
+ const updateFn = queue.current.shift();
24
+ setState(updateFn);
25
+ await new Promise(requestAnimationFrame);
26
+ }
27
+ processing.current = false;
28
+ }, []);
29
+ const enqueue = useCallback((updateFn) => {
30
+ queue.current.push(updateFn);
31
+ processQueue();
32
+ }, [processQueue]);
33
+ return [state, enqueue];
34
+ }
35
+
36
+ // src/state/useDelayedValue.ts
37
+ import { useEffect, useState as useState3 } from "react";
38
+ function useDelayedValue(value, delayInMs) {
39
+ const [delayedValue, setDelayedValue] = useState3(value);
40
+ useEffect(() => {
41
+ const timeout = setTimeout(() => {
42
+ setDelayedValue(value);
43
+ }, delayInMs);
44
+ return () => {
45
+ clearTimeout(timeout);
46
+ };
47
+ }, [value]);
48
+ return delayedValue;
49
+ }
50
+ var useDelayedState = useDelayedValue;
51
+
52
+ // src/state/useChangeTracking.ts
53
+ import { useLayoutEffect, useState as useState4 } from "react";
54
+ function useChangeTracking(initialValue) {
55
+ const [value, setValue] = useState4(initialValue);
56
+ const [propertiesChanged, setPropertiesChanged] = useState4(
57
+ /* @__PURE__ */ new Set()
58
+ );
59
+ useLayoutEffect(() => {
60
+ setValue(initialValue);
61
+ setPropertiesChanged(/* @__PURE__ */ new Set());
62
+ }, [initialValue]);
63
+ function update(newProps) {
64
+ setValue((prev) => {
65
+ const updatedObject = { ...prev, ...newProps };
66
+ setPropertiesChanged((prev2) => {
67
+ const updatedProperties = new Set(prev2);
68
+ if (initialValue) {
69
+ Object.keys(newProps).forEach((propertyName) => {
70
+ const originalValue = initialValue[propertyName];
71
+ const newValue = updatedObject[propertyName];
72
+ if (originalValue !== newValue) {
73
+ updatedProperties.add(propertyName);
74
+ } else {
75
+ updatedProperties.delete(propertyName);
76
+ }
77
+ });
78
+ }
79
+ return updatedProperties;
80
+ });
81
+ return updatedObject;
82
+ });
83
+ }
84
+ return { initialValue, update, value, propertiesChanged };
85
+ }
86
+
87
+ // src/state/useForceUpdate.ts
88
+ import { useState as useState5, useCallback as useCallback2 } from "react";
89
+ function useForceUpdate() {
90
+ const [, setState] = useState5(0);
91
+ return useCallback2(() => setState((prev) => prev + 1), []);
92
+ }
93
+
94
+ // src/state/ChangeTrackingContext.tsx
95
+ import { createContext } from "react";
96
+ import { jsx } from "react/jsx-runtime";
97
+ var defaultValue = {
98
+ initialValue: {},
99
+ value: {},
100
+ onChange: (newValue) => {
101
+ },
102
+ propertiesChanged: /* @__PURE__ */ new Set()
103
+ };
104
+ var ChangeTrackingContext = createContext(defaultValue);
105
+ function ChangeTrackingProvider({ children, initialValue }) {
106
+ return /* @__PURE__ */ jsx(
107
+ ChangeTrackingContext.Provider,
108
+ {
109
+ value: {
110
+ initialValue,
111
+ value: null,
112
+ propertiesChanged: /* @__PURE__ */ new Set(),
113
+ onChange: (newValue) => {
114
+ }
115
+ },
116
+ children
117
+ }
118
+ );
119
+ }
120
+
121
+ export {
122
+ usePartialState,
123
+ useQueuedState,
124
+ useDelayedValue,
125
+ useDelayedState,
126
+ useChangeTracking,
127
+ useForceUpdate,
128
+ ChangeTrackingContext,
129
+ ChangeTrackingProvider
130
+ };
@@ -0,0 +1,133 @@
1
+ // src/toast/useToast.ts
2
+ import * as React from "react";
3
+ var TOAST_LIMIT = 1;
4
+ var TOAST_REMOVE_DELAY = 1e6;
5
+ var count = 0;
6
+ function genId() {
7
+ count = (count + 1) % Number.MAX_VALUE;
8
+ return count.toString();
9
+ }
10
+ var toastTimeouts = /* @__PURE__ */ new Map();
11
+ var addToRemoveQueue = (toastId) => {
12
+ if (toastTimeouts.has(toastId)) {
13
+ return;
14
+ }
15
+ const timeout = setTimeout(() => {
16
+ toastTimeouts.delete(toastId);
17
+ dispatch({
18
+ type: "REMOVE_TOAST",
19
+ toastId
20
+ });
21
+ }, TOAST_REMOVE_DELAY);
22
+ toastTimeouts.set(toastId, timeout);
23
+ };
24
+ var reducer = (state, action) => {
25
+ switch (action.type) {
26
+ case "ADD_TOAST":
27
+ return {
28
+ ...state,
29
+ toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT)
30
+ };
31
+ case "UPDATE_TOAST":
32
+ return {
33
+ ...state,
34
+ toasts: state.toasts.map((t) => t.id === action.toast.id ? { ...t, ...action.toast } : t)
35
+ };
36
+ case "DISMISS_TOAST": {
37
+ const { toastId } = action;
38
+ if (toastId) {
39
+ addToRemoveQueue(toastId);
40
+ } else {
41
+ state.toasts.forEach((toast2) => {
42
+ addToRemoveQueue(toast2.id);
43
+ });
44
+ }
45
+ return {
46
+ ...state,
47
+ toasts: state.toasts.map(
48
+ (t) => t.id === toastId || toastId === void 0 ? {
49
+ ...t,
50
+ open: false
51
+ } : t
52
+ )
53
+ };
54
+ }
55
+ case "REMOVE_TOAST":
56
+ if (action.toastId === void 0) {
57
+ return {
58
+ ...state,
59
+ toasts: []
60
+ };
61
+ }
62
+ return {
63
+ ...state,
64
+ toasts: state.toasts.filter((t) => t.id !== action.toastId)
65
+ };
66
+ }
67
+ };
68
+ var listeners = [];
69
+ var memoryState = { toasts: [] };
70
+ function dispatch(action) {
71
+ memoryState = reducer(memoryState, action);
72
+ listeners.forEach((listener) => {
73
+ listener(memoryState);
74
+ });
75
+ }
76
+ function toast({ ...props }) {
77
+ const id = genId();
78
+ const update = (props2) => dispatch({
79
+ type: "UPDATE_TOAST",
80
+ toast: { ...props2, id }
81
+ });
82
+ const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });
83
+ dispatch({
84
+ type: "ADD_TOAST",
85
+ toast: {
86
+ ...props,
87
+ id,
88
+ open: true,
89
+ onOpenChange: (open) => {
90
+ if (!open) dismiss();
91
+ }
92
+ }
93
+ });
94
+ return {
95
+ id,
96
+ dismiss,
97
+ update
98
+ };
99
+ }
100
+ function useToast() {
101
+ const [state, setState] = React.useState(memoryState);
102
+ React.useEffect(() => {
103
+ listeners.push(setState);
104
+ return () => {
105
+ const index = listeners.indexOf(setState);
106
+ if (index > -1) {
107
+ listeners.splice(index, 1);
108
+ }
109
+ };
110
+ }, [state]);
111
+ return {
112
+ ...state,
113
+ toast,
114
+ dismiss: (toastId) => dispatch({ type: "DISMISS_TOAST", toastId })
115
+ };
116
+ }
117
+ var useToast_default = useToast;
118
+
119
+ // src/toast/types.ts
120
+ var actionTypes = {
121
+ ADD_TOAST: "ADD_TOAST",
122
+ UPDATE_TOAST: "UPDATE_TOAST",
123
+ DISMISS_TOAST: "DISMISS_TOAST",
124
+ REMOVE_TOAST: "REMOVE_TOAST"
125
+ };
126
+
127
+ export {
128
+ reducer,
129
+ toast,
130
+ useToast,
131
+ useToast_default,
132
+ actionTypes
133
+ };
@@ -0,0 +1,67 @@
1
+ import {
2
+ isDevEnvironment
3
+ } from "./chunk-MMIPLCL4.js";
4
+
5
+ // src/debug/useRenderCount.ts
6
+ import { useEffect, useRef } from "react";
7
+ var useRenderCount = () => {
8
+ const renderCountRef = useRef(0);
9
+ useEffect(() => {
10
+ renderCountRef.current += 1;
11
+ });
12
+ return renderCountRef.current;
13
+ };
14
+ var useRenderCount_default = useRenderCount;
15
+
16
+ // src/debug/useRenderHookCount.ts
17
+ import { useState, useEffect as useEffect2 } from "react";
18
+ var useRenderHookCount = (hook) => {
19
+ const [count, setCount] = useState(0);
20
+ useEffect2(() => {
21
+ setCount((prevCount) => prevCount + 1);
22
+ }, [hook]);
23
+ return count;
24
+ };
25
+ var useRenderHookCount_default = useRenderHookCount;
26
+
27
+ // src/debug/useStateTracker.ts
28
+ import { useEffect as useEffect3 } from "react";
29
+ function useStateTracker(state) {
30
+ if (isDevEnvironment() === false) {
31
+ return;
32
+ }
33
+ useEffect3(() => {
34
+ console.log("State changed:", state);
35
+ }, [state]);
36
+ }
37
+
38
+ // src/debug/useTrackedState.ts
39
+ import { useMemo, useState as useState2 } from "react";
40
+ function useTrackedState(initialValue, hookName) {
41
+ const [state, setState] = useState2(initialValue);
42
+ const setTrackedState = useMemo(
43
+ () => (newValue) => {
44
+ setState((oldValue) => {
45
+ const stack = new Error().stack;
46
+ if (newValue !== oldValue) {
47
+ console.log(
48
+ `${hookName} changed from ${JSON.stringify(oldValue)} to ${JSON.stringify(newValue)} from ${stack}`
49
+ );
50
+ return newValue;
51
+ } else {
52
+ console.log(`${hookName} did not change from ${JSON.stringify(oldValue)} to ${JSON.stringify(newValue)}`);
53
+ return oldValue;
54
+ }
55
+ });
56
+ },
57
+ [setState, hookName]
58
+ );
59
+ return [state, setTrackedState];
60
+ }
61
+
62
+ export {
63
+ useRenderCount_default,
64
+ useRenderHookCount_default,
65
+ useStateTracker,
66
+ useTrackedState
67
+ };
@@ -0,0 +1,14 @@
1
+ import {
2
+ useRenderCount_default,
3
+ useRenderHookCount_default,
4
+ useStateTracker,
5
+ useTrackedState
6
+ } from "../chunk-XIISC67B.js";
7
+ import "../chunk-MMIPLCL4.js";
8
+ import "../chunk-QGM4M3NI.js";
9
+ export {
10
+ useRenderCount_default as useRenderCount,
11
+ useRenderHookCount_default as useRenderHookCount,
12
+ useStateTracker,
13
+ useTrackedState
14
+ };
@@ -0,0 +1,24 @@
1
+ import {
2
+ setReducedMotionPreference,
3
+ useDevice,
4
+ useDimensions,
5
+ useIsMobile,
6
+ useMobile,
7
+ useReducedMotion,
8
+ useTheme,
9
+ useThemeDetector,
10
+ useWindowDimensions
11
+ } from "../chunk-AIS4L5ID.js";
12
+ import "../chunk-MMIPLCL4.js";
13
+ import "../chunk-QGM4M3NI.js";
14
+ export {
15
+ setReducedMotionPreference,
16
+ useDevice,
17
+ useDimensions,
18
+ useIsMobile,
19
+ useMobile,
20
+ useReducedMotion,
21
+ useTheme,
22
+ useThemeDetector,
23
+ useWindowDimensions
24
+ };
@@ -0,0 +1,12 @@
1
+ import {
2
+ useDownload,
3
+ useDownloadApiFile,
4
+ useFileDialog
5
+ } from "../chunk-BKB34ZQJ.js";
6
+ import "../chunk-MMIPLCL4.js";
7
+ import "../chunk-QGM4M3NI.js";
8
+ export {
9
+ useDownload,
10
+ useDownloadApiFile,
11
+ useFileDialog
12
+ };
@@ -0,0 +1,13 @@
1
+ import {
2
+ OnValueChangedContext,
3
+ OnValueChangedContextProvider,
4
+ OnValueChangedTriggerContext,
5
+ OnValueChangedTriggerContextProvider
6
+ } from "../chunk-BHNC4MM6.js";
7
+ import "../chunk-QGM4M3NI.js";
8
+ export {
9
+ OnValueChangedContext,
10
+ OnValueChangedContextProvider,
11
+ OnValueChangedTriggerContext,
12
+ OnValueChangedTriggerContextProvider
13
+ };
package/dist/index.js ADDED
@@ -0,0 +1,192 @@
1
+ import {
2
+ actionTypes,
3
+ reducer,
4
+ toast,
5
+ useToast
6
+ } from "./chunk-VVFZ7HTT.js";
7
+ import {
8
+ OnValueChangedContext,
9
+ OnValueChangedContextProvider,
10
+ OnValueChangedTriggerContext,
11
+ OnValueChangedTriggerContextProvider
12
+ } from "./chunk-BHNC4MM6.js";
13
+ import {
14
+ useRenderCount_default,
15
+ useRenderHookCount_default,
16
+ useStateTracker,
17
+ useTrackedState
18
+ } from "./chunk-XIISC67B.js";
19
+ import {
20
+ ChangeTrackingContext,
21
+ ChangeTrackingProvider,
22
+ useChangeTracking,
23
+ useDelayedState,
24
+ useDelayedValue,
25
+ useForceUpdate,
26
+ usePartialState,
27
+ useQueuedState
28
+ } from "./chunk-RX4FIJI6.js";
29
+ import {
30
+ useHideOnScroll,
31
+ useHidingOnScroll,
32
+ useScrollDirection,
33
+ useScrollPosition,
34
+ useScrollRestoration
35
+ } from "./chunk-GKXIKVGH.js";
36
+ import {
37
+ IndexedDBService,
38
+ LocalStorageService,
39
+ useIndexedDB,
40
+ useLocalStorage,
41
+ useLocalStorageState,
42
+ useQueuedLocalStorage,
43
+ useQueuedLocalStorageState,
44
+ useSessionStorage,
45
+ useSessionStorageState
46
+ } from "./chunk-EZXGO56T.js";
47
+ import {
48
+ setReducedMotionPreference,
49
+ useDevice,
50
+ useDimensions,
51
+ useIsMobile,
52
+ useMobile,
53
+ useReducedMotion,
54
+ useTheme,
55
+ useThemeDetector,
56
+ useWindowDimensions
57
+ } from "./chunk-AIS4L5ID.js";
58
+ import {
59
+ useMutationQueue,
60
+ useNetworkStatus,
61
+ useOnline
62
+ } from "./chunk-IFSEJVAF.js";
63
+ import {
64
+ IndexDbProvider,
65
+ LocalStorageProvider,
66
+ indexDbContext,
67
+ localStorageContext,
68
+ useLocalStorageService
69
+ } from "./chunk-OF7JZIAL.js";
70
+ import {
71
+ useAfterMountedEffect,
72
+ useMounted,
73
+ useOnScreen
74
+ } from "./chunk-EVE7TM7O.js";
75
+ import {
76
+ useDownload,
77
+ useDownloadApiFile,
78
+ useFileDialog
79
+ } from "./chunk-BKB34ZQJ.js";
80
+ import {
81
+ dateReviver,
82
+ downloadFile,
83
+ getFile,
84
+ getFilePost,
85
+ hasCameraAsync,
86
+ isDev,
87
+ isDevEnvironment,
88
+ isIphone,
89
+ isPwaLaunched,
90
+ isUsable,
91
+ parseWithDate,
92
+ withComponentMemo
93
+ } from "./chunk-MMIPLCL4.js";
94
+ import "./chunk-QGM4M3NI.js";
95
+
96
+ // src/index.ts
97
+ import {
98
+ CommentProvider,
99
+ useComments
100
+ } from "@pol-studios/features";
101
+ import { useOrderManager } from "@pol-studios/features";
102
+ import {
103
+ genId,
104
+ getDefaultCondition,
105
+ getDefaultValue,
106
+ getComparisonOptions,
107
+ FilterProvider,
108
+ useFilterContext
109
+ } from "@pol-studios/features";
110
+ export {
111
+ ChangeTrackingContext,
112
+ ChangeTrackingProvider,
113
+ CommentProvider as CommentSectionProvider,
114
+ FilterProvider,
115
+ IndexedDBService as IndexDb,
116
+ IndexDbProvider,
117
+ IndexedDBService,
118
+ LocalStorageProvider,
119
+ LocalStorageService,
120
+ OnValueChangedContext,
121
+ OnValueChangedContextProvider,
122
+ OnValueChangedTriggerContext,
123
+ OnValueChangedTriggerContextProvider,
124
+ actionTypes,
125
+ dateReviver,
126
+ downloadFile,
127
+ genId,
128
+ getComparisonOptions,
129
+ getDefaultCondition,
130
+ getDefaultValue,
131
+ getFile,
132
+ getFilePost,
133
+ hasCameraAsync,
134
+ indexDbContext,
135
+ isDev,
136
+ isDevEnvironment,
137
+ isIphone,
138
+ isPwaLaunched,
139
+ isUsable,
140
+ localStorageContext,
141
+ parseWithDate,
142
+ reducer,
143
+ setReducedMotionPreference,
144
+ toast,
145
+ useAfterMountedEffect as useAfterMount,
146
+ useAfterMountedEffect,
147
+ useChangeTracking,
148
+ useComments as useCommentSection,
149
+ useDelayedState,
150
+ useDelayedValue,
151
+ useDevice,
152
+ useDimensions,
153
+ useDownload,
154
+ useDownloadApiFile,
155
+ useFileDialog,
156
+ useFilterContext,
157
+ useForceUpdate,
158
+ useHideOnScroll,
159
+ useHidingOnScroll,
160
+ useIndexedDB as useIndexDbService,
161
+ useIndexedDB,
162
+ useIsMobile,
163
+ useLocalStorage,
164
+ useLocalStorageService,
165
+ useLocalStorageState,
166
+ useMobile,
167
+ useMounted,
168
+ useMutationQueue,
169
+ useNetworkStatus,
170
+ useOnScreen,
171
+ useOnline,
172
+ useOrderManager as useOrderHintOld,
173
+ usePartialState,
174
+ useQueuedLocalStorage,
175
+ useQueuedLocalStorageState,
176
+ useQueuedState,
177
+ useReducedMotion,
178
+ useRenderCount_default as useRenderCount,
179
+ useRenderHookCount_default as useRenderHookCount,
180
+ useScrollDirection,
181
+ useScrollPosition,
182
+ useScrollRestoration,
183
+ useSessionStorage,
184
+ useSessionStorageState,
185
+ useStateTracker,
186
+ useTheme,
187
+ useThemeDetector,
188
+ useToast,
189
+ useTrackedState,
190
+ useWindowDimensions,
191
+ withComponentMemo
192
+ };
@@ -0,0 +1,12 @@
1
+ import {
2
+ useAfterMountedEffect,
3
+ useMounted,
4
+ useOnScreen
5
+ } from "../chunk-EVE7TM7O.js";
6
+ import "../chunk-QGM4M3NI.js";
7
+ export {
8
+ useAfterMountedEffect as useAfterMount,
9
+ useAfterMountedEffect,
10
+ useMounted,
11
+ useOnScreen
12
+ };
@@ -0,0 +1,12 @@
1
+ import {
2
+ useMutationQueue,
3
+ useNetworkStatus,
4
+ useOnline
5
+ } from "../chunk-IFSEJVAF.js";
6
+ import "../chunk-OF7JZIAL.js";
7
+ import "../chunk-QGM4M3NI.js";
8
+ export {
9
+ useMutationQueue,
10
+ useNetworkStatus,
11
+ useOnline
12
+ };