feat(core): add global loading state (#4840)

This commit is contained in:
fourdim
2023-11-09 22:25:05 -05:00
committed by GitHub
parent 1fe5a0fffa
commit af7d331610
5 changed files with 120 additions and 1 deletions
@@ -0,0 +1,35 @@
import { atom } from 'jotai';
import { nanoid } from 'nanoid';
export type GlobalLoadingEvent = {
key?: string;
};
const globalLoadingEventsBaseAtom = atom<GlobalLoadingEvent[]>([]);
export const globalLoadingEventsAtom = atom<GlobalLoadingEvent[]>(get =>
get(globalLoadingEventsBaseAtom)
);
export const resolveGlobalLoadingEventAtom = atom(
null,
(_, set, key: string) => {
set(globalLoadingEventsBaseAtom, globalLoadingEvent =>
globalLoadingEvent.filter(notification => notification.key !== key)
);
}
);
export const pushGlobalLoadingEventAtom = atom<
null,
[GlobalLoadingEvent],
void
>(null, (_, set, newGlobalLoadingEvent) => {
newGlobalLoadingEvent.key = newGlobalLoadingEvent.key || nanoid();
set(globalLoadingEventsBaseAtom, globalLoadingEvents => [
// push to the top
{ ...newGlobalLoadingEvent },
...globalLoadingEvents,
]);
});