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

feat: remove a link from a Links field #5313

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -4,8 +4,8 @@ import { Key } from 'ts-key-enum';
import { IconPlus } from 'twenty-ui';

import { useLinksField } from '@/object-record/record-field/meta-types/hooks/useLinksField';
import { LinksFieldMenuItem } from '@/object-record/record-field/meta-types/input/components/LinksFieldMenuItem';
import { FieldInputEvent } from '@/object-record/record-field/types/FieldInputEvent';
import { LinkDisplay } from '@/ui/field/display/components/LinkDisplay';
import { LightIconButton } from '@/ui/input/button/components/LightIconButton';
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu';
import { DropdownMenuInput } from '@/ui/layout/dropdown/components/DropdownMenuInput';
Expand All @@ -14,6 +14,7 @@ import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownM
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
import { toSpliced } from '~/utils/array/toSpliced';
import { isDefined } from '~/utils/isDefined';

const StyledDropdownMenu = styled(DropdownMenu)`
Expand All @@ -35,7 +36,7 @@ export const LinksFieldInput = ({

const containerRef = useRef<HTMLDivElement>(null);

const links = useMemo(
const links = useMemo<{ url: string; label: string }[]>(
() =>
[
fieldValue.primaryLinkUrl
Expand All @@ -53,51 +54,47 @@ export const LinksFieldInput = ({
],
);

const handleDropdownClose = () => {
onCancel?.();
};

useListenClickOutside({
refs: [containerRef],
callback: (event) => {
event.stopImmediatePropagation();

const isTargetInput =
event.target instanceof HTMLInputElement &&
event.target.tagName === 'INPUT';

if (!isTargetInput) {
onCancel?.();
}
},
callback: handleDropdownClose,
});

useScopedHotkeys(Key.Escape, handleDropdownClose, hotkeyScope);

const [isInputDisplayed, setIsInputDisplayed] = useState(false);
const [inputValue, setInputValue] = useState('');

useScopedHotkeys(Key.Escape, onCancel ?? (() => {}), hotkeyScope);

const handleSubmit = () => {
const handleAddLink = () => {
if (!inputValue) return;

setIsInputDisplayed(false);
setInputValue('');

if (!links.length) {
onSubmit?.(() =>
persistLinksField({
primaryLinkUrl: inputValue,
primaryLinkLabel: '',
secondaryLinks: [],
}),
);
const nextLinks = [...links, { label: '', url: inputValue }];
const [nextPrimaryLink, ...nextSecondaryLinks] = nextLinks;

return;
}
onSubmit?.(() =>
persistLinksField({
primaryLinkUrl: nextPrimaryLink.url ?? '',
primaryLinkLabel: nextPrimaryLink.label ?? '',
secondaryLinks: nextSecondaryLinks,
}),
);
};

const handleDeleteLink = (index: number) => {
onSubmit?.(() =>
persistLinksField({
...fieldValue,
secondaryLinks: [
...(fieldValue.secondaryLinks ?? []),
{ label: '', url: inputValue },
],
secondaryLinks: toSpliced(
fieldValue.secondaryLinks ?? [],
index - 1,
1,
),
}),
);
};
Expand All @@ -108,9 +105,13 @@ export const LinksFieldInput = ({
<>
<DropdownMenuItemsContainer>
{links.map(({ label, url }, index) => (
<MenuItem
<LinksFieldMenuItem
key={index}
text={<LinkDisplay value={{ label, url }} />}
dropdownId={`${hotkeyScope}-links-${index}`}
isPrimary={index === 0}
label={label}
onDelete={() => handleDeleteLink(index)}
url={url}
/>
))}
</DropdownMenuItemsContainer>
Expand All @@ -124,9 +125,9 @@ export const LinksFieldInput = ({
value={inputValue}
hotkeyScope={hotkeyScope}
onChange={(event) => setInputValue(event.target.value)}
onEnter={handleSubmit}
onEnter={handleAddLink}
rightComponent={
<LightIconButton Icon={IconPlus} onClick={handleSubmit} />
<LightIconButton Icon={IconPlus} onClick={handleAddLink} />
}
/>
) : (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import styled from '@emotion/styled';
import {
IconBookmark,
IconComponent,
IconDotsVertical,
IconTrash,
} from 'twenty-ui';

import { LinkDisplay } from '@/ui/field/display/components/LinkDisplay';
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';

type LinksFieldMenuItemProps = {
dropdownId: string;
isPrimary?: boolean;
label: string;
onDelete: () => void;
url: string;
};

const StyledIconBookmark = styled(IconBookmark)`
color: ${({ theme }) => theme.font.color.light};
height: ${({ theme }) => theme.icon.size.sm}px;
width: ${({ theme }) => theme.icon.size.sm}px;
`;

export const LinksFieldMenuItem = ({
dropdownId,
isPrimary,
label,
onDelete,
url,
}: LinksFieldMenuItemProps) => {
const { isDropdownOpen } = useDropdown(dropdownId);

return (
<MenuItem
text={<LinkDisplay value={{ label, url }} />}
isIconDisplayedOnHoverOnly={!isPrimary && !isDropdownOpen}
iconButtons={[
{
Wrapper: isPrimary
? undefined
: ({ iconButton }) => (
<Dropdown
dropdownId={dropdownId}
dropdownHotkeyScope={{
scope: dropdownId,
}}
dropdownPlacement="right-start"
dropdownStrategy="fixed"
disableBlur
clickableComponent={iconButton}
dropdownComponents={
<DropdownMenuItemsContainer>
<MenuItem
accent="danger"
LeftIcon={IconTrash}
text="Delete"
onClick={onDelete}
/>
</DropdownMenuItemsContainer>
}
/>
),
Icon: isPrimary
? (StyledIconBookmark as IconComponent)
: IconDotsVertical,
accent: 'tertiary',
onClick: isPrimary ? undefined : () => {},
},
]}
/>
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEvent } from 'react';
import { FunctionComponent, MouseEvent, ReactElement } from 'react';
import styled from '@emotion/styled';
import { IconComponent } from 'twenty-ui';

Expand All @@ -14,7 +14,9 @@ export type LightIconButtonGroupProps = Pick<
'className' | 'size'
> & {
iconButtons: {
Wrapper?: FunctionComponent<{ iconButton: ReactElement }>;
Icon: IconComponent;
accent?: LightIconButtonProps['accent'];
onClick?: (event: MouseEvent<any>) => void;
disabled?: boolean;
}[];
Expand All @@ -26,16 +28,26 @@ export const LightIconButtonGroup = ({
className,
}: LightIconButtonGroupProps) => (
<StyledLightIconButtonGroupContainer className={className}>
{iconButtons.map(({ Icon, onClick }, index) => {
return (
{iconButtons.map(({ Wrapper, Icon, accent, onClick }, index) => {
const iconButton = (
<LightIconButton
key={`light-icon-button-${index}`}
Icon={Icon}
accent={accent}
disabled={!onClick}
onClick={onClick}
size={size}
/>
);

return Wrapper ? (
<Wrapper
key={`light-icon-button-wrapper-${index}`}
iconButton={iconButton}
/>
) : (
iconButton
);
})}
</StyledLightIconButtonGroupContainer>
);
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export const InternalDatePicker = ({
/>
</div>
{clearable && (
<StyledButtonContainer onClick={handleClear} isMenuOpen={false}>
<StyledButtonContainer onClick={handleClear}>
<StyledButton LeftIcon={IconCalendarX} text="Clear" />
</StyledButtonContainer>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type DropdownProps = {
dropdownPlacement?: Placement;
dropdownMenuWidth?: `${string}px` | `${number}%` | 'auto' | number;
dropdownOffset?: { x?: number; y?: number };
dropdownStrategy?: 'fixed' | 'absolute';
disableBlur?: boolean;
onClickOutside?: () => void;
onClose?: () => void;
Expand All @@ -51,6 +52,7 @@ export const Dropdown = ({
dropdownId,
dropdownHotkeyScope,
dropdownPlacement = 'bottom-end',
dropdownStrategy = 'absolute',
dropdownOffset = { x: 0, y: 0 },
disableBlur = false,
onClickOutside,
Expand All @@ -75,6 +77,7 @@ export const Dropdown = ({
placement: dropdownPlacement,
middleware: [flip(), ...offsetMiddlewares],
whileElementsMounted: autoUpdate,
strategy: dropdownStrategy,
});

const handleHotkeyTriggered = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const StyledDropdownMenu = styled.div<{

flex-direction: column;
z-index: 1;
width: ${({ width }) =>
width ? `${typeof width === 'number' ? `${width}px` : width}` : '160px'};
width: ${({ width = 160 }) =>
typeof width === 'number' ? `${width}px` : width};
`;

