|
1 | | -import React, { ForwardRefRenderFunction, useEffect } from "react"; |
2 | | - |
| 1 | +import React, { useEffect, useRef, forwardRef } from "react"; |
| 2 | + |
3 | 3 | interface ChatMessageContentAreaProps { |
4 | 4 | children?: React.ReactNode; |
5 | 5 | } |
6 | | - |
7 | | -const ChatMessageContentArea: ForwardRefRenderFunction< |
| 6 | + |
| 7 | +const ChatMessageContentArea: React.ForwardRefRenderFunction< |
8 | 8 | HTMLDivElement, |
9 | 9 | ChatMessageContentAreaProps |
10 | 10 | > = (props, ref) => { |
| 11 | + const internalRef = useRef<HTMLDivElement>(null); |
| 12 | + const combinedRef = ref || internalRef; |
| 13 | + |
11 | 14 | useEffect(() => { |
12 | | - // Check if ref.current is not null |
13 | | - if (ref.current) { |
14 | | - const links = ref.current.querySelectorAll('a'); // Select all anchor tags in the current ref |
| 15 | + if (combinedRef && combinedRef.current) { |
| 16 | + const links = combinedRef.current.querySelectorAll("a"); |
15 | 17 | links.forEach(link => { |
16 | | - link.setAttribute('target', '_blank'); // Open in new tab |
17 | | - link.setAttribute('rel', 'noopener noreferrer'); // Security measure |
| 18 | + link.setAttribute("target", "_blank"); |
| 19 | + link.setAttribute("rel", "noopener noreferrer"); |
18 | 20 | }); |
19 | 21 | } |
20 | | - }, [props.children]); // Run effect when children change |
21 | | - |
| 22 | + }, [props.children]); |
| 23 | + |
22 | 24 | return ( |
23 | | - <div |
24 | | - ref={ref} |
| 25 | +<div |
| 26 | + ref={combinedRef} |
25 | 27 | className="container max-w-3xl relative min-h-screen pb-[240px] pt-16 flex flex-col gap-16" |
26 | | - > |
| 28 | +> |
27 | 29 | {props.children} |
28 | | - </div> |
| 30 | +</div> |
29 | 31 | ); |
30 | 32 | }; |
31 | | - |
32 | | -export default React.forwardRef(ChatMessageContentArea); |
| 33 | + |
| 34 | +export default forwardRef(ChatMessageContentArea); |
0 commit comments