|
| 1 | +import { useSelector } from 'react-redux'; |
| 2 | +import { useLocation, useHistory } from 'react-router-dom'; |
| 3 | +import type { ObjectBrowserItem, GetSiteResponse } from '@plone/types'; |
| 4 | +import linkSVG from '@plone/volto/icons/link.svg'; |
| 5 | +import Icon from '@plone/volto/components/theme/Icon/Icon'; |
| 6 | +import { Button } from '@plone/components'; |
| 7 | +import { flattenToAppURL } from '@plone/volto/helpers/Url/Url'; |
| 8 | + |
| 9 | +type FormState = { |
| 10 | + site: { data: GetSiteResponse }; |
| 11 | +}; |
| 12 | + |
| 13 | +const useLinkIconNavigation = (item?: Partial<ObjectBrowserItem>) => { |
| 14 | + const location = useLocation(); |
| 15 | + const history = useHistory(); |
| 16 | + |
| 17 | + const handleLinkIconClick = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 18 | + e.preventDefault(); |
| 19 | + e.stopPropagation(); |
| 20 | + |
| 21 | + const targetUrl = item?.['@id']; |
| 22 | + if (targetUrl) { |
| 23 | + const flattenedTargetUrl = flattenToAppURL(targetUrl); |
| 24 | + const searchParams = new URLSearchParams(); |
| 25 | + searchParams.set('return_to', location.pathname); |
| 26 | + |
| 27 | + history.push({ |
| 28 | + pathname: flattenedTargetUrl, |
| 29 | + search: searchParams.toString(), |
| 30 | + }); |
| 31 | + } |
| 32 | + }; |
| 33 | + return handleLinkIconClick; |
| 34 | +}; |
| 35 | +const LinkIconButton = ({ item }: { item?: Partial<ObjectBrowserItem> }) => { |
| 36 | + const site = useSelector<FormState, GetSiteResponse>( |
| 37 | + (state) => state.site?.data, |
| 38 | + ); |
| 39 | + const hideProfileLinks = site?.['kitconcept.disable_profile_links']; |
| 40 | + const handleLinkIconClick = useLinkIconNavigation(item); |
| 41 | + return ( |
| 42 | + hideProfileLinks && ( |
| 43 | + <div className="card-link-icon"> |
| 44 | + <Button aria-label="link" onClick={handleLinkIconClick}> |
| 45 | + <Icon name={linkSVG} size="33px" /> |
| 46 | + </Button> |
| 47 | + </div> |
| 48 | + ) |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default LinkIconButton; |
0 commit comments