Files
Folo/packages/internal/utils/src/jotai.ts
T
Innei 4d746522f1 refactor: remove unused files and clean up project structure
- Deleted obsolete configuration files and unused components across various packages.
- Updated package.json files to reflect the removal of dependencies.
- Cleaned up code by removing unnecessary exports and unused imports in multiple modules.

Signed-off-by: Innei <tukon479@gmail.com>
2025-04-28 20:58:51 +08:00

58 lines
1.5 KiB
TypeScript

import type { Atom, PrimitiveAtom } from "jotai"
import { createStore, useAtom, useAtomValue, useSetAtom } from "jotai"
import { selectAtom } from "jotai/utils"
import { useCallback } from "react"
import { shallow } from "zustand/shallow"
export const jotaiStore = createStore()
export const createAtomAccessor = <T>(atom: PrimitiveAtom<T>) =>
[
() => jotaiStore.get(atom),
(value: T | ((prev: T) => T)) => jotaiStore.set(atom, value),
] as const
const options = { store: jotaiStore }
/**
* @param atom - jotai
* @returns - [atom, useAtom, useAtomValue, useSetAtom, jotaiStore.get, jotaiStore.set, useSelector]
*/
export const createAtomHooks = <T>(atom: PrimitiveAtom<T>) => {
let _atomSelector: ReturnType<typeof createAtomSelector<T>> | undefined
const result = [
atom,
() => useAtom(atom, options),
() => useAtomValue(atom, options),
() => useSetAtom(atom, options),
...createAtomAccessor(atom),
] as const
type Result = [...typeof result, ReturnType<typeof createAtomSelector<T>>]
Object.defineProperty(result, result.length, {
get() {
if (!_atomSelector) {
_atomSelector = createAtomSelector(atom)
}
return _atomSelector
},
})
return result as any as Result
}
const noop: any[] = []
const createAtomSelector = <T>(atom: Atom<T>) => {
const useHook = <R>(selector: (a: T) => R, deps: any[] = noop) =>
useAtomValue(
selectAtom(
atom,
useCallback((a) => selector(a as T), deps),
shallow,
),
)
return useHook
}