* Increase coverage for coverage for `twenty-front:storybook:modules` Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Chiazokam <chiazokamecheta@gmail.com> * Increase code coverage threshold * Increase code coverage threshold * Increase code coverage threshold --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Chiazokam <chiazokamecheta@gmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useRecoilCallback } from 'recoil';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import { SnackBarManagerScopeInternalContext } from '@/ui/feedback/snack-bar-manager/scopes/scope-internal-context/SnackBarManagerScopeInternalContext';
|
|
import {
|
|
snackBarInternalScopedState,
|
|
SnackBarOptions,
|
|
} from '@/ui/feedback/snack-bar-manager/states/snackBarInternalScopedState';
|
|
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
|
|
|
|
export const useSnackBar = () => {
|
|
const scopeId = useAvailableScopeIdOrThrow(
|
|
SnackBarManagerScopeInternalContext,
|
|
);
|
|
|
|
const handleSnackBarClose = useRecoilCallback(
|
|
({ set }) =>
|
|
(id: string) => {
|
|
set(snackBarInternalScopedState({ scopeId }), (prevState) => ({
|
|
...prevState,
|
|
queue: prevState.queue.filter((snackBar) => snackBar.id !== id),
|
|
}));
|
|
},
|
|
[scopeId],
|
|
);
|
|
|
|
const setSnackBarQueue = useRecoilCallback(
|
|
({ set }) =>
|
|
(newValue) =>
|
|
set(snackBarInternalScopedState({ scopeId }), (prev) => {
|
|
if (prev.queue.length >= prev.maxQueue) {
|
|
return {
|
|
...prev,
|
|
queue: [...prev.queue.slice(1), newValue] as SnackBarOptions[],
|
|
};
|
|
}
|
|
|
|
return {
|
|
...prev,
|
|
queue: [...prev.queue, newValue] as SnackBarOptions[],
|
|
};
|
|
}),
|
|
[scopeId],
|
|
);
|
|
|
|
const enqueueSnackBar = useCallback(
|
|
(message: string, options?: Omit<SnackBarOptions, 'message' | 'id'>) => {
|
|
setSnackBarQueue({
|
|
id: uuidv4(),
|
|
message,
|
|
...options,
|
|
});
|
|
},
|
|
[setSnackBarQueue],
|
|
);
|
|
|
|
return { handleSnackBarClose, enqueueSnackBar };
|
|
};
|