- refactored to use multiple states
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import React, { useRef } from 'react';
|
||||
import styled from '@emotion/styled';
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
|
||||
import { contextMenuPositionState } from '@/ui/table/states/contextMenuPositionState';
|
||||
import { actionBarEntriesState } from '@/ui/table/states/ActionBarEntriesState';
|
||||
import { actionBarOpenState } from '@/ui/table/states/ActionBarIsOpenState';
|
||||
import { contextMenuOpenState } from '@/ui/table/states/ContextMenuIsOpenState';
|
||||
|
||||
type OwnProps = {
|
||||
children: React.ReactNode | React.ReactNode[];
|
||||
selectedIds: string[];
|
||||
};
|
||||
|
||||
@ -29,31 +30,18 @@ const StyledContainerActionBar = styled.div`
|
||||
z-index: 1;
|
||||
`;
|
||||
|
||||
export function ActionBar({ children, selectedIds }: OwnProps) {
|
||||
const position = useRecoilValue(contextMenuPositionState);
|
||||
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
|
||||
export function ActionBar({ selectedIds }: OwnProps) {
|
||||
const actionBarOpen = useRecoilValue(actionBarOpenState);
|
||||
const contextMenuOpen = useRecoilValue(contextMenuOpenState);
|
||||
const actionBarEntries = useRecoilValue(actionBarEntriesState);
|
||||
const wrapperRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (!(event.target as HTMLElement).closest('.action-bar')) {
|
||||
setContextMenuPosition({ x: null, y: null });
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
|
||||
// Cleanup the event listener when the component unmounts
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
};
|
||||
}, [setContextMenuPosition]);
|
||||
|
||||
if (selectedIds.length === 0 || position.x || position.y) {
|
||||
if (selectedIds.length === 0 || !actionBarOpen || contextMenuOpen) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<StyledContainerActionBar className="action-bar">
|
||||
{children}
|
||||
<StyledContainerActionBar ref={wrapperRef}>
|
||||
{actionBarEntries}
|
||||
</StyledContainerActionBar>
|
||||
);
|
||||
}
|
||||
|
||||
@ -4,18 +4,18 @@ import styled from '@emotion/styled';
|
||||
type OwnProps = {
|
||||
icon: ReactNode;
|
||||
label: string;
|
||||
type?: 'standard' | 'warning';
|
||||
type?: 'standard' | 'danger';
|
||||
onClick: () => void;
|
||||
};
|
||||
|
||||
type StyledButtonProps = {
|
||||
type: 'standard' | 'warning';
|
||||
type: 'standard' | 'danger';
|
||||
};
|
||||
|
||||
const StyledButton = styled.div<StyledButtonProps>`
|
||||
border-radius: ${({ theme }) => theme.border.radius.sm};
|
||||
color: ${(props) =>
|
||||
props.type === 'warning'
|
||||
props.type === 'danger'
|
||||
? props.theme.color.red
|
||||
: props.theme.font.color.secondary};
|
||||
cursor: pointer;
|
||||
@ -28,9 +28,7 @@ const StyledButton = styled.div<StyledButtonProps>`
|
||||
|
||||
&:hover {
|
||||
background: ${({ theme, type }) =>
|
||||
type === 'warning'
|
||||
? theme.tag.background.red
|
||||
: theme.background.tertiary};
|
||||
type === 'danger' ? theme.tag.background.red : theme.background.tertiary};
|
||||
}
|
||||
`;
|
||||
|
||||
@ -39,7 +37,7 @@ const StyledButtonLabel = styled.div`
|
||||
margin-left: ${({ theme }) => theme.spacing(2)};
|
||||
`;
|
||||
|
||||
export function EntityTableActionBarButton({
|
||||
export function ActionBarEntry({
|
||||
label,
|
||||
icon,
|
||||
type = 'standard',
|
||||
@ -8,7 +8,7 @@ const meta: Meta<typeof ActionBar> = {
|
||||
title: 'UI/ActionBar/ActionBar',
|
||||
component: ActionBar,
|
||||
decorators: [ComponentDecorator],
|
||||
args: { children: 'Lorem ipsum', selectedIds: [] },
|
||||
args: { selectedIds: [] },
|
||||
};
|
||||
|
||||
export default meta;
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { ActionBarEntry } from '@/ui/action-bar/components/ActionBarEntry';
|
||||
import { boardCardIdsByColumnIdFamilyState } from '@/ui/board/states/boardCardIdsByColumnIdFamilyState';
|
||||
import { boardColumnsState } from '@/ui/board/states/boardColumnsState';
|
||||
import { selectedBoardCardIdsState } from '@/ui/board/states/selectedBoardCardIdsState';
|
||||
import { IconTrash } from '@/ui/icon/index';
|
||||
import { EntityTableActionBarButton } from '@/ui/table/action-bar/components/EntityTableActionBarButton';
|
||||
|
||||
export function BoardActionBarButtonDeleteBoardCard({
|
||||
onDelete,
|
||||
@ -51,10 +51,10 @@ export function BoardActionBarButtonDeleteBoardCard({
|
||||
}
|
||||
|
||||
return (
|
||||
<EntityTableActionBarButton
|
||||
<ActionBarEntry
|
||||
label="Delete"
|
||||
icon={<IconTrash size={16} />}
|
||||
type="warning"
|
||||
type="danger"
|
||||
onClick={handleDeleteClick}
|
||||
/>
|
||||
);
|
||||
|
||||
@ -4,7 +4,7 @@ import { useTheme } from '@emotion/react';
|
||||
import styled from '@emotion/styled';
|
||||
import { DragDropContext, OnDragEndResponder } from '@hello-pangea/dnd'; // Atlassian dnd does not support StrictMode from RN 18, so we use a fork @hello-pangea/dnd https://github.com/atlassian/react-beautiful-dnd/issues/2350
|
||||
import { IconList } from '@tabler/icons-react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { CompanyBoardContext } from '@/companies/states/CompanyBoardContext';
|
||||
import { BoardHeader } from '@/ui/board/components/BoardHeader';
|
||||
@ -12,6 +12,7 @@ import { StyledBoard } from '@/ui/board/components/StyledBoard';
|
||||
import { useUpdateBoardCardIds } from '@/ui/board/hooks/useUpdateBoardCardIds';
|
||||
import { BoardColumnIdContext } from '@/ui/board/states/BoardColumnIdContext';
|
||||
import { SelectedSortType } from '@/ui/filter-n-sort/types/interface';
|
||||
import { actionBarOpenState } from '@/ui/table/states/ActionBarIsOpenState';
|
||||
import { DragSelect } from '@/ui/utilities/drag-select/components/DragSelect';
|
||||
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
|
||||
import {
|
||||
@ -107,10 +108,12 @@ export function EntityBoard({
|
||||
const [selectedBoardCards, setSelectedBoardCards] = useRecoilState(
|
||||
selectedBoardCardIdsState,
|
||||
);
|
||||
const setActionBarOpenState = useSetRecoilState(actionBarOpenState);
|
||||
|
||||
function setRowSelectedState(boardCardId: string, selected: boolean) {
|
||||
if (selected && !selectedBoardCards.includes(boardCardId)) {
|
||||
setSelectedBoardCards([...selectedBoardCards, boardCardId ?? '']);
|
||||
setActionBarOpenState(true);
|
||||
} else if (!selected && selectedBoardCards.includes(boardCardId)) {
|
||||
setSelectedBoardCards(
|
||||
selectedBoardCards.filter((id) => id !== boardCardId),
|
||||
|
||||
@ -5,11 +5,7 @@ import { ActionBar } from '@/ui/action-bar/components/ActionBar';
|
||||
|
||||
import { selectedBoardCardIdsState } from '../states/selectedBoardCardIdsState';
|
||||
|
||||
type OwnProps = {
|
||||
children: React.ReactNode | React.ReactNode[];
|
||||
};
|
||||
|
||||
export function EntityBoardActionBar({ children }: OwnProps) {
|
||||
export function EntityBoardActionBar() {
|
||||
const selectedBoardCards = useRecoilValue(selectedBoardCardIdsState);
|
||||
return <ActionBar selectedIds={selectedBoardCards}>{children}</ActionBar>;
|
||||
return <ActionBar selectedIds={selectedBoardCards}></ActionBar>;
|
||||
}
|
||||
|
||||
30
front/src/modules/ui/board/hooks/useActionBar.tsx
Normal file
30
front/src/modules/ui/board/hooks/useActionBar.tsx
Normal file
@ -0,0 +1,30 @@
|
||||
import { getOperationName } from '@apollo/client/utilities';
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
|
||||
import { GET_PIPELINES } from '@/pipeline/queries';
|
||||
import { actionBarEntriesState } from '@/ui/table/states/ActionBarEntriesState';
|
||||
import { useDeleteManyPipelineProgressMutation } from '~/generated/graphql';
|
||||
|
||||
import { BoardActionBarButtonDeleteBoardCard } from '../components/BoardActionBarButtonDeleteBoardCard';
|
||||
|
||||
export function useOpenActionBar() {
|
||||
const setActionBarEntries = useSetRecoilState(actionBarEntriesState);
|
||||
|
||||
const [deletePipelineProgress] = useDeleteManyPipelineProgressMutation({
|
||||
refetchQueries: [getOperationName(GET_PIPELINES) ?? ''],
|
||||
});
|
||||
|
||||
async function handleDelete(cardIdsToDelete: string[]) {
|
||||
await deletePipelineProgress({
|
||||
variables: {
|
||||
ids: cardIdsToDelete,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return () => {
|
||||
setActionBarEntries([
|
||||
<BoardActionBarButtonDeleteBoardCard onDelete={handleDelete} />,
|
||||
]);
|
||||
};
|
||||
}
|
||||
@ -1,13 +1,16 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import React, { useRef } from 'react';
|
||||
import styled from '@emotion/styled';
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { actionBarOpenState } from '@/ui/table/states/ActionBarIsOpenState';
|
||||
import { contextMenuEntriesState } from '@/ui/table/states/ContextMenuEntriesState';
|
||||
import { contextMenuOpenState } from '@/ui/table/states/ContextMenuIsOpenState';
|
||||
import { contextMenuPositionState } from '@/ui/table/states/contextMenuPositionState';
|
||||
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
|
||||
|
||||
import { PositionType } from '../types/PositionType';
|
||||
|
||||
type OwnProps = {
|
||||
children: React.ReactNode | React.ReactNode[];
|
||||
selectedIds: string[];
|
||||
};
|
||||
|
||||
@ -34,31 +37,28 @@ const StyledContainerContextMenu = styled.div<StyledContainerProps>`
|
||||
z-index: 1;
|
||||
`;
|
||||
|
||||
export function ContextMenu({ children, selectedIds }: OwnProps) {
|
||||
export function ContextMenu({ selectedIds }: OwnProps) {
|
||||
const position = useRecoilValue(contextMenuPositionState);
|
||||
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
|
||||
const contextMenuOpen = useRecoilValue(contextMenuOpenState);
|
||||
const contextMenuEntries = useRecoilValue(contextMenuEntriesState);
|
||||
const setContextMenuOpenState = useSetRecoilState(contextMenuOpenState);
|
||||
const setActionBarOpenState = useSetRecoilState(actionBarOpenState);
|
||||
const wrapperRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (!(event.target as HTMLElement).closest('.action-bar')) {
|
||||
setContextMenuPosition({ x: null, y: null });
|
||||
}
|
||||
}
|
||||
useListenClickOutside({
|
||||
refs: [wrapperRef],
|
||||
callback: () => {
|
||||
setContextMenuOpenState(false);
|
||||
setActionBarOpenState(true);
|
||||
},
|
||||
});
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
|
||||
// Cleanup the event listener when the component unmounts
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
};
|
||||
}, [setContextMenuPosition]);
|
||||
|
||||
if (selectedIds.length === 0 || (!position.x && !position.y)) {
|
||||
if (selectedIds.length === 0 || !contextMenuOpen) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<StyledContainerContextMenu className="action-bar" position={position}>
|
||||
{children}
|
||||
<StyledContainerContextMenu ref={wrapperRef} position={position}>
|
||||
{contextMenuEntries}
|
||||
</StyledContainerContextMenu>
|
||||
);
|
||||
}
|
||||
|
||||
@ -4,12 +4,12 @@ import styled from '@emotion/styled';
|
||||
type OwnProps = {
|
||||
icon: ReactNode;
|
||||
label: string;
|
||||
type?: 'standard' | 'warning';
|
||||
type?: 'standard' | 'danger';
|
||||
onClick: () => void;
|
||||
};
|
||||
|
||||
type StyledButtonProps = {
|
||||
type: 'standard' | 'warning';
|
||||
type: 'standard' | 'danger';
|
||||
};
|
||||
|
||||
const StyledButton = styled.div<StyledButtonProps>`
|
||||
@ -17,7 +17,7 @@ const StyledButton = styled.div<StyledButtonProps>`
|
||||
align-self: stretch;
|
||||
border-radius: ${({ theme }) => theme.border.radius.sm};
|
||||
color: ${(props) =>
|
||||
props.type === 'warning'
|
||||
props.type === 'danger'
|
||||
? props.theme.color.red
|
||||
: props.theme.font.color.secondary};
|
||||
cursor: pointer;
|
||||
@ -32,9 +32,7 @@ const StyledButton = styled.div<StyledButtonProps>`
|
||||
|
||||
&:hover {
|
||||
background: ${({ theme, type }) =>
|
||||
type === 'warning'
|
||||
? theme.tag.background.red
|
||||
: theme.background.tertiary};
|
||||
type === 'danger' ? theme.tag.background.red : theme.background.tertiary};
|
||||
}
|
||||
`;
|
||||
|
||||
@ -43,7 +41,7 @@ const StyledButtonLabel = styled.div`
|
||||
margin-left: ${({ theme }) => theme.spacing(2)};
|
||||
`;
|
||||
|
||||
export function EntityTableContextMenuEntry({
|
||||
export function ContextMenuEntry({
|
||||
label,
|
||||
icon,
|
||||
type = 'standard',
|
||||
@ -5,12 +5,8 @@ import { ActionBar } from '@/ui/action-bar/components/ActionBar';
|
||||
|
||||
import { selectedRowIdsSelector } from '../../states/selectedRowIdsSelector';
|
||||
|
||||
type OwnProps = {
|
||||
children: React.ReactNode | React.ReactNode[];
|
||||
};
|
||||
|
||||
export function EntityTableActionBar({ children }: OwnProps) {
|
||||
export function EntityTableActionBar() {
|
||||
const selectedRowIds = useRecoilValue(selectedRowIdsSelector);
|
||||
|
||||
return <ActionBar selectedIds={selectedRowIds}>{children}</ActionBar>;
|
||||
return <ActionBar selectedIds={selectedRowIds}></ActionBar>;
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ import { useSetRecoilState } from 'recoil';
|
||||
import { Checkbox } from '@/ui/input/checkbox/components/Checkbox';
|
||||
|
||||
import { useCurrentRowSelected } from '../hooks/useCurrentRowSelected';
|
||||
import { contextMenuPositionState } from '../states/contextMenuPositionState';
|
||||
import { actionBarOpenState } from '../states/ActionBarIsOpenState';
|
||||
|
||||
const StyledContainer = styled.div`
|
||||
align-items: center;
|
||||
@ -18,14 +18,13 @@ const StyledContainer = styled.div`
|
||||
`;
|
||||
|
||||
export function CheckboxCell() {
|
||||
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
|
||||
|
||||
const setActionBarOpenState = useSetRecoilState(actionBarOpenState);
|
||||
const { currentRowSelected, setCurrentRowSelected } = useCurrentRowSelected();
|
||||
|
||||
const handleClick = useCallback(() => {
|
||||
setCurrentRowSelected(!currentRowSelected);
|
||||
setContextMenuPosition({ x: null, y: null });
|
||||
}, [currentRowSelected, setContextMenuPosition, setCurrentRowSelected]);
|
||||
setActionBarOpenState(true);
|
||||
}, [currentRowSelected, setActionBarOpenState, setCurrentRowSelected]);
|
||||
|
||||
return (
|
||||
<StyledContainer onClick={handleClick}>
|
||||
|
||||
@ -6,23 +6,24 @@ import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope'
|
||||
import { GenericEditableCell } from '../editable-cell/components/GenericEditableCell';
|
||||
import { useCurrentRowSelected } from '../hooks/useCurrentRowSelected';
|
||||
import { ColumnIndexContext } from '../states/ColumnIndexContext';
|
||||
import { contextMenuOpenState } from '../states/ContextMenuIsOpenState';
|
||||
import { contextMenuPositionState } from '../states/contextMenuPositionState';
|
||||
import { ViewFieldContext } from '../states/ViewFieldContext';
|
||||
|
||||
export function EntityTableCell({ cellIndex }: { cellIndex: number }) {
|
||||
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
|
||||
const setContextMenuOpenState = useSetRecoilState(contextMenuOpenState);
|
||||
|
||||
const { setCurrentRowSelected } = useCurrentRowSelected();
|
||||
|
||||
function handleContextMenu(event: React.MouseEvent) {
|
||||
event.preventDefault();
|
||||
|
||||
setCurrentRowSelected(true);
|
||||
|
||||
setContextMenuPosition({
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
});
|
||||
setContextMenuOpenState(true);
|
||||
}
|
||||
|
||||
const viewField = useContext(ViewFieldContext);
|
||||
|
||||
@ -5,12 +5,7 @@ import { ContextMenu } from '@/ui/context-menu/components/ContextMenu';
|
||||
|
||||
import { selectedRowIdsSelector } from '../../states/selectedRowIdsSelector';
|
||||
|
||||
type OwnProps = {
|
||||
children: React.ReactNode | React.ReactNode[];
|
||||
};
|
||||
|
||||
export function EntityTableContextMenu({ children }: OwnProps) {
|
||||
export function EntityTableContextMenu() {
|
||||
const selectedRowIds = useRecoilValue(selectedRowIdsSelector);
|
||||
|
||||
return <ContextMenu selectedIds={selectedRowIds}>{children}</ContextMenu>;
|
||||
return <ContextMenu selectedIds={selectedRowIds}></ContextMenu>;
|
||||
}
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
import { ReactElement } from 'react';
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const actionBarEntriesState = atom<ReactElement[]>({
|
||||
key: 'actionBarEntriesState',
|
||||
default: [],
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const actionBarOpenState = atom<boolean>({
|
||||
key: 'actionBarOpenState',
|
||||
default: false,
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
import { ReactElement } from 'react';
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const contextMenuEntriesState = atom<ReactElement[]>({
|
||||
key: 'contextMenuEntriesState',
|
||||
default: [],
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const contextMenuOpenState = atom<boolean>({
|
||||
key: 'contextMenuOpenState',
|
||||
default: false,
|
||||
});
|
||||
Reference in New Issue
Block a user