767 lines
32 KiB
TypeScript
767 lines
32 KiB
TypeScript
import React, { useState, useMemo } from 'react';
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { useForm } from 'react-hook-form';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { z } from 'zod';
|
|
import {
|
|
FileText,
|
|
Plus,
|
|
Search,
|
|
Filter,
|
|
RefreshCw,
|
|
Edit2,
|
|
Eye,
|
|
Layers,
|
|
Calendar,
|
|
User as UserIcon,
|
|
Tag,
|
|
CheckCircle2,
|
|
AlertCircle,
|
|
Coins,
|
|
} from 'lucide-react';
|
|
import { Modal } from '../ui/Modal';
|
|
import { HelpTooltip } from '../ui/HelpTooltip';
|
|
import { useToast } from '../ui/Toast';
|
|
import { ExportButton } from '../ui/ExportButton';
|
|
import { ExportColumn } from '../../utils/exportUtils';
|
|
import { useTableSort, SortableTh } from '../../utils/tableSort';
|
|
import {
|
|
Instrument,
|
|
Instruction,
|
|
Programme,
|
|
ActivityStatus,
|
|
} from '../../types';
|
|
import {
|
|
getAllInstruments,
|
|
createInstrument,
|
|
updateInstrument,
|
|
} from '../../services/api';
|
|
import {
|
|
getActivityStatusBadge,
|
|
formatDate,
|
|
getUserEmail,
|
|
ACTIVITY_STATUS_OPTIONS,
|
|
} from '../../lib/formatters';
|
|
|
|
const instrumentSchema = z.object({
|
|
programmeId: z.coerce.number().min(1, 'Please select an associated programme'),
|
|
name: z.string().min(3, 'Funding instrument title must be at least 3 characters'),
|
|
code: z.string().optional(),
|
|
description: z.string().min(5, 'Description is required (minimum 5 characters)'),
|
|
status: z.coerce.number(),
|
|
});
|
|
|
|
type InstrumentFormValues = z.infer<typeof instrumentSchema>;
|
|
|
|
interface FundingInstrumentsSectionProps {
|
|
programmes: Programme[];
|
|
currentUserId: string;
|
|
preselectedProgrammeId?: string | number | null;
|
|
onClearPreselectedProgramme?: () => void;
|
|
isCreateModalOpenExternal?: boolean;
|
|
onCloseCreateModalExternal?: () => void;
|
|
}
|
|
|
|
export const FundingInstrumentsSection: React.FC<FundingInstrumentsSectionProps> = ({
|
|
programmes,
|
|
currentUserId,
|
|
preselectedProgrammeId,
|
|
onClearPreselectedProgramme,
|
|
isCreateModalOpenExternal,
|
|
onCloseCreateModalExternal,
|
|
}) => {
|
|
const queryClient = useQueryClient();
|
|
const { success, error: toastError } = useToast();
|
|
|
|
const [searchTerm, setSearchTerm] = useState('');
|
|
const [selectedProgrammeFilter, setSelectedProgrammeFilter] = useState<string>(
|
|
preselectedProgrammeId ? String(preselectedProgrammeId) : 'all'
|
|
);
|
|
const [statusFilter, setStatusFilter] = useState<string>('all');
|
|
|
|
// Modals
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const [editingInstrument, setEditingInstrument] = useState<Instrument | null>(null);
|
|
const [inspectingInstrument, setInspectingInstrument] = useState<Instrument | null>(null);
|
|
|
|
// Form setup
|
|
const instrumentForm = useForm<InstrumentFormValues>({
|
|
resolver: zodResolver(instrumentSchema),
|
|
defaultValues: {
|
|
programmeId: programmes[0]?.id ? Number(programmes[0].id) : 0,
|
|
name: '',
|
|
code: '',
|
|
description: '',
|
|
status: ActivityStatus.Active,
|
|
},
|
|
});
|
|
|
|
// Sync external create modal trigger if provided
|
|
React.useEffect(() => {
|
|
if (isCreateModalOpenExternal) {
|
|
setEditingInstrument(null);
|
|
instrumentForm.reset({
|
|
programmeId: preselectedProgrammeId ? Number(preselectedProgrammeId) : (programmes[0]?.id ? Number(programmes[0].id) : 0),
|
|
name: '',
|
|
code: '',
|
|
description: '',
|
|
status: ActivityStatus.Active,
|
|
});
|
|
setIsModalOpen(true);
|
|
}
|
|
}, [isCreateModalOpenExternal, preselectedProgrammeId, programmes, instrumentForm]);
|
|
|
|
// Query: Get all funding instruments primarily from API
|
|
const {
|
|
data: instruments = [],
|
|
isLoading,
|
|
isFetching,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: ['all-instruments', programmes],
|
|
queryFn: () => getAllInstruments(programmes),
|
|
});
|
|
|
|
// Programme lookup helper
|
|
const programmeMap = useMemo(() => {
|
|
const map = new Map<string, Programme>();
|
|
programmes.forEach((p) => map.set(String(p.id), p));
|
|
return map;
|
|
}, [programmes]);
|
|
|
|
// Mutation: Create or Update Funding Instrument
|
|
const mutation = useMutation({
|
|
mutationFn: async (data: InstrumentFormValues) => {
|
|
if (editingInstrument) {
|
|
await updateInstrument({
|
|
instructionId: editingInstrument.id,
|
|
instrumentId: editingInstrument.id,
|
|
id: editingInstrument.id,
|
|
programmeId: data.programmeId,
|
|
code: data.code,
|
|
name: data.name,
|
|
description: data.description,
|
|
status: data.status !== undefined ? Number(data.status) : ActivityStatus.Active,
|
|
userId: currentUserId,
|
|
});
|
|
} else {
|
|
await createInstrument({
|
|
programmeId: data.programmeId,
|
|
code: data.code,
|
|
name: data.name,
|
|
description: data.description,
|
|
status: data.status !== undefined ? Number(data.status) : ActivityStatus.Active,
|
|
userId: currentUserId,
|
|
});
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['all-instruments'] });
|
|
queryClient.invalidateQueries({ queryKey: ['all-instructions'] });
|
|
queryClient.invalidateQueries({ queryKey: ['instruments'] });
|
|
success(
|
|
editingInstrument ? 'Funding Instrument Updated' : 'Funding Instrument Created',
|
|
editingInstrument
|
|
? 'Funding instrument record updated successfully via the API.'
|
|
: 'New funding instrument created and linked to strategic programme successfully.'
|
|
);
|
|
setIsModalOpen(false);
|
|
setEditingInstrument(null);
|
|
if (onCloseCreateModalExternal) onCloseCreateModalExternal();
|
|
},
|
|
onError: (err: unknown) => {
|
|
toastError('Save Failed', err);
|
|
},
|
|
});
|
|
|
|
// Handlers
|
|
const openCreateModal = (programmeId?: number | string) => {
|
|
setEditingInstrument(null);
|
|
instrumentForm.reset({
|
|
programmeId: programmeId
|
|
? Number(programmeId)
|
|
: selectedProgrammeFilter !== 'all'
|
|
? Number(selectedProgrammeFilter)
|
|
: programmes[0]?.id
|
|
? Number(programmes[0].id)
|
|
: 0,
|
|
name: '',
|
|
code: '',
|
|
description: '',
|
|
status: ActivityStatus.Active,
|
|
});
|
|
setIsModalOpen(true);
|
|
};
|
|
|
|
const openEditModal = (inst: Instrument) => {
|
|
setEditingInstrument(inst);
|
|
instrumentForm.reset({
|
|
programmeId: Number(inst.programmeId),
|
|
name: inst.name || '',
|
|
code: inst.code || '',
|
|
description: inst.description || '',
|
|
status: Number(inst.status),
|
|
});
|
|
setIsModalOpen(true);
|
|
};
|
|
|
|
const handleCloseModal = () => {
|
|
setIsModalOpen(false);
|
|
setEditingInstrument(null);
|
|
if (onCloseCreateModalExternal) onCloseCreateModalExternal();
|
|
};
|
|
|
|
// Filter funding instruments
|
|
const filteredInstruments = useMemo(() => {
|
|
return instruments.filter((inst) => {
|
|
// Programme filter
|
|
if (selectedProgrammeFilter !== 'all' && String(inst.programmeId) !== String(selectedProgrammeFilter)) {
|
|
return false;
|
|
}
|
|
// Status filter
|
|
if (statusFilter !== 'all' && String(inst.status) !== String(statusFilter)) {
|
|
return false;
|
|
}
|
|
// Search term
|
|
if (searchTerm.trim()) {
|
|
const query = searchTerm.toLowerCase();
|
|
const pName = programmeMap.get(String(inst.programmeId))?.name?.toLowerCase() || '';
|
|
const matchTitle = (inst.name || '').toLowerCase().includes(query);
|
|
const matchCode = (inst.code || '').toLowerCase().includes(query);
|
|
const matchDesc = (inst.description || '').toLowerCase().includes(query);
|
|
const matchProg = pName.includes(query);
|
|
if (!matchTitle && !matchCode && !matchDesc && !matchProg) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
}, [instruments, selectedProgrammeFilter, statusFilter, searchTerm, programmeMap]);
|
|
|
|
// Sorting hook
|
|
const {
|
|
sortedData: sortedInstruments,
|
|
sortColumn: instSortCol,
|
|
sortDirection: instSortDir,
|
|
handleSort: handleInstSort,
|
|
} = useTableSort<Instrument>(filteredInstruments, null, null, {
|
|
code: (inst: Instrument) => inst.code || `INST-${inst.id}`,
|
|
name: (inst: Instrument) => inst.name,
|
|
programmeId: (inst: Instrument) => programmeMap.get(String(inst.programmeId))?.name || `Programme #${inst.programmeId}`,
|
|
status: (inst: Instrument) => getActivityStatusBadge(inst.status).label,
|
|
createdAt: (inst: Instrument) => inst.createdAt,
|
|
});
|
|
|
|
// Export columns definition
|
|
const instrumentExportColumns: ExportColumn<Instrument>[] = useMemo(
|
|
() => [
|
|
{ header: 'Instrument ID', key: 'id' },
|
|
{ header: 'Instrument Code', key: 'code', format: (val) => val || '—' },
|
|
{ header: 'Title / Name', key: 'name', format: (val) => val || 'Untitled' },
|
|
{
|
|
header: 'Strategic Programme',
|
|
key: 'programmeId',
|
|
format: (val) => programmeMap.get(String(val))?.name || `Programme #${val}`,
|
|
},
|
|
{
|
|
header: 'Status',
|
|
key: 'status',
|
|
format: (val) => (Number(val) === ActivityStatus.Active ? 'Active' : 'Inactive'),
|
|
},
|
|
{ header: 'Description', key: 'description', format: (val) => val || '—' },
|
|
{ header: 'Created Date', key: 'createdAt', format: (val) => (val ? formatDate(val) : '—') },
|
|
],
|
|
[programmeMap]
|
|
);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
{/* Top Filter and Actions Bar */}
|
|
<div className="flex flex-col lg:flex-row lg:items-center justify-between gap-3 bg-[#0d121f]/90 p-4 rounded-2xl border border-slate-800/80 backdrop-blur-sm">
|
|
<div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-3 flex-1">
|
|
{/* Search Input */}
|
|
<div className="relative flex-1 max-w-md">
|
|
<Search className="w-4 h-4 text-slate-500 absolute left-3 top-3" />
|
|
<input
|
|
type="text"
|
|
placeholder="Search funding instruments by title, code, description, or programme..."
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
className="w-full pl-9 pr-4 py-2.5 text-xs bg-[#090d16] border border-slate-700/80 rounded-xl focus:outline-none focus:ring-1 focus:ring-blue-500 text-slate-200 placeholder:text-slate-500 transition shadow-xs"
|
|
/>
|
|
</div>
|
|
|
|
{/* Programme Filter Dropdown */}
|
|
<div className="flex items-center gap-2">
|
|
<label className="text-xs text-slate-400 whitespace-nowrap flex items-center gap-1 font-medium">
|
|
<Layers className="w-3.5 h-3.5 text-slate-400" />
|
|
Programme:
|
|
</label>
|
|
<select
|
|
value={selectedProgrammeFilter}
|
|
onChange={(e) => {
|
|
setSelectedProgrammeFilter(e.target.value);
|
|
if (onClearPreselectedProgramme && e.target.value === 'all') {
|
|
onClearPreselectedProgramme();
|
|
}
|
|
}}
|
|
className="px-3 py-2 text-xs bg-[#090d16] border border-slate-700/80 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500 max-w-[200px]"
|
|
>
|
|
<option value="all">All Programmes ({programmes.length})</option>
|
|
{programmes.map((p) => (
|
|
<option key={p.id} value={String(p.id)}>
|
|
{p.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
{/* Status Filter Dropdown */}
|
|
<div className="flex items-center gap-2">
|
|
<label className="text-xs text-slate-400 whitespace-nowrap flex items-center gap-1 font-medium">
|
|
<Filter className="w-3.5 h-3.5 text-slate-400" />
|
|
Status:
|
|
</label>
|
|
<select
|
|
value={statusFilter}
|
|
onChange={(e) => setStatusFilter(e.target.value)}
|
|
className="px-3 py-2 text-xs bg-[#090d16] border border-slate-700/80 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
|
>
|
|
<option value="all">All Statuses</option>
|
|
<option value={String(ActivityStatus.Active)}>Active</option>
|
|
<option value={String(ActivityStatus.Inactive)}>Inactive</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Action Buttons: Refresh, Export & Add */}
|
|
<div className="flex items-center gap-2 self-end lg:self-center">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
refetch();
|
|
success('Refreshed', 'Funding instruments updated from API.');
|
|
}}
|
|
disabled={isFetching}
|
|
className="inline-flex items-center justify-center gap-1.5 px-3.5 py-2 text-xs font-semibold text-slate-300 bg-[#0d121f] hover:bg-slate-800 border border-slate-700/80 rounded-xl transition cursor-pointer shadow-xs disabled:opacity-50"
|
|
title="Refresh Funding Instruments from API"
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 text-blue-400 ${isFetching ? 'animate-spin' : ''}`} />
|
|
<span>Refresh</span>
|
|
</button>
|
|
|
|
<ExportButton
|
|
claim="governance"
|
|
title="Funding Instruments Registry"
|
|
subtitle="Authoritative funding instruments and financing directives attached to strategic programmes"
|
|
filename={`pfm-funding-instruments-${new Date().toISOString().split('T')[0]}`}
|
|
columns={instrumentExportColumns}
|
|
data={filteredInstruments}
|
|
/>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => openCreateModal()}
|
|
className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500 rounded-xl shadow-md shadow-blue-500/20 transition cursor-pointer"
|
|
>
|
|
<Plus className="w-4 h-4" />
|
|
<span>Add Instrument</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Funding Instruments Table & Cards */}
|
|
<div className="rounded-2xl border border-slate-800/80 bg-[#0d121f]/90 overflow-hidden shadow-xl">
|
|
<div className="p-4 border-b border-slate-800/80 flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<Coins className="w-4 h-4 text-blue-400" />
|
|
<h3 className="text-xs font-bold text-slate-200 uppercase tracking-wider">
|
|
Funding Instruments & Financing Directives ({filteredInstruments.length})
|
|
</h3>
|
|
<HelpTooltip
|
|
title="Funding Instruments"
|
|
content="Binding funding instruments and financing directives tied to strategic funding programmes. Defines capital allocation mechanisms, rules, and statutory guidelines."
|
|
placement="right"
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<span className="text-[11px] text-slate-400 font-mono">
|
|
{filteredInstruments.length} of {instruments.length} recorded
|
|
</span>
|
|
</div>
|
|
|
|
{isLoading ? (
|
|
<div className="p-12 text-center text-xs text-slate-400 flex flex-col items-center justify-center gap-2">
|
|
<RefreshCw className="w-5 h-5 animate-spin text-blue-400" />
|
|
<span>Loading funding instruments directory...</span>
|
|
</div>
|
|
) : filteredInstruments.length === 0 ? (
|
|
<div className="p-12 text-center text-xs text-slate-400 space-y-3">
|
|
<div className="text-slate-500">
|
|
{searchTerm || selectedProgrammeFilter !== 'all' || statusFilter !== 'all'
|
|
? 'No funding instruments match the active filters.'
|
|
: 'No funding instruments registered yet. Link funding instruments to programmes using the button below.'}
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={() => openCreateModal()}
|
|
className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-blue-600 hover:bg-blue-500 rounded-xl transition cursor-pointer"
|
|
>
|
|
<Plus className="w-4 h-4" />
|
|
Add First Funding Instrument
|
|
</button>
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left border-collapse">
|
|
<thead>
|
|
<tr className="border-b border-slate-800/80 bg-slate-900/50 text-[11px] font-semibold text-slate-400 uppercase tracking-wider">
|
|
<SortableTh column="code" currentColumn={instSortCol} direction={instSortDir} onSort={handleInstSort} className="py-3 px-4">
|
|
Code / Ref
|
|
</SortableTh>
|
|
<SortableTh column="name" currentColumn={instSortCol} direction={instSortDir} onSort={handleInstSort} className="py-3 px-4">
|
|
Instrument Title
|
|
</SortableTh>
|
|
<SortableTh column="programmeId" currentColumn={instSortCol} direction={instSortDir} onSort={handleInstSort} className="py-3 px-4">
|
|
Associated Programme
|
|
</SortableTh>
|
|
<SortableTh column="status" currentColumn={instSortCol} direction={instSortDir} onSort={handleInstSort} className="py-3 px-4">
|
|
Status
|
|
</SortableTh>
|
|
<SortableTh column="createdAt" currentColumn={instSortCol} direction={instSortDir} onSort={handleInstSort} className="py-3 px-4">
|
|
Created Info
|
|
</SortableTh>
|
|
<th className="py-3 px-4 text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-slate-800/60 text-xs text-slate-300">
|
|
{sortedInstruments.map((inst) => {
|
|
const statusBadge = getActivityStatusBadge(inst.status);
|
|
const parentProg = programmeMap.get(String(inst.programmeId));
|
|
|
|
return (
|
|
<tr key={inst.id} className="hover:bg-slate-800/30 transition-colors">
|
|
<td className="py-3.5 px-4 font-mono font-bold text-blue-400">
|
|
<span className="bg-blue-500/10 px-2 py-0.5 rounded border border-blue-500/20 text-[11px]">
|
|
{inst.code || `INST-${inst.id}`}
|
|
</span>
|
|
</td>
|
|
<td className="py-3.5 px-4">
|
|
<div className="font-semibold text-white max-w-sm">{inst.name}</div>
|
|
{inst.description && (
|
|
<div className="text-[11px] text-slate-400 line-clamp-1 mt-0.5 max-w-md">
|
|
{inst.description}
|
|
</div>
|
|
)}
|
|
</td>
|
|
<td className="py-3.5 px-4">
|
|
{parentProg ? (
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-lg text-xs bg-slate-800/80 text-slate-200 border border-slate-700 font-medium">
|
|
<Layers className="w-3 h-3 text-blue-400" />
|
|
{parentProg.name}
|
|
</span>
|
|
) : (
|
|
<span className="text-slate-500 font-mono text-xs">
|
|
Programme #{inst.programmeId}
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="py-3.5 px-4">
|
|
<span
|
|
className={`inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-semibold border ${statusBadge.className}`}
|
|
>
|
|
{statusBadge.label}
|
|
</span>
|
|
</td>
|
|
<td className="py-3.5 px-4 text-[11px] text-slate-400 font-mono">
|
|
<div>{formatDate(inst.createdAt)}</div>
|
|
{inst.createdBy && (
|
|
<div className="text-[10px] text-slate-500">{getUserEmail(inst.createdBy)}</div>
|
|
)}
|
|
</td>
|
|
<td className="py-3.5 px-4 text-right">
|
|
<div className="flex items-center justify-end gap-1.5">
|
|
<button
|
|
type="button"
|
|
onClick={() => setInspectingInstrument(inst)}
|
|
className="inline-flex items-center gap-1 text-xs font-semibold text-cyan-400 hover:text-white bg-cyan-500/10 hover:bg-cyan-500/20 border border-cyan-500/30 px-2.5 py-1 rounded-lg transition shadow-xs cursor-pointer"
|
|
title="Inspect Instrument"
|
|
>
|
|
<Eye className="w-3 h-3" />
|
|
Inspect
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => openEditModal(inst)}
|
|
className="inline-flex items-center gap-1 text-xs font-semibold text-slate-200 hover:text-white bg-slate-900/80 hover:bg-slate-800 border border-slate-700/80 px-2.5 py-1 rounded-lg transition shadow-xs cursor-pointer"
|
|
title="Edit / Update Instrument"
|
|
>
|
|
<Edit2 className="w-3 h-3" />
|
|
Edit
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Modal: Create / Edit Funding Instrument */}
|
|
<Modal
|
|
isOpen={isModalOpen}
|
|
onClose={handleCloseModal}
|
|
title={editingInstrument ? 'Edit Funding Instrument' : 'Add New Funding Instrument'}
|
|
subtitle={
|
|
editingInstrument
|
|
? `Updating Instrument ${editingInstrument.code || `#${editingInstrument.id}`}`
|
|
: 'Authoritative funding instrument attached to a strategic programme'
|
|
}
|
|
helpText="Funding instruments operationalize funding programmes into concrete financing mechanisms. Select a strategic programme from the dropdown."
|
|
>
|
|
<form
|
|
onSubmit={instrumentForm.handleSubmit((data) => mutation.mutate(data))}
|
|
className="space-y-4"
|
|
>
|
|
{/* Associated Programme Dropdown */}
|
|
<div>
|
|
<div className="flex items-center gap-1 mb-1">
|
|
<label className="block text-xs font-semibold text-slate-300">
|
|
Associated Programme *
|
|
</label>
|
|
<HelpTooltip
|
|
title="Associated Programme"
|
|
content="Select the strategic funding programme under which this operational funding instrument is established."
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<select
|
|
{...instrumentForm.register('programmeId')}
|
|
className="w-full px-3 py-2 text-xs bg-[#090d16] border border-slate-700 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
|
>
|
|
<option value="">Select an associated programme...</option>
|
|
{programmes.map((p) => (
|
|
<option key={p.id} value={Number(p.id)}>
|
|
{p.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
{instrumentForm.formState.errors.programmeId && (
|
|
<p className="text-red-400 text-[11px] mt-1">
|
|
{instrumentForm.formState.errors.programmeId.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Title */}
|
|
<div>
|
|
<div className="flex items-center gap-1 mb-1">
|
|
<label className="block text-xs font-semibold text-slate-300">
|
|
Instrument Title *
|
|
</label>
|
|
<HelpTooltip
|
|
title="Instrument Title"
|
|
content="Formal title of the funding instrument (e.g., SME Technology Transfer Subsidy Guideline)."
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<input
|
|
type="text"
|
|
placeholder="e.g. Clean Energy Grant Directive 2026"
|
|
{...instrumentForm.register('name')}
|
|
className="w-full px-3 py-2 text-xs bg-[#090d16] border border-slate-700 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
|
/>
|
|
{instrumentForm.formState.errors.name && (
|
|
<p className="text-red-400 text-[11px] mt-1">
|
|
{instrumentForm.formState.errors.name.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Code */}
|
|
<div>
|
|
<div className="flex items-center gap-1 mb-1">
|
|
<label className="block text-xs font-semibold text-slate-300">
|
|
Instrument Code (Optional)
|
|
</label>
|
|
<HelpTooltip
|
|
title="Instrument Code"
|
|
content="Unique operational reference code (e.g., INST-CE-001). If left blank, one is generated automatically."
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<input
|
|
type="text"
|
|
placeholder="e.g. INST-CE-001"
|
|
{...instrumentForm.register('code')}
|
|
className="w-full px-3 py-2 text-xs bg-[#090d16] border border-slate-700 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500 font-mono"
|
|
/>
|
|
</div>
|
|
|
|
{/* Description */}
|
|
<div>
|
|
<div className="flex items-center gap-1 mb-1">
|
|
<label className="block text-xs font-semibold text-slate-300">
|
|
Description & Financing Directives *
|
|
</label>
|
|
<HelpTooltip
|
|
title="Instrument Description"
|
|
content="Comprehensive directives, statutory mandate, eligibility scope, and financing guidelines."
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<textarea
|
|
rows={4}
|
|
placeholder="Detail the financing mandate, eligibility scope, disbursement triggers, and statutory compliance requirements..."
|
|
{...instrumentForm.register('description')}
|
|
className="w-full px-3 py-2 text-xs bg-[#090d16] border border-slate-700 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
|
/>
|
|
{instrumentForm.formState.errors.description && (
|
|
<p className="text-red-400 text-[11px] mt-1">
|
|
{instrumentForm.formState.errors.description.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Status */}
|
|
<div>
|
|
<div className="flex items-center gap-1 mb-1">
|
|
<label className="block text-xs font-semibold text-slate-300">Status</label>
|
|
<HelpTooltip
|
|
title="Instrument Lifecycle Status"
|
|
content="Indicates whether this funding instrument is actively enforceable or archived/inactive."
|
|
size="sm"
|
|
/>
|
|
</div>
|
|
<select
|
|
{...instrumentForm.register('status')}
|
|
className="w-full px-3 py-2 text-xs bg-[#090d16] border border-slate-700 rounded-xl text-slate-200 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
|
>
|
|
{ACTIVITY_STATUS_OPTIONS.map((opt) => (
|
|
<option key={opt.value} value={opt.value}>
|
|
{opt.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
|
|
{/* Footer Actions */}
|
|
<div className="pt-3 border-t border-slate-800 flex justify-end gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleCloseModal}
|
|
className="px-4 py-2 text-xs font-semibold text-slate-300 bg-slate-800 hover:bg-slate-700 rounded-xl transition cursor-pointer"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={mutation.isPending}
|
|
className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-blue-600 hover:bg-blue-500 rounded-xl shadow-md transition disabled:opacity-50 cursor-pointer"
|
|
>
|
|
{mutation.isPending && <RefreshCw className="w-3.5 h-3.5 animate-spin" />}
|
|
{editingInstrument ? 'Save Instrument Changes' : 'Create Funding Instrument'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</Modal>
|
|
|
|
{/* Modal: Inspect Funding Instrument Detail */}
|
|
{inspectingInstrument && (
|
|
<Modal
|
|
isOpen={!!inspectingInstrument}
|
|
onClose={() => setInspectingInstrument(null)}
|
|
title={`Funding Instrument: ${inspectingInstrument.name || ''}`}
|
|
subtitle={`Reference: ${inspectingInstrument.code || `INST-${inspectingInstrument.id}`}`}
|
|
maxWidth="lg"
|
|
>
|
|
<div className="space-y-4 text-xs">
|
|
<div className="p-4 rounded-xl border border-slate-800 bg-[#0d121f] space-y-3">
|
|
<div className="flex items-center justify-between">
|
|
<span className="font-mono text-xs font-bold text-blue-400 bg-blue-500/10 px-2.5 py-1 rounded border border-blue-500/20">
|
|
{inspectingInstrument.code || `INST-${inspectingInstrument.id}`}
|
|
</span>
|
|
{(() => {
|
|
const badge = getActivityStatusBadge(inspectingInstrument.status);
|
|
return (
|
|
<span
|
|
className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-semibold border ${badge.className}`}
|
|
>
|
|
{badge.label}
|
|
</span>
|
|
);
|
|
})()}
|
|
</div>
|
|
|
|
<div>
|
|
<h4 className="text-sm font-bold text-white mb-1">{inspectingInstrument.name}</h4>
|
|
<p className="text-slate-300 whitespace-pre-wrap leading-relaxed">
|
|
{inspectingInstrument.description || 'No detailed description provided.'}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="pt-3 border-t border-slate-800/80 grid grid-cols-2 gap-3 text-[11px]">
|
|
<div>
|
|
<span className="text-slate-500 block">Associated Programme</span>
|
|
<span className="text-slate-200 font-semibold">
|
|
{programmeMap.get(String(inspectingInstrument.programmeId))?.name ||
|
|
`Programme #${inspectingInstrument.programmeId}`}
|
|
</span>
|
|
</div>
|
|
<div>
|
|
<span className="text-slate-500 block">Created On</span>
|
|
<span className="text-slate-200 font-mono">
|
|
{formatDate(inspectingInstrument.createdAt)}
|
|
</span>
|
|
</div>
|
|
<div>
|
|
<span className="text-slate-500 block">Created By</span>
|
|
<span className="text-slate-200 font-mono">
|
|
{inspectingInstrument.createdBy ? getUserEmail(inspectingInstrument.createdBy) : '—'}
|
|
</span>
|
|
</div>
|
|
{inspectingInstrument.updatedAt && (
|
|
<div>
|
|
<span className="text-slate-500 block">Last Mutated</span>
|
|
<span className="text-slate-200 font-mono">
|
|
{formatDate(inspectingInstrument.updatedAt)}
|
|
</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="pt-2 border-t border-slate-800 flex justify-between items-center">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
const inst = inspectingInstrument;
|
|
setInspectingInstrument(null);
|
|
openEditModal(inst);
|
|
}}
|
|
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-semibold text-slate-200 bg-slate-800 hover:bg-slate-700 rounded-xl border border-slate-700 cursor-pointer"
|
|
>
|
|
<Edit2 className="w-3.5 h-3.5" />
|
|
Edit Funding Instrument
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setInspectingInstrument(null)}
|
|
className="px-4 py-2 text-xs font-semibold text-slate-300 bg-slate-800 hover:bg-slate-700 rounded-xl cursor-pointer"
|
|
>
|
|
Close
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export const InstructionsSection = FundingInstrumentsSection;
|
|
|