Spaces:
Sleeping
Sleeping
File size: 7,825 Bytes
9bd50b9 | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import React, { useState } from 'react';
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Eye, EyeClosed } from "lucide-react"
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip"
import { Button } from "@/components/ui/button"
// Internal Badge component to avoid import errors
const Badge = ({ children, className }) => (
<span className={` py-0.5 transition-colors text-main-700 ${className}`}>
{children}
</span>
);
function MessageWithHighlight({ message, dic, show }) {
if (!message) return null;
const [localMessage, setLocalMessage] = useState(message);
const [open, setOpen] = useState(false);
const [position, setPosition] = useState({ x: 0, y: 0 });
const [selectionData, setSelectionData] = useState(null);
const handleMouseUp = () => {
const selection = window.getSelection();
// 1. Check basic validity
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
// If just a click (collapsed), we assume menu shouldn't open or should close
// But we don't want to close immediately if clicking inside the menu, handled by DropdownMenu logic
return;
}
// 2. Expand Selection to Word Boundaries
const anchorNode = selection.anchorNode;
const focusNode = selection.focusNode;
// Only support single text node selection for simplicity to map back to data-index
if (anchorNode !== focusNode || anchorNode.nodeType !== 3) {
setOpen(false);
return;
}
const textContent = anchorNode.textContent;
let start = Math.min(selection.anchorOffset, selection.focusOffset);
let end = Math.max(selection.anchorOffset, selection.focusOffset);
// Expand left
while (start > 0 && /\S/.test(textContent[start - 1])) {
start--;
}
// Expand right
while (end < textContent.length && /\S/.test(textContent[end])) {
end++;
}
// Update visual selection
const newRange = document.createRange();
newRange.setStart(anchorNode, start);
newRange.setEnd(anchorNode, end);
selection.removeAllRanges();
selection.addRange(newRange);
const newText = textContent.slice(start, end);
if (!newText.trim()) return;
// 3. Validation Logic
// Check 1: Less than 4 words
const words = newText.split(/\s+/);
if (words.length >= 4) {
setOpen(false);
return;
}
// Check 2: Does not contain highlighted word (brackets)
if (newText.includes('[') || newText.includes(']')) {
setOpen(false);
return;
}
// Check 3: Check if selection is inside a Badge
const parentElement = anchorNode.parentElement;
if (parentElement.closest('.bg-pii')) {
setOpen(false);
return;
}
// Identify Part Index
const partIndex = parseInt(parentElement.getAttribute('data-index'), 10);
if (isNaN(partIndex)) {
setOpen(false);
return;
}
const rect = newRange.getBoundingClientRect();
setPosition({
x: rect.left,
y: rect.bottom
});
setSelectionData({
partIndex,
start,
end,
text: newText
});
setOpen(true);
};
const handleAnonymize = () => {
if (!selectionData) return;
const { partIndex, start, end, text } = selectionData;
// Split by brackets to reconstruct parts array as seen in render
const parts = localMessage.split(/(\[[^\]]+\])/g);
if (parts[partIndex] === undefined) return;
const originalPart = parts[partIndex];
const newPart = originalPart.substring(0, start) + `[ANONYME_${text}]` + originalPart.substring(end);
parts[partIndex] = newPart;
const newMessage = parts.join('');
setLocalMessage(newMessage);
setOpen(false);
// Clear selection
window.getSelection().removeAllRanges();
};
// Split by brackets, keeping the brackets in the split result for now
// Regex: split by [content]
const parts = localMessage.split(/(\[[^\]]+\])/g);
return (
<div onMouseUp={handleMouseUp} className="relative px-5 py-2.5">
<p className="leading-[1.75] whitespace-pre-wrap">
{parts.map((part, index) => {
// Check if part is [something]
const match = part.match(/^\[(\w+)_(.*?)\]$/);
if (match && dic[match[2]]) {
return (
show ? <Badge key={index} className="bg-pii text-blue-800 rounded-full px-2 py-1 mx-1 cursor-pointer">
{dic[match[2]].value}
</Badge> :
<Tooltip>
<TooltipTrigger asChild>
<button className=""><Badge key={index} className="bg-pii text-blue-800 rounded-sm px-1 py-1 mx-1 cursor-pointer">
{part.toUpperCase()}
</Badge></button>
</TooltipTrigger>
<TooltipContent>
<p>{dic[match[2]].value}</p>
</TooltipContent>
</Tooltip>
);
}
return <span key={index} data-index={index}>{part}</span>;
})}
</p>
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger asChild>
<div
style={{
position: 'fixed',
top: position.y,
left: position.x,
width: '1px',
height: '1px',
visibility: 'hidden',
pointerEvents: 'none'
}}
/>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
<DropdownMenuGroup>
<DropdownMenuItem onSelect={handleAnonymize}>
Anonymiser
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}
export default function Message() {
const [show, setShow] = useState(true);
return (
<div className="flex flex-col gap-2">
{/* <ReactMarkdown> */}
<MessageWithHighlight message={props.message} dic={props.dic} show={show} />
{/* </ReactMarkdown> */}
{/* <div className='flex flex-row-reverse w-full'>
<div className="relative bg-accent rounded-3xl px-5 py-2.5 user-message flex-grow-0">
<Markdown dic={props.dic} show={show}>{props.message}</Markdown>
</div>
</div> */}
<div className="flex justify-end">
<Button variant="outline" size="icon" onClick={() => setShow(!show)}>
{show ? <Eye /> : <EyeClosed />}
</Button>
</div>
</div>
);
}
|