|
| 1 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 2 | +import { LuImage, LuRefreshCw } from 'react-icons/lu'; |
| 3 | + |
| 4 | +import { useT } from '../../lib/i18n/I18nContext'; |
| 5 | +import { |
| 6 | + type DiagramViewerSettings, |
| 7 | + openhumanGetDashboardSettings, |
| 8 | +} from '../../utils/tauriCommands/config'; |
| 9 | + |
| 10 | +const DEFAULT_SETTINGS: DiagramViewerSettings = { |
| 11 | + enabled: true, |
| 12 | + source_url: 'http://localhost:8787/workspace/diagrams/latest.png', |
| 13 | + refresh_interval_seconds: 10, |
| 14 | +}; |
| 15 | + |
| 16 | +type ImageState = 'idle' | 'loaded' | 'error'; |
| 17 | + |
| 18 | +function normalizeSettings( |
| 19 | + settings?: Partial<DiagramViewerSettings> | null |
| 20 | +): DiagramViewerSettings { |
| 21 | + const sourceUrl = settings?.source_url?.trim() || DEFAULT_SETTINGS.source_url; |
| 22 | + const refreshInterval = Number(settings?.refresh_interval_seconds); |
| 23 | + |
| 24 | + return { |
| 25 | + enabled: settings?.enabled ?? DEFAULT_SETTINGS.enabled, |
| 26 | + source_url: sourceUrl, |
| 27 | + refresh_interval_seconds: |
| 28 | + Number.isFinite(refreshInterval) && refreshInterval > 0 |
| 29 | + ? Math.round(refreshInterval) |
| 30 | + : DEFAULT_SETTINGS.refresh_interval_seconds, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +export function buildDiagramImageUrl(sourceUrl: string, refreshKey: number): string { |
| 35 | + try { |
| 36 | + const url = new URL(sourceUrl); |
| 37 | + url.searchParams.set('openhuman_refresh', String(refreshKey)); |
| 38 | + return url.toString(); |
| 39 | + } catch { |
| 40 | + const separator = sourceUrl.includes('?') ? '&' : '?'; |
| 41 | + return `${sourceUrl}${separator}openhuman_refresh=${refreshKey}`; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export default function DiagramViewerTab() { |
| 46 | + const { t } = useT(); |
| 47 | + const [settings, setSettings] = useState<DiagramViewerSettings>(DEFAULT_SETTINGS); |
| 48 | + const [refreshKey, setRefreshKey] = useState(0); |
| 49 | + const [imageState, setImageState] = useState<ImageState>('idle'); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + let alive = true; |
| 53 | + |
| 54 | + openhumanGetDashboardSettings() |
| 55 | + .then(response => { |
| 56 | + if (!alive) return; |
| 57 | + setSettings(normalizeSettings(response.result.diagram_viewer)); |
| 58 | + }) |
| 59 | + .catch(() => { |
| 60 | + if (!alive) return; |
| 61 | + setSettings(DEFAULT_SETTINGS); |
| 62 | + }); |
| 63 | + |
| 64 | + return () => { |
| 65 | + alive = false; |
| 66 | + }; |
| 67 | + }, []); |
| 68 | + |
| 69 | + const refreshDiagram = useCallback(() => { |
| 70 | + setImageState('idle'); |
| 71 | + setRefreshKey(prev => prev + 1); |
| 72 | + }, []); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (!settings.enabled || settings.refresh_interval_seconds <= 0) return undefined; |
| 76 | + |
| 77 | + const interval = window.setInterval(refreshDiagram, settings.refresh_interval_seconds * 1000); |
| 78 | + return () => window.clearInterval(interval); |
| 79 | + }, [refreshDiagram, settings.enabled, settings.refresh_interval_seconds]); |
| 80 | + |
| 81 | + const sourceUrl = settings.source_url.trim(); |
| 82 | + const imageUrl = useMemo( |
| 83 | + () => (sourceUrl ? buildDiagramImageUrl(sourceUrl, refreshKey) : ''), |
| 84 | + [refreshKey, sourceUrl] |
| 85 | + ); |
| 86 | + |
| 87 | + const showImage = settings.enabled && sourceUrl.length > 0 && imageState !== 'error'; |
| 88 | + const showEmptyState = !settings.enabled || sourceUrl.length === 0 || imageState === 'error'; |
| 89 | + |
| 90 | + return ( |
| 91 | + <section className="space-y-5" aria-labelledby="diagram-viewer-title"> |
| 92 | + <div className="flex flex-wrap items-start justify-between gap-3"> |
| 93 | + <div> |
| 94 | + <h2 |
| 95 | + id="diagram-viewer-title" |
| 96 | + className="text-lg font-semibold text-stone-900 dark:text-neutral-100"> |
| 97 | + {t('intelligence.diagram.title')} |
| 98 | + </h2> |
| 99 | + <p className="mt-1 text-sm text-stone-500 dark:text-neutral-400"> |
| 100 | + {t('intelligence.diagram.description')} |
| 101 | + </p> |
| 102 | + </div> |
| 103 | + <button |
| 104 | + type="button" |
| 105 | + onClick={refreshDiagram} |
| 106 | + className="inline-flex items-center gap-2 rounded-md border border-stone-200 bg-white px-3 py-2 text-sm font-medium text-stone-700 transition-colors hover:bg-stone-50 dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-200 dark:hover:bg-neutral-800" |
| 107 | + aria-label={t('intelligence.diagram.refreshAria')}> |
| 108 | + <LuRefreshCw aria-hidden="true" className="h-4 w-4" /> |
| 109 | + {t('intelligence.diagram.refresh')} |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + |
| 113 | + {showEmptyState && ( |
| 114 | + <div className="flex min-h-72 flex-col items-center justify-center gap-3 rounded-lg border border-dashed border-stone-300 bg-stone-50 px-6 py-10 text-center dark:border-neutral-700 dark:bg-neutral-950/60"> |
| 115 | + <div className="flex h-12 w-12 items-center justify-center rounded-full bg-primary-50 text-primary-600 dark:bg-primary-500/10 dark:text-primary-300"> |
| 116 | + <LuImage aria-hidden="true" className="h-6 w-6" /> |
| 117 | + </div> |
| 118 | + <div> |
| 119 | + <h3 className="text-sm font-semibold text-stone-900 dark:text-neutral-100"> |
| 120 | + {t('intelligence.diagram.emptyTitle')} |
| 121 | + </h3> |
| 122 | + <p className="mt-1 max-w-md text-sm text-stone-500 dark:text-neutral-400"> |
| 123 | + {t('intelligence.diagram.emptyDescription')} |
| 124 | + </p> |
| 125 | + </div> |
| 126 | + <div className="flex max-w-full flex-col gap-2"> |
| 127 | + <code className="max-w-full overflow-x-auto rounded-md bg-white px-3 py-2 text-xs text-stone-600 dark:bg-neutral-900 dark:text-neutral-300"> |
| 128 | + {t('intelligence.diagram.skillInstallCommand')} |
| 129 | + </code> |
| 130 | + <code className="max-w-full overflow-x-auto rounded-md bg-white px-3 py-2 text-xs text-stone-600 dark:bg-neutral-900 dark:text-neutral-300"> |
| 131 | + {t('intelligence.diagram.promptExample')} |
| 132 | + </code> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + )} |
| 136 | + |
| 137 | + {showImage && ( |
| 138 | + <figure className="space-y-3"> |
| 139 | + <img |
| 140 | + key={imageUrl} |
| 141 | + src={imageUrl} |
| 142 | + alt={t('intelligence.diagram.imageAlt')} |
| 143 | + className="block w-full rounded-lg border border-stone-200 bg-white object-contain dark:border-neutral-800 dark:bg-neutral-950" |
| 144 | + onLoad={() => setImageState('loaded')} |
| 145 | + onError={() => setImageState('error')} |
| 146 | + /> |
| 147 | + <figcaption className="flex flex-wrap items-center justify-between gap-2 text-xs text-stone-500 dark:text-neutral-400"> |
| 148 | + <span> |
| 149 | + {t('intelligence.diagram.refreshesEvery').replace( |
| 150 | + '{seconds}', |
| 151 | + String(settings.refresh_interval_seconds) |
| 152 | + )} |
| 153 | + </span> |
| 154 | + <span className="max-w-full truncate">{sourceUrl}</span> |
| 155 | + </figcaption> |
| 156 | + </figure> |
| 157 | + )} |
| 158 | + </section> |
| 159 | + ); |
| 160 | +} |
0 commit comments