export const DropdownMenu = StyledDropdownMenu;
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const useDropdown = (dropdownId?: string) => {

return {
scopeId,
isDropdownOpen: isDropdownOpen,
isDropdownOpen,
closeDropdown,
toggleDropdown,
openDropdown,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MouseEvent, ReactNode } from 'react';
import { FunctionComponent, MouseEvent, ReactElement, ReactNode } from 'react';
import { IconComponent } from 'twenty-ui';

import { LightIconButtonProps } from '@/ui/input/button/components/LightIconButton';
import { LightIconButtonGroup } from '@/ui/input/button/components/LightIconButtonGroup';

import { MenuItemLeftContent } from '../internals/components/MenuItemLeftContent';
Expand All @@ -11,7 +12,9 @@ import {
import { MenuItemAccent } from '../types/MenuItemAccent';

export type MenuItemIconButton = {
Wrapper?: FunctionComponent<{ iconButton: ReactElement }>;
Icon: IconComponent;
accent?: LightIconButtonProps['accent'];
onClick?: (event: MouseEvent<any>) => void;
};

Expand All @@ -24,23 +27,22 @@ export type MenuItemProps = {
isTooltipOpen?: boolean;
className?: string;
testId?: string;
onClick?: (event: MouseEvent<HTMLLIElement>) => void;
onClick?: (event: MouseEvent<HTMLDivElement>) => void;
};

export const MenuItem = ({
LeftIcon,
accent = 'default',
text,
iconButtons,
isTooltipOpen,
isIconDisplayedOnHoverOnly = true,
className,
testId,
onClick,
}: MenuItemProps) => {
const showIconButtons = Array.isArray(iconButtons) && iconButtons.length > 0;

const handleMenuItemClick = (event: MouseEvent<HTMLLIElement>) => {
const handleMenuItemClick = (event: MouseEvent<HTMLDivElement>) => {
if (!onClick) return;
event.preventDefault();
event.stopPropagation();
Expand All @@ -54,7 +56,6 @@ export const MenuItem = ({
onClick={handleMenuItemClick}
className={className}
accent={accent}
isMenuOpen={!!isTooltipOpen}
isIconDisplayedOnHoverOnly={isIconDisplayedOnHoverOnly}
>
<StyledMenuItemLeftContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const MenuItemDraggable = ({
LeftIcon,
accent = 'default',
iconButtons,
isTooltipOpen,
onClick,
text,
isDragDisabled = false,
Expand All @@ -37,7 +36,6 @@ export const MenuItemDraggable = ({
onClick={onClick}
accent={accent}
className={className}
isMenuOpen={!!isTooltipOpen}
isIconDisplayedOnHoverOnly={isIconDisplayedOnHoverOnly}
>
<MenuItemLeftContent
Expand Down
Loading
Loading