@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.
package/README.md CHANGED
@@ -74,6 +74,11 @@
74
74
  - [`useIsFirstRender`](#useisfirstrender)
75
75
  - [`usePagination`](#usepagination)
76
76
  - [`useSelection`](#useselection)
77
+ - [Queue](#queue)
78
+ - [Queue Types](#queue-types)
79
+ - [Queue Utils](#queue-utils)
80
+ - [`useShuffle`](#useshuffle)
81
+ - [`useQueue`](#usequeue)
77
82
  - [Timers](#timers)
78
83
  - [`useDebounce`](#usedebounce)
79
84
  - [`useInterval`](#useinterval)
@@ -82,7 +87,7 @@
82
87
  - [`useTimeout`](#usetimeout)
83
88
  - [`useLightTimeout`](#uselighttimeout)
84
89
  - [Development](#development)
85
- - [Install depenendencies](#install-depenendencies)
90
+ - [Install dependencies](#install-dependencies)
86
91
  - [Build the source code](#build-the-source-code)
87
92
  - [ESLint](#eslint)
88
93
  - [Jest](#jest)
@@ -132,12 +137,14 @@ export default config;
132
137
 
133
138
  #### Updates in the latest release 🎉
134
139
 
135
- - Added `usePreventContextMenu` hook. See [API Reference](#usepreventcontextmenu) for more info.
140
+ - Added `useQueue` hook. See [API Reference](#usequeue) for more info.
141
+ - Added `useShuffle` hook. See [API Reference](#useshuffle) for more info.
136
142
 
137
143
  ---
138
144
 
139
- Old updates
145
+ Old major updates
140
146
 
147
+ - Added `usePreventContextMenu` hook. See [API Reference](#usepreventcontextmenu) for more info.
141
148
  - Improved `useConnection` hook. It now returns
142
149
  [`NetworkInformation`](https://github.com/alessiofrittoli/web-utils?tab=readme-ov-file#network-information) when available.
143
150
  See [API Reference](#useconnection) for more info.
@@ -2090,6 +2097,506 @@ const MyComponent: React.FC = () => {
2090
2097
 
2091
2098
  ---
2092
2099
 
2100
+ ##### Queue
2101
+
2102
+ ###### Queue Types
2103
+
2104
+ ###### `QueueItem<T>`
2105
+
2106
+ Queue item with an optional UUID.
2107
+
2108
+ <details>
2109
+
2110
+ <summary style="cursor:pointer">Type parameters</summary>
2111
+
2112
+ | Parameter | Type | Description |
2113
+ | --------- | --------------------------- | ----------------------------------------------- |
2114
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2115
+
2116
+ </details>
2117
+
2118
+ ---
2119
+
2120
+ ###### `QueueItems<T>`
2121
+
2122
+ An array of `QueueItem<T>`.
2123
+
2124
+ <details>
2125
+
2126
+ <summary style="cursor:pointer">Type parameters</summary>
2127
+
2128
+ | Parameter | Type | Description |
2129
+ | --------- | --------------------------- | ----------------------------------------------- |
2130
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2131
+
2132
+ </details>
2133
+
2134
+ ---
2135
+
2136
+ ###### `QueuedItem<T>`
2137
+
2138
+ Queue item with a required UUID.
2139
+
2140
+ <details>
2141
+
2142
+ <summary style="cursor:pointer">Type parameters</summary>
2143
+
2144
+ | Parameter | Type | Description |
2145
+ | --------- | --------------------------- | ----------------------------------------------- |
2146
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2147
+
2148
+ </details>
2149
+
2150
+ ---
2151
+
2152
+ ###### `QueuedItems<T>`
2153
+
2154
+ An array of `QueuedItem<T>`.
2155
+
2156
+ <details>
2157
+
2158
+ <summary style="cursor:pointer">Type parameters</summary>
2159
+
2160
+ | Parameter | Type | Description |
2161
+ | --------- | --------------------------- | ----------------------------------------------- |
2162
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2163
+
2164
+ </details>
2165
+
2166
+ ---
2167
+
2168
+ ###### `OptionalQueuedItem<T>`
2169
+
2170
+ Item shape accepted when enqueuing, with an optional UUID.
2171
+
2172
+ <details>
2173
+
2174
+ <summary style="cursor:pointer">Type parameters</summary>
2175
+
2176
+ | Parameter | Type | Description |
2177
+ | --------- | --------------------------- | ----------------------------------------------- |
2178
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2179
+
2180
+ </details>
2181
+
2182
+ ---
2183
+
2184
+ ###### `OptionalQueuedItems<T>`
2185
+
2186
+ List of items accepted when enqueuing.
2187
+
2188
+ <details>
2189
+
2190
+ <summary style="cursor:pointer">Type parameters</summary>
2191
+
2192
+ | Parameter | Type | Description |
2193
+ | --------- | --------------------------- | ----------------------------------------------- |
2194
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2195
+
2196
+ </details>
2197
+
2198
+ ---
2199
+
2200
+ ###### `Queue<T>`
2201
+
2202
+ Defines the queue interface.
2203
+
2204
+ <details>
2205
+
2206
+ <summary style="cursor:pointer">Type parameters</summary>
2207
+
2208
+ | Parameter | Type | Description |
2209
+ | --------- | --------------------------- | ----------------------------------------------------------- |
2210
+ | `T` | `T extends object = object` | The type of a single item in the queue, must extend object. |
2211
+
2212
+ </details>
2213
+
2214
+ ---
2215
+
2216
+ ###### `QueuedItemType<T>`
2217
+
2218
+ Extracts the queued item type from a queue.
2219
+
2220
+ <details>
2221
+
2222
+ <summary style="cursor:pointer">Type parameters</summary>
2223
+
2224
+ | Parameter | Type | Description |
2225
+ | --------- | ------------------------- | ------------------------------------------- |
2226
+ | `T` | `T extends Queue = Queue` | The type of the queue, must extend `Queue`. |
2227
+
2228
+ </details>
2229
+
2230
+ ---
2231
+
2232
+ ###### `QueuedItemsType<T>`
2233
+
2234
+ Extracts the queued items type from a queue.
2235
+
2236
+ <details>
2237
+
2238
+ <summary style="cursor:pointer">Type parameters</summary>
2239
+
2240
+ | Parameter | Type | Description |
2241
+ | --------- | ------------------------- | ------------------------------------------- |
2242
+ | `T` | `T extends Queue = Queue` | The type of the queue, must extend `Queue`. |
2243
+
2244
+ </details>
2245
+
2246
+ ---
2247
+
2248
+ ###### `NewQueue<T>`
2249
+
2250
+ Queue shape used when creating a new queue with optional item UUIDs.
2251
+
2252
+ <details>
2253
+
2254
+ <summary style="cursor:pointer">Type parameters</summary>
2255
+
2256
+ | Parameter | Type | Description |
2257
+ | --------- | ------------------------- | ------------------------------------------- |
2258
+ | `T` | `T extends Queue = Queue` | The type of the queue, must extend `Queue`. |
2259
+
2260
+ </details>
2261
+
2262
+ ---
2263
+
2264
+ ###### Queue Utils
2265
+
2266
+ ###### `addItemUUID`
2267
+
2268
+ Adds a UUID to the given item.
2269
+
2270
+ <details>
2271
+
2272
+ <summary style="cursor:pointer">Type parameters</summary>
2273
+
2274
+ | Parameter | Type | Description |
2275
+ | --------- | --------------------------- | ----------------------------------------------- |
2276
+ | `T` | `T extends object = object` | The type of the given item, must extend object. |
2277
+
2278
+ </details>
2279
+
2280
+ ---
2281
+
2282
+ <details>
2283
+
2284
+ <summary style="cursor:pointer">Parameters</summary>
2285
+
2286
+ | Parameter | Type | Description |
2287
+ | --------- | ----------------------------- | ---------------------------------------- |
2288
+ | `item` | `QueueItem<T>\|QueuedItem<T>` | The item to add a UUID to. |
2289
+ | | | - See [`QueueItem`](#queueitemt) type. |
2290
+ | | | - See [`QueuedItem`](#queueditemt) type. |
2291
+
2292
+ </details>
2293
+
2294
+ ---
2295
+
2296
+ <details>
2297
+
2298
+ <summary style="cursor:pointer">Returns</summary>
2299
+
2300
+ Type: `QueuedItem<T>`
2301
+
2302
+ A new `item` with the same properties as the input `item` plus a UUID.
2303
+
2304
+ </details>
2305
+
2306
+ ---
2307
+
2308
+ <details>
2309
+
2310
+ <summary style="cursor:pointer">Examples</summary>
2311
+
2312
+ ```ts
2313
+ import { addItemUUID } from "@alessiofrittoli/react-hooks/queue";
2314
+
2315
+ const item = { foo: "bar" };
2316
+ const newItem = addItemUUID(item);
2317
+ // Returns: { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' }
2318
+ ```
2319
+
2320
+ </details>
2321
+
2322
+ ---
2323
+
2324
+ ###### `addItemsUUID`
2325
+
2326
+ Adds a UUID to one or more items.
2327
+
2328
+ - Applies same API of [`addItemUUID`](#additemuuid) where `items` could be a single or an array of items.
2329
+
2330
+ <details>
2331
+
2332
+ <summary style="cursor:pointer">Examples</summary>
2333
+
2334
+ ```ts
2335
+ import { addItemsUUID } from "@alessiofrittoli/react-hooks/queue";
2336
+
2337
+ const item = { foo: "bar" };
2338
+ const newItem = addItemsUUID(item);
2339
+ // Returns: [ { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' } ]
2340
+
2341
+ const items = [{ foo: "bar" }, { baz: "qux" }];
2342
+ const newItems = addItemsUUID(items);
2343
+ // Returns: [
2344
+ // { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' },
2345
+ // { baz: 'qux', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' },
2346
+ // ]
2347
+ ```
2348
+
2349
+ </details>
2350
+
2351
+ ---
2352
+
2353
+ ###### `maybeAddItemUUID`
2354
+
2355
+ Adds a UUID to the given item only when it does not already define one.
2356
+
2357
+ - Applies same API of [`addItemUUID`](#additemuuid).
2358
+
2359
+ <details>
2360
+
2361
+ <summary style="cursor:pointer">Examples</summary>
2362
+
2363
+ ```ts
2364
+ import { maybeAddItemUUID } from "@alessiofrittoli/react-hooks/queue";
2365
+
2366
+ const item = { foo: "bar" };
2367
+ const newItem = maybeAddItemUUID(item);
2368
+ // Returns: [ { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' } ]
2369
+
2370
+ const item2 = { baz: "qux", uuid: "658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c" };
2371
+ const newItem2 = maybeAddItemUUID(item);
2372
+ // Returns: [ { baz: 'qux', uuid: '658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c' } ]
2373
+ ```
2374
+
2375
+ </details>
2376
+
2377
+ ---
2378
+
2379
+ ###### `maybeAddItemsUUID`
2380
+
2381
+ Adds UUIDs to one or more items, preserving existing UUIDs when present.
2382
+
2383
+ - Applies same API of [`maybeAddItemUUID`](#maybeadditemuuid) where `items` could be a single or an array of items.
2384
+
2385
+ <details>
2386
+
2387
+ <summary style="cursor:pointer">Examples</summary>
2388
+
2389
+ ```ts
2390
+ import { maybeAddItemsUUID } from "@alessiofrittoli/react-hooks/queue";
2391
+
2392
+ const item = { foo: "bar" };
2393
+ const newItem = maybeAddItemsUUID(item);
2394
+ // Returns: [ { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' } ]
2395
+
2396
+ const items = [
2397
+ { foo: "bar" },
2398
+ { baz: "qux", uuid: "658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c" },
2399
+ ];
2400
+ const newItems = maybeAddItemsUUID(items);
2401
+ // Returns: [
2402
+ // { foo: 'bar', uuid: 'XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX' },
2403
+ // { baz: 'qux', uuid: '658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c' },
2404
+ // ]
2405
+ ```
2406
+
2407
+ </details>
2408
+
2409
+ ---
2410
+
2411
+ ###### `findIndexByUUID`
2412
+
2413
+ Finds the index of the item matching the given UUID.
2414
+
2415
+ <details>
2416
+
2417
+ <summary style="cursor:pointer">Parameters</summary>
2418
+
2419
+ | Parameter | Type | Description |
2420
+ | --------- | ------------- | ----------------------------- |
2421
+ | `items` | `QueuedItems` | The queue items to search in. |
2422
+ | `uuid` | `UUID` | The UUID to match. |
2423
+
2424
+ </details>
2425
+
2426
+ ---
2427
+
2428
+ <details>
2429
+
2430
+ <summary style="cursor:pointer">Returns</summary>
2431
+
2432
+ Type: `number`
2433
+
2434
+ The matching index, or `-1` when the UUID is missing or not found.
2435
+
2436
+ </details>
2437
+
2438
+ ---
2439
+
2440
+ <details>
2441
+
2442
+ <summary style="cursor:pointer">Examples</summary>
2443
+
2444
+ ```ts
2445
+ import { findIndexByUUID } from "@alessiofrittoli/react-hooks/queue";
2446
+
2447
+ const items = [
2448
+ { uuid: "x" },
2449
+ { uuid: "y" },
2450
+ { uuid: "658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c" },
2451
+ ];
2452
+
2453
+ findIndexByUUID(items, "658ebade-ce6c-4bf3-a11e-e2bebb3a1f9c");
2454
+ // Returns: 2
2455
+ ```
2456
+
2457
+ </details>
2458
+
2459
+ ---
2460
+
2461
+ ###### `useShuffle`
2462
+
2463
+ Handle shuffle functionality for queues.
2464
+
2465
+ This hook manages the shuffle state and provides methods to shuffle, unshuffle, and toggle shuffle for a queue. When shuffling, it preserves the order of items up to the current cursor position and only shuffles upcoming items.
2466
+
2467
+ <details>
2468
+
2469
+ <summary style="cursor:pointer">Returns</summary>
2470
+
2471
+ Type: `UseShuffle`
2472
+
2473
+ | Property | Type | Description |
2474
+ | --------------- | ----------------------------------------------- | ----------------------------------------------- |
2475
+ | `enabled` | `boolean` | Indicates whether shuffle is currently enabled. |
2476
+ | `shuffle` | `<T extends Queue>(queue: T, uuid?: UUID) => T` | Shuffle given queue items. |
2477
+ | `unshuffle` | `<T extends Queue>(queue: T, uuid?: UUID) => T` | Un-shuffle given queue items. |
2478
+ | `toggleShuffle` | `<T extends Queue>(queue: T, uuid?: UUID) => T` | Shuffle/un-shuffle given queue items. |
2479
+
2480
+ </details>
2481
+
2482
+ ---
2483
+
2484
+ <details>
2485
+
2486
+ <summary style="cursor:pointer">Examples</summary>
2487
+
2488
+ ```ts
2489
+ import { useShuffle } from "@alessiofrittoli/react-hooks/queue";
2490
+
2491
+ const { enabled, shuffle, unshuffle, toggleShuffle } = useShuffle();
2492
+
2493
+ // Shuffle the queue
2494
+ const shuffledQueue = shuffle(currentQueue, currentUUID);
2495
+
2496
+ // Restore original order
2497
+ const restoredQueue = unshuffle(currentQueue, currentUUID);
2498
+
2499
+ // Toggle shuffle state
2500
+ const updatedQueue = toggleShuffle(currentQueue, currentUUID);
2501
+ ```
2502
+
2503
+ </details>
2504
+
2505
+ ---
2506
+
2507
+ ###### `useQueue`
2508
+
2509
+ Manage a queue of items with support for shuffling, repeating, and custom queue items.
2510
+
2511
+ <details>
2512
+
2513
+ <summary style="cursor:pointer">Type parameters</summary>
2514
+
2515
+ | Parameter | Type | Default | Description |
2516
+ | --------- | ------- | ------- | ---------------------- |
2517
+ | `T` | `Queue` | `Queue` | The type of the queue. |
2518
+
2519
+ </details>
2520
+
2521
+ ---
2522
+
2523
+ <details>
2524
+
2525
+ <summary style="cursor:pointer">Parameters</summary>
2526
+
2527
+ | Parameter | Type | Default | Description |
2528
+ | --------- | -------------------- | ------- | ---------------------------------------------- |
2529
+ | `options` | `UseQueueOptions<T>` | - | Configuration options for the `useQueue` hook. |
2530
+
2531
+ </details>
2532
+
2533
+ ---
2534
+
2535
+ <details>
2536
+
2537
+ <summary style="cursor:pointer">Returns</summary>
2538
+
2539
+ Type: `UseQueue<T>`
2540
+
2541
+ | Property | Type | Description |
2542
+ | ------------------ | ------------------------------------------------------------------------------------------------- | ----------------------------------------------- |
2543
+ | `queue` | `T` | The main queue. |
2544
+ | `current` | `QueuedItemType<T>` | The current active item. |
2545
+ | `currentId` | `UUID` | Defines the `current` item cursor ID. |
2546
+ | `customQueue` | `QueuedItemsType<T>` | Array of queued items in the custom queue. |
2547
+ | `effectiveQueue` | `QueuedItemsType<T>` | The complete queue (main queue + custom queue). |
2548
+ | `nextFromQueue` | `QueuedItemsType<T>` | Upcoming main queue items. |
2549
+ | `hasPrevious` | `boolean` | Whether the current item has a previous item. |
2550
+ | `hasNext` | `boolean` | Whether the current item has a next item. |
2551
+ | `isShuffleEnabled` | `boolean` | Whether shuffle is currently enabled. |
2552
+ | `shuffle` | `VoidFunction` | Shuffle main queue. |
2553
+ | `unshuffle` | `VoidFunction` | Un-shuffle main queue. |
2554
+ | `toggleShuffle` | `VoidFunction` | Toggle shuffle for the main queue. |
2555
+ | `isRepeatEnabled` | `boolean` | Whether repeat is enabled or not. |
2556
+ | `toggleRepeat` | `VoidFunction` | Toggle repeat on/off. |
2557
+ | `jumpTo` | `JumpToHandler<T>` | Jump to a specific item. |
2558
+ | `getPrevious` | `() => QueuedItemType<T> \| undefined` | Get previous item. |
2559
+ | `previous` | `() => QueuedItemType<T> \| undefined` | Jump to previous item. |
2560
+ | `getNext` | `() => QueuedItemType<T> \| undefined` | Get next item. |
2561
+ | `next` | `() => QueuedItemType<T> \| undefined` | Jump to next item. |
2562
+ | `setQueue` | `(queue: NewQueue<T>) => T` | Overwrite the main queue. |
2563
+ | `addToQueue` | `(item: OptionalQueuedItem<QueuedItemType<T>> \| OptionalQueuedItems<QueuedItemType<T>>) => void` | Add a new item to the custom queue. |
2564
+ | `removeFromQueue` | `(uuid: UUID) => void` | Remove item from the queues. |
2565
+ | `clearQueue` | `VoidFunction` | Wipe custom queue. |
2566
+
2567
+ </details>
2568
+
2569
+ ---
2570
+
2571
+ <details>
2572
+
2573
+ <summary style="cursor:pointer">Usage</summary>
2574
+
2575
+ ```tsx
2576
+ import { useQueue } from "@alessiofrittoli/react-hooks/queue";
2577
+
2578
+ const queue = [{ foo: "bar" }, { foo: "baz" }];
2579
+
2580
+ const { next, jumpTo, shuffle, addToQueue } = useQueue({ queue });
2581
+
2582
+ // Move to next item
2583
+ const nextItem = next();
2584
+
2585
+ // Move to next item
2586
+ const newItem = jumpTo({ uuid: "XXXXXXXX-XXXX-4XXX-YXXX-XXXXXXXXXXXX" });
2587
+
2588
+ // Shuffle the queue
2589
+ shuffle();
2590
+
2591
+ // Add a custom item
2592
+ addToQueue({ foo: "custom" });
2593
+
2594
+ // Move to first of a new queue
2595
+ const newItem = jumpTo({ queue: { items: [ ... ] } });
2596
+ ```
2597
+
2598
+ </details>
2599
+
2093
2600
  #### Timers
2094
2601
 
2095
2602
  #### `useDebounce`
@@ -2575,7 +3082,7 @@ const MyComponent: React.FC = () => {
2575
3082
 
2576
3083
  ### Development
2577
3084
 
2578
- #### Install depenendencies
3085
+ #### Install dependencies
2579
3086
 
2580
3087
  ```bash
2581
3088
  npm install
@@ -2597,7 +3104,7 @@ pnpm build
2597
3104
 
2598
3105
  #### [ESLint](https://www.npmjs.com/package/eslint)
2599
3106
 
2600
- warnings / errors check.
3107
+ Run warnings and errors checks.
2601
3108
 
2602
3109
  ```bash
2603
3110
  pnpm lint
@@ -0,0 +1 @@
1
+ import{randomUUID as I}from"@alessiofrittoli/math-utils";import{findIndexBy as m}from"@alessiofrittoli/web-utils";var s=e=>({...e,uuid:I()}),c=e=>Array.isArray(e)?e.map(u=>s(u)):c([e]),n=e=>({...e,uuid:"uuid"in e&&e.uuid||I()}),r=e=>Array.isArray(e)?e.map(n):r([e]),Q=(e,u)=>typeof u<"u"?m({items:e,field:"uuid",value:u}):-1,p=e=>{let{queue:u,customQueue:d,uuid:o}=e;if(!o)return u;let t=Q(u,o);return t===-1?[...u,...d]:[...u.slice(0,t+1),...d,...u.slice(t+1)]};export{s as a,c as b,n as c,r as d,Q as e,p as f};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _mathutils = require('@alessiofrittoli/math-utils');var _webutils = require('@alessiofrittoli/web-utils');var s=e=>({...e,uuid:_mathutils.randomUUID.call(void 0, )}),c= exports.b =e=>Array.isArray(e)?e.map(u=>s(u)):c([e]),n= exports.c =e=>({...e,uuid:"uuid"in e&&e.uuid||_mathutils.randomUUID.call(void 0, )}),r= exports.d =e=>Array.isArray(e)?e.map(n):r([e]),Q= exports.e =(e,u)=>typeof u<"u"?_webutils.findIndexBy.call(void 0, {items:e,field:"uuid",value:u}):-1,f= exports.f =e=>{let{queue:u,customQueue:d,uuid:o}=e;if(!o)return u;let t=Q(u,o);return t===-1?[...u,...d]:[...u.slice(0,t+1),...d,...u.slice(t+1)]};exports.a = s; exports.b = c; exports.c = n; exports.d = r; exports.e = Q; exports.f = f;