|
| 1 | +/* |
| 2 | + * Copyright 2025 EPAM Systems |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React, { useState } from 'react'; |
| 18 | +import { useDrag, useDrop } from 'react-dnd'; |
| 19 | +import { Icon } from 'components/main/icon'; |
| 20 | +import { DragControl } from 'pages/inside/projectSettingsPageContainer/content/elements/ruleList/ruleItem/draggable/dragControl'; |
| 21 | +import classNames from 'classnames/bind'; |
| 22 | +import PropTypes from 'prop-types'; |
| 23 | +import { useTracking } from 'react-tracking'; |
| 24 | +import { ADMIN_SERVER_SETTINGS_PAGE_EVENTS } from 'components/main/analytics/events'; |
| 25 | +import styles from '../linksAndBrandingTab.scss'; |
| 26 | + |
| 27 | +const cx = classNames.bind(styles); |
| 28 | +const FOOTER_LINK_DRAG_SOURCE_TYPE = 'FOOTER_LINK'; |
| 29 | +export const DraggableLink = ({ item, onDrop, onDelete, disabled, disabledTitle }) => { |
| 30 | + const [isDragging, setIsDragging] = useState(false); |
| 31 | + const { trackEvent } = useTracking(); |
| 32 | + const [{ isInDraggingState }, dragRef, dragPreviewRef] = useDrag( |
| 33 | + () => ({ |
| 34 | + type: FOOTER_LINK_DRAG_SOURCE_TYPE, |
| 35 | + item: { name: item.name, index: item.index }, |
| 36 | + collect: (monitor) => ({ |
| 37 | + isInDraggingState: monitor.isDragging(), |
| 38 | + }), |
| 39 | + canDrag: () => !disabled, |
| 40 | + }), |
| 41 | + [item], |
| 42 | + ); |
| 43 | + const [{ draggedItemIndex }, dropRef] = useDrop( |
| 44 | + () => ({ |
| 45 | + accept: FOOTER_LINK_DRAG_SOURCE_TYPE, |
| 46 | + collect: (monitor) => { |
| 47 | + const draggedItem = monitor.getItem(); |
| 48 | + const isAbleToDrop = draggedItem?.name !== item.name; |
| 49 | + const isOver = isAbleToDrop ? monitor.isOver() : false; |
| 50 | + return { |
| 51 | + draggedItemIndex: isOver ? draggedItem?.index : null, |
| 52 | + }; |
| 53 | + }, |
| 54 | + drop: (dragObject) => { |
| 55 | + if (dragObject.name !== item.name) { |
| 56 | + onDrop(dragObject.index, item.index); |
| 57 | + } |
| 58 | + }, |
| 59 | + }), |
| 60 | + [item, onDrop], |
| 61 | + ); |
| 62 | + const dropTargetType = draggedItemIndex > item.index ? 'top' : 'bottom'; |
| 63 | + const handleDragStart = () => { |
| 64 | + setIsDragging(true); |
| 65 | + }; |
| 66 | + const handleDragEnd = () => { |
| 67 | + trackEvent(ADMIN_SERVER_SETTINGS_PAGE_EVENTS.DRAG_END_FOOTER_LINK); |
| 68 | + setIsDragging(false); |
| 69 | + }; |
| 70 | + return ( |
| 71 | + <div |
| 72 | + key={item.name} |
| 73 | + className={cx('link-item', { |
| 74 | + [`drop-target-${dropTargetType}`]: draggedItemIndex !== null, |
| 75 | + })} |
| 76 | + ref={(node) => dropRef(dragPreviewRef(node))} |
| 77 | + style={{ opacity: isInDraggingState ? 0 : 1 }} |
| 78 | + > |
| 79 | + <div className={cx('content', { 'is-dragging': isDragging })}> |
| 80 | + <div className={cx('link-item-name')}>{item.name}</div> |
| 81 | + <div className={cx('link-item-url')}> |
| 82 | + {item.url.startsWith('mailto:') ? item.url.slice(7) : item.url} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + {!isDragging && ( |
| 86 | + <Icon |
| 87 | + type="icon-delete" |
| 88 | + className={cx('icon-delete')} |
| 89 | + onClick={() => onDelete(item.name)} |
| 90 | + /> |
| 91 | + )} |
| 92 | + <DragControl |
| 93 | + dragRef={dragRef} |
| 94 | + dragPreviewRef={dragPreviewRef} |
| 95 | + handleDragStart={handleDragStart} |
| 96 | + handleDragEnd={handleDragEnd} |
| 97 | + isDragging={isDragging} |
| 98 | + disabled={disabled} |
| 99 | + title={disabled ? disabledTitle : ''} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +DraggableLink.propTypes = { |
| 106 | + item: PropTypes.shape({ |
| 107 | + name: PropTypes.string.isRequired, |
| 108 | + url: PropTypes.string.isRequired, |
| 109 | + index: PropTypes.number.isRequired, |
| 110 | + }).isRequired, |
| 111 | + onDrop: PropTypes.func.isRequired, |
| 112 | + onDelete: PropTypes.func.isRequired, |
| 113 | + disabled: PropTypes.bool, |
| 114 | + disabledTitle: PropTypes.string, |
| 115 | +}; |
0 commit comments