Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions e2e/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ test.describe('upload flow detects and alerts', () => {
await expect(page.getByText('Redaction grade')).toBeVisible()

// Recovered text is masked until the user clicks reveal.
await page
.getByRole('button', {name: /reveal what was under the box/i})
.click()
await page.getByRole('button', {name: 'Reveal text for area 1'}).click()
await expect(page.getByText('Jordan Hayes')).toBeVisible()
})

Expand Down Expand Up @@ -108,12 +106,25 @@ test.describe('upload flow detects and alerts', () => {
).toHaveCount(2)
await expect(first.getByText('4 findings', {exact: true})).toBeVisible()
await expect(second.getByText('1 finding', {exact: true})).toBeVisible()
await expect(first.getByText('Areas 1, 2', {exact: true})).toBeVisible()
await expect(
first.getByRole('heading', {
name: 'Text is still under the box',
exact: true,
})
).toHaveCount(1)
await expect(
first.getByText(
'A black box was drawn over this text, but the text itself was never removed. It stays selectable, searchable, and copy-pasteable.',
{exact: true}
)
).toHaveCount(1)
await expect(
first.getByText('This redaction leaks its length')
).toBeVisible()
await expect(first.getByText('Northwind', {exact: true})).toHaveCount(0)
const reveal = first.getByRole('button', {
name: 'Reveal what was under the box',
name: /^Reveal text for area/,
})
await expect(reveal).toHaveCount(3)
// Revealing one passage must not reveal the other passages.
Expand All @@ -125,14 +136,32 @@ test.describe('upload flow detects and alerts', () => {
await expect(
first.getByText('Project Cardinal', {exact: true})
).toBeVisible()
await first
.getByRole('button', {name: 'Show all findings on this page'})
.click()
await first.getByRole('button', {name: 'Show original page'}).click()
await expect(
first.getByRole('img', {name: 'Page 1', exact: true})
).toHaveCount(1)
await expect(first.locator('[aria-label^="Finding "]')).toHaveCount(4)
await expect(first.getByText(/Annotation marks are hidden/)).toBeVisible()
// The synthetic square annotation covers [72, 180, 164.5, 195] on a
// 612x792 page. Its interior must remain black even after text is revealed.
const blackFraction = await first
.getByRole('img', {name: 'Page 1', exact: true})
.evaluate(async (element) => {
const image = element as HTMLImageElement
await image.decode()
const canvas = document.createElement('canvas')
canvas.width = 612
canvas.height = 792
const context = canvas.getContext('2d')!
context.drawImage(image, 0, 0, 612, 792)
const pixels = context.getImageData(80, 185, 70, 5).data
let black = 0
for (let i = 0; i < pixels.length; i += 4) {
if (pixels[i]! < 40 && pixels[i + 1]! < 40 && pixels[i + 2]! < 40)
black++
}
return black / (pixels.length / 4)
})
expect(blackFraction).toBeGreaterThan(0.98)
await expect(
second.getByRole('img', {name: 'Page 2', exact: true})
).toHaveCount(0)
Expand Down
4 changes: 2 additions & 2 deletions src/components/TryIt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export function TryIt() {
}, [])

const renderPage = useCallback(
(page: number, opts?: {revealAnnotations?: boolean}) =>
(page: number) =>
analyzerRef.current
? analyzerRef.current.renderPage(page, opts)
? analyzerRef.current.renderPage(page)
: Promise.reject(new Error('No document')),
[]
)
Expand Down
145 changes: 73 additions & 72 deletions src/components/results/FindingCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {useState} from 'react'
import {FiEye, FiEyeOff, FiImage, FiAlertTriangle} from 'react-icons/fi'
import type {Finding} from '@/lib/analyzer/types'
import {CHECK_META} from '@/lib/checkMeta'
import {cn} from '@/lib/cn'
import {WordSizeGuess} from './WordSizeGuess'

Expand Down Expand Up @@ -35,30 +34,30 @@ export function FindingCard({
}: {
findings: Finding[]
docTokens: string[]
onRenderPage?: (
page: number,
opts?: {revealAnnotations?: boolean}
) => Promise<RenderResult>
onRenderPage?: (page: number) => Promise<RenderResult>
}) {
const page = findings[0]!.page
const explanations = new Map<string, {finding: Finding; areas: number[]}>()
findings.forEach((finding, index) => {
const key = JSON.stringify([
finding.check,
finding.severity,
finding.title,
finding.detail,
])
const explanation = explanations.get(key)
if (explanation) explanation.areas.push(index + 1)
else explanations.set(key, {finding, areas: [index + 1]})
})
const [render, setRender] = useState<RenderResult | null>(null)
const [rendering, setRendering] = useState(false)
const [renderError, setRenderError] = useState(false)
// Removing annotations exposes their contents; drawn covers remain in place.
const hasAnnotationCover = findings.some((finding) =>
['redact-annot', 'square-annot', 'highlight-annot'].includes(
finding.coverKind
)
)

const handleViewInDoc = async () => {
if (!onRenderPage || page < 1 || render || rendering) return
setRendering(true)
setRenderError(false)
try {
setRender(
await onRenderPage(page, {revealAnnotations: hasAnnotationCover})
)
setRender(await onRenderPage(page))
} catch {
setRenderError(true)
} finally {
Expand All @@ -79,17 +78,29 @@ export function FindingCard({
</span>
</div>

<ol className="mt-3 divide-y divide-brand-almond">
{findings.map((finding, index) => (
<li key={finding.id} className="py-4 first:pt-0">
<FindingDetail
finding={finding}
number={index + 1}
docTokens={docTokens}
/>
</li>
<div className="mt-3 space-y-3">
{[...explanations.values()].map(({finding, areas}) => (
<div key={finding.id}>
<div className="flex items-start justify-between gap-3">
<h4 className="font-semibold leading-tight">{finding.title}</h4>
<span
className={cn(
'shrink-0 rounded-full border px-2.5 py-1 text-xs font-semibold',
SEVERITY_STYLE[finding.severity].chip
)}
>
{SEVERITY_STYLE[finding.severity].label}
</span>
</div>
<p className="mt-0.5 text-xs font-medium text-brand-ink/50">
{areas.length === 1 ? 'Area' : 'Areas'} {areas.join(', ')}
</p>
<p className="mt-1 text-sm leading-relaxed text-brand-ink/80">
{finding.detail}
</p>
</div>
))}
</ol>
</div>

{onRenderPage && page > 0 && findings.some((f) => f.bbox) && (
<div className="mt-3">
Expand All @@ -101,9 +112,7 @@ export function FindingCard({
className="inline-flex items-center gap-1.5 text-xs font-medium text-brand-blue hover:underline disabled:opacity-50"
>
<FiImage className="h-3.5 w-3.5" />
{rendering
? 'Rendering page…'
: 'Show all findings on this page'}
{rendering ? 'Rendering page…' : 'Show original page'}
</button>
)}
{renderError && (
Expand All @@ -114,12 +123,9 @@ export function FindingCard({
)}
{render && (
<>
{hasAnnotationCover && (
<p className="mb-2 text-xs text-brand-ink/60">
Annotation marks are hidden in this preview to show the
content underneath.
</p>
)}
<p className="mb-2 text-xs text-brand-ink/60">
Original page. Numbered outlines show the affected areas.
</p>
<div className="relative mt-2 overflow-hidden rounded-xl border border-brand-almond">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
Expand Down Expand Up @@ -155,6 +161,18 @@ export function FindingCard({
)}
</div>
)}

<ol className="mt-4 divide-y divide-brand-almond border-t border-brand-almond">
{findings.map((finding, index) => (
<li key={finding.id} className="py-3 last:pb-0">
<FindingDetail
finding={finding}
number={index + 1}
docTokens={docTokens}
/>
</li>
))}
</ol>
</div>
</section>
)
Expand All @@ -169,60 +187,43 @@ function FindingDetail({
number: number
docTokens: string[]
}) {
const meta = CHECK_META[finding.check]
const sev = SEVERITY_STYLE[finding.severity]
const [revealed, setRevealed] = useState(false)
const revealText = finding.recovered?.text

return (
<>
<div className="flex items-start justify-between gap-3">
<div className="flex items-start gap-3">
<span className="mt-0.5 flex h-9 w-9 shrink-0 items-center justify-center rounded-xl bg-brand-cream text-sm font-bold text-brand-ink/70">
{number}
</span>
<div>
<h4 className="font-semibold leading-tight">{finding.title}</h4>
<p className="mt-0.5 text-xs font-medium text-brand-ink/50">
{meta.short}
</p>
</div>
</div>
<span
className={cn(
'shrink-0 rounded-full border px-2.5 py-1 text-xs font-semibold',
sev.chip
)}
>
{sev.label}
</span>
</div>
<p className="mt-3 text-sm leading-relaxed text-brand-ink/80">
{finding.detail}
</p>
{revealText && (
<div className="mt-3">
<div className="flex items-center justify-between gap-3">
<h5 className="text-sm font-semibold">Area {number}</h5>
{revealText && (
<button
type="button"
onClick={() => setRevealed((v) => !v)}
className="mb-2 inline-flex items-center gap-1.5 text-xs font-semibold text-brand-vermillion hover:underline"
aria-label={`${revealed ? 'Hide' : 'Reveal'} text for area ${number}`}
aria-expanded={revealed}
className="inline-flex items-center gap-1.5 text-xs font-semibold text-brand-vermillion hover:underline"
>
{revealed ? (
<FiEyeOff className="h-3.5 w-3.5" />
) : (
<FiEye className="h-3.5 w-3.5" />
)}
{revealed ? 'Hide' : 'Reveal'} what was under the box
{revealed ? 'Hide' : 'Reveal'} text
</button>
<div
className={cn(
'select-text rounded-xl border border-brand-almond bg-brand-ink/5 px-3 py-2 font-mono text-sm text-brand-ink',
!revealed && 'pointer-events-none select-none blur-sm'
)}
>
{revealed ? revealText : '•••••••••••••••••••'}
</div>
)}
</div>
{revealText ? (
<div
className={cn(
'mt-2 select-text rounded-xl border border-brand-almond bg-brand-ink/5 px-3 py-2 font-mono text-sm text-brand-ink',
!revealed && 'pointer-events-none select-none blur-sm'
)}
>
{revealed ? revealText : '•••••••••••••••••••'}
</div>
) : (
<p className="mt-1 text-xs text-brand-ink/60">
No readable text recovered.
</p>
)}
{finding.width && (
<WordSizeGuess width={finding.width} docTokens={docTokens} />
Expand Down
5 changes: 1 addition & 4 deletions src/components/results/ResultsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export function ResultsView({
}: {
result: AnalysisResult
onReset: () => void
onRenderPage?: (
page: number,
opts?: {revealAnnotations?: boolean}
) => Promise<RenderResult>
onRenderPage?: (page: number) => Promise<RenderResult>
}) {
const {summary} = result
const meta = GRADE_META[summary.grade]
Expand Down
11 changes: 4 additions & 7 deletions src/lib/analyzer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,12 @@ export class PdfAnalyzer {
}

/**
* Render one page to a PNG data URL for the results view. `revealAnnotations`
* disables annotation rendering, so sticky-note redactions vanish and the
* text they were covering becomes visible — the "before/after" proof.
* Render the original page, including its annotation marks. Recovered text
* is shown separately in the results view, never by altering this preview.
*/
async renderPage(
pageNum: number,
opts: {scale?: number; revealAnnotations?: boolean} = {}
opts: {scale?: number} = {}
): Promise<{dataUrl: string; pageWidth: number; pageHeight: number}> {
if (!this.doc) throw new AnalyzerError('unknown', 'No document open.')
const pdfjs = await getPdfjs()
Expand All @@ -212,9 +211,7 @@ export class PdfAnalyzer {
await page.render({
canvasContext: ctx,
viewport,
annotationMode: opts.revealAnnotations
? pdfjs.AnnotationMode.DISABLE
: pdfjs.AnnotationMode.ENABLE,
annotationMode: pdfjs.AnnotationMode.ENABLE,
}).promise
const dataUrl = canvas.toDataURL('image/png')
page.cleanup()
Expand Down
Loading