@alessiofrittoli/react-hooks 4.1.1 → 4.2.1

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,266 @@
1
+ import { Q as Queue, U as UUID, N as NewQueue, a as QueuedItemType, b as QueuedItemsType, O as OptionalQueuedItem, c as OptionalQueuedItems } from '../../types-Cx6f9FS1.mjs';
2
+ export { d as QueueItem, e as QueueItems, f as QueuedItem, g as QueuedItems } from '../../types-Cx6f9FS1.mjs';
3
+ import '@alessiofrittoli/math-utils';
4
+
5
+ type JumpToOptions<T extends Queue = Queue> = {
6
+ /**
7
+ * The item UUID where to jump to.
8
+ *
9
+ * If not given, queue will jump to first item.
10
+ */
11
+ uuid?: UUID;
12
+ /**
13
+ * A new queue to set.
14
+ *
15
+ */
16
+ queue?: NewQueue<T>;
17
+ };
18
+ /**
19
+ * Jump to a specific item.
20
+ *
21
+ * @param options An object defining an optional item UUID where to jump to and an optional new queue to set. See {@link JumpToOptions}.
22
+ *
23
+ * @returns The item being set as active if any. It could be included in the main queue, custom queue or the new given queue.
24
+ */
25
+ type JumpToHandler<T extends Queue = Queue> = (options: JumpToOptions<T>) => QueuedItemType<T> | undefined;
26
+ /**
27
+ * Defines configuration options for the `useQueue` hook.
28
+ *
29
+ */
30
+ interface UseQueueOptions<T extends Queue = Queue> {
31
+ /**
32
+ * The queue.
33
+ *
34
+ */
35
+ queue: T;
36
+ /**
37
+ * Defines the initial active item in the queue.
38
+ *
39
+ */
40
+ current?: QueuedItemType<T>;
41
+ /**
42
+ * Indicates whether repeatition of the given queue is initially active.
43
+ *
44
+ * @default true
45
+ */
46
+ repeat?: boolean;
47
+ }
48
+ /**
49
+ * Defines `useQueue` hook API.
50
+ *
51
+ */
52
+ interface UseQueue<T extends Queue = Queue> {
53
+ /**
54
+ * The main queue.
55
+ *
56
+ */
57
+ queue: T;
58
+ /**
59
+ * The current active item.
60
+ *
61
+ * It could be included in either the main `queue` or custom queue.
62
+ */
63
+ current?: QueuedItemType<T>;
64
+ /**
65
+ * Defines the `current` item cursor ID.
66
+ *
67
+ */
68
+ currentId?: QueuedItemType<T>['uuid'];
69
+ /**
70
+ * Defines an array of queued items in the custom queue.
71
+ *
72
+ */
73
+ customQueue: QueuedItemsType<T>;
74
+ /**
75
+ * Defines the complete queue (main queue + custom queue).
76
+ *
77
+ */
78
+ effectiveQueue: QueuedItemsType<T>;
79
+ /**
80
+ * Upcoming main queue items.
81
+ *
82
+ */
83
+ nextFromQueue: QueuedItemsType<T>;
84
+ /**
85
+ * Indicates whether the current item has a previous item.
86
+ *
87
+ * - `true` if main queue has more than 0 items.
88
+ * - `true` if `repeat` is enabled.
89
+ * - `true` if current item index in the main queue is greather than 0.
90
+ * - `false` otherwise.
91
+ */
92
+ hasPrevious: boolean;
93
+ /**
94
+ * Indicates whether the current item has a next item.
95
+ *
96
+ * - `true` if custom queue has more than 0 items.
97
+ * - `true` if main queue has more than 0 items.
98
+ * - `true` if `repeat` is enabled.
99
+ * - `true` if current item index in the main queue is less than the main queue length.
100
+ * - `false` otherwise.
101
+ */
102
+ hasNext: boolean;
103
+ /**
104
+ * Indicates whether shuffle is currently enabled.
105
+ *
106
+ */
107
+ isShuffleEnabled: boolean;
108
+ /**
109
+ * Shuffle main queue.
110
+ *
111
+ */
112
+ shuffle: VoidFunction;
113
+ /**
114
+ * Un-shuffle main queue.
115
+ *
116
+ */
117
+ unshuffle: VoidFunction;
118
+ /**
119
+ * Toggle shuffle for the main queue.
120
+ *
121
+ */
122
+ toggleShuffle: VoidFunction;
123
+ /**
124
+ * Indicates whether `repeat` is enabled or not.
125
+ *
126
+ */
127
+ isRepeatEnabled: boolean;
128
+ /**
129
+ * Toggle `repeat` on/off.
130
+ *
131
+ */
132
+ toggleRepeat: VoidFunction;
133
+ /**
134
+ * Jump to a specific item.
135
+ *
136
+ * @param options An object defining an optional item UUID where to jump to and an optional new queue to set. See {@link JumpToOptions}.
137
+ *
138
+ * @returns The item being set as active if any. It could be included in the main queue, custom queue or the new given queue.
139
+ */
140
+ jumpTo: JumpToHandler<T>;
141
+ /**
142
+ * Get previous item.
143
+ *
144
+ * @returns The previous item, `undefined` if none has been found.
145
+ */
146
+ getPrevious: () => QueuedItemType<T> | undefined;
147
+ /**
148
+ * Jump to previous item.
149
+ *
150
+ * @returns The previous item being set as active, `undefined` if none has been found.
151
+ */
152
+ previous: () => QueuedItemType<T> | undefined;
153
+ /**
154
+ * Get next item.
155
+ *
156
+ * @returns The next item, `undefined` if none has been found.
157
+ */
158
+ getNext: () => QueuedItemType<T> | undefined;
159
+ /**
160
+ * Jump to next item.
161
+ *
162
+ * @returns The next item being set as active, `undefined` if none has been found.
163
+ */
164
+ next: () => QueuedItemType<T> | undefined;
165
+ /**
166
+ * Completely overwrite the main queue and optionally generate a new UUID for each queue item.
167
+ *
168
+ * @param queue The new queue.
169
+ */
170
+ setQueue: (queue: NewQueue<T>) => T;
171
+ /**
172
+ * Add a new item the custom queue.
173
+ *
174
+ * A new UUID is automatically generated for each item.
175
+ *
176
+ * @param item The item or an array of items to add in the queue.
177
+ */
178
+ addToQueue: (item: OptionalQueuedItem<QueuedItemType<T>> | OptionalQueuedItems<QueuedItemType<T>>) => void;
179
+ /**
180
+ * Remove item from the queues.
181
+ *
182
+ * @param uuid The item UUID to remove from the queue.
183
+ */
184
+ removeFromQueue: (uuid: UUID) => void;
185
+ /**
186
+ * Wipe custom queue.
187
+ *
188
+ */
189
+ clearQueue: VoidFunction;
190
+ }
191
+ /**
192
+ * Manage a queue of items with support for shuffling, repeating, and custom queue items.
193
+ *
194
+ * @template T The type of the queue, defaults to `Queue`.
195
+ * @param options Configuration options for the `useQueue` hook. See {@link UseQueueOptions} for more info.
196
+ *
197
+ * @returns An object containing state and utilities. See {@link UseQueue} for more info.
198
+ */
199
+ declare const useQueue: <T extends Queue = Queue>(options: UseQueueOptions<T>) => UseQueue<T>;
200
+
201
+ /**
202
+ * Defines `useShuffle` hook API.
203
+ *
204
+ */
205
+ interface UseShuffle {
206
+ /**
207
+ * Indicates whether shuffle is currently enabled.
208
+ *
209
+ */
210
+ enabled: boolean;
211
+ /**
212
+ * Shuffle given `queue` items.
213
+ *
214
+ * @param queue The Queue.
215
+ * @param uuid The current queue item UUID. This is used as entry point in order to shuffle upcoming items.
216
+ *
217
+ * @returns The given `queue` with shuffled items.
218
+ */
219
+ shuffle: <T extends Queue>(queue: T, uuid?: UUID) => T;
220
+ /**
221
+ * Un-shuffle given `queue` items.
222
+ *
223
+ * @param queue The Queue.
224
+ * @param uuid The current queue item UUID. This is used as entry point in order to restore upcoming items.
225
+ *
226
+ * @returns The given `queue` with restored items order.
227
+ */
228
+ unshuffle: <T extends Queue>(queue: T, uuid?: UUID) => T;
229
+ /**
230
+ * Shuffle/un-shuffle given `queue` items.
231
+ *
232
+ * @param queue The Queue.
233
+ * @param cursor The current queue item UUID. This is used as entry point in order to shuffle/restore upcoming items.
234
+ *
235
+ * @returns The given `queue` with shuffled items or the given `queue` with restored items order.
236
+ */
237
+ toggleShuffle: <T extends Queue>(queue: T, cursor?: UUID) => T;
238
+ }
239
+ /**
240
+ * Handle shuffle functionality for queues.
241
+ *
242
+ * This hook manages the shuffle state and provides methods to shuffle, unshuffle,
243
+ * and toggle shuffle for a queue. When shuffling, it preserves the order
244
+ * of items up to the current cursor position and only shuffles upcoming items.
245
+ *
246
+ * @returns An object containing state and utilities. See {@link UseShuffle} for more info.
247
+ *
248
+ * @example
249
+ * ```tsx
250
+ * const {
251
+ * enabled, shuffle, unshuffle, toggleShuffle
252
+ * } = useShuffle()
253
+ *
254
+ * // Shuffle the queue
255
+ * const shuffledQueue = shuffle( currentQueue, currentUUID )
256
+ *
257
+ * // Restore original order
258
+ * const restoredQueue = unshuffle( currentQueue, currentUUID )
259
+ *
260
+ * // Toggle shuffle state
261
+ * const updatedQueue = toggleShuffle( currentQueue, currentUUID )
262
+ * ```
263
+ */
264
+ declare const useShuffle: () => UseShuffle;
265
+
266
+ export { type JumpToHandler, type JumpToOptions, NewQueue, OptionalQueuedItem, OptionalQueuedItems, Queue, QueuedItemType, QueuedItemsType, UUID, type UseQueue, type UseQueueOptions, type UseShuffle, useQueue, useShuffle };
@@ -0,0 +1,266 @@
1
+ import { Q as Queue, U as UUID, N as NewQueue, a as QueuedItemType, b as QueuedItemsType, O as OptionalQueuedItem, c as OptionalQueuedItems } from '../../types-Cx6f9FS1.js';
2
+ export { d as QueueItem, e as QueueItems, f as QueuedItem, g as QueuedItems } from '../../types-Cx6f9FS1.js';
3
+ import '@alessiofrittoli/math-utils';
4
+
5
+ type JumpToOptions<T extends Queue = Queue> = {
6
+ /**
7
+ * The item UUID where to jump to.
8
+ *
9
+ * If not given, queue will jump to first item.
10
+ */
11
+ uuid?: UUID;
12
+ /**
13
+ * A new queue to set.
14
+ *
15
+ */
16
+ queue?: NewQueue<T>;
17
+ };
18
+ /**
19
+ * Jump to a specific item.
20
+ *
21
+ * @param options An object defining an optional item UUID where to jump to and an optional new queue to set. See {@link JumpToOptions}.
22
+ *
23
+ * @returns The item being set as active if any. It could be included in the main queue, custom queue or the new given queue.
24
+ */
25
+ type JumpToHandler<T extends Queue = Queue> = (options: JumpToOptions<T>) => QueuedItemType<T> | undefined;
26
+ /**
27
+ * Defines configuration options for the `useQueue` hook.
28
+ *
29
+ */
30
+ interface UseQueueOptions<T extends Queue = Queue> {
31
+ /**
32
+ * The queue.
33
+ *
34
+ */
35
+ queue: T;
36
+ /**
37
+ * Defines the initial active item in the queue.
38
+ *
39
+ */
40
+ current?: QueuedItemType<T>;
41
+ /**
42
+ * Indicates whether repeatition of the given queue is initially active.
43
+ *
44
+ * @default true
45
+ */
46
+ repeat?: boolean;
47
+ }
48
+ /**
49
+ * Defines `useQueue` hook API.
50
+ *
51
+ */
52
+ interface UseQueue<T extends Queue = Queue> {
53
+ /**
54
+ * The main queue.
55
+ *
56
+ */
57
+ queue: T;
58
+ /**
59
+ * The current active item.
60
+ *
61
+ * It could be included in either the main `queue` or custom queue.
62
+ */
63
+ current?: QueuedItemType<T>;
64
+ /**
65
+ * Defines the `current` item cursor ID.
66
+ *
67
+ */
68
+ currentId?: QueuedItemType<T>['uuid'];
69
+ /**
70
+ * Defines an array of queued items in the custom queue.
71
+ *
72
+ */
73
+ customQueue: QueuedItemsType<T>;
74
+ /**
75
+ * Defines the complete queue (main queue + custom queue).
76
+ *
77
+ */
78
+ effectiveQueue: QueuedItemsType<T>;
79
+ /**
80
+ * Upcoming main queue items.
81
+ *
82
+ */
83
+ nextFromQueue: QueuedItemsType<T>;
84
+ /**
85
+ * Indicates whether the current item has a previous item.
86
+ *
87
+ * - `true` if main queue has more than 0 items.
88
+ * - `true` if `repeat` is enabled.
89
+ * - `true` if current item index in the main queue is greather than 0.
90
+ * - `false` otherwise.
91
+ */
92
+ hasPrevious: boolean;
93
+ /**
94
+ * Indicates whether the current item has a next item.
95
+ *
96
+ * - `true` if custom queue has more than 0 items.
97
+ * - `true` if main queue has more than 0 items.
98
+ * - `true` if `repeat` is enabled.
99
+ * - `true` if current item index in the main queue is less than the main queue length.
100
+ * - `false` otherwise.
101
+ */
102
+ hasNext: boolean;
103
+ /**
104
+ * Indicates whether shuffle is currently enabled.
105
+ *
106
+ */
107
+ isShuffleEnabled: boolean;
108
+ /**
109
+ * Shuffle main queue.
110
+ *
111
+ */
112
+ shuffle: VoidFunction;
113
+ /**
114
+ * Un-shuffle main queue.
115
+ *
116
+ */
117
+ unshuffle: VoidFunction;
118
+ /**
119
+ * Toggle shuffle for the main queue.
120
+ *
121
+ */
122
+ toggleShuffle: VoidFunction;
123
+ /**
124
+ * Indicates whether `repeat` is enabled or not.
125
+ *
126
+ */
127
+ isRepeatEnabled: boolean;
128
+ /**
129
+ * Toggle `repeat` on/off.
130
+ *
131
+ */
132
+ toggleRepeat: VoidFunction;
133
+ /**
134
+ * Jump to a specific item.
135
+ *
136
+ * @param options An object defining an optional item UUID where to jump to and an optional new queue to set. See {@link JumpToOptions}.
137
+ *
138
+ * @returns The item being set as active if any. It could be included in the main queue, custom queue or the new given queue.
139
+ */
140
+ jumpTo: JumpToHandler<T>;
141
+ /**
142
+ * Get previous item.
143
+ *
144
+ * @returns The previous item, `undefined` if none has been found.
145
+ */
146
+ getPrevious: () => QueuedItemType<T> | undefined;
147
+ /**
148
+ * Jump to previous item.
149
+ *
150
+ * @returns The previous item being set as active, `undefined` if none has been found.
151
+ */
152
+ previous: () => QueuedItemType<T> | undefined;
153
+ /**
154
+ * Get next item.
155
+ *
156
+ * @returns The next item, `undefined` if none has been found.
157
+ */
158
+ getNext: () => QueuedItemType<T> | undefined;
159
+ /**
160
+ * Jump to next item.
161
+ *
162
+ * @returns The next item being set as active, `undefined` if none has been found.
163
+ */
164
+ next: () => QueuedItemType<T> | undefined;
165
+ /**
166
+ * Completely overwrite the main queue and optionally generate a new UUID for each queue item.
167
+ *
168
+ * @param queue The new queue.
169
+ */
170
+ setQueue: (queue: NewQueue<T>) => T;
171
+ /**
172
+ * Add a new item the custom queue.
173
+ *
174
+ * A new UUID is automatically generated for each item.
175
+ *
176
+ * @param item The item or an array of items to add in the queue.
177
+ */
178
+ addToQueue: (item: OptionalQueuedItem<QueuedItemType<T>> | OptionalQueuedItems<QueuedItemType<T>>) => void;
179
+ /**
180
+ * Remove item from the queues.
181
+ *
182
+ * @param uuid The item UUID to remove from the queue.
183
+ */
184
+ removeFromQueue: (uuid: UUID) => void;
185
+ /**
186
+ * Wipe custom queue.
187
+ *
188
+ */
189
+ clearQueue: VoidFunction;
190
+ }
191
+ /**
192
+ * Manage a queue of items with support for shuffling, repeating, and custom queue items.
193
+ *
194
+ * @template T The type of the queue, defaults to `Queue`.
195
+ * @param options Configuration options for the `useQueue` hook. See {@link UseQueueOptions} for more info.
196
+ *
197
+ * @returns An object containing state and utilities. See {@link UseQueue} for more info.
198
+ */
199
+ declare const useQueue: <T extends Queue = Queue>(options: UseQueueOptions<T>) => UseQueue<T>;
200
+
201
+ /**
202
+ * Defines `useShuffle` hook API.
203
+ *
204
+ */
205
+ interface UseShuffle {
206
+ /**
207
+ * Indicates whether shuffle is currently enabled.
208
+ *
209
+ */
210
+ enabled: boolean;
211
+ /**
212
+ * Shuffle given `queue` items.
213
+ *
214
+ * @param queue The Queue.
215
+ * @param uuid The current queue item UUID. This is used as entry point in order to shuffle upcoming items.
216
+ *
217
+ * @returns The given `queue` with shuffled items.
218
+ */
219
+ shuffle: <T extends Queue>(queue: T, uuid?: UUID) => T;
220
+ /**
221
+ * Un-shuffle given `queue` items.
222
+ *
223
+ * @param queue The Queue.
224
+ * @param uuid The current queue item UUID. This is used as entry point in order to restore upcoming items.
225
+ *
226
+ * @returns The given `queue` with restored items order.
227
+ */
228
+ unshuffle: <T extends Queue>(queue: T, uuid?: UUID) => T;
229
+ /**
230
+ * Shuffle/un-shuffle given `queue` items.
231
+ *
232
+ * @param queue The Queue.
233
+ * @param cursor The current queue item UUID. This is used as entry point in order to shuffle/restore upcoming items.
234
+ *
235
+ * @returns The given `queue` with shuffled items or the given `queue` with restored items order.
236
+ */
237
+ toggleShuffle: <T extends Queue>(queue: T, cursor?: UUID) => T;
238
+ }
239
+ /**
240
+ * Handle shuffle functionality for queues.
241
+ *
242
+ * This hook manages the shuffle state and provides methods to shuffle, unshuffle,
243
+ * and toggle shuffle for a queue. When shuffling, it preserves the order
244
+ * of items up to the current cursor position and only shuffles upcoming items.
245
+ *
246
+ * @returns An object containing state and utilities. See {@link UseShuffle} for more info.
247
+ *
248
+ * @example
249
+ * ```tsx
250
+ * const {
251
+ * enabled, shuffle, unshuffle, toggleShuffle
252
+ * } = useShuffle()
253
+ *
254
+ * // Shuffle the queue
255
+ * const shuffledQueue = shuffle( currentQueue, currentUUID )
256
+ *
257
+ * // Restore original order
258
+ * const restoredQueue = unshuffle( currentQueue, currentUUID )
259
+ *
260
+ * // Toggle shuffle state
261
+ * const updatedQueue = toggleShuffle( currentQueue, currentUUID )
262
+ * ```
263
+ */
264
+ declare const useShuffle: () => UseShuffle;
265
+
266
+ export { type JumpToHandler, type JumpToOptions, NewQueue, OptionalQueuedItem, OptionalQueuedItems, Queue, QueuedItemType, QueuedItemsType, UUID, type UseQueue, type UseQueueOptions, type UseShuffle, useQueue, useShuffle };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkSV2TCLLEjs = require('../../chunk-SV2TCLLE.js');var _react = require('react');var _webutils = require('@alessiofrittoli/web-utils');var M=()=>{let[F,v]=_react.useState.call(void 0, !1),[I,x]=_react.useState.call(void 0, ),h=_react.useRef.call(void 0, !1),U=_react.useCallback.call(void 0, (s,a)=>{let{items:l}=s;if(l.length<=1)return s;v(!0),h.current||x(l.map(({uuid:p})=>p)),h.current=!0;let f=_chunkSV2TCLLEjs.e.call(void 0, l,a),c=l.slice(0,f+1),n=l.slice(f+1);return n.length<=1?s:{...s,items:[...c,..._webutils.shuffle.call(void 0, n)]}},[]),g=_react.useCallback.call(void 0, (s,a)=>{if(!I)return s;v(!1);let{items:l}=s,f=new Map(I.map((m,u)=>[m,u]));h.current=!1;let c=_chunkSV2TCLLEjs.e.call(void 0, l,a),n=l.slice(0,c+1),b=[...l.slice(c+1)].sort((m,u)=>(_nullishCoalesce(f.get(m.uuid), () => (0)))-(_nullishCoalesce(f.get(u.uuid), () => (0))));return x(void 0),{...s,items:[...n,...b]}},[I]),S=_react.useCallback.call(void 0, (s,a)=>h.current?g(s,a):U(s,a),[U,g]);return{enabled:F,shuffle:U,unshuffle:g,toggleShuffle:S}};var pe=F=>{let{queue:v,repeat:I=!0,current:x}=F,{enabled:h,shuffle:U,unshuffle:g,toggleShuffle:S}=M(),[s,a]=_react.useState.call(void 0, I),l=_react.useCallback.call(void 0, ()=>a(e=>!e),[]),[f,c]=_react.useState.call(void 0, v),[n,p]=_react.useState.call(void 0, []),[b,m]=_react.useState.call(void 0, x),[u,N]=_react.useState.call(void 0, _optionalChain([x, 'optionalAccess', _2 => _2.uuid])),t=f.items,T=_optionalChain([b, 'optionalAccess', _3 => _3.uuid]),A=_react.useMemo.call(void 0, ()=>_chunkSV2TCLLEjs.f.call(void 0, {queue:t,customQueue:n,uuid:T}),[t,n,T]),B=_react.useMemo.call(void 0, ()=>t.length<=0?!1:s?!0:u?_chunkSV2TCLLEjs.e.call(void 0, t,u)>0:t.length>0,[t,u,s]),z=_react.useMemo.call(void 0, ()=>n.length>0?!0:t.length<=0?!1:s?!0:u?_chunkSV2TCLLEjs.e.call(void 0, t,u)<t.length-1:t.length>0,[t,n,u,s]),O=_react.useCallback.call(void 0, e=>{let r={...e,items:_chunkSV2TCLLEjs.d.call(void 0, e.items)};return c(r),r},[]),G=_react.useCallback.call(void 0, e=>p(r=>[...r,..._chunkSV2TCLLEjs.b.call(void 0, e)]),[]),K=_react.useCallback.call(void 0, e=>{let r=o=>o.uuid===e,Q=o=>o.uuid!==e;c(o=>o.items.find(r)?{...o,items:o.items.filter(Q)}:o),p(o=>o.find(r)?o.filter(Q):o)},[]),L=_react.useCallback.call(void 0, ()=>p([]),[]),W=_react.useCallback.call(void 0, ({uuid:e,queue:r})=>{let Q=r?O(r).items:t;if(n.length>0){let w=_chunkSV2TCLLEjs.e.call(void 0, n,e);if(w>=0){let H=n.at(w);return m(H),p(n.slice(w+1)),H}}let o=e?_chunkSV2TCLLEjs.e.call(void 0, Q,e):0,E=Q[o===-1?0:o];return m(E),N(_optionalChain([E, 'optionalAccess', _4 => _4.uuid])),E},[t,n,O]),P=_react.useCallback.call(void 0, ()=>{if(!T)return t.at(-1);let e=!!t.find(o=>o.uuid===T),r=_chunkSV2TCLLEjs.e.call(void 0, t,e?T:u);return t.at(e?_webutils.getPreviousIndex.call(void 0, t.length,r):r)},[T,u,t]),X=_react.useCallback.call(void 0, ()=>{let e=P();return m(e),N(_optionalChain([e, 'optionalAccess', _5 => _5.uuid])),e},[P]),R=_react.useCallback.call(void 0, ()=>{if(n.length>0)return n.at(0);let e=_chunkSV2TCLLEjs.e.call(void 0, f.items,u);return f.items.at(_webutils.getNextIndex.call(void 0, f.items.length,e))},[f,n,u]),Y=_react.useCallback.call(void 0, ()=>{if(n.length>0){let[r,...Q]=n;return m(r),p(Q),r}let e=R();return m(e),N(_optionalChain([e, 'optionalAccess', _6 => _6.uuid])),e},[n,R]),Z=_react.useCallback.call(void 0, ()=>c(e=>U(e,u)),[u,U]),_=_react.useCallback.call(void 0, ()=>c(e=>g(e,u)),[u,g]),$=_react.useCallback.call(void 0, ()=>c(e=>S(e,u)),[u,S]),ee=_react.useMemo.call(void 0, ()=>u?t.slice(_chunkSV2TCLLEjs.e.call(void 0, t,u)+1):t,[t,u]);return{queue:f,currentId:T,current:b,customQueue:n,effectiveQueue:A,nextFromQueue:ee,hasPrevious:B,hasNext:z,isShuffleEnabled:h,shuffle:Z,unshuffle:_,toggleShuffle:$,isRepeatEnabled:s,toggleRepeat:l,jumpTo:W,getPrevious:P,previous:X,getNext:R,next:Y,setQueue:O,addToQueue:G,removeFromQueue:K,clearQueue:L}};exports.useQueue = pe; exports.useShuffle = M;
@@ -0,0 +1 @@
1
+ import{b as V,d as J,e as d,f as j}from"../../chunk-OXQJOVCZ.mjs";import{useCallback as i,useMemo as D,useState as y}from"react";import{getNextIndex as se,getPreviousIndex as re}from"@alessiofrittoli/web-utils";import{useCallback as C,useRef as te,useState as k}from"react";import{shuffle as ne}from"@alessiofrittoli/web-utils";var A=()=>{let[F,v]=k(!1),[I,x]=k(),h=te(!1),U=C((s,a)=>{let{items:l}=s;if(l.length<=1)return s;v(!0),h.current||x(l.map(({uuid:p})=>p)),h.current=!0;let f=d(l,a),c=l.slice(0,f+1),n=l.slice(f+1);return n.length<=1?s:{...s,items:[...c,...ne(n)]}},[]),g=C((s,a)=>{if(!I)return s;v(!1);let{items:l}=s,f=new Map(I.map((m,u)=>[m,u]));h.current=!1;let c=d(l,a),n=l.slice(0,c+1),b=[...l.slice(c+1)].sort((m,u)=>(f.get(m.uuid)??0)-(f.get(u.uuid)??0));return x(void 0),{...s,items:[...n,...b]}},[I]),S=C((s,a)=>h.current?g(s,a):U(s,a),[U,g]);return{enabled:F,shuffle:U,unshuffle:g,toggleShuffle:S}};var Qe=F=>{let{queue:v,repeat:I=!0,current:x}=F,{enabled:h,shuffle:U,unshuffle:g,toggleShuffle:S}=A(),[s,a]=y(I),l=i(()=>a(e=>!e),[]),[f,c]=y(v),[n,p]=y([]),[b,m]=y(x),[u,N]=y(x?.uuid),t=f.items,T=b?.uuid,B=D(()=>j({queue:t,customQueue:n,uuid:T}),[t,n,T]),z=D(()=>t.length<=0?!1:s?!0:u?d(t,u)>0:t.length>0,[t,u,s]),G=D(()=>n.length>0?!0:t.length<=0?!1:s?!0:u?d(t,u)<t.length-1:t.length>0,[t,n,u,s]),O=i(e=>{let r={...e,items:J(e.items)};return c(r),r},[]),K=i(e=>p(r=>[...r,...V(e)]),[]),L=i(e=>{let r=o=>o.uuid===e,Q=o=>o.uuid!==e;c(o=>o.items.find(r)?{...o,items:o.items.filter(Q)}:o),p(o=>o.find(r)?o.filter(Q):o)},[]),W=i(()=>p([]),[]),X=i(({uuid:e,queue:r})=>{let Q=r?O(r).items:t;if(n.length>0){let w=d(n,e);if(w>=0){let H=n.at(w);return m(H),p(n.slice(w+1)),H}}let o=e?d(Q,e):0,E=Q[o===-1?0:o];return m(E),N(E?.uuid),E},[t,n,O]),P=i(()=>{if(!T)return t.at(-1);let e=!!t.find(o=>o.uuid===T),r=d(t,e?T:u);return t.at(e?re(t.length,r):r)},[T,u,t]),Y=i(()=>{let e=P();return m(e),N(e?.uuid),e},[P]),R=i(()=>{if(n.length>0)return n.at(0);let e=d(f.items,u);return f.items.at(se(f.items.length,e))},[f,n,u]),Z=i(()=>{if(n.length>0){let[r,...Q]=n;return m(r),p(Q),r}let e=R();return m(e),N(e?.uuid),e},[n,R]),_=i(()=>c(e=>U(e,u)),[u,U]),$=i(()=>c(e=>g(e,u)),[u,g]),ee=i(()=>c(e=>S(e,u)),[u,S]),ue=D(()=>u?t.slice(d(t,u)+1):t,[t,u]);return{queue:f,currentId:T,current:b,customQueue:n,effectiveQueue:B,nextFromQueue:ue,hasPrevious:z,hasNext:G,isShuffleEnabled:h,shuffle:_,unshuffle:$,toggleShuffle:ee,isRepeatEnabled:s,toggleRepeat:l,jumpTo:X,getPrevious:P,previous:Y,getNext:R,next:Z,setQueue:O,addToQueue:K,removeFromQueue:L,clearQueue:W}};export{Qe as useQueue,A as useShuffle};
@@ -0,0 +1,104 @@
1
+ import { g as QueuedItems, U as UUID, d as QueueItem, f as QueuedItem, e as QueueItems } from '../../types-Cx6f9FS1.mjs';
2
+ import '@alessiofrittoli/math-utils';
3
+
4
+ /**
5
+ * Adds a UUID to the given item.
6
+ *
7
+ * @template T The type of the given item, must extend object.
8
+ * @param item The item to add a UUID to.
9
+ * @returns A new item with the same properties as the input item plus a UUID.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const item = { foo: 'bar' }
14
+ * const newItem = addItemUUID( item )
15
+ * // Returns: { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' }
16
+ * ```
17
+ */
18
+ declare const addItemUUID: <T extends object = object>(item: QueueItem<T> | QueuedItem<T>) => QueuedItem<T>;
19
+ /**
20
+ * Adds a UUID to one or more items.
21
+ *
22
+ * If a single item is provided, it is normalized to an array and returned as
23
+ * a one-element list with generated UUIDs.
24
+ *
25
+ * @template T The type of the given item, must extend object.
26
+ * @param items A single item or an array of items where UUIDs are added to.
27
+ * @returns An array of items with a UUID on each item.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const result = addItemsUUID( [ { foo: 'bar' } ] )
32
+ * // Returns: [ { foo: 'bar', uuid: '...' } ]
33
+ * ```
34
+ */
35
+ declare const addItemsUUID: <T extends object = object>(items: QueueItem<T> | QueuedItem<T> | QueueItems<T> | QueuedItems<T>) => QueuedItems<T>;
36
+ /**
37
+ * Adds a UUID to the given item only when it does not already define one.
38
+ *
39
+ * @template T The type of the given item, must extend object.
40
+ * @param item The item to normalize with a UUID.
41
+ * @returns A new item with a guaranteed UUID.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const result = maybeAddItemUUID( { foo: 'bar' } )
46
+ * // Returns: { foo: 'bar', uuid: '...' }
47
+ * ```
48
+ */
49
+ declare const maybeAddItemUUID: <T extends object = object>(item: QueueItem<T> | QueuedItem<T>) => QueuedItem<T>;
50
+ /**
51
+ * Adds UUIDs to one or more items, preserving existing UUIDs when present.
52
+ *
53
+ * If a single item is provided, it is normalized to an array and returned as
54
+ * a one-element list.
55
+ *
56
+ * @template T The type of the given item, must extend object.
57
+ * @param items A single item or an array of items to normalize with UUIDs.
58
+ * @returns An array of items with UUIDs on each item.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const result = maybeAddItemsUUID( [ { foo: 'bar' }, { foo: 'baz', uuid: '1' } ] )
63
+ * // Returns: [ { foo: 'bar', uuid: '...' }, { foo: 'baz', uuid: '1' } ]
64
+ * ```
65
+ */
66
+ declare const maybeAddItemsUUID: <T extends object = object>(items: QueueItem<T> | QueuedItem<T> | (QueueItem<T> | QueuedItem<T>)[]) => QueuedItems<T>;
67
+ /**
68
+ * Finds the index of the item matching the given UUID.
69
+ *
70
+ * @param items The queue items to search in.
71
+ * @param uuid The UUID to match.
72
+ * @returns The matching index, or `-1` when the UUID is missing or not found.
73
+ */
74
+ declare const findIndexByUUID: (items: QueuedItems, uuid?: UUID) => number;
75
+ interface GetEffectiveQueueOptions<T extends object = object> {
76
+ /**
77
+ * The main queue.
78
+ *
79
+ */
80
+ queue: QueuedItems<T>;
81
+ /**
82
+ * The custom queue.
83
+ *
84
+ */
85
+ customQueue: QueuedItems<T>;
86
+ /**
87
+ * The UUID after which the custom items are inserted.
88
+ *
89
+ */
90
+ uuid?: UUID;
91
+ }
92
+ /**
93
+ * Returns the queue with `customQueue` inserted after the item identified by `uuid`.
94
+ *
95
+ * If `uuid` is not provided, the original queue is returned unchanged. If `uuid`
96
+ * is provided but not found, `customQueue` is appended to the end of `queue`.
97
+ *
98
+ * @template T The type of the queue items, must extend object.
99
+ * @param options An object defining required parameters. See {@link GetEffectiveQueueOptions}.
100
+ * @returns The effective queue.
101
+ */
102
+ declare const getEffectiveQueue: <T extends object = object>(options: GetEffectiveQueueOptions<T>) => QueuedItems<T>;
103
+
104
+ export { type GetEffectiveQueueOptions, addItemUUID, addItemsUUID, findIndexByUUID, getEffectiveQueue, maybeAddItemUUID, maybeAddItemsUUID };