Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display columns on Record Board #3626

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useRef } from 'react';
import styled from '@emotion/styled';
import { DragDropContext } 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 { useRecoilValue } from 'recoil';

import { useRecordBoard } from '@/object-record/record-board/hooks/useRecordBoard';
import { RecordBoardColumn } from '@/object-record/record-board/record-board-column/components/RecordBoardColumn';
import { RecordBoardScope } from '@/object-record/record-board/scopes/RecordBoardScope';
import { DragSelect } from '@/ui/utilities/drag-select/components/DragSelect';
import { getScopeIdFromComponentIdStrict } from '@/ui/utilities/recoil-scope/utils/getScopeIdFromComponentIdStrict';
import { ScrollWrapper } from '@/ui/utilities/scroll/components/ScrollWrapper';

export type RecordBoardProps = {
Expand Down Expand Up @@ -37,9 +40,13 @@ const StyledBoardHeader = styled.div`
export const RecordBoard = ({ recordBoardId }: RecordBoardProps) => {
const boardRef = useRef<HTMLDivElement>(null);

const { getColumnIdsState } = useRecordBoard(recordBoardId);

const columnIds = useRecoilValue(getColumnIdsState());

return (
<RecordBoardScope
recordBoardScopeId={recordBoardId}
recordBoardScopeId={getScopeIdFromComponentIdStrict(recordBoardId)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why strict here? I feel like strict does not really tell what you want?

onColumnsChange={() => {}}
onFieldsChange={() => {}}
>
Expand All @@ -48,11 +55,10 @@ export const RecordBoard = ({ recordBoardId }: RecordBoardProps) => {
<ScrollWrapper>
<StyledContainer ref={boardRef}>
<DragDropContext onDragEnd={() => {}}>
{[].map((column) => (
{columnIds.map((columnId) => (
<RecordBoardColumn
key={'a'}
recordBoardColumnId={'a'}
columnDefinition={column}
key={columnId}
recordBoardColumnId={columnId}
/>
))}
</DragDropContext>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { createContext } from 'react';

import { BoardColumnDefinition } from '@/object-record/record-board-deprecated/types/BoardColumnDefinition';
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition';

type RecordBoardColumnContextProps = {
id: string;
columnDefinition: BoardColumnDefinition;
columnDefinition: RecordBoardColumnDefinition;
isColumnFirst: boolean;
isColumnLast: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have name is isFirst/LastColumn

};

export const RecordBoardColumnContext =
createContext<RecordBoardColumnContextProps | null>(null);
createContext<RecordBoardColumnContextProps>(
{} as RecordBoardColumnContextProps,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { RecordBoardScopeInternalContext } from '@/object-record/record-board/scopes/scope-internal-context/RecordBoardScopeInternalContext';
import { isRecordBoardColumnFirstFamilyStateScopeMap } from '@/object-record/record-board/states/isRecordBoardColumnFirstFamilyStateScopeMap';
import { recordBoardColumnIdsStateScopeMap } from '@/object-record/record-board/states/recordBoardColumnIdsStateScopeMap';
import { recordBoardColumnsFamilySelectorScopeMap } from '@/object-record/record-board/states/selectors/recordBoardColumnsFamilySelectorScopeMap';
import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId';
import { getFamilyState } from '@/ui/utilities/recoil-scope/utils/getFamilyState';
import { getScopeIdFromComponentId } from '@/ui/utilities/recoil-scope/utils/getScopeIdFromComponentId';
import { getState } from '@/ui/utilities/recoil-scope/utils/getState';

export const useRecordBoardStates = (recordBoardId?: string) => {
const scopeId = useAvailableScopeIdOrThrow(
RecordBoardScopeInternalContext,
getScopeIdFromComponentId(recordBoardId),
);

return {
scopeId,
getColumnIdsState: getState(recordBoardColumnIdsStateScopeMap, scopeId),
isColumnLastFamilyState: getFamilyState(
isRecordBoardColumnFirstFamilyStateScopeMap,
scopeId,
),
isColumnFirstFamilyState: getFamilyState(
isRecordBoardColumnFirstFamilyStateScopeMap,
scopeId,
),
columnsFamilySelector: getFamilyState(
recordBoardColumnsFamilySelectorScopeMap,
scopeId,
),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useRecoilCallback } from 'recoil';

import { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates';
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';

export const useSetRecordBoardColumns = (recordBoardId?: string) => {
const { scopeId, getColumnIdsState, columnsFamilySelector } =
useRecordBoardStates(recordBoardId);

const setRecordBoardColumns = useRecoilCallback(
({ set, snapshot }) =>
(columns: RecordBoardColumnDefinition[]) => {
const currentColumns = snapshot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const currentColumns = snapshot
const currentColumnIds = snapshot

.getLoadable(getColumnIdsState())
.getValue();

const columnIds = columns.map(({ id }) => id);
Weiko marked this conversation as resolved.
Show resolved Hide resolved

if (isDeeplyEqual(currentColumns, columnIds)) {
return;
}

set(
getColumnIdsState(),
columns.map(({ id }) => id),
);

columns.forEach((column) => {
const currentColumn = snapshot
.getLoadable(columnsFamilySelector(column.id))
.getValue();

if (isDeeplyEqual(currentColumn, column)) {
return;
}

set(columnsFamilySelector(column.id), column);
});
},
[columnsFamilySelector, getColumnIdsState],
);

return {
scopeId,
setRecordBoardColumns,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates';
import { useSetRecordBoardColumns } from '@/object-record/record-board/hooks/internal/useSetRecordBoardColumns';

export const useRecordBoard = (recordBoardId?: string) => {
const { scopeId, getColumnIdsState, columnsFamilySelector } =
useRecordBoardStates(recordBoardId);

const { setRecordBoardColumns } = useSetRecordBoardColumns(recordBoardId);

return {
scopeId,
getColumnIdsState,
columnsFamilySelector,
setRecordBoardColumns,
};
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import styled from '@emotion/styled';
import { Droppable } from '@hello-pangea/dnd';
import { useRecoilValue } from 'recoil';

import { RecordBoardColumnContext } from '@/object-record/record-board/contexts/RecordBoardColumnContext';
import { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates';
import { RecordBoardColumnCardsContainer } from '@/object-record/record-board/record-board-column/components/RecordBoardColumnCardsContainer';
import { RecordBoardColumnHeader } from '@/object-record/record-board/record-board-column/components/RecordBoardColumnHeader';
import { BoardCardIdContext } from '@/object-record/record-board-deprecated/contexts/BoardCardIdContext';
import { BoardColumnDefinition } from '@/object-record/record-board-deprecated/types/BoardColumnDefinition';

const StyledColumn = styled.div<{ isFirstColumn: boolean }>`
background-color: ${({ theme }) => theme.background.primary};
Expand All @@ -22,25 +24,44 @@ const StyledColumn = styled.div<{ isFirstColumn: boolean }>`

type RecordBoardColumnProps = {
recordBoardColumnId: string;
columnDefinition: BoardColumnDefinition;
};

export const RecordBoardColumn = ({
recordBoardColumnId,
columnDefinition,
}: RecordBoardColumnProps) => {
const isFirstColumn = columnDefinition.position === 0;
const {
isColumnFirstFamilyState,
isColumnLastFamilyState,
columnsFamilySelector,
} = useRecordBoardStates();
const columnDefinition = useRecoilValue(
columnsFamilySelector(recordBoardColumnId),
);

const isColumnFirst = useRecoilValue(
isColumnFirstFamilyState(recordBoardColumnId),
);

const isColumnLast = useRecoilValue(
isColumnLastFamilyState(recordBoardColumnId),
);

if (!columnDefinition) {
return null;
}

return (
<RecordBoardColumnContext.Provider
value={{
id: recordBoardColumnId,
columnDefinition: columnDefinition,
isColumnFirst: isColumnFirst,
isColumnLast: isColumnLast,
}}
>
<Droppable droppableId={recordBoardColumnId}>
{(droppableProvided) => (
<StyledColumn isFirstColumn={isFirstColumn}>
<StyledColumn isFirstColumn={isColumnFirst}>
<RecordBoardColumnHeader />
<RecordBoardColumnCardsContainer
droppableProvided={droppableProvided}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useCallback, useContext, useRef } from 'react';
import styled from '@emotion/styled';
import { MenuItem } from 'tsup.ui.index';

import { RecordBoardColumnContext } from '@/object-record/record-board/contexts/RecordBoardColumnContext';
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';

const StyledMenuContainer = styled.div`
position: absolute;
top: ${({ theme }) => theme.spacing(10)};
width: 200px;
z-index: 1;
`;

type RecordBoardColumnDropdownMenuProps = {
onClose: () => void;
onDelete?: (id: string) => void;
stageId: string;
};

export const RecordBoardColumnDropdownMenu = ({
onClose,
}: RecordBoardColumnDropdownMenuProps) => {
const boardColumnMenuRef = useRef<HTMLDivElement>(null);

const closeMenu = useCallback(() => {
onClose();
}, [onClose]);

useListenClickOutside({
refs: [boardColumnMenuRef],
callback: closeMenu,
});

const { columnDefinition } = useContext(RecordBoardColumnContext);

return (
<StyledMenuContainer ref={boardColumnMenuRef}>
<DropdownMenu data-select-disable>
<DropdownMenuItemsContainer>
{columnDefinition.actions.map((action) => (
<MenuItem
key={action.id}
onClick={() => {
action.callback();
closeMenu();
}}
LeftIcon={action.icon}
text={action.label}
/>
))}
</DropdownMenuItemsContainer>
</DropdownMenu>
</StyledMenuContainer>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React, { useContext, useState } from 'react';
import styled from '@emotion/styled';

import { RecordBoardColumnContext } from '@/object-record/record-board/contexts/RecordBoardColumnContext';
import { RecordBoardColumnDropdownMenu } from '@/object-record/record-board/record-board-column/components/RecordBoardColumnDropdownMenu';
import { BoardColumnHotkeyScope } from '@/object-record/record-board-deprecated/types/BoardColumnHotkeyScope';
import { IconDotsVertical } from '@/ui/display/icon';
import { Tag } from '@/ui/display/tag/components/Tag';
import { LightIconButton } from '@/ui/input/button/components/LightIconButton';
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';

const StyledHeader = styled.div`
align-items: center;
cursor: pointer;
display: flex;
flex-direction: row;
height: 24px;
justify-content: left;
margin-bottom: ${({ theme }) => theme.spacing(2)};
width: 100%;
`;

const StyledAmount = styled.div`
color: ${({ theme }) => theme.font.color.tertiary};
margin-left: ${({ theme }) => theme.spacing(2)};
`;

const StyledNumChildren = styled.div`
align-items: center;
background-color: ${({ theme }) => theme.background.tertiary};
border-radius: ${({ theme }) => theme.border.radius.rounded};
color: ${({ theme }) => theme.font.color.tertiary};
display: flex;
height: 20px;
justify-content: center;
line-height: ${({ theme }) => theme.text.lineHeight.lg};
margin-left: auto;
width: 16px;
`;

const StyledHeaderActions = styled.div`
display: flex;
margin-left: auto;
`;

export const RecordBoardColumnHeader = () => {
const [isBoardColumnMenuOpen, setIsBoardColumnMenuOpen] = useState(false);
const [isHeaderHovered, setIsHeaderHovered] = useState(false);

const { columnDefinition } = useContext(RecordBoardColumnContext);

const {
setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope,
} = usePreviousHotkeyScope();

const handleBoardColumnMenuOpen = () => {
setIsBoardColumnMenuOpen(true);
setHotkeyScopeAndMemorizePreviousScope(BoardColumnHotkeyScope.BoardColumn, {
goto: false,
});
};

const handleBoardColumnMenuClose = () => {
goBackToPreviousHotkeyScope();
setIsBoardColumnMenuOpen(false);
};

const boardColumnTotal = 0;
const cardIds = [];

return (
<>
<StyledHeader
onMouseEnter={() => setIsHeaderHovered(true)}
onMouseLeave={() => setIsHeaderHovered(false)}
>
<Tag
onClick={handleBoardColumnMenuOpen}
color={columnDefinition.color}
text={columnDefinition.title}
/>
{!!boardColumnTotal && <StyledAmount>${boardColumnTotal}</StyledAmount>}
{!isHeaderHovered && (
<StyledNumChildren>{cardIds.length}</StyledNumChildren>
)}
{isHeaderHovered && (
<StyledHeaderActions>
<LightIconButton
accent="tertiary"
Icon={IconDotsVertical}
onClick={handleBoardColumnMenuOpen}
/>
</StyledHeaderActions>
)}
</StyledHeader>
{isBoardColumnMenuOpen && (
<RecordBoardColumnDropdownMenu
onClose={handleBoardColumnMenuClose}
stageId={columnDefinition.id}
/>
)}
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createFamilyStateScopeMap } from '@/ui/utilities/recoil-scope/utils/createFamilyStateScopeMap';

export const isRecordBoardColumnFirstFamilyStateScopeMap =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also change this naming

createFamilyStateScopeMap<boolean, string>({
key: 'isRecordBoardColumnFirstFamilyStateScopeMap',
defaultValue: false,
});
Loading
Loading