File size: 1,506 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import { ExternalLink } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import React from 'react';
import useSupportDocData from 'calypso/components/inline-support-link/use-support-doc-data'; // eslint-disable-line no-restricted-imports
import type { SupportDocData } from 'calypso/components/inline-support-link/types'; // eslint-disable-line no-restricted-imports
const InlineSupportLink = ( {
className,
title,
supportPostId,
supportLink,
supportContext,
children = __( 'Learn more' ),
onClick,
}: {
className?: string;
title?: string;
supportPostId?: number;
supportLink?: string;
supportContext?: string;
children?: React.ReactNode;
onClick?: ( supportData: SupportDocData | null ) => void;
} ) => {
const { supportDocData, openSupportDoc } = useSupportDocData( {
supportPostId,
supportLink,
supportContext,
} );
const handleClick = ( event: React.SyntheticEvent< HTMLAnchorElement > ) => {
if ( supportDocData?.postId ) {
event.preventDefault();
openSupportDoc();
}
onClick?.( supportDocData );
};
if ( ! supportDocData?.postId && ! supportDocData?.link ) {
return null;
}
const linkProps = {
className: className,
href: supportDocData.link,
title,
onClick: handleClick,
};
if ( supportDocData?.postId ) {
return (
<a { ...linkProps } target="_blank" rel="noopener noreferrer">
{ children }
</a>
);
}
return <ExternalLink { ...linkProps }>{ children }</ExternalLink>;
};
export default InlineSupportLink;
|