ItemSelectionProvider: promote toot expansion
state * used in Profile, TrendTimelinePanel, TimelinePanel
This commit is contained in:
parent
dafc2c47a8
commit
9499182a8d
5 changed files with 120 additions and 68 deletions
35
src/timelines/toots/ItemSelectionProvider.ts
Normal file
35
src/timelines/toots/ItemSelectionProvider.ts
Normal 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;
|
Loading…
Add table
Add a link
Reference in a new issue