Minor refactors

This commit is contained in:
2025-03-28 22:06:09 +01:00
parent 0c903e1087
commit 709165bab0
10 changed files with 20 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
import { UserId } from "common";
import * as Function from "common/Function";
import { identity } from "effect";
import { useLayoutEffect, useState } from "react";
export type Update<T> = T | ((prev: T) => T);
@@ -19,14 +19,16 @@ export namespace Store {
export interface Store {
readonly user: Store.User | null;
readonly setUser: Updater<Store.User | null>;
}
let store: Store = Object.freeze<Store>({
user: null,
setUser: (action) => set(mapProp("user", action)),
});
export function setUser(action: Update<Store.User | null>) {
set(mapProp("user", action));
}
// --- STORE IMPLEMENTATION ----------------------------------------------------
class Listener<T> {
@@ -64,7 +66,7 @@ function set(action: Partial<Store> | ((store: Store) => Partial<Store>), replac
}
}
export function useStore<T = Store>(selector: Selector<T> = Function.identity as Selector<T>): T {
export function useStore<T = Store>(selector: Selector<T> = identity as Selector<T>): T {
const [state, setState] = useState(() => selector(store));