📦 EqualifyEverything / equalify-reflow

📄 PiiReviewPanel.tsx · 214 lines
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
214import { useMemo, useState } from 'react';
import { Button } from '@/components/ui/button';
import { ShieldAlert, ShieldCheck, XCircle, ChevronDown, ChevronRight, Eye, EyeOff, Loader2 } from 'lucide-react';
import type { PIIFinding } from '@/types/pipeline-viewer';

type PanelState = 'scanning' | 'awaiting' | 'approved' | 'denied' | 'clean' | 'error';

interface PiiReviewPanelProps {
  state: PanelState;
  findings: PIIFinding[];
  error?: string | null;
  onDecision: (decision: 'approved' | 'denied') => void;
}

const ENTITY_LABELS: Record<string, string> = {
  EMAIL_ADDRESS: 'Email address',
  PHONE_NUMBER: 'Phone number',
  US_SSN: 'Social security number',
  CREDIT_CARD: 'Credit card',
  IBAN_CODE: 'Bank account (IBAN)',
  US_DRIVER_LICENSE: "Driver's license",
};

function maskSnippet(text: string, entityType: string): string {
  if (entityType === 'EMAIL_ADDRESS') {
    const at = text.indexOf('@');
    if (at <= 0) return '***';
    return `${text[0]}${'*'.repeat(Math.max(1, at - 1))}${text.slice(at)}`;
  }
  if (text.length <= 4) return '*'.repeat(text.length);
  return `${text.slice(0, 2)}${'*'.repeat(text.length - 4)}${text.slice(-2)}`;
}

