Spaces:
Sleeping
Sleeping
File size: 2,716 Bytes
ce72224 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import { useEffect, useState } from 'react'
/**
* PluginLoader Component
* Lädt automatisch alle JS-Plugins aus dem /plugins Ordner
*
* Jedes Plugin kann folgende Hooks nutzen:
* - onPluginInit(pluginContext)
* - onMessageSent(event)
* - onResponseReceived(event)
*/
export default function PluginLoader({ onPluginsLoad }) {
const [loadedCount, setLoadedCount] = useState(0)
useEffect(() => {
loadPlugins()
}, [])
const loadPlugins = async () => {
try {
// Import alle JS-Dateien aus dem plugins Ordner
// Für Vite: Nutze import.meta.glob
const pluginModules = import.meta.glob('/plugins/*.js', { eager: false })
const loadedPlugins = []
for (const [path, importFn] of Object.entries(pluginModules)) {
try {
const pluginName = path.split('/').pop().replace('.js', '')
console.log(`🔌 Loading plugin: ${pluginName}`)
// Plugin laden
const module = await importFn()
// Plugin-Kontext bereitstellen
const pluginContext = {
name: pluginName,
log: (msg) => console.log(`[${pluginName}]`, msg),
warn: (msg) => console.warn(`[${pluginName}]`, msg),
error: (msg) => console.error(`[${pluginName}]`, msg),
}
// onPluginInit Hook aufrufen (wenn vorhanden)
if (module.onPluginInit) {
module.onPluginInit(pluginContext)
}
// Message-Hooks registrieren
if (module.onMessageSent) {
window.addEventListener('messageSent', (e) => {
module.onMessageSent(pluginContext, e.detail)
})
}
if (module.onResponseReceived) {
window.addEventListener('responseReceived', (e) => {
module.onResponseReceived(pluginContext, e.detail)
})
}
loadedPlugins.push(pluginName)
console.log(`✓ Plugin ${pluginName} initialized`)
} catch (err) {
console.error(`✗ Failed to load plugin from ${path}:`, err)
}
}
setLoadedCount(loadedPlugins.length)
onPluginsLoad?.(loadedPlugins)
// Event für geladene Plugins
window.dispatchEvent(new CustomEvent('pluginsLoaded', {
detail: loadedPlugins
}))
console.log(`✓ Loaded ${loadedPlugins.length} plugin(s)`)
} catch (err) {
console.error('Error loading plugins:', err)
}
}
return null // Dieser Component rendert nichts
}
|