import { WritableAtom, createStore, Atom } from 'jotai/vanilla'; import React, { PropsWithChildren } from 'react'; import { useHydrateAtoms } from 'jotai/utils'; /** * Jotai atoms don't allow functions as values by default. This function is a * drop-in replacement for `atom` that wraps functions in an object while * leaving non-functions unchanged. The wrapper object should be completely * invisible to consumers of the atom. */ declare const atomWithFn: (initialValue: T) => WritableAtom; type JotaiStore = ReturnType; type UseAtomOptions = { scope?: string; store?: JotaiStore; delay?: number; warnIfNoStore?: boolean; }; type UseAtomOptionsOrScope = UseAtomOptions | string; type StoreAtomsWithoutExtend = { [K in keyof T]: T[K] extends Atom ? T[K] : SimpleWritableAtom; }; type ValueTypesForAtoms = { [K in keyof T]: T[K] extends Atom ? V : never; }; type StoreInitialValues = ValueTypesForAtoms>; type StoreAtoms = StoreAtomsWithoutExtend & E; type SimpleWritableAtom = WritableAtom; type SimpleWritableAtomRecord = { [K in keyof T]: SimpleWritableAtom; }; type AtomRecord = { [K in keyof O]: Atom; }; type UseNameStore = `use${Capitalize}Store`; type NameStore = N extends '' ? 'store' : `${N}Store`; type NameProvider = `${Capitalize}Provider`; type UseKeyValue = `use${Capitalize}Value`; type GetKey = `get${Capitalize}`; type UseSetKey = `useSet${Capitalize}`; type SetKey = `set${Capitalize}`; type UseKeyState = `use${Capitalize}State`; type SubscribeKey = `subscribe${Capitalize}`; type UseHydrateAtoms = (initialValues: Partial>, options?: Parameters[1]) => void; type UseSyncAtoms = (values: Partial>, options?: { store?: JotaiStore; }) => void; type StoreApi, N extends string = ''> = { atom: StoreAtoms; name: N; }; type UseValueOptions = { selector?: (v: V, prevSelectorOutput?: S) => S; equalityFn?: (prev: S, next: S) => boolean; } & UseAtomOptions; type UseKeyValueApis = { [K in keyof O as UseKeyValue]: { (): O[K] extends Atom ? V : never; (selector: O[K] extends Atom ? (v: V, prevSelectorOutput?: S) => S : never, deps?: unknown[]): S; (selector: (O[K] extends Atom ? (v: V, prevSelectorOutput?: S) => S : never) | undefined, equalityFn: (prevSelectorOutput: S, selectorOutput: S) => boolean, deps?: unknown[]): S; }; }; type GetKeyApis = { [K in keyof O as GetKey]: O[K] extends Atom ? () => V : never; }; type UseSetKeyApis = { [K in keyof O as UseSetKey]: O[K] extends WritableAtom ? () => (...args: A) => R : never; }; type SetKeyApis = { [K in keyof O as SetKey]: O[K] extends WritableAtom ? (...args: A) => R : never; }; type UseKeyStateApis = { [K in keyof O as UseKeyState]: O[K] extends WritableAtom ? () => [V, (...args: A) => R] : never; }; type SubscribeKeyApis = { [K in keyof O as SubscribeKey]: O[K] extends Atom ? (callback: (newValue: V) => void) => () => void : never; }; type UseParamKeyValueApi = { (key: K): O[K] extends Atom ? V : never; (key: K, selector: O[K] extends Atom ? (v: V, prevSelectorOutput?: S) => S : never, deps?: unknown[]): S; (key: K, selector: (O[K] extends Atom ? (v: V, prevSelectorOutput?: S) => S : never) | undefined, equalityFn: (prevSelectorOutput: S, selectorOutput: S) => boolean, deps?: unknown[]): S; }; type GetParamKeyApi = (key: K) => O[K] extends Atom ? V : never; type UseSetParamKeyApi = (key: K) => O[K] extends WritableAtom ? (...args: A) => R : never; type SetParamKeyApi = (key: K, ...args: A) => O[K] extends WritableAtom ? R : never; type UseParamKeyStateApi = (key: K) => O[K] extends WritableAtom ? [V, (...args: A) => R] : never; type SubscribeParamKeyApi = (key: K, callback: (newValue: V) => void) => O[K] extends Atom ? () => void : never; type UseAtomParamValueApi = { (atom: Atom): V; (atom: Atom, selector: (v: V, prevSelectorOutput?: S) => S, deps?: unknown[]): S; (atom: Atom, selector: ((v: V, prevSelectorOutput?: S) => S) | undefined, equalityFn: (prevSelectorOutput: S, selectorOutput: S) => boolean, deps?: unknown[]): S; }; type GetAtomParamApi = (atom: Atom) => V; type UseSetAtomParamApi = (atom: WritableAtom) => (...args: A) => R; type SetAtomParamApi = (atom: WritableAtom) => (...args: A) => R; type UseAtomParamStateApi = (atom: WritableAtom) => [V, (...args: A) => R]; type SubscribeAtomParamApi = (atom: Atom) => (callback: (newValue: V) => void) => () => void; type ReturnOfUseStoreApi = UseKeyValueApis> & GetKeyApis> & UseSetKeyApis> & SetKeyApis> & UseKeyStateApis> & SubscribeKeyApis> & { /** * When providing `selector`, the atom value will be transformed using the selector function. * The selector and equalityFn MUST be memoized. * * @see https://jotai.org/docs/utilities/select#selectatom * * @example * const store = useStore() * // only rerenders when the first element of the array changes * const arrayFirst = store.useValue('array', array => array[0], []) * // only rerenders when the first element of the array changes, but returns the whole array * const array = store.useValue('array', undefined, (prev, next) => prev[0] === next[0], []) * // without dependency array, then you need to memoize the selector and equalityFn yourself * const cb = useCallback((array) => array[n], [n]) * const arrayNth = store.useValue('array', cb) * * @param key The key of the atom * @param selector A function that takes the atom value and returns the value to be used. Defaults to identity function that returns the atom value. * @param equalityFnOrDeps Dependency array or a function that compares the previous selector output and the new selector output. Defaults to comparing outputs of the selector function. * @param deps Dependency array for the selector and equalityFn */ useValue: UseParamKeyValueApi>; get: GetParamKeyApi>; useSet: UseSetParamKeyApi>; set: SetParamKeyApi>; useState: UseParamKeyStateApi>; subscribe: SubscribeParamKeyApi>; /** * When providing `selector`, the atom value will be transformed using the selector function. * The selector and equalityFn MUST be memoized. * * @see https://jotai.org/docs/utilities/select#selectatom * * @example * const store = useStore() * // only rerenders when the first element of the array changes * const arrayFirst = store.useAtomValue(arrayAtom, array => array[0]) * // only rerenders when the first element of the array changes, but returns the whole array * const array = store.useAtomValue(arrayAtom, undefined, (prev, next) => prev[0] === next[0]) * // without dependency array, then you need to memoize the selector and equalityFn yourself * const cb = useCallback((array) => array[n], [n]) * const arrayNth = store.useAtomValue(arrayAtom, cb) * * @param atom The atom to use * @param selector A function that takes the atom value and returns the value to be used. Defaults to identity function that returns the atom value. * @param equalityFn Dependency array or a function that compares the previous selector output and the new selector output. Defaults to comparing outputs of the selector function. * @param deps Dependency array for the selector and equalityFn */ useAtomValue: UseAtomParamValueApi; getAtom: GetAtomParamApi; useSetAtom: UseSetAtomParamApi; setAtom: SetAtomParamApi; useAtomState: UseAtomParamStateApi; subscribeAtom: SubscribeAtomParamApi; store: JotaiStore | undefined; }; type UseKeyStateUtil = >(key: K, options?: UseAtomOptionsOrScope) => StoreAtoms[K] extends WritableAtom ? [V, (...args: A) => R] : never; type UseKeyValueUtil = , S = StoreAtoms[K] extends Atom ? V : never>(key: K, options?: UseValueOptions[K] extends Atom ? V : never, S>, deps?: unknown[]) => S; type UseKeySetUtil = >(key: K, options?: UseAtomOptionsOrScope) => StoreAtoms[K] extends WritableAtom ? (...args: A) => R : never; type AtomStoreApi, N extends string = ''> = { name: N; } & { [key in keyof Record, object>]: React.FC>>; } & { [key in keyof Record, object>]: StoreApi; } & { [key in keyof Record, object>]: UseStoreApi; } & { [key in keyof Record<`use${Capitalize}State`, object>]: UseKeyStateUtil; } & { [key in keyof Record<`use${Capitalize}Value`, object>]: UseKeyValueUtil; } & { [key in keyof Record<`use${Capitalize}Set`, object>]: UseKeySetUtil; }; type UseStoreApi = (options?: UseAtomOptionsOrScope) => ReturnOfUseStoreApi; interface CreateAtomStoreOptions, N extends string> { name: N; delay?: UseAtomOptions['delay']; effect?: React.FC; extend?: (atomsWithoutExtend: StoreAtomsWithoutExtend) => E; suppressWarnings?: boolean; } /** * Create an atom store from an initial value. * Each property will have a getter and setter. * * @example * const { exampleStore, useExampleStore, useExampleValue, useExampleState, useExampleSet } = createAtomStore({ count: 1, say: 'hello' }, { name: 'example' as const }) * const [count, setCount] = useExampleState() * const say = useExampleValue('say') * const setSay = useExampleSet('say') * setSay('world') * const countAtom = exampleStore.atom.count */ declare const createAtomStore: , N extends string = "">(initialState: T, { name, delay: delayRoot, effect, extend, suppressWarnings, }: CreateAtomStoreOptions) => AtomStoreApi; declare function useAtomStoreValue>(store: ReturnOfUseStoreApi, key: K): StoreAtoms[K] extends Atom ? V : never; declare function useAtomStoreValue, S>(store: ReturnOfUseStoreApi, key: K, selector: StoreAtoms[K] extends Atom ? (v: V, prevSelectorOutput?: S) => S : never, deps?: unknown[]): S; declare function useAtomStoreValue, S>(store: ReturnOfUseStoreApi, key: K, selector: StoreAtoms[K] extends Atom ? ((v: V, prevSelectorOutput?: S) => S) | undefined : never, equalityFn: (prevSelectorOutput: S, selectorOutput: S) => boolean, deps?: unknown[]): S; declare function useAtomStoreSet>(store: ReturnOfUseStoreApi, key: K): StoreAtoms[K] extends WritableAtom ? (...args: A) => R : never; declare function useAtomStoreState>(store: ReturnOfUseStoreApi, key: K): StoreAtoms[K] extends WritableAtom ? [V, (...args: A) => R] : never; declare function useStoreAtomValue(store: ReturnOfUseStoreApi, atom: Atom): V; declare function useStoreAtomValue(store: ReturnOfUseStoreApi, atom: Atom, selector: (v: V, prevSelectorOutput?: S) => S, deps?: unknown[]): S; declare function useStoreAtomValue(store: ReturnOfUseStoreApi, atom: Atom, selector: ((v: V, prevSelectorOutput?: S) => S) | undefined, equalityFn: (prevSelectorOutput: S, selectorOutput: S) => boolean, deps?: unknown[]): S; declare function useStoreSetAtom(store: ReturnOfUseStoreApi, atom: WritableAtom): (...args: A) => R; declare function useStoreAtomState(store: ReturnOfUseStoreApi, atom: WritableAtom): [V, (...args: A) => R]; /** * Tries to find a store in each of the following places, in order: * 1. The store context, matching the store name and scope * 2. The store context, matching the store name and 'provider' scope * 3. Otherwise, return undefined */ declare const useAtomStore: (storeName: string, scope?: string, warnIfUndefined?: boolean) => JotaiStore | undefined; type ProviderProps = Partial & PropsWithChildren<{ store?: JotaiStore; scope?: string; initialValues?: Partial; resetKey?: any; }>; declare const HydrateAtoms: ({ initialValues, children, store, atoms, ...props }: Omit, "scope"> & { atoms: SimpleWritableAtomRecord; }) => React.JSX.Element; /** * Creates a generic provider for a jotai store. * - `initialValues`: Initial values for the store. * - `props`: Dynamic values for the store. */ declare const createAtomProvider: (storeScope: N, atoms: SimpleWritableAtomRecord, options?: { effect?: React.FC; }) => ({ store, scope, children, resetKey, ...props }: ProviderProps) => React.JSX.Element; /** * Hydrate atoms with initial values for SSR. */ declare const useHydrateStore: (atoms: SimpleWritableAtomRecord, initialValues: Parameters>[0], options?: Parameters>[1]) => void; /** * Update atoms with new values on changes. */ declare const useSyncStore: (atoms: SimpleWritableAtomRecord, values: any, { store }?: Parameters>[1]) => void; export { type AtomRecord, type AtomStoreApi, type CreateAtomStoreOptions, type GetAtomParamApi, type GetKeyApis, type GetParamKeyApi, HydrateAtoms, type JotaiStore, type ProviderProps, type ReturnOfUseStoreApi, type SetAtomParamApi, type SetKeyApis, type SetParamKeyApi, type SimpleWritableAtom, type SimpleWritableAtomRecord, type StoreApi, type SubscribeAtomParamApi, type SubscribeKeyApis, type SubscribeParamKeyApi, type UseAtomOptions, type UseAtomParamStateApi, type UseAtomParamValueApi, type UseHydrateAtoms, type UseKeyStateApis, type UseKeyValueApis, type UseParamKeyStateApi, type UseParamKeyValueApi, type UseSetAtomParamApi, type UseSetKeyApis, type UseSetParamKeyApi, type UseStoreApi, type UseSyncAtoms, atomWithFn, createAtomProvider, createAtomStore, useAtomStore, useAtomStoreSet, useAtomStoreState, useAtomStoreValue, useHydrateStore, useStoreAtomState, useStoreAtomValue, useStoreSetAtom, useSyncStore };