All files / components/records ViewRecordDialog.tsx

41.17% Statements 21/51
40.27% Branches 29/72
35.29% Functions 6/17
43.75% Lines 21/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                                                                                                          127x 28x                   127x                                                                                                                                         127x 49x 2x       47x                   47x 12x               35x         35x 11x 11x               24x         24x 11x       13x     127x                                                   49x 49x                                                   49x                                                                                                                        
/**
 * View record dialog component
 * Read-only display of a single record
 */
 
import { AppDialog } from '@/components/common/AppDialog';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Calculator, ExternalLink } from 'lucide-react';
import type { FieldDefinition } from '@/services/collections.service';
import type { RecordData } from '@/types/records.types';
import { shouldMaskField, maskPiiValue } from '@/lib/form-helpers';
 
interface ViewRecordDialogProps {
	open: boolean;
	onOpenChange: (open: boolean) => void;
	schema: FieldDefinition[];
	collectionName: string;
	record: RecordData | null;
	hasPiiAccess?: boolean;
	referenceRecords?: Record<string, RecordData[]>;
	onNavigateToRecord?: (collection: string) => void;
}
 
/** Best display value for a referenced record. */
function getRefDisplayValue(record: RecordData): string {
	if (typeof record['name'] === 'string' && record['name']) return record['name'];
	const systemKeys = ['id', 'account_id', 'created_at', 'updated_at', 'created_by', 'updated_by'];
	const key = Object.keys(record).find(
		(k) => !systemKeys.includes(k) && typeof record[k] === 'string' && record[k],
	);
	if (key) return String(record[key]);
	return String(record.id ?? '').slice(0, 12) + '…';
}
 
/** Non-system fields, up to `limit`. */
function getUserFields(record: RecordData, limit = 3): [string, unknown][] {
	const systemKeys = ['id', 'account_id', 'created_at', 'updated_at', 'created_by', 'updated_by'];
	return Object.entries(record)
		.filter(([key]) => !systemKeys.includes(key))
		.slice(0, limit);
}
 
