* Change to using arrow functions Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> * Add lint rule --------- Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { useContext } from 'react';
|
|
import { useRecoilCallback, useRecoilState } from 'recoil';
|
|
|
|
import { RowIdContext } from '../contexts/RowIdContext';
|
|
import { isRowSelectedFamilyState } from '../states/isRowSelectedFamilyState';
|
|
|
|
export const useCurrentRowSelected = () => {
|
|
const currentRowId = useContext(RowIdContext);
|
|
|
|
const [isRowSelected] = useRecoilState(
|
|
isRowSelectedFamilyState(currentRowId ?? ''),
|
|
);
|
|
|
|
const setCurrentRowSelected = useRecoilCallback(
|
|
({ set, snapshot }) =>
|
|
(newSelectedState: boolean) => {
|
|
if (!currentRowId) return;
|
|
|
|
const isRowSelected = snapshot
|
|
.getLoadable(isRowSelectedFamilyState(currentRowId))
|
|
.valueOrThrow();
|
|
|
|
if (newSelectedState && !isRowSelected) {
|
|
set(isRowSelectedFamilyState(currentRowId), true);
|
|
} else if (!newSelectedState && isRowSelected) {
|
|
set(isRowSelectedFamilyState(currentRowId), false);
|
|
}
|
|
},
|
|
[currentRowId],
|
|
);
|
|
|
|
return {
|
|
currentRowSelected: isRowSelected,
|
|
setCurrentRowSelected,
|
|
};
|
|
};
|