277 lines
12 KiB
TypeScript
277 lines
12 KiB
TypeScript
import React from 'react';
|
|
import { Edit } from 'lucide-react';
|
|
import type { ModuleItem } from '../../types';
|
|
import { Pagination } from '../common/Pagination';
|
|
|
|
interface ModulesTabProps {
|
|
modules: ModuleItem[];
|
|
searchQuery: string;
|
|
statusFilter: string;
|
|
categoryFilter: string;
|
|
currentPage: number;
|
|
perPage: number;
|
|
onSearchChange: (query: string) => void;
|
|
onStatusFilterChange: (status: string) => void;
|
|
onCategoryFilterChange: (cat: string) => void;
|
|
onPageChange: (page: number) => void;
|
|
onPerPageChange: (perPage: number) => void;
|
|
onToggleStatus: (mod: ModuleItem) => void;
|
|
onEditModule: (mod: ModuleItem) => void;
|
|
}
|
|
|
|
export const ModulesTab: React.FC<ModulesTabProps> = ({
|
|
modules,
|
|
searchQuery,
|
|
statusFilter,
|
|
categoryFilter,
|
|
currentPage,
|
|
perPage,
|
|
onSearchChange,
|
|
onStatusFilterChange,
|
|
onCategoryFilterChange,
|
|
onPageChange,
|
|
onPerPageChange,
|
|
onToggleStatus,
|
|
onEditModule,
|
|
}) => {
|
|
const filtered = modules.filter((m) => {
|
|
const matchesSearch =
|
|
!searchQuery ||
|
|
(m.name || '').toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
(m.slug || '').toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
(m.code || '').toLowerCase().includes(searchQuery.toLowerCase()) ||
|
|
(m.description || '').toLowerCase().includes(searchQuery.toLowerCase());
|
|
|
|
const matchesStatus =
|
|
statusFilter === 'all' ||
|
|
(statusFilter === 'active' && m.is_active) ||
|
|
(statusFilter === 'inactive' && !m.is_active);
|
|
|
|
const matchesCategory =
|
|
categoryFilter === 'all' ||
|
|
(m.category || '').toLowerCase() === categoryFilter.toLowerCase();
|
|
|
|
return matchesSearch && matchesStatus && matchesCategory;
|
|
});
|
|
|
|
const totalFiltered = filtered.length;
|
|
const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage));
|
|
const safePage = Math.min(currentPage, totalPages);
|
|
const startIndex = (safePage - 1) * perPage;
|
|
const paginatedModules = filtered.slice(startIndex, startIndex + perPage);
|
|
|
|
const categories = [
|
|
{ key: 'all', label: 'All Categories' },
|
|
{ key: 'point', label: 'Points' },
|
|
{ key: 'Patterns', label: 'Patterns' },
|
|
{ key: 'Nutrition', label: 'Nutrition' },
|
|
{ key: 'Therapeutics', label: 'Therapeutics' },
|
|
];
|
|
|
|
return (
|
|
<div className="space-y-5">
|
|
{/* Search & Status Bar */}
|
|
<div className="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 space-y-3.5">
|
|
<div className="flex flex-col sm:flex-row gap-3 items-stretch sm:items-center justify-between">
|
|
{/* Search Box */}
|
|
<div className="flex-1 max-w-md relative">
|
|
<input
|
|
type="text"
|
|
placeholder="Search by name, slug, or code..."
|
|
value={searchQuery}
|
|
onChange={(e) => {
|
|
onSearchChange(e.target.value);
|
|
onPageChange(1);
|
|
}}
|
|
className="w-full bg-white border border-slate-200 rounded-xl pl-9 pr-4 py-2 text-xs text-slate-800 font-semibold focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 shadow-sm"
|
|
/>
|
|
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400 text-xs">🔍</span>
|
|
</div>
|
|
|
|
{/* Status Filter Segmented Control (Fixed Alignment) */}
|
|
<div className="flex items-center gap-1 bg-slate-200/70 p-1 rounded-xl shrink-0 flex-nowrap overflow-x-auto shadow-inner">
|
|
<span className="text-[10px] uppercase font-bold text-slate-500 px-1.5 hidden sm:inline whitespace-nowrap">
|
|
Status:
|
|
</span>
|
|
{[
|
|
{ key: 'all', label: `All (${modules.length})` },
|
|
{ key: 'active', label: `Active (${modules.filter((m) => m.is_active).length})` },
|
|
{ key: 'inactive', label: `Inactive (${modules.filter((m) => !m.is_active).length})` },
|
|
].map((f) => (
|
|
<button
|
|
key={f.key}
|
|
type="button"
|
|
onClick={() => {
|
|
onStatusFilterChange(f.key);
|
|
onPageChange(1);
|
|
}}
|
|
className={`px-3 py-1 rounded-lg text-xs font-bold transition-all cursor-pointer whitespace-nowrap ${
|
|
statusFilter === f.key
|
|
? 'bg-indigo-600 text-white shadow-sm'
|
|
: 'text-slate-600 hover:text-slate-900 hover:bg-white/60'
|
|
}`}
|
|
>
|
|
{f.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Category Filter Pills */}
|
|
<div className="flex items-center gap-1.5 flex-wrap border-t border-slate-200/60 pt-3">
|
|
<span className="text-[10px] uppercase font-bold text-slate-400 mr-1">Category:</span>
|
|
{categories.map((cat) => {
|
|
const count =
|
|
cat.key === 'all'
|
|
? modules.length
|
|
: modules.filter((m) => (m.category || '').toLowerCase() === cat.key.toLowerCase()).length;
|
|
const isSelected = categoryFilter.toLowerCase() === cat.key.toLowerCase();
|
|
return (
|
|
<button
|
|
key={cat.key}
|
|
type="button"
|
|
onClick={() => {
|
|
onCategoryFilterChange(cat.key);
|
|
onPageChange(1);
|
|
}}
|
|
className={`px-2.5 py-1 rounded-lg text-xs font-bold transition-colors cursor-pointer whitespace-nowrap ${
|
|
isSelected
|
|
? 'bg-slate-900 text-white shadow-sm'
|
|
: 'bg-white border border-slate-200 text-slate-600 hover:bg-slate-100 shadow-sm'
|
|
}`}
|
|
>
|
|
{cat.label} ({count})
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Modules Table */}
|
|
{filtered.length === 0 ? (
|
|
<div className="text-center py-12 bg-slate-50 border border-slate-200/60 rounded-2xl space-y-2">
|
|
<p className="text-sm font-bold text-slate-600">No modules match the selected filters.</p>
|
|
<p className="text-xs text-slate-400">Try changing your search keywords or filter pills.</p>
|
|
</div>
|
|
) : (
|
|
<div className="bg-white border border-slate-200 rounded-2xl overflow-hidden shadow-sm p-4">
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left border-collapse text-xs">
|
|
<thead>
|
|
<tr className="bg-slate-50/80 text-[10px] uppercase font-bold text-slate-400 tracking-wider border-b border-slate-200">
|
|
<th className="py-3 px-3.5">Module Name & Details</th>
|
|
<th className="py-3 px-3.5">Identifier / Code</th>
|
|
<th className="py-3 px-3.5">Category</th>
|
|
<th className="py-3 px-3.5">Linked Plans</th>
|
|
<th className="py-3 px-3.5">Catalog Status</th>
|
|
<th className="py-3 px-3.5 text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-slate-100">
|
|
{paginatedModules.map((mod) => (
|
|
<tr
|
|
key={mod.id}
|
|
className={`hover:bg-slate-50/80 transition-colors ${
|
|
!mod.is_active ? 'bg-slate-50/40 text-slate-500' : 'text-slate-800'
|
|
}`}
|
|
>
|
|
{/* Column 1: Module & Details */}
|
|
<td className="py-3.5 px-3.5">
|
|
<div className="flex items-center gap-3">
|
|
<div
|
|
className="h-8 w-8 rounded-xl flex items-center justify-center font-black text-xs text-white shadow-sm shrink-0"
|
|
style={{ backgroundColor: (mod as any).color || '#6366F1' }}
|
|
>
|
|
{(mod.name || 'M')[0].toUpperCase()}
|
|
</div>
|
|
<div className="min-w-0 max-w-xs md:max-w-md">
|
|
<p className="font-bold text-slate-800 text-xs truncate">{mod.name}</p>
|
|
<p className="text-[11px] text-slate-400 truncate mt-0.5 font-medium">
|
|
{mod.description || 'No description provided.'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
|
|
{/* Column 2: Code / Slug */}
|
|
<td className="py-3.5 px-3.5 font-mono">
|
|
<div className="flex items-center gap-1.5 flex-wrap">
|
|
{mod.code && (
|
|
<span className="text-[9px] font-black uppercase px-1.5 py-0.5 rounded bg-slate-100 text-slate-700 border border-slate-200">
|
|
{mod.code}
|
|
</span>
|
|
)}
|
|
<span className="text-[11px] text-slate-500">{mod.slug}</span>
|
|
</div>
|
|
</td>
|
|
|
|
{/* Column 3: Category */}
|
|
<td className="py-3.5 px-3.5">
|
|
<span className="text-[10px] font-bold text-indigo-600 bg-indigo-50 px-2.5 py-1 rounded-lg border border-indigo-100 uppercase tracking-wider">
|
|
{mod.category || 'General'}
|
|
</span>
|
|
</td>
|
|
|
|
{/* Column 4: Linked Plans */}
|
|
<td className="py-3.5 px-3.5 font-semibold text-slate-600">
|
|
<span className="bg-slate-100 text-slate-700 px-2 py-0.5 rounded-md text-[10px] font-bold border border-slate-200">
|
|
{mod.linked_plans_count || 0} {mod.linked_plans_count === 1 ? 'Plan' : 'Plans'}
|
|
</span>
|
|
</td>
|
|
|
|
{/* Column 5: Status */}
|
|
<td className="py-3.5 px-3.5">
|
|
<div className="flex items-center gap-2">
|
|
<span
|
|
className={`px-2.5 py-0.5 rounded-full text-[10px] font-black uppercase border whitespace-nowrap ${
|
|
mod.is_active
|
|
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
|
|
: 'bg-slate-100 text-slate-500 border-slate-200'
|
|
}`}
|
|
>
|
|
{mod.is_active ? '● Active' : '○ Inactive'}
|
|
</span>
|
|
<button
|
|
type="button"
|
|
onClick={() => onToggleStatus(mod)}
|
|
className={`text-[10px] font-bold px-2 py-1 rounded-lg transition-colors cursor-pointer border whitespace-nowrap ${
|
|
mod.is_active
|
|
? 'bg-white hover:bg-rose-50 text-slate-500 hover:text-rose-600 border-slate-200'
|
|
: 'bg-emerald-600 hover:bg-emerald-500 text-white border-transparent shadow-sm'
|
|
}`}
|
|
>
|
|
{mod.is_active ? 'Deactivate' : 'Activate'}
|
|
</button>
|
|
</div>
|
|
</td>
|
|
|
|
{/* Column 6: Actions */}
|
|
<td className="py-3.5 px-3.5 text-right">
|
|
<button
|
|
type="button"
|
|
onClick={() => onEditModule(mod)}
|
|
className="py-1 px-2.5 bg-white hover:bg-slate-100 border border-slate-200 rounded-lg text-xs font-bold text-slate-700 transition-colors inline-flex items-center gap-1 cursor-pointer shadow-sm"
|
|
>
|
|
<Edit className="h-3.5 w-3.5 text-slate-400" /> Edit
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{/* Unified Pagination */}
|
|
<Pagination
|
|
currentPage={safePage}
|
|
totalItems={totalFiltered}
|
|
itemsPerPage={perPage}
|
|
onPageChange={onPageChange}
|
|
onItemsPerPageChange={onPerPageChange}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|