export default function ViewRecordDialog({
	open,
	onOpenChange,
	schema,
	collectionName,
	record,
	hasPiiAccess = false,
	referenceRecords = {},
	onNavigateToRecord,
}: ViewRecordDialogProps) {
	const formatDate = (dateString: string) => {
		return new Date(dateString).toLocaleString('en-US', {
			year: 'numeric',
			month: 'short',
			day: 'numeric',
			hour: '2-digit',
			minute: '2-digit',
			second: '2-digit',
		});
	};
 
	const renderReferenceField = (field: FieldDefinition, value: unknown) => {
		const id = String(value);
		const collectionCache = referenceRecords[field.collection || ''] ?? [];
		const refRecord = collectionCache.find((r) => r.id === id) ?? null;
 
		if (!refRecord) {
			// Not in cache — show ID with a "Deleted?" hint
			return (
				<div className="flex items-center gap-2">
					<span className="font-mono text-xs text-muted-foreground break-all">{id}</span>
					<Badge variant="secondary" className="text-xs shrink-0">Deleted?</Badge>
				</div>
			);
		}
 
		const displayValue = getRefDisplayValue(refRecord);
		const userFields = getUserFields(refRecord);
 
		return (
			<div className="border rounded-lg p-3 bg-muted/30 space-y-2">
				{/* Header row */}
				<div className="flex items-center justify-between gap-2">
					<div className="flex items-center gap-2 min-w-0">
						<span className="font-medium text-sm truncate">{displayValue}</span>
						{field.collection && (
							<Badge variant="outline" className="text-xs shrink-0">{field.collection}</Badge>
						)}
					</div>
					{onNavigateToRecord && field.collection && (
						<Button
							variant="ghost"
							size="sm"
							className="h-7 gap-1 shrink-0"
							onClick={() => {
								onOpenChange(false);
								onNavigateToRecord(field.collection!);
							}}
							title={`Open ${field.collection} records`}
						>
							<ExternalLink className="h-3 w-3" />
							Open
						</Button>
					)}
				</div>
 
				{/* ID */}
				<p className="font-mono text-xs text-muted-foreground break-all">{id}</p>
 
				{/* Key fields */}
				{userFields.length > 0 && (
					<div className="space-y-1 pt-1 border-t border-border/50">
						{userFields.map(([key, val]) => (
							<div key={key} className="flex justify-between gap-2 text-sm">
								<span className="text-muted-foreground">{key}:</span>
								<span className="font-medium truncate max-w-[200px]">
									{val === null || val === undefined ? (
										<span className="italic text-muted-foreground">null</span>
									) : (
										String(val)
									)}
								</span>
							</div>
						))}
					</div>
				)}
			</div>
		);
	};
 
	const renderFieldValue = (field: FieldDefinition, value: unknown) => {
		if (value === null || value === undefined || value === '') {
			return <span className="text-muted-foreground italic">null</span>;
		}
 
		// Handle PII masking
		Iif (shouldMaskField(field, hasPiiAccess)) {
			const maskedValue = maskPiiValue(String(value), field.mask_type || 'full');
			return (
				<span className="text-muted-foreground" title="PII masked">
					{maskedValue}
				</span>
			);
		}
 
		// Handle boolean
		if (field.type === 'boolean') {
			return value ? (
				<Badge variant="default">Yes</Badge>
			) : (
				<Badge variant="secondary">No</Badge>
			);
		}
 
		// Handle reference fields — mini-card
		Iif (field.type === 'reference') {
			return renderReferenceField(field, value);
		}
 
		// Handle JSON
		if (field.type === 'json') {
			const jsonStr = typeof value === 'string' ? value : JSON.stringify(value, null, 2);
			return (
				<pre className="bg-muted p-2 rounded text-xs font-mono overflow-x-auto">
					{jsonStr}
				</pre>
			);
		}
 
		// Handle datetime
		Iif (field.type === 'datetime') {
			return <span className="text-sm">{typeof value === 'string' ? formatDate(value) : String(value)}</span>;
		}
 
		// Handle number
		if (field.type === 'number') {
			return <span className="font-mono">{String(value)}</span>;
		}
 
		// Default text rendering
		return <span className="text-sm break-all">{String(value)}</span>;
	};
 
	return (
		<AppDialog
			open={open}
			onOpenChange={onOpenChange}
			title="Record Details"
			description={
				<>
					Viewing record from <strong>{collectionName}</strong> collection
					{record && (
						<span className="ml-2 font-mono text-xs text-muted-foreground">
							(ID: {(record.id as string)?.slice(0, 8)}...)
						</span>
					)}
				</>
			}
			className="max-w-2xl"
			footer={<Button onClick={() => onOpenChange(false)}>Close</Button>}
		>
			{!record ? (
				<div className="py-8 text-center text-muted-foreground">
					No record data available
				</div>
			) : (
				<div className="max-h-[60vh] overflow-y-auto">
					{/* Schema Fields */}
					<div className="space-y-4 pb-4">
						{schema.filter(f => f.type !== 'computed').map((field) => (
							<div key={field.name} className="border-b pb-3 last:border-0">
								<div className="flex items-center gap-2 mb-1">
									<span className="font-medium text-sm">{field.name}</span>
									{field.required && (
										<span className="text-destructive text-xs">*</span>
									)}
									{field.unique && (
										<Badge variant="outline" className="text-xs">
											Unique
										</Badge>
									)}
									{field.pii && (
										<Badge variant="secondary" className="text-xs">
											PII
										</Badge>
									)}
									<Badge variant="outline" className="text-xs">
										{field.type}
									</Badge>
								</div>
								<div className="pl-4">{renderFieldValue(field, record[field.name])}</div>
							</div>
						))}
					</div>
 
					{/* Computed Fields */}
					{schema.some(f => f.type === 'computed') && (
						<div className="space-y-3 pb-4">
							<div className="flex items-center gap-2">
								<Calculator className="h-4 w-4 text-muted-foreground" />
								<p className="text-xs font-semibold text-muted-foreground uppercase">Computed Fields</p>
							</div>
							{schema.filter(f => f.type === 'computed').map((field) => (
								<div key={field.name} className="border-b pb-3 last:border-0 bg-blue-50/30 dark:bg-blue-950/10 rounded px-2">
									<div className="flex items-center gap-2 mb-1">
										<span className="font-medium text-sm">{field.name}</span>
										<Badge variant="secondary" className="text-xs">Computed</Badge>
										{field.return_type && (
											<Badge variant="outline" className="text-xs">{field.return_type}</Badge>
										)}
									</div>
									<div className="pl-4 text-muted-foreground">
										{renderFieldValue(field, record[field.name])}
									</div>
								</div>
							))}
						</div>
					)}
 
					{/* System Fields */}
					<div className="bg-muted/50 rounded-lg p-4 space-y-2">
						<p className="text-xs font-semibold text-muted-foreground uppercase">
							System Fields
						</p>
						<div className="grid grid-cols-2 gap-4 text-sm">
							<div>
								<span className="text-muted-foreground">ID:</span>{' '}
								<span className="font-mono text-xs">{record.id as React.ReactNode}</span>
							</div>
							<div>
								<span className="text-muted-foreground">Account ID:</span>{' '}
								<span className="font-mono text-xs">{record.account_id as React.ReactNode}</span>
							</div>
							<div>
								<span className="text-muted-foreground">Created:</span>{' '}
								<span>{formatDate(record.created_at as string)}</span>
							</div>
							<div>
								<span className="text-muted-foreground">Updated:</span>{' '}
								<span>{formatDate(record.updated_at as string)}</span>
							</div>
							<div>
								<span className="text-muted-foreground">Created By:</span>{' '}
								<span className="font-mono text-xs">{record.created_by as React.ReactNode}</span>
							</div>
							<div>
								<span className="text-muted-foreground">Updated By:</span>{' '}
								<span className="font-mono text-xs">{record.updated_by as React.ReactNode}</span>
							</div>
						</div>
					</div>
				</div>
			)}
		</AppDialog>
	);
}