sozsoft-platform/ui/src/views/developerKit/ComponentManager.tsx

320 lines
13 KiB
TypeScript
Raw Normal View History

2026-02-24 20:44:16 +00:00
import React, { useState } from 'react'
2026-06-26 08:59:13 +00:00
import { Link, useNavigate } from 'react-router-dom'
2026-02-24 20:44:16 +00:00
import { useComponents } from '../../contexts/ComponentContext'
import {
FaPlus,
FaSearch,
FaRegEdit,
FaTrashAlt,
FaEye,
FaEyeSlash,
FaFilter,
FaCalendarAlt,
} from 'react-icons/fa'
2026-05-12 12:49:25 +00:00
import Widget from '@/components/common/Widget'
2026-02-24 20:44:16 +00:00
import { ROUTES_ENUM } from '@/routes/route.constant'
import { useLocalization } from '@/utils/hooks/useLocalization'
2026-03-01 20:43:25 +00:00
import { Loading } from '../../components/shared'
import { APP_NAME } from '@/constants/app.constant'
import { Helmet } from 'react-helmet'
import { Button } from '@/components/ui'
2026-02-24 20:44:16 +00:00
const ComponentManager: React.FC = () => {
const { components, loading, updateComponent, deleteComponent } = useComponents()
2026-06-26 08:59:13 +00:00
const navigate = useNavigate()
2026-02-24 20:44:16 +00:00
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)
}
}
2026-07-06 09:44:26 +00:00
const handleDelete = async (id: string) => {
2026-02-24 20:44:16 +00:00
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">
2026-03-01 20:43:25 +00:00
<Helmet
titleTemplate={`%s | ${APP_NAME}`}
title={translate('::' + 'App.DeveloperKit.Components')}
defaultTitle={APP_NAME}
></Helmet>
2026-02-24 20:44:16 +00:00
2026-03-01 20:43:25 +00:00
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mt-2 mb-4">
2026-05-12 12:49:25 +00:00
<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')}
/>
2026-02-24 20:44:16 +00:00
</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)}
2026-06-26 08:59:13 +00:00
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"
2026-02-24 20:44:16 +00:00
/>
</div>
<div className="flex items-center gap-2">
2026-05-19 20:22:25 +00:00
<FaFilter className="w-5 h-5 text-slate-500 dark:text-gray-400" />
2026-02-24 20:44:16 +00:00
<select
value={filterActive}
onChange={(e) => setFilterActive(e.target.value as 'all' | 'active' | 'inactive')}
2026-06-26 08:59:13 +00:00
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"
2026-02-24 20:44:16 +00:00
>
<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>
2026-03-01 20:43:25 +00:00
<div>
2026-06-26 08:59:13 +00:00
<Button
type="button"
variant="solid"
className="flex items-center gap-2"
onClick={() => navigate(ROUTES_ENUM.protected.saas.developerKit.componentsNew)}
2026-03-01 20:43:25 +00:00
>
<FaPlus className="w-4 h-4" />
{translate('::App.DeveloperKit.Component.New')}
2026-06-26 08:59:13 +00:00
</Button>
2026-03-01 20:43:25 +00:00
</div>
2026-02-24 20:44:16 +00:00
</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}
2026-05-19 20:22:25 +00:00
className="bg-white dark:bg-gray-900 rounded-lg border border-slate-200 dark:border-gray-700 shadow-sm hover:shadow-md transition-shadow"
2026-02-24 20:44:16 +00:00
>
<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">
2026-05-19 20:22:25 +00:00
<h3 className="text-lg font-semibold text-slate-900 dark:text-gray-100">{component.name}</h3>
2026-02-24 20:44:16 +00:00
<div
className={`w-2 h-2 rounded-full ${
2026-05-19 20:22:25 +00:00
component.isActive ? 'bg-green-500' : 'bg-slate-300 dark:bg-gray-700'
2026-02-24 20:44:16 +00:00
}`}
/>
</div>
2026-05-19 20:22:25 +00:00
<p className="text-slate-600 dark:text-gray-300 text-sm mb-3">
2026-02-24 20:44:16 +00:00
{(() => {
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>
{component.description && (
2026-05-19 20:22:25 +00:00
<p className="text-slate-600 dark:text-gray-300 text-sm mb-3">{component.description}</p>
2026-02-24 20:44:16 +00:00
)}
</div>
{/* Sağ taraf */}
{component.lastModificationTime && (
2026-05-19 20:22:25 +00:00
<div className="flex items-center gap-1 text-xs text-slate-500 dark:text-gray-400 ml-4 whitespace-nowrap">
2026-02-24 20:44:16 +00:00
<FaCalendarAlt className="w-3 h-3" />
<span>
{new Date(component.lastModificationTime).toLocaleDateString() ?? ''}
</span>
</div>
)}
</div>
{/* Props Preview */}
{component.props && (
<div className="mb-4">
2026-05-19 20:22:25 +00:00
<p className="text-xs font-medium text-slate-700 dark:text-gray-400 mb-1">
2026-02-24 20:44:16 +00:00
{translate('::App.DeveloperKit.Component.PropsLabel')}
</p>
2026-05-19 20:22:25 +00:00
<code className="text-xs bg-slate-100 dark:bg-gray-800 text-slate-600 dark:text-gray-300 px-2 py-1 rounded">
2026-02-24 20:44:16 +00:00
{component.props}
</code>
</div>
)}
{/* Actions */}
2026-05-19 20:22:25 +00:00
<div className="flex items-center justify-between pt-2 border-t border-slate-100 dark:border-gray-700">
2026-02-24 20:44:16 +00:00
<div className="flex items-center gap-2">
<Button
type="button"
variant="plain"
2026-06-26 08:59:13 +00:00
shape="circle"
2026-02-24 20:44:16 +00:00
onClick={() => handleToggleActive(component.id, !component.isActive)}
2026-06-26 08:59:13 +00:00
className={`!inline-flex !h-auto items-center gap-1 rounded !px-2 py-1 text-xs font-medium transition-colors`}
2026-02-24 20:44:16 +00:00
>
{component.isActive ? (
<>
2026-07-06 09:44:26 +00:00
<FaEye className="w-4 h-4" />
{translate('::App.Status.Active')}
2026-02-24 20:44:16 +00:00
</>
) : (
<>
2026-07-06 09:44:26 +00:00
<FaEyeSlash className="w-4 h-4" />
{translate('::App.Status.Inactive')}
2026-02-24 20:44:16 +00:00
</>
)}
</Button>
2026-02-24 20:44:16 +00:00
</div>
<div className="flex items-center gap-1">
2026-06-26 08:59:13 +00:00
<Button
type="button"
variant="solid"
shape="circle"
color="blue-600"
onClick={() =>
window.open(
ROUTES_ENUM.protected.saas.developerKit.componentsEdit.replace(
':id',
component.id,
),
'_blank',
)
}
2026-02-24 20:44:16 +00:00
title={translate('::App.DeveloperKit.Component.Edit')}
>
2026-07-06 09:44:26 +00:00
<FaRegEdit className="w-4 h-4" />
2026-06-26 08:59:13 +00:00
</Button>
<Button
type="button"
variant="solid"
shape="circle"
color="green-600"
onClick={() =>
navigate(
ROUTES_ENUM.protected.saas.developerKit.componentsView.replace(
':id',
component.id,
),
)
}
2026-02-24 20:44:16 +00:00
title={translate('::App.DeveloperKit.Component.View')}
>
2026-07-06 09:44:26 +00:00
<FaEye className="w-4 h-4" />
2026-06-26 08:59:13 +00:00
</Button>
<Button
type="button"
2026-06-26 08:59:13 +00:00
variant="solid"
shape="circle"
color="red-600"
2026-07-06 09:44:26 +00:00
onClick={() => handleDelete(component.id)}
2026-02-24 20:44:16 +00:00
title={translate('::App.DeveloperKit.Component.Delete')}
>
2026-07-06 09:44:26 +00:00
<FaTrashAlt className="w-4 h-4" />
</Button>
2026-02-24 20:44:16 +00:00
</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