From 2dab05c6f5d788dc646b389bd5267602411bb9fc Mon Sep 17 00:00:00 2001 From: Codetrauma Date: Mon, 19 Aug 2024 18:55:19 -0300 Subject: [PATCH 1/2] feat: changing selection logic for redacted ranges in alpha.6 --- src/pages/Notarize/index.tsx | 59 ++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/pages/Notarize/index.tsx b/src/pages/Notarize/index.tsx index be11bc9f..6918dc6f 100644 --- a/src/pages/Notarize/index.tsx +++ b/src/pages/Notarize/index.tsx @@ -289,37 +289,54 @@ function HideResponseStep(props: { onNext: () => void; onCancel: () => void; setSecretResps: (secrets: string[]) => void; -}): ReactElement { +}): React.ReactElement { const params = useParams<{ requestId: string }>(); const req = useRequest(params.requestId); const [responseText, setResponseText] = useState(''); - const [selections, setSelections] = useState([]); + const [redactedRanges, setRedactedRanges] = useState([]); const taRef = useRef(null); const onSelectionChange: React.ChangeEventHandler = useCallback( (e) => { const ta = e.currentTarget; if (ta.selectionEnd > ta.selectionStart) { - const newSelection: Selection = { + const newRange: Selection = { start: ta.selectionStart, end: ta.selectionEnd, }; - setSelections((prevSelections) => { - const updatedSelections = [...prevSelections, newSelection]; - // Extract and set secret responses based on updated selections - const secretResps = updatedSelections.map(({ start, end }) => - responseText.substring(start, end) - ).filter((d) => !!d); + setRedactedRanges((prevRanges) => { + const updatedRanges = [...prevRanges, newRange].sort((a, b) => a.start - b.start); + const mergedRanges = mergeRanges(updatedRanges); + + const secretResps = mergedRanges + .map(({ start, end }) => responseText.substring(start, end)) + .filter((d) => !!d); props.setSecretResps(secretResps); - return updatedSelections; + return mergedRanges; }); } }, [responseText, props] ); + const mergeRanges = (ranges: Selection[]): Selection[] => { + if (ranges.length === 0) return []; + const mergedRanges: Selection[] = [ranges[0]]; + + for (let i = 1; i < ranges.length; i++) { + const lastRange = mergedRanges[mergedRanges.length - 1]; + if (ranges[i].start <= lastRange.end) { + lastRange.end = Math.max(lastRange.end, ranges[i].end); + } else { + mergedRanges.push(ranges[i]); + } + } + + return mergedRanges; + }; + useEffect(() => { if (!req) return; @@ -359,28 +376,23 @@ function HideResponseStep(props: { if (!req) return <>; - let shieldedText = responseText; - - if (selections.length > 0) { - let result = responseText.split(''); - selections.forEach(({ start, end }) => { - for (let i = start; i < end; i++) { - result[i] = '*'; - } - }); - shieldedText = result.join(''); - } + let shieldedText = responseText.split(''); + redactedRanges.forEach(({ start, end }) => { + for (let i = start; i < end; i++) { + shieldedText[i] = '*'; + } + }); return (
- Step 2 of 2: Highlight text to show only selected text from response + Step 2 of 2: Highlight text to redact selected portions from the response