Files
cinny/src/app/state/list.ts
Ginger dd4c1a94e6 Add support for spoilers on images (MSC4193) (#2212)
* Add support for MSC4193: Spoilers on Media

* Clarify variable names and wording

* Restore list atom

* Improve spoilered image UX with autoload off

* Use `aria-pressed` to indicate attachment spoiler state

* Improve spoiler button tooltip wording, keep reveal button from conflicting with load errors
2025-02-22 14:25:13 +05:30

42 lines
1.0 KiB
TypeScript

import { atom } from 'jotai';
export type ListAction<T> =
| {
type: 'PUT';
item: T | T[];
}
| {
type: 'REPLACE';
item: T;
replacement: T;
}
| {
type: 'DELETE';
item: T | T[];
};
export const createListAtom = <T>() => {
const baseListAtom = atom<T[]>([]);
return atom<T[], [ListAction<T>], undefined>(
(get) => get(baseListAtom),
(get, set, action) => {
const items = get(baseListAtom);
const newItems = Array.isArray(action.item) ? action.item : [action.item];
if (action.type === 'DELETE') {
set(
baseListAtom,
items.filter((item) => !newItems.includes(item))
);
return;
}
if (action.type === 'PUT') {
set(baseListAtom, [...items, ...newItems]);
return;
}
if (action.type === 'REPLACE') {
set(baseListAtom, items.map((item) => item === action.item ? action.replacement : item));
}
}
);
};
export type TListAtom<T> = ReturnType<typeof createListAtom<T>>;