@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 2.49 kB
Source Map (JSON)
{"version":3,"file":"use-queue.cjs","names":[],"sources":["../../src/use-queue/use-queue.ts"],"sourcesContent":["import { useState } from 'react';\n\nexport interface UseQueueOptions<T> {\n /** Initial values to be added to the queue */\n initialValues?: T[];\n\n /** Maximum number of items in the state */\n limit: number;\n}\n\nexport interface UseQueueReturnValue<T> {\n /** Array of items in the queue */\n queue: T[];\n\n /** Array of items in the state */\n state: T[];\n\n /** Function to add items to state or queue */\n add: (...items: T[]) => void;\n\n /** Function to apply updates to current items */\n update: (fn: (state: T[]) => T[]) => void;\n\n /** Function to clear the queue */\n cleanQueue: () => void;\n}\n\nexport function useQueue<T>({\n initialValues = [],\n limit,\n}: UseQueueOptions<T>): UseQueueReturnValue<T> {\n const [state, setState] = useState({\n state: initialValues.slice(0, limit),\n queue: initialValues.slice(limit),\n });\n\n const add = (...items: T[]) =>\n setState((current) => {\n const results = [...current.state, ...current.queue, ...items];\n\n return {\n state: results.slice(0, limit),\n queue: results.slice(limit),\n };\n });\n\n const update = (fn: (state: T[]) => T[]) =>\n setState((current) => {\n const results = fn([...current.state, ...current.queue]);\n\n return {\n state: results.slice(0, limit),\n queue: results.slice(limit),\n };\n });\n\n const cleanQueue = () => setState((current) => ({ state: current.state, queue: [] }));\n\n return {\n state: state.state,\n queue: state.queue,\n add,\n update,\n cleanQueue,\n };\n}\n\nexport namespace useQueue {\n export type Options<T> = UseQueueOptions<T>;\n export type ReturnValue<T> = UseQueueReturnValue<T>;\n}\n"],"mappings":";;;AA2BA,SAAgB,SAAY,EAC1B,gBAAgB,EAAE,EAClB,SAC6C;CAC7C,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,UAAqB;EACjC,OAAO,cAAc,MAAM,GAAG,MAAM;EACpC,OAAO,cAAc,MAAM,MAAM;EAClC,CAAC;CAEF,MAAM,OAAO,GAAG,UACd,UAAU,YAAY;EACpB,MAAM,UAAU;GAAC,GAAG,QAAQ;GAAO,GAAG,QAAQ;GAAO,GAAG;GAAM;AAE9D,SAAO;GACL,OAAO,QAAQ,MAAM,GAAG,MAAM;GAC9B,OAAO,QAAQ,MAAM,MAAM;GAC5B;GACD;CAEJ,MAAM,UAAU,OACd,UAAU,YAAY;EACpB,MAAM,UAAU,GAAG,CAAC,GAAG,QAAQ,OAAO,GAAG,QAAQ,MAAM,CAAC;AAExD,SAAO;GACL,OAAO,QAAQ,MAAM,GAAG,MAAM;GAC9B,OAAO,QAAQ,MAAM,MAAM;GAC5B;GACD;CAEJ,MAAM,mBAAmB,UAAU,aAAa;EAAE,OAAO,QAAQ;EAAO,OAAO,EAAE;EAAE,EAAE;AAErF,QAAO;EACL,OAAO,MAAM;EACb,OAAO,MAAM;EACb;EACA;EACA;EACD"}