|
| 1 | +(function(){ |
| 2 | +// ============================================================================= |
| 3 | +// Unified Integration for AI Button + Task Dashboard |
| 4 | +// - Injects MinimalAIButton into MainNavBar.UtilityItems |
| 5 | +// - Registers /plugins/ai-tasks route mounting TaskDashboard |
| 6 | +// - Adds SettingsToolsSection entry linking to the dashboard |
| 7 | +// - Adds simple "AI" nav utility link (in case button not visible) |
| 8 | +// - All logging gated by window.AIDebug |
| 9 | +// ============================================================================= |
| 10 | +(function () { |
| 11 | + var _a, _b, _c; |
| 12 | + const g = window; |
| 13 | + const PluginApi = g.PluginApi; |
| 14 | + if (!PluginApi) { |
| 15 | + console.warn('[AIIntegration] PluginApi not ready'); |
| 16 | + return; |
| 17 | + } |
| 18 | + const React = PluginApi.React; |
| 19 | + const debug = !!g.AIDebug; |
| 20 | + const dlog = (...a) => { if (debug) |
| 21 | + console.log('[AIIntegration]', ...a); }; |
| 22 | + // Helper to safely get components |
| 23 | + const Button = ((_b = (_a = PluginApi.libraries) === null || _a === void 0 ? void 0 : _a.Bootstrap) === null || _b === void 0 ? void 0 : _b.Button) || ((p) => React.createElement('button', p, p.children)); |
| 24 | + const { Link, NavLink } = ((_c = PluginApi.libraries) === null || _c === void 0 ? void 0 : _c.ReactRouterDOM) || {}; |
| 25 | + function getMinimalButton() { return g.MinimalAIButton || g.AIButton; } |
| 26 | + function getTaskDashboard() { return g.TaskDashboard || g.AITaskDashboard; } |
| 27 | + function getPluginSettings() { return g.AIPluginSettings; } |
| 28 | + // Main nav utility items: inject AI button + nav link |
| 29 | + try { |
| 30 | + PluginApi.patch.before('MainNavBar.UtilityItems', function (props) { |
| 31 | + const MinimalAIButton = getMinimalButton(); |
| 32 | + const children = [props.children]; |
| 33 | + if (MinimalAIButton) { |
| 34 | + children.push(React.createElement('div', { key: 'ai-btn-wrap', style: { marginRight: 8, display: 'flex', alignItems: 'center' } }, React.createElement(MinimalAIButton))); |
| 35 | + } |
| 36 | + return [{ children }]; |
| 37 | + }); |
| 38 | + dlog('Patched MainNavBar.UtilityItems'); |
| 39 | + } |
| 40 | + catch (e) { |
| 41 | + if (debug) |
| 42 | + console.warn('[AIIntegration] main nav patch failed', e); |
| 43 | + } |
| 44 | + // Register dashboard route |
| 45 | + try { |
| 46 | + PluginApi.register.route('/plugins/ai-tasks', () => { |
| 47 | + const Dash = getTaskDashboard(); |
| 48 | + return Dash ? React.createElement(Dash, {}) : React.createElement('div', { style: { padding: 16 } }, 'Loading AI Tasks...'); |
| 49 | + }); |
| 50 | + dlog('Registered /plugins/ai-tasks route'); |
| 51 | + } |
| 52 | + catch (e) { |
| 53 | + if (debug) |
| 54 | + console.warn('[AIIntegration] route register failed', e); |
| 55 | + } |
| 56 | + // Register settings route (event-driven, no polling) |
| 57 | + try { |
| 58 | + const SettingsWrapper = () => { |
| 59 | + const [Comp, setComp] = React.useState(() => getPluginSettings()); |
| 60 | + React.useEffect(() => { |
| 61 | + if (Comp) |
| 62 | + return; // already there |
| 63 | + const handler = () => { |
| 64 | + const found = getPluginSettings(); |
| 65 | + if (found) { |
| 66 | + if (debug) |
| 67 | + console.debug('[AIIntegration] AIPluginSettingsReady event captured'); |
| 68 | + setComp(() => found); |
| 69 | + } |
| 70 | + }; |
| 71 | + window.addEventListener('AIPluginSettingsReady', handler); |
| 72 | + // one immediate async attempt (in case script loaded right after) |
| 73 | + setTimeout(handler, 0); |
| 74 | + return () => window.removeEventListener('AIPluginSettingsReady', handler); |
| 75 | + }, [Comp]); |
| 76 | + const C = Comp; |
| 77 | + return C ? React.createElement(C, {}) : React.createElement('div', { style: { padding: 16 } }, 'Loading AI Overhaul Settings...'); |
| 78 | + }; |
| 79 | + PluginApi.register.route('/plugins/ai-settings', () => React.createElement(SettingsWrapper)); |
| 80 | + dlog('Registered /plugins/ai-settings route (event)'); |
| 81 | + } |
| 82 | + catch (e) { |
| 83 | + if (debug) |
| 84 | + console.warn('[AIIntegration] settings route register failed', e); |
| 85 | + } |
| 86 | + // Settings tools entry |
| 87 | + try { |
| 88 | + PluginApi.patch.before('SettingsToolsSection', function (props) { |
| 89 | + var _a; |
| 90 | + const Setting = (_a = PluginApi.components) === null || _a === void 0 ? void 0 : _a.Setting; |
| 91 | + if (!Setting) |
| 92 | + return props; |
| 93 | + return [{ children: (React.createElement(React.Fragment, null, |
| 94 | + props.children, |
| 95 | + React.createElement(Setting, { heading: Link ? React.createElement(Link, { to: "/plugins/ai-tasks" }, |
| 96 | + React.createElement(Button, null, "AI Tasks")) : React.createElement(Button, { onClick: () => (location.href = '/plugins/ai-tasks') }, 'AI Tasks') }), |
| 97 | + React.createElement(Setting, { heading: Link ? React.createElement(Link, { to: "/plugins/ai-settings" }, |
| 98 | + React.createElement(Button, null, "AI Overhaul Settings")) : React.createElement(Button, { onClick: () => (location.href = '/plugins/ai-settings') }, 'AI Overhaul Settings') }))) }]; |
| 99 | + }); |
| 100 | + dlog('Patched SettingsToolsSection'); |
| 101 | + } |
| 102 | + catch (e) { |
| 103 | + if (debug) |
| 104 | + console.warn('[AIIntegration] settings tools patch failed', e); |
| 105 | + } |
| 106 | + if (debug) |
| 107 | + console.log('[AIIntegration] Unified integration loaded'); |
| 108 | +})(); |
| 109 | +})(); |
| 110 | + |
0 commit comments