sozsoft-platform/ui/src/views/developerKit/ComponentManager.tsx
2026-07-11 23:35:32 +03:00

327 lines
13 KiB
TypeScript

import React, { useState } from 'react'
import { Link, useNavigate } from 'react-router-dom'
import { useComponents } from '../../contexts/ComponentContext'
import {
FaPlus,
FaSearch,
FaRegEdit,
FaTrashAlt,
FaEye,
FaEyeSlash,
FaFilter,
FaCalendarAlt,
} from 'react-icons/fa'
import Widget from '@/components/common/Widget'
import { ROUTES_ENUM } from '@/routes/route.constant'
import { useLocalization } from '@/utils/hooks/useLocalization'
import { Loading } from '../../components/shared'
import { APP_NAME } from '@/constants/app.constant'
import { Helmet } from 'react-helmet'
import { Button } from '@/components/ui'
const ComponentManager: React.FC = () => {
const { components, loading, updateComponent, deleteComponent } = useComponents()
const navigate = useNavigate()
const [searchTerm, setSearchTerm] = useState('')
const [filterActive, setFilterActive] = useState<'all' | 'active' | 'inactive'>('all')
// Calculate statistics
const totalComponents = components?.length || 0
const activeComponents = components?.filter((c) => c.isActive).length || 0
const inactiveComponents = totalComponents - activeComponents
const { translate } = useLocalization()
const filteredComponents = components?.filter((component) => {
const matchesSearch =
component.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
(component.description || '').toLowerCase().includes(searchTerm.toLowerCase())
const matchesFilter =
filterActive === 'all' ||
(filterActive === 'active' && component.isActive) ||
(filterActive === 'inactive' && !component.isActive)
return matchesSearch && matchesFilter
})
const handleToggleActive = async (id: string, isActive: boolean) => {
try {
const component = components?.find((c) => c.id === id)
if (component) {
await updateComponent(id, { ...component, isActive })
}
} catch (err) {
console.error('Failed to toggle component status:', err)
}
}
const handleDelete = async (id: string) => {
if (window.confirm(translate('::App.DeveloperKit.Component.ConfirmDelete'))) {
try {
await deleteComponent(id)
} catch (err) {
console.error('Failed to delete component:', err)
}
}
}
return (
<div className="space-y-4">
<Helmet
titleTemplate={`%s | ${APP_NAME}`}
title={translate('::' + 'App.DeveloperKit.Components')}
defaultTitle={APP_NAME}
></Helmet>
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mt-2 mb-4">
<Widget
title={translate('::App.DeveloperKit.Component.Total')}
value={totalComponents}
color="purple"
icon="FaPuzzlePiece"
valueClassName="text-3xl"
subTitle={translate('::App.DeveloperKit.Component.TotalDescription')}
/>
<Widget
title={translate('::App.DeveloperKit.Component.Active')}
value={activeComponents}
color="green"
icon="FaCheckCircle"
valueClassName="text-3xl"
subTitle={translate('::App.DeveloperKit.Component.ActiveDescription')}
/>
<Widget
title={translate('::App.DeveloperKit.Component.Inactive')}
value={inactiveComponents}
color="gray"
icon="FaTimesCircle"
valueClassName="text-3xl"
subTitle={translate('::App.DeveloperKit.Component.InactiveDescription')}
/>
</div>
{/* Filters */}
<div className="flex flex-col sm:flex-row gap-4">
<div className="flex-1 relative">
<FaSearch className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-slate-400" />
<input
type="text"
placeholder={translate('::App.DeveloperKit.Component.SearchPlaceholder')}
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full pl-10 pr-2 py-1 border border-slate-300 dark:border-gray-700 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div className="flex items-center gap-2">
<FaFilter className="w-5 h-5 text-slate-500 dark:text-gray-400" />
<select
value={filterActive}
onChange={(e) => setFilterActive(e.target.value as 'all' | 'active' | 'inactive')}
className="px-2 py-1 border border-slate-300 dark:border-gray-700 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
>
<option value="all">{translate('::App.DeveloperKit.Component.Filter.All')}</option>
<option value="active">
{translate('::App.DeveloperKit.Component.Filter.Active')}
</option>
<option value="inactive">
{translate('::App.DeveloperKit.Component.Filter.Inactive')}
</option>
</select>
</div>
<div>
<Button
type="button"
variant="solid"
className="flex items-center gap-2"
onClick={() => navigate(ROUTES_ENUM.protected.saas.developerKit.componentsNew)}
>
<FaPlus className="w-4 h-4" />
{translate('::App.DeveloperKit.Component.New')}
</Button>
</div>
</div>
{/* Components List */}
{loading ? (
<div className="flex items-center justify-center min-h-screen bg-gray-50">
<div className="text-center">
<Loading loading={loading} />
</div>
</div>
) : filteredComponents?.length > 0 ? (
<div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-4 gap-6">
{filteredComponents.map((component) => (
<div
key={component.id}
className="bg-white dark:bg-gray-900 rounded-lg border border-slate-200 dark:border-gray-700 shadow-sm hover:shadow-md transition-shadow"
>
<div className="p-6">
<div className="flex items-start justify-between">
{/* Sol taraf */}
<div className="flex-1">
<div className="flex items-center gap-2 mb-1">
<h3 className="text-lg font-semibold text-slate-900 dark:text-gray-100">
{component.name}
</h3>
<div
className={`w-2 h-2 rounded-full ${
component.isActive ? 'bg-green-500' : 'bg-slate-300 dark:bg-gray-700'
}`}
/>
</div>
<p className="text-slate-600 dark:text-gray-300 text-sm mb-2">
{(() => {
try {
const parsed = JSON.parse(component.dependencies ?? '[]')
return Array.isArray(parsed) && parsed.length > 0
? `${parsed.join(', ')}`
: translate('::App.DeveloperKit.Component.NoDependencies')
} catch {
return translate('::App.DeveloperKit.Component.NoDependencies')
}
})()}
</p>
<p className="text-slate-600 dark:text-gray-300 text-sm mb-2">
{component.routePath}
</p>
{component.description && (
<p className="text-slate-600 dark:text-gray-300 text-sm mb-2">
{component.description}
</p>
)}
</div>
{/* Sağ taraf */}
{component.lastModificationTime && (
<div className="flex items-center gap-1 text-xs text-slate-500 dark:text-gray-400 ml-4 whitespace-nowrap">
<FaCalendarAlt className="w-3 h-3" />
<span>
{new Date(component.lastModificationTime).toLocaleDateString() ?? ''}
</span>
</div>
)}
</div>
{/* Props Preview */}
{component.props && (
<div className="mb-4">
<p className="text-xs font-medium text-slate-700 dark:text-gray-400 mb-1">
{translate('::App.DeveloperKit.Component.PropsLabel')}
</p>
<code className="text-xs bg-slate-100 dark:bg-gray-800 text-slate-600 dark:text-gray-300 px-2 py-1 rounded">
{component.props}
</code>
</div>
)}
{/* Actions */}
<div className="flex items-center justify-between pt-2 border-t border-slate-100 dark:border-gray-700">
<div className="flex items-center gap-2">
<Button
type="button"
variant="plain"
shape="circle"
onClick={() => handleToggleActive(component.id, !component.isActive)}
className={`!inline-flex !h-auto items-center gap-1 rounded !px-2 py-1 text-xs font-medium transition-colors`}
>
{component.isActive ? (
<>
<FaEye className="w-4 h-4" />
{translate('::App.Status.Active')}
</>
) : (
<>
<FaEyeSlash className="w-4 h-4" />
{translate('::App.Status.Inactive')}
</>
)}
</Button>
</div>
<div className="flex items-center gap-1">
<Button
type="button"
variant="solid"
shape="circle"
color="blue-600"
onClick={() =>
window.open(
ROUTES_ENUM.protected.saas.developerKit.componentsEdit.replace(
':id',
component.id,
),
'_blank',
)
}
title={translate('::App.DeveloperKit.Component.Edit')}
>
<FaRegEdit className="w-3 h-3" />
</Button>
<Button
type="button"
variant="solid"
shape="circle"
color="green-600"
onClick={() =>
navigate(
ROUTES_ENUM.protected.saas.developerKit.componentsView.replace(
':id',
component.id,
),
)
}
title={translate('::App.DeveloperKit.Component.View')}
>
<FaEye className="w-3 h-3" />
</Button>
<Button
type="button"
variant="solid"
shape="circle"
color="red-600"
onClick={() => handleDelete(component.id)}
title={translate('::App.DeveloperKit.Component.Delete')}
>
<FaTrashAlt className="w-3 h-3" />
</Button>
</div>
</div>
</div>
</div>
))}
</div>
) : (
<div className="text-center py-12">
<div className="mx-auto">
<div className="bg-slate-100 rounded-full w-16 h-16 flex items-center justify-center mx-auto mb-4">
<FaPlus className="w-8 h-8 text-slate-500" />
</div>
<h3 className="text-lg font-medium text-slate-900 mb-2">
{searchTerm || filterActive !== 'all'
? translate('::App.DeveloperKit.Component.Empty.Filtered.Title')
: translate('::App.DeveloperKit.Component.Empty.Initial.Title')}
</h3>
<p className="text-slate-600 mb-6">
{searchTerm || filterActive !== 'all'
? translate('::App.DeveloperKit.EmptyFilteredDescription')
: translate('::App.DeveloperKit.Component.Empty.Initial.Description')}
</p>
{!searchTerm && filterActive === 'all' && (
<Link
to={ROUTES_ENUM.protected.saas.developerKit.componentsNew}
className="inline-flex items-center gap-2 bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors"
>
<FaPlus className="w-4 h-4" />
{translate('::App.DeveloperKit.Component.Empty.Initial.Action')}
</Link>
)}
</div>
</div>
)}
</div>
)
}
export default ComponentManager