Skip to content

Commit

Permalink
Add event for citation export download/copy
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Nov 7, 2023
1 parent 73db926 commit 893cec4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/components/CitationExporter/components/ResultArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useDownloadFile } from '@lib/useDownloadFile';
import { useIsClient } from '@lib/useIsClient';
import { exportFormats } from '../models';
import { LabeledCopyButton } from '@components/CopyButton';
import { useGTMDispatch } from '@elgorditosalsero/react-gtm-hook';

export const ResultArea = ({
result = '',
Expand All @@ -16,8 +17,16 @@ export const ResultArea = ({
format: ExportApiFormatKey;
isLoading?: boolean;
} & StackProps) => {
const sendDataToGTM = useGTMDispatch();
const { onDownload, hasDownloaded, isDownloading } = useDownloadFile(result, {
filename: () => `export-${format}.${exportFormats[format].ext}`,
onDownloaded() {
sendDataToGTM({
event: 'citation_export',
export_type: 'download',
export_format: format,
});
},
});
const isFullWidth = useBreakpointValue([true, false]);
const isClient = useIsClient();
Expand All @@ -42,6 +51,13 @@ export const ResultArea = ({
isDisabled={isLoading}
width={isFullWidth ? 'full' : 'auto'}
variant="outline"
onCopyComplete={() => {
sendDataToGTM({
event: 'citation_export',
export_type: 'copy',
export_format: format,
});
}}
/>
</HStack>
)}
Expand Down
17 changes: 15 additions & 2 deletions src/components/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import { ReactElement, useEffect } from 'react';

export interface ICopyButtonProps extends ButtonProps {
text: string;
onCopyComplete?: () => void;
options?: UseClipboardOptions;
}

export const SimpleCopyButton = (props: ICopyButtonProps): ReactElement => {
const { text, options, ...rest } = props;
const { text, options, onCopyComplete, ...rest } = props;
const { hasCopied, onCopy, setValue } = useClipboard(text, options);

useEffect(() => {
setValue(text);
}, [text]);

useEffect(() => {
if (hasCopied) {
onCopyComplete?.();
}
}, [hasCopied]);

return (
<>
<Tooltip label={hasCopied ? 'Copied' : 'Copy to clipboard'}>
Expand All @@ -29,13 +36,19 @@ export const SimpleCopyButton = (props: ICopyButtonProps): ReactElement => {
};

export const LabeledCopyButton = (props: ICopyButtonProps & { label: string }): ReactElement => {
const { label, text, options, ...rest } = props;
const { label, text, options, onCopyComplete, ...rest } = props;
const { hasCopied, onCopy, setValue } = useClipboard(text, options);

useEffect(() => {
setValue(text);
}, [text]);

useEffect(() => {
if (hasCopied) {
onCopyComplete?.();
}
}, [hasCopied]);

return (
<>
<Button leftIcon={<CopyIcon />} variant="link" aria-label="copy to clipboard" onClick={onCopy} {...rest}>
Expand Down

0 comments on commit 893cec4

Please sign in to comment.