Skip to content

Commit

Permalink
Fix duplicate copy warning alert (Azure-Samples#230)
Browse files Browse the repository at this point in the history
* Fix duplicate copy warning alert

- It was being added to the whole page everytime an answer was returned
- This meant that if **any** content on the page was copied the alert was
  being shown multiple times based on how many answers there had been
- This change will mean the alert will now only be shown when an answer
  is copied and only once

Required by Azure-Samples#215

* Address review comments
  • Loading branch information
adamdougal authored Jan 30, 2024
1 parent cd8d536 commit aa0df8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions code/app/frontend/src/components/Answer/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import supersub from 'remark-supersub'
interface Props {
answer: AskResponse;
onCitationClicked: (citedDocument: Citation) => void;
index: number;
}

export const Answer = ({
answer,
onCitationClicked
onCitationClicked,
index,
}: Props) => {
const [isRefAccordionOpen, { toggle: toggleIsRefAccordionOpen }] = useBoolean(false);
const filePathTruncationLimit = 50;

const messageBoxId = "message-" + index;

const parsedAnswer = useMemo(() => parseAnswer(answer), [answer]);
const [chevronIsExpanded, setChevronIsExpanded] = useState(isRefAccordionOpen);

Expand Down Expand Up @@ -57,15 +61,16 @@ export const Answer = ({
const handleCopy = () => {
alert("Please consider where you paste this content.");
};
document.addEventListener("copy", handleCopy);
const messageBox = document.getElementById(messageBoxId);
messageBox?.addEventListener("copy", handleCopy);
return () => {
document.removeEventListener("copy", handleCopy);
messageBox?.removeEventListener("copy", handleCopy);
};
}, []);

return (
<>
<Stack className={styles.answerContainer}>
<Stack className={styles.answerContainer} id={messageBoxId}>
<Stack.Item grow>
<ReactMarkdown
remarkPlugins={[remarkGfm, supersub]}
Expand Down
1 change: 1 addition & 0 deletions code/app/frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ const Chat = () => {
: [],
}}
onCitationClicked={(c) => onShowCitation(c)}
index={index}
/>
</div>
) : null}
Expand Down

0 comments on commit aa0df8d

Please sign in to comment.