@@ -14,6 +14,7 @@ import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownM
14
14
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem' ;
15
15
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys' ;
16
16
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside' ;
17
+ import { toSpliced } from '~/utils/array/toSpliced' ;
17
18
import { isDefined } from '~/utils/isDefined' ;
18
19
19
20
const StyledDropdownMenu = styled ( DropdownMenu ) `
@@ -35,14 +36,13 @@ export const LinksFieldInput = ({
35
36
36
37
const containerRef = useRef < HTMLDivElement > ( null ) ;
37
38
38
- const links = useMemo < { url : string ; label : string ; isPrimary ?: boolean } [ ] > (
39
+ const links = useMemo < { url : string ; label : string } [ ] > (
39
40
( ) =>
40
41
[
41
42
fieldValue . primaryLinkUrl
42
43
? {
43
44
url : fieldValue . primaryLinkUrl ,
44
45
label : fieldValue . primaryLinkLabel ,
45
- isPrimary : true ,
46
46
}
47
47
: null ,
48
48
...( fieldValue . secondaryLinks ?? [ ] ) ,
@@ -74,37 +74,27 @@ export const LinksFieldInput = ({
74
74
setIsInputDisplayed ( false ) ;
75
75
setInputValue ( '' ) ;
76
76
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 ;
88
79
89
80
onSubmit ?.( ( ) =>
90
81
persistLinksField ( {
91
- ...fieldValue ,
92
- secondaryLinks : [
93
- ...( fieldValue . secondaryLinks ?? [ ] ) ,
94
- { label : '' , url : inputValue } ,
95
- ] ,
82
+ primaryLinkUrl : nextPrimaryLink . url ?? '' ,
83
+ primaryLinkLabel : nextPrimaryLink . label ?? '' ,
84
+ secondaryLinks : nextSecondaryLinks ,
96
85
} ) ,
97
86
) ;
98
87
} ;
99
88
100
89
const handleDeleteLink = ( index : number ) => {
101
- const nextSecondaryLinks = [ ...( fieldValue . secondaryLinks ?? [ ] ) ] ;
102
- nextSecondaryLinks . splice ( index - 1 , 1 ) ;
103
-
104
90
onSubmit ?.( ( ) =>
105
91
persistLinksField ( {
106
92
...fieldValue ,
107
- secondaryLinks : nextSecondaryLinks ,
93
+ secondaryLinks : toSpliced (
94
+ fieldValue . secondaryLinks ?? [ ] ,
95
+ index - 1 ,
96
+ 1 ,
97
+ ) ,
108
98
} ) ,
109
99
) ;
110
100
} ;
@@ -114,11 +104,11 @@ export const LinksFieldInput = ({
114
104
{ ! ! links . length && (
115
105
< >
116
106
< DropdownMenuItemsContainer >
117
- { links . map ( ( { isPrimary , label, url } , index ) => (
107
+ { links . map ( ( { label, url } , index ) => (
118
108
< LinksFieldMenuItem
119
109
key = { index }
120
110
dropdownId = { `${ hotkeyScope } -links-${ index } ` }
121
- isPrimary = { isPrimary }
111
+ isPrimary = { index === 0 }
122
112
label = { label }
123
113
onDelete = { ( ) => handleDeleteLink ( index ) }
124
114
url = { url }
0 commit comments