import { createAtom } from '@tanstack/store' import { useState } from 'octane' import { splitSlot } from './internal' import type { Atom, AtomOptions, ReadonlyAtom } from '@tanstack/store' /** * Creates a stable atom instance for the lifetime of the component. * * Pass an initial value to create a writable atom, or a getter function to * create a readonly derived atom. This hook mirrors the overloads from * {@link createAtom}, but ensures the atom is only created once per mount. * * @example * ```tsx * const countAtom = useCreateAtom(0) * ``` */ export function useCreateAtom( getValue: (prev?: NoInfer) => T, options?: AtomOptions, ): ReadonlyAtom export function useCreateAtom( initialValue: T, options?: AtomOptions, ): Atom export function useCreateAtom( valueOrFn: T | ((prev?: T) => T), ...rest: [options?: AtomOptions, slot?: symbol] ): Atom | ReadonlyAtom { const [user, slot] = splitSlot(rest) const options = user[0] as AtomOptions | undefined const [atom] = useState | ReadonlyAtom>(() => { if (typeof valueOrFn === 'function') { return createAtom(valueOrFn as (prev?: NoInfer) => T, options) } return createAtom(valueOrFn, options) }, slot) return atom }