tenantpilot/components/policy-explorer/PolicyDetailSheet.tsx
Ahmed Darrazi f592e5f55b feat: Policy Explorer UX Upgrade (003)
Implemented MVP with all core features:
- Browse 50 newest policies on load with null filtering
- Click row to view details in slide-over sheet
- JSON detection and pretty formatting
- Search with real-time filtering
- Badge colors for policy types (Security=red, Compliance=blue, Config=gray, App=outline)
- Navigation consolidated to 'Policy Explorer'

New components:
- PolicyTable.tsx - table with badges and hover effects
- PolicySearchContainer.tsx - search state management
- PolicyDetailSheet.tsx - JSON detail view with formatting
- PolicyExplorerClient.tsx - client wrapper
- lib/utils/policyBadges.ts - badge color mapping

Updated:
- lib/actions/policySettings.ts - added getRecentPolicySettings() with null filtering
- app/(app)/search/page.tsx - converted to Server Component
- config/nav.ts - renamed Search to Policy Explorer, removed All Settings
- components/search/EmptyState.tsx - updated messaging

Tasks complete: 36/47 (MVP ready)
- Phase 1-7: All critical features implemented
- Phase 8: Core polish complete (T041), optional tasks remain

TypeScript:  No errors
Status: Production-ready MVP
2025-12-07 02:28:15 +01:00

120 lines
3.3 KiB
TypeScript

'use client';
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
} from '@/components/ui/sheet';
import type { PolicySettingSearchResult } from '@/lib/actions/policySettings';
import { formatDistanceToNow } from 'date-fns';
import { de } from 'date-fns/locale';
interface PolicyDetailSheetProps {
policy: PolicySettingSearchResult | null;
open: boolean;
onOpenChange: (open: boolean) => void;
}
function isJsonString(str: string): boolean {
if (!str || typeof str !== 'string') return false;
const trimmed = str.trim();
return trimmed.startsWith('{') || trimmed.startsWith('[');
}
function formatJson(value: string): string {
try {
const parsed = JSON.parse(value);
return JSON.stringify(parsed, null, 2);
} catch {
return value;
}
}
export function PolicyDetailSheet({
policy,
open,
onOpenChange,
}: PolicyDetailSheetProps) {
if (!policy) return null;
const isJson = isJsonString(policy.settingValue);
const displayValue = isJson
? formatJson(policy.settingValue)
: policy.settingValue;
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="w-[600px] sm:max-w-[600px] overflow-y-auto">
<SheetHeader>
<SheetTitle>{policy.settingName}</SheetTitle>
<SheetDescription>
Policy Setting Details
</SheetDescription>
</SheetHeader>
<div className="mt-6 space-y-6">
{/* Policy Name */}
<div>
<h3 className="text-sm font-medium text-muted-foreground mb-1">
Policy Name
</h3>
<p className="text-sm">{policy.policyName}</p>
</div>
{/* Policy Type */}
<div>
<h3 className="text-sm font-medium text-muted-foreground mb-1">
Policy Type
</h3>
<p className="text-sm capitalize">
{policy.policyType.replace(/([A-Z])/g, ' $1').trim()}
</p>
</div>
{/* Setting Name */}
<div>
<h3 className="text-sm font-medium text-muted-foreground mb-1">
Setting Name
</h3>
<p className="text-sm font-mono">{policy.settingName}</p>
</div>
{/* Setting Value */}
<div>
<h3 className="text-sm font-medium text-muted-foreground mb-2">
Setting Value
</h3>
{isJson ? (
<pre className="text-xs bg-muted p-4 rounded-md overflow-x-auto max-h-96 overflow-y-auto">
<code>{displayValue}</code>
</pre>
) : (
<p className="text-sm whitespace-pre-wrap break-words">
{displayValue}
</p>
)}
</div>
{/* Last Synced */}
<div>
<h3 className="text-sm font-medium text-muted-foreground mb-1">
Last Synced
</h3>
<p className="text-sm">
{formatDistanceToNow(new Date(policy.lastSyncedAt), {
addSuffix: true,
locale: de,
})}
</p>
<p className="text-xs text-muted-foreground mt-1">
{new Date(policy.lastSyncedAt).toLocaleString('de-DE')}
</p>
</div>
</div>
</SheetContent>
</Sheet>
);
}