ItemSelectionProvider: promote toot expansion

state

* used in Profile, TrendTimelinePanel,
  TimelinePanel
This commit is contained in:
thislight 2024-11-25 20:08:03 +08:00
parent dafc2c47a8
commit 9499182a8d
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
5 changed files with 120 additions and 68 deletions

View file

@ -0,0 +1,35 @@
import {
createContext,
createSelector,
createSignal,
useContext,
type Accessor,
} from "solid-js";
export type ItemSelectionState<T> = [(value: T) => boolean, (value: T) => void];
const ItemSelectionContext = /* @__PURE__ */ createContext<
ItemSelectionState<any>
>([() => false, () => undefined]);
export function createSingluarItemSelection<T extends {}>(
intial?: T,
): readonly [Accessor<T | undefined>, ItemSelectionState<T | undefined>] {
const [value, setValue] = createSignal<T | undefined>(intial);
const select = createSelector(value, (a, b) =>
typeof b !== "undefined" ? a === b : false,
);
const toggle = (v: T | undefined) => {
setValue((o) => (o ? undefined : v));
};
return [value, [select, toggle]];
}
export function useItemSelection() {
return useContext(ItemSelectionContext);
}
export default ItemSelectionContext.Provider;