All files / components/records DynamicFieldInput.tsx

47.82% Statements 33/69
57.36% Branches 74/129
37.5% Functions 9/24
49.25% Lines 33/67

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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477                                                                                                                                                    3x 3x 3x   3x 3x   3x                 3x                                                                                                                                                                                                                                                                               215x     215x         215x 2x         2x 2x 2x   1x         215x 215x       141x         54x               58x         1x                 5x           1x                     1x                           2x                                                                               5x           5x           2x                       3x                                                                           215x 215x                       215x     215x                                                                                            
/**
 * Dynamic field input component
 * Renders different input types based on field definition
 */
 
import { useState } from 'react';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Badge } from '@/components/ui/badge';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Button } from '@/components/ui/button';
import { Calendar } from '@/components/ui/calendar';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
import { format } from 'date-fns';
import { CalendarIcon, Check, ChevronsUpDown, Eye, X } from 'lucide-react';
import { cn } from '@/lib/utils';
import type { FieldDefinition } from '@/services/collections.service';
import type { RecordData } from '@/types/records.types';
import { isoToDatetimeLocal, datetimeLocalToIso } from '@/lib/form-helpers';
import FileUploadInput from './FileUploadInput';
import { type FileMetadata } from '@/services/files.service';
 
interface DynamicFieldInputProps {
	field: FieldDefinition;
	value: unknown;
	onChange: (value: unknown) => void;
	error?: string;
	disabled?: boolean;
	referenceRecords?: RecordData[];
}
 
/** Returns a human-readable display value for a record (prefers 'name' field, then first text field). */
function getRecordDisplayValue(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 textKey = Object.keys(record).find(
		(key) => !systemKeys.includes(key) && typeof record[key] === 'string' && record[key],
	);
	if (textKey) return String(record[textKey]);
	return String(record.id ?? '').slice(0, 12);
}
 
/** User-facing (non-system) fields from a record, 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);
}
 
// ---------------------------------------------------------------------------
// ReferenceCombobox — async-search combobox for reference fields
// ---------------------------------------------------------------------------
 
interface ReferenceComboboxProps {
	field: FieldDefinition;
	value: string;
	onChange: (value: unknown) => void;
	disabled?: boolean;
	error?: boolean;
	referenceRecords: RecordData[];
}
 
function ReferenceCombobox({
	field,
	value,
	onChange,
	disabled,
	error,
	referenceRecords,
}: ReferenceComboboxProps) {
	const [open, setOpen] = useState(false);
	const [query, setQuery] = useState('');
	const [viewOpen, setViewOpen] = useState(false);
 
	const selectedRecord = referenceRecords.find((r) => r.id === value) ?? null;
	const displayValue = selectedRecord ? getRecordDisplayValue(selectedRecord) : null;
 
	const filteredRecords = query
		? referenceRecords.filter((record) => {
				const dv = getRecordDisplayValue(record).toLowerCase();
				const id = String(record.id ?? '').toLowerCase();
				const q = query.toLowerCase();
				return dv.includes(q) || id.includes(q);
			})
		: referenceRecords;
 
	return (
		<div className="flex gap-1">
			{/* Combobox trigger */}
			<Popover open={open} onOpenChange={setOpen}>
				<PopoverTrigger asChild>
					<Button
						variant="outline"
						role="combobox"
						disabled={disabled}
						className={cn(
							'flex-1 justify-between font-normal min-w-0',
							!value && 'text-muted-foreground',
							error ? 'border-destructive' : '',
						)}
					>
						<span className="truncate">
							{displayValue ?? (value ? (
								<span className="font-mono text-xs">{String(value).slice(0, 12)}…</span>
							) : (
								`Select ${field.collection}…`
							))}
						</span>
						<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
					</Button>
				</PopoverTrigger>
				<PopoverContent className="w-[300px] p-0" align="start">
					<Command shouldFilter={false}>
						<CommandInput
							placeholder={`Search ${field.collection}…`}
							value={query}
							onValueChange={setQuery}
						/>
						<CommandList>
							<CommandEmpty>No records found.</CommandEmpty>
							<CommandGroup>
								{filteredRecords.map((record) => {
									const id = record.id as string;
									const dv = getRecordDisplayValue(record);
									return (
										<CommandItem
											key={id}
											value={id}
											onSelect={() => {
												onChange(id);
												setOpen(false);
												setQuery('');
											}}
										>
											<Check
												className={cn('mr-2 h-4 w-4 shrink-0', value === id ? 'opacity-100' : 'opacity-0')}
											/>
											<div className="flex flex-col min-w-0 flex-1">
												<span className="truncate text-sm">{dv}</span>
												<span className="font-mono text-xs text-muted-foreground truncate">
													{id.slice(0, 20)}…
												</span>
											</div>
										</CommandItem>
									);
								})}
							</CommandGroup>
						</CommandList>
					</Command>
				</PopoverContent>
			</Popover>
 
			{/* Clear button */}
			{value && !disabled && (
				<Button
					type="button"
					variant="ghost"
					size="icon"
					className="h-9 w-9 shrink-0"
					onClick={() => onChange(null)}
					title="Clear selection"
				>
					<X className="h-4 w-4" />
				</Button>
			)}
 
			{/* View popover — only when a record is found in the cache */}
			{value && selectedRecord && (
				<Popover open={viewOpen} onOpenChange={setViewOpen}>
					<PopoverTrigger asChild>
						<Button
							type="button"
							variant="ghost"
							size="icon"
							className="h-9 w-9 shrink-0"
							title="View referenced record"
						>
							<Eye className="h-4 w-4" />
						</Button>
					</PopoverTrigger>
					<PopoverContent className="w-64" align="end">
						<div className="space-y-2">
							<p className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">
								{field.collection} record
							</p>
							<p className="font-mono text-xs text-muted-foreground break-all">
								{selectedRecord.id as string}
							</p>
							<div className="space-y-1 pt-1 border-t">
								{getUserFields(selectedRecord).map(([key, val]) => (
									<div key={key} className="flex justify-between gap-2 text-sm">
										<span className="text-muted-foreground shrink-0">{key}:</span>
										<span className="font-medium truncate max-w-[130px]">
											{val === null || val === undefined ? (
												<span className="italic text-muted-foreground">null</span>
											) : (
												String(val)
											)}
										</span>
									</div>
								))}
							</div>
						</div>
					</PopoverContent>
				</Popover>
			)}
		</div>
	);
}
 
