mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 22:02:43 -08:00
28 lines
684 B
TypeScript
28 lines
684 B
TypeScript
import type { MouseEventHandler } from 'react';
|
|
|
|
type CategoryAction =
|
|
| {
|
|
type: 'PUT';
|
|
value: string;
|
|
}
|
|
| {
|
|
type: 'DELETE';
|
|
value: string;
|
|
};
|
|
export const useCategoryHandler = (
|
|
setAtom: (action: CategoryAction) => void,
|
|
closed: (categoryId: string) => boolean
|
|
) => {
|
|
const handleCategoryClick: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
|
const categoryId = evt.currentTarget.getAttribute('data-category-id');
|
|
if (!categoryId) return;
|
|
if (closed(categoryId)) {
|
|
setAtom({ type: 'DELETE', value: categoryId });
|
|
return;
|
|
}
|
|
setAtom({ type: 'PUT', value: categoryId });
|
|
};
|
|
|
|
return handleCategoryClick;
|
|
};
|