TT-004: complete modular frontend integration
This commit is contained in:
@@ -21,7 +21,7 @@ export default function App() {
|
||||
<Route path="/register" element={<Register />} />
|
||||
<Route path="/" element={<ProtectedRoute><Layout /></ProtectedRoute>}>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="module/:slug" element={<ModuleView />} />
|
||||
<Route path="module/:id" element={<ModuleView />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</AuthProvider>
|
||||
|
||||
@@ -26,7 +26,7 @@ export const authApi = {
|
||||
}
|
||||
|
||||
export const modulesApi = {
|
||||
list: () => api.get('/modules'),
|
||||
list: () => api.get('/platform/modules'),
|
||||
}
|
||||
|
||||
export default api
|
||||
|
||||
@@ -5,7 +5,7 @@ import { modulesApi } from '../api/client'
|
||||
import { Package, LogOut } from 'lucide-react'
|
||||
|
||||
export default function Layout() {
|
||||
const { user, logout } = useAuth()
|
||||
const { logout } = useAuth()
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const [modules, setModules] = useState<any[]>([])
|
||||
@@ -36,8 +36,8 @@ export default function Layout() {
|
||||
{modules.length > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
{modules.map(mod => (
|
||||
<Link key={mod.slug} to={`/module/${mod.slug}`}
|
||||
className={`px-3 py-1.5 text-sm rounded-md transition-colors flex items-center gap-1.5 ${location.pathname === `/module/${mod.slug}` ? 'bg-blue-600 text-white font-medium' : 'text-gray-600 hover:bg-gray-100'}`}>
|
||||
<Link key={mod.id} to={`/module/${mod.id}`}
|
||||
className={`px-3 py-1.5 text-sm rounded-md transition-colors flex items-center gap-1.5 ${location.pathname === `/module/${mod.id}` ? 'bg-blue-600 text-white font-medium' : 'text-gray-600 hover:bg-gray-100'}`}>
|
||||
<Package className="w-3.5 h-3.5" />{mod.name}
|
||||
</Link>
|
||||
))}
|
||||
|
||||
@@ -26,9 +26,9 @@ export default function Dashboard() {
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{modules.map(mod => {
|
||||
const userRoles = user?.module_roles?.[mod.slug] || []
|
||||
const userRoles = user?.module_roles?.[mod.id] || []
|
||||
return (
|
||||
<Link key={mod.slug} to={`/module/${mod.slug}`}
|
||||
<Link key={mod.id} to={`/module/${mod.id}`}
|
||||
className="bg-white rounded-lg shadow hover:shadow-md transition-shadow p-6 group">
|
||||
<div className="flex items-start justify-between mb-4">
|
||||
<div className="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
|
||||
|
||||
@@ -3,29 +3,45 @@ import { useParams } from 'react-router-dom'
|
||||
import { modulesApi } from '../api/client'
|
||||
import { ExternalLink, Loader2 } from 'lucide-react'
|
||||
|
||||
const isDev = import.meta.env.DEV
|
||||
|
||||
function getModuleFrontend(module: any): string | null {
|
||||
if (!module.frontend) return null
|
||||
const url = isDev ? module.frontend.dev_url : module.frontend.prod_path
|
||||
return url || null
|
||||
}
|
||||
|
||||
export default function ModuleView() {
|
||||
const { slug } = useParams()
|
||||
const { id } = useParams()
|
||||
const [module, setModule] = useState<any>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [iframeLoading, setIframeLoading] = useState(true)
|
||||
const [moduleUrl, setModuleUrl] = useState('')
|
||||
const [error, setError] = useState('')
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
modulesApi.list().then(res => {
|
||||
const found = res.data.find((m: any) => m.slug === slug)
|
||||
const found = res.data.find((m: any) => String(m.id) === String(id))
|
||||
setModule(found)
|
||||
}).finally(() => setLoading(false))
|
||||
}, [slug])
|
||||
}).catch(() => setError('Ошибка загрузки модулей'))
|
||||
.finally(() => setLoading(false))
|
||||
}, [id])
|
||||
|
||||
useEffect(() => {
|
||||
if (!module) return
|
||||
|
||||
const frontendUrl = getModuleFrontend(module)
|
||||
if (!frontendUrl) {
|
||||
setError('Фронтенд модуля не настроен')
|
||||
return
|
||||
}
|
||||
|
||||
const getToken = () => localStorage.getItem('platform_token') || ''
|
||||
|
||||
const updateUrl = () => {
|
||||
const token = getToken()
|
||||
const url = `${module.url}?token=${encodeURIComponent(token)}`
|
||||
const url = `${frontendUrl}?token=${encodeURIComponent(token)}`
|
||||
setModuleUrl(url)
|
||||
}
|
||||
|
||||
@@ -35,7 +51,7 @@ export default function ModuleView() {
|
||||
if (iframe?.contentWindow) {
|
||||
iframe.contentWindow.postMessage(
|
||||
{ type: 'PLATFORM_TOKEN_UPDATE', token },
|
||||
module.url
|
||||
'*'
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -68,7 +84,7 @@ export default function ModuleView() {
|
||||
|
||||
useEffect(() => {
|
||||
setIframeLoading(true)
|
||||
}, [slug])
|
||||
}, [id])
|
||||
|
||||
if (loading) return (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
@@ -76,6 +92,15 @@ export default function ModuleView() {
|
||||
</div>
|
||||
)
|
||||
|
||||
if (error) return (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<div className="text-center">
|
||||
<p className="text-red-500 mb-2">{error}</p>
|
||||
<p className="text-sm text-gray-400">Модуль: {id}</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
if (!module) return (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<p className="text-gray-500">Модуль не найден</p>
|
||||
@@ -86,10 +111,12 @@ export default function ModuleView() {
|
||||
<div className="flex-1 flex flex-col h-full">
|
||||
<div className="flex items-center justify-between px-6 py-3 bg-white border-b flex-shrink-0">
|
||||
<h1 className="text-lg font-semibold text-gray-900">{module.name}</h1>
|
||||
<a href={moduleUrl} target="_blank" rel="noopener noreferrer"
|
||||
className="flex items-center gap-1 text-sm text-blue-600 hover:text-blue-800">
|
||||
<ExternalLink className="w-4 h-4" />Открыть отдельно
|
||||
</a>
|
||||
{moduleUrl && (
|
||||
<a href={moduleUrl} target="_blank" rel="noopener noreferrer"
|
||||
className="flex items-center gap-1 text-sm text-blue-600 hover:text-blue-800">
|
||||
<ExternalLink className="w-4 h-4" />Открыть отдельно
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex-1 relative">
|
||||
{iframeLoading && (
|
||||
|
||||
@@ -7,7 +7,7 @@ export default defineConfig({
|
||||
server: {
|
||||
port: 5175,
|
||||
proxy: {
|
||||
'/api': 'http://localhost:8001',
|
||||
'/api': 'http://localhost:8000',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user