// ---------------------------------------------------------------------------
// DynamicFieldInput — main export
// ---------------------------------------------------------------------------
 
export default function DynamicFieldInput({
	field,
	value,
	onChange,
	error,
	disabled = false,
	referenceRecords = [],
}: DynamicFieldInputProps) {
	const [jsonError, setJsonError] = useState<string | null>(null);
 
	// JSON field validation
	const handleJsonChange = (newValue: string) => {
		setJsonError(null);
		onChange(newValue);
	};
 
	const handleJsonBlur = (currentValue: string) => {
		Iif (!currentValue.trim()) {
			setJsonError(null);
			return;
		}
 
		try {
			JSON.parse(currentValue);
			setJsonError(null);
		} catch {
			setJsonError('Invalid JSON format');
		}
	};
 
	// Render input based on field type
	const renderInput = () => {
		switch (field.type) {
			case 'text':
			case 'email':
			case 'url':
				return (
					<Input
						id={`field-${field.name}`}
						type={field.type === 'email' ? 'email' : field.type === 'url' ? 'url' : 'text'}
						value={(value as string | number) || ''}
						onChange={(e) => onChange(e.target.value)}
						disabled={disabled}
						placeholder={`Enter ${field.name}`}
						className={error ? 'border-destructive' : ''}
					/>
				);
 
			case 'number':
				return (
					<Input
						id={`field-${field.name}`}
						type="number"
						value={(value as string | number) ?? ''}
						onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}
						disabled={disabled}
						placeholder={`Enter ${field.name}`}
						step="any"
						className={error ? 'border-destructive' : ''}
					/>
				);
 
			case 'boolean':
				return (
					<div className="flex items-center space-x-2">
						<input
							id={`field-${field.name}`}
							type="checkbox"
							checked={Boolean(value) || false}
							onChange={(e) => onChange(e.target.checked)}
							disabled={disabled}
							className="h-4 w-4 rounded border-gray-300"
						/>
						<Label htmlFor={`field-${field.name}`} className="cursor-pointer">
							{value ? 'Yes' : 'No'}
						</Label>
					</div>
				);
 
			case 'datetime':
				return (
					<Input
						id={`field-${field.name}`}
						type="datetime-local"
						value={typeof value === 'string' ? isoToDatetimeLocal(value) : ''}
						onChange={(e) =>
							onChange(e.target.value ? datetimeLocalToIso(e.target.value) : null)
						}
						disabled={disabled}
						className={error ? 'border-destructive' : ''}
					/>
				);
 
			case 'date':
				return (
					<Popover>
						<PopoverTrigger asChild>
							<Button
								variant={'outline'}
								className={cn(
									'w-full justify-start text-left font-normal',
									!value && 'text-muted-foreground',
									error ? 'border-destructive' : '',
								)}
								disabled={disabled}
							>
								<CalendarIcon className="mr-2 h-4 w-4" />
								{value ? (
									format(new Date(value as string), 'PPP')
								) : (
									<span>Pick a date</span>
								)}
							</Button>
						</PopoverTrigger>
						<PopoverContent className="w-auto p-0">
							<Calendar
								mode="single"
								selected={value ? new Date(value as string) : undefined}
								onSelect={(date) => {
									if (date) {
										// Format as YYYY-MM-DD for backend
										onChange(format(date, 'yyyy-MM-dd'));
									} else {
										onChange(null);
									}
								}}
								initialFocus
							/>
						</PopoverContent>
					</Popover>
				);
 
			case 'json': {
				const displayValue =
					typeof value === 'string'
						? value
						: value !== null && value !== undefined
							? JSON.stringify(value, null, 2)
							: '';
 
				return (
					<div className="space-y-2">
						<Textarea
							id={`field-${field.name}`}
							value={displayValue}
							onChange={(e) => handleJsonChange(e.target.value)}
							onBlur={(e) => handleJsonBlur(e.target.value)}
							disabled={disabled}
							placeholder={`Enter JSON for ${field.name}`}
							rows={4}
							className={`font-mono text-sm ${error || jsonError ? 'border-destructive' : ''}`}
						/>
						{jsonError && <p className="text-sm text-destructive">{jsonError}</p>}
					</div>
				);
			}
 
			case 'reference':
				return (
					<ReferenceCombobox
						field={field}
						value={(value as string) || ''}
						onChange={onChange}
						disabled={disabled}
						error={!!error}
						referenceRecords={referenceRecords}
					/>
				);
 
			case 'file':
				// File upload with drag-and-drop support
				return (
					<FileUploadInput
						value={value as FileMetadata | null}
						onChange={onChange}
						disabled={disabled}
						fieldName={field.name}
					/>
				);
 
			default:
				return (
					<Input
						id={`field-${field.name}`}
						type="text"
						value={(value as string | number) || ''}
						onChange={(e) => onChange(e.target.value)}
						disabled={disabled}
						placeholder={`Enter ${field.name}`}
						className={error ? 'border-destructive' : ''}
					/>
				);
		}
	};
 
	// Field info badge
	const getFieldTypeInfo = () => {
		const typeLabels: Record<string, string> = {
			text: 'Text',
			number: 'Number',
			boolean: 'Boolean',
			datetime: 'DateTime',
			email: 'Email',
			url: 'URL',
			json: 'JSON',
			reference: `Reference to ${field.collection}`,
			file: 'File',
			date: 'Date',
		};
		return typeLabels[field.type] || field.type;
	};
 
	return (
		<div className="space-y-2">
			<div className="flex items-center justify-between">
				<Label htmlFor={`field-${field.name}`} className="flex items-center gap-2">
					{field.name}
					{field.required && <span className="text-destructive">*</span>}
					{field.unique && (
						<Badge variant="outline" className="text-xs">
							Unique
						</Badge>
					)}
					{field.pii && (
						<Badge variant="secondary" className="text-xs">
							PII
						</Badge>
					)}
				</Label>
				<Badge variant="outline" className="text-xs">
					{getFieldTypeInfo()}
				</Badge>
			</div>
 
			{renderInput()}
 
			{error && !jsonError && <p className="text-sm text-destructive">{error}</p>}
 
			{field.type === 'json' && !jsonError && (
				<p className="text-xs text-muted-foreground">
					Enter valid JSON. Example: {`{"key": "value"}`}
				</p>
			)}
 
			{field.type === 'reference' && referenceRecords.length === 0 && (
				<p className="text-xs text-muted-foreground">
					No records found in target collection
				</p>
			)}
 
			{field.default !== undefined && !value && (
				<p className="text-xs text-muted-foreground">
					Default: {String(field.default)}
				</p>
			)}
		</div>
	);
}