export function PiiReviewPanel({ state, findings, error, onDecision }: PiiReviewPanelProps) {
  const [expanded, setExpanded] = useState<Record<string, boolean>>({});
  const [revealed, setRevealed] = useState(false);

  const grouped = useMemo(() => {
    const map = new Map<string, PIIFinding[]>();
    for (const f of findings) {
      const list = map.get(f.entity_type) ?? [];
      list.push(f);
      map.set(f.entity_type, list);
    }
    return Array.from(map.entries()).sort((a, b) => b[1].length - a[1].length);
  }, [findings]);

  if (state === 'scanning') {
    return (
      <div className="h-full flex items-center justify-center p-8">
        <div className="flex flex-col items-center gap-3 text-center max-w-md">
          <Loader2 className="w-8 h-8 animate-spin text-uic-blue" aria-hidden="true" />
          <h2 className="text-base font-semibold text-gray-900">Scanning for sensitive information</h2>
          <p className="text-sm text-muted-foreground">
            Presidio is scanning the document text for patterns that may be personally
            identifiable information (emails, phone numbers, SSNs, card numbers). The
            pipeline will pause here if anything is flagged.
          </p>
        </div>
      </div>
    );
  }

  if (state === 'error') {
    return (
      <div className="h-full flex items-center justify-center p-8">
        <div className="flex flex-col items-center gap-3 text-center max-w-md">
          <XCircle className="w-8 h-8 text-red-600" aria-hidden="true" />
          <h2 className="text-base font-semibold text-gray-900">PII scan failed</h2>
          <p className="text-sm text-muted-foreground">{error ?? 'Unknown error'}</p>
          <p className="text-xs text-muted-foreground">
            The pipeline is continuing without a completed PII check. Review the output
            carefully before sharing.
          </p>
        </div>
      </div>
    );
  }

  if (state === 'clean') {
    return (
      <div className="h-full flex items-center justify-center p-8">
        <div className="flex flex-col items-center gap-3 text-center max-w-md">
          <ShieldCheck className="w-8 h-8 text-green-600" aria-hidden="true" />
          <h2 className="text-base font-semibold text-gray-900">No sensitive information detected</h2>
          <p className="text-sm text-muted-foreground">
            The pattern-based scan found no emails, phone numbers, or similar identifiers.
            False negatives are possible — review the output before sharing.
          </p>
        </div>
      </div>
    );
  }

  if (state === 'approved' || state === 'awaiting' || state === 'denied') {
    const heading =
      state === 'denied'
        ? 'Processing cancelled'
        : state === 'approved'
          ? 'Processing continuing with flagged content'
          : 'Potentially sensitive information detected';
    const Icon = state === 'denied' ? XCircle : ShieldAlert;
    const iconColor = state === 'denied' ? 'text-red-600' : 'text-amber-600';

    return (
      <div className="h-full overflow-y-auto">
        <div className="max-w-2xl mx-auto p-6 flex flex-col gap-4">
          <div className="flex items-start gap-3">
            <Icon className={`w-7 h-7 shrink-0 mt-0.5 ${iconColor}`} aria-hidden="true" />
            <div>
              <h2 className="text-base font-semibold text-gray-900">{heading}</h2>
              <p className="text-sm text-muted-foreground mt-1">
                {findings.length} match{findings.length === 1 ? '' : 'es'} from a pattern-based
                scan (Microsoft Presidio). False positives are possible.
              </p>
            </div>
          </div>

          <div className="border rounded-lg bg-white">
            <div className="flex items-center justify-between px-4 py-2 border-b">
              <h3 className="text-sm font-medium text-gray-900">Findings</h3>
              <button
                onClick={() => setRevealed((v) => !v)}
                className="text-xs text-uic-blue hover:underline inline-flex items-center gap-1"
              >
                {revealed ? (
                  <>
                    <EyeOff className="w-3.5 h-3.5" aria-hidden="true" />
                    Mask matches
                  </>
                ) : (
                  <>
                    <Eye className="w-3.5 h-3.5" aria-hidden="true" />
                    Show matches
                  </>
                )}
              </button>
            </div>
            <ul className="divide-y">
              {grouped.map(([entityType, items]) => {
                const open = expanded[entityType] ?? false;
                const label = ENTITY_LABELS[entityType] ?? entityType;
                return (
                  <li key={entityType}>
                    <button
                      onClick={() => setExpanded((s) => ({ ...s, [entityType]: !open }))}
                      className="w-full flex items-center justify-between px-4 py-2 hover:bg-gray-50"
                      aria-expanded={open}
                    >
                      <span className="flex items-center gap-2 text-sm font-medium text-gray-900">
                        {open ? (
                          <ChevronDown className="w-3.5 h-3.5 text-muted-foreground" aria-hidden="true" />
                        ) : (
                          <ChevronRight className="w-3.5 h-3.5 text-muted-foreground" aria-hidden="true" />
                        )}
                        {label}
                      </span>
                      <span className="text-xs text-muted-foreground">
                        {items.length} match{items.length === 1 ? '' : 'es'}
                      </span>
                    </button>
                    {open && (
                      <ul className="px-4 pb-2 space-y-1 text-xs font-mono text-gray-700">
                        {items.slice(0, 50).map((f, idx) => (
                          <li key={idx} className="flex items-center justify-between gap-2 py-0.5">
                            <span className="truncate">
                              {revealed ? f.text : maskSnippet(f.text, f.entity_type)}
                            </span>
                            <span className="text-[10px] text-muted-foreground shrink-0">
                              {Math.round(f.score * 100)}%
                            </span>
                          </li>
                        ))}
                        {items.length > 50 && (
                          <li className="text-[11px] text-muted-foreground py-0.5">
                            + {items.length - 50} more
                          </li>
                        )}
                      </ul>
                    )}
                  </li>
                );
              })}
            </ul>
          </div>

          {state === 'awaiting' && (
            <div className="flex items-center justify-end gap-2 pt-2">
              <Button variant="outline" onClick={() => onDecision('denied')}>
                Cancel processing
              </Button>
              <Button onClick={() => onDecision('approved')}>
                Continue anyway
              </Button>
            </div>
          )}
          {state === 'approved' && (
            <p className="text-xs text-muted-foreground">
              You chose to continue. Processing is resuming with the flagged content.
            </p>
          )}
          {state === 'denied' && (
            <p className="text-xs text-muted-foreground">
              You cancelled processing. No document data was sent to the AI pipeline.
            </p>
          )}
        </div>
      </div>
    );
  }

  return null;
}