Skip to content

Commit 2b04b90

Browse files
committed
refactor: use toSpliced to delete link, improve handleAddLink, and remove isPrimary property
1 parent 1de92bf commit 2b04b90

File tree

1 file changed

+14
-24
lines changed
  • packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components

1 file changed

+14
-24
lines changed

packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/LinksFieldInput.tsx

+14-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownM
1414
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
1515
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
1616
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
17+
import { toSpliced } from '~/utils/array/toSpliced';
1718
import { isDefined } from '~/utils/isDefined';
1819

1920
const StyledDropdownMenu = styled(DropdownMenu)`
@@ -35,14 +36,13 @@ export const LinksFieldInput = ({
3536

3637
const containerRef = useRef<HTMLDivElement>(null);
3738

38-
const links = useMemo<{ url: string; label: string; isPrimary?: boolean }[]>(
39+
const links = useMemo<{ url: string; label: string }[]>(
3940
() =>
4041
[
4142
fieldValue.primaryLinkUrl
4243
? {
4344
url: fieldValue.primaryLinkUrl,
4445
label: fieldValue.primaryLinkLabel,
45-
isPrimary: true,
4646
}
4747
: null,
4848
...(fieldValue.secondaryLinks ?? []),
@@ -74,37 +74,27 @@ export const LinksFieldInput = ({
7474
setIsInputDisplayed(false);
7575
setInputValue('');
7676

77-
if (!links.length) {
78-
onSubmit?.(() =>
79-
persistLinksField({
80-
primaryLinkUrl: inputValue,
81-
primaryLinkLabel: '',
82-
secondaryLinks: [],
83-
}),
84-
);
85-
86-
return;
87-
}
77+
const nextLinks = [...links, { label: '', url: inputValue }];
78+
const [nextPrimaryLink, ...nextSecondaryLinks] = nextLinks;
8879

8980
onSubmit?.(() =>
9081
persistLinksField({
91-
...fieldValue,
92-
secondaryLinks: [
93-
...(fieldValue.secondaryLinks ?? []),
94-
{ label: '', url: inputValue },
95-
],
82+
primaryLinkUrl: nextPrimaryLink.url ?? '',
83+
primaryLinkLabel: nextPrimaryLink.label ?? '',
84+
secondaryLinks: nextSecondaryLinks,
9685
}),
9786
);
9887
};
9988

10089
const handleDeleteLink = (index: number) => {
101-
const nextSecondaryLinks = [...(fieldValue.secondaryLinks ?? [])];
102-
nextSecondaryLinks.splice(index - 1, 1);
103-
10490
onSubmit?.(() =>
10591
persistLinksField({
10692
...fieldValue,
107-
secondaryLinks: nextSecondaryLinks,
93+
secondaryLinks: toSpliced(
94+
fieldValue.secondaryLinks ?? [],
95+
index - 1,
96+
1,
97+
),
10898
}),
10999
);
110100
};
@@ -114,11 +104,11 @@ export const LinksFieldInput = ({
114104
{!!links.length && (
115105
<>
116106
<DropdownMenuItemsContainer>
117-
{links.map(({ isPrimary, label, url }, index) => (
107+
{links.map(({ label, url }, index) => (
118108
<LinksFieldMenuItem
119109
key={index}
120110
dropdownId={`${hotkeyScope}-links-${index}`}
121-
isPrimary={isPrimary}
111+
isPrimary={index === 0}
122112
label={label}
123113
onDelete={() => handleDeleteLink(index)}
124114
url={url}

0 commit comments

Comments
 (0)