2021-09-21 08:20:44 -07:00
|
|
|
import { devtools } from 'zustand/middleware';
|
|
|
|
|
|
2021-12-06 14:33:20 -07:00
|
|
|
import type { StateCreator, SetState, GetState, StoreApi } from 'zustand';
|
2021-09-21 08:20:44 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrap a zustand state function with devtools, if applicable.
|
|
|
|
|
*
|
|
|
|
|
* @param store zustand store function.
|
|
|
|
|
* @param name Store name.
|
|
|
|
|
*/
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
|
export function withDev<T extends object = {}>(
|
|
|
|
|
store: StateCreator<T>,
|
|
|
|
|
name: string,
|
|
|
|
|
): StateCreator<T> {
|
|
|
|
|
if (typeof window !== 'undefined' && process.env.NODE_ENV === 'development') {
|
2021-12-06 14:33:20 -07:00
|
|
|
return devtools<T, SetState<T>, GetState<T>, StoreApi<T>>(store, { name });
|
2021-09-21 08:20:44 -07:00
|
|
|
}
|
|
|
|
|
return store;
|
|
|
|
|
}
|