import React, { useState, useMemo, useEffect } from 'react'; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { useRecoilValue } from 'recoil'; import { callFnState } from '@chainlit/react-client'; import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card" import { ClipboardPaste, Plus, Lock, LockOpen } from 'lucide-react' import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" // Internal Badge component to avoid import errors const Badge = ({ children, className }) => ( {children} ); function PII() { // const dic = props.dic || {}; const [dic, setDict] = useState(props.dic || {}); const [searchTerm, setSearchTerm] = useState(''); const [dictionary, setDictionary] = useState({}); const [systemLang, setSystemLang] = useState((typeof navigator !== 'undefined' && (navigator.language || navigator.userLanguage)) ? (navigator.language || navigator.userLanguage).split('-')[0] : 'en'); const callFn = useRecoilValue(callFnState); const handleUpdatePii = (key, value) => { callAction( { name: "update_pii", payload: { key: key, value: value } } ) setDict({ ...dic, [key]: { ...dic[key], locked: !dic[key]['locked'] } }); }; useEffect(() => { // Fetch the file from the public path fetch('/public/dictionnaire.json') .then(response => response.json()) // Parse the response as JSON .then(data => setDictionary(data)) // Set the data in state .catch(error => console.error('Error fetching JSON:', error)); }, []); // Determine system language // const systemLang = (typeof navigator !== 'undefined' && (navigator.language || navigator.userLanguage)) ? (navigator.language || navigator.userLanguage).split('-')[0] : 'en'; // Enrich dictionary with localized types function getType(typ) { if (dictionary[typ]) { return dictionary[typ][systemLang] || dictionary[typ]['en'] || typ; } return typ; } const enrichedDic = useMemo(() => { return Object.entries(dic).reduce((acc, [key, value]) => { const typeKey = value.type; const dictEntry = dictionary[typeKey]; const localizedType = dictEntry ? (dictEntry[systemLang] || dictEntry['en'] || typeKey) : typeKey; acc[key] = { ...value, typ: localizedType }; return acc; }, {}); }, [dic, systemLang]); const filteredDic = useMemo(() => { if (searchTerm.trim() === '') { return enrichedDic; } return Object.fromEntries( Object.entries(enrichedDic).filter(([key, value]) => String(value.type).toLowerCase().includes(searchTerm.toLowerCase()) || String(value.value).toLowerCase().includes(searchTerm.toLowerCase()) ) ); }, [searchTerm, enrichedDic]); const handlInsert = (value) => { console.log("value", value); const chatInput = document.getElementById('chat-input'); if (chatInput && chatInput instanceof HTMLTextAreaElement) { const start = chatInput.selectionStart; const end = chatInput.selectionEnd; const currentValue = chatInput.value; const spaceToAdd = " "; const newValue = currentValue.substring(0, start) + spaceToAdd + value + spaceToAdd + currentValue.substring(end); chatInput.value = newValue; chatInput.selectionStart = chatInput.selectionEnd = start + spaceToAdd.length + String(value).length + spaceToAdd.length; chatInput.focus(); } else { console.warn('Textarea with id "chat-input" not found.'); } } return (
setSearchTerm(e.target.value)} className="pl-8" />
{Object.entries(filteredDic).map(([key, value]) => (
{value.value}
{getType(value.type)}
))}
) }; export default PII;