'use client' import React, { useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { runGeoGebraCommand, type GeoGebraCommandAPI } from '@/lib/geogebra-commands' interface GeogebraProps { script?: string /** A board the learner works on: GeoGebra's toolbar and input bar shown, * labels draggable. Off for figures the tutor drew to be looked at. */ editable?: boolean payload?: GeogebraPayload title?: string className?: string width?: number height?: number perspective?: string } export interface GeogebraPayload { app_name?: 'geometry' | 'graphing' | '3d' | 'classic' | string commands: string[] show_axes?: boolean show_grid?: boolean view?: { x_min: number x_max: number y_min: number y_max: number } } declare global { interface Window { GGBApplet?: new ( params: Record, html5: boolean ) => { inject: (containerId: string) => void } } } const GGB_SCRIPT_SRC = 'https://www.geogebra.org/apps/deployggb.js' // Single in-flight loader shared by every Geogebra instance on the page. // deployggb.js is ~MB and lives on geogebra.org's CDN; fetching it once and // reusing window.GGBApplet keeps subsequent applets snappy. let ggbLoader: Promise | null = null function loadGgbScript(): Promise { if (typeof window === 'undefined') { return Promise.reject(new Error('window unavailable')) } if (window.GGBApplet) return Promise.resolve() if (ggbLoader) return ggbLoader ggbLoader = new Promise((resolve, reject) => { const existing = document.querySelector(`script[src="${GGB_SCRIPT_SRC}"]`) if (existing) { if (window.GGBApplet) { resolve() return } existing.addEventListener('load', () => resolve()) existing.addEventListener('error', () => { ggbLoader = null reject(new Error('GeoGebra script failed to load')) }) return } const script = document.createElement('script') script.src = GGB_SCRIPT_SRC script.async = true script.onload = () => resolve() script.onerror = () => { ggbLoader = null reject(new Error('GeoGebra script failed to load')) } document.head.appendChild(script) }) return ggbLoader } // The agent emits one command per line and prefixes human-readable // descriptions with `#`. evalCommand only takes one command at a time, so we // strip comments + empty lines and feed the rest sequentially. function parseGgbCommands(raw: string): string[] { return raw .split(/\r?\n/) .map(line => line.trim()) .filter(line => line.length > 0 && !line.startsWith('#')) } let containerCounter = 0 const Geogebra: React.FC = ({ script = '', editable = false, payload, title, className = '', width = 760, height = 480, perspective, }) => { const { t } = useTranslation() const containerRef = useRef(null) const containerIdRef = useRef(`ggb-${++containerCounter}-${Date.now().toString(36)}`) const [error, setError] = useState(null) const [loading, setLoading] = useState(true) const [commandErrors, setCommandErrors] = useState([]) useEffect(() => { let cancelled = false // Snapshot the ref now so the cleanup closure sees the same node React // mounted with, not whatever the ref points at after a later re-render. const containerAtMount = containerRef.current setLoading(true) setError(null) setCommandErrors([]) async function mount() { try { await loadGgbScript() if (cancelled) return if (!window.GGBApplet) { throw new Error('GGBApplet global missing after script load') } const commands = payload?.commands?.length ? payload.commands.map(String).filter(command => command.trim()) : parseGgbCommands(script) const container = containerRef.current if (!container) return container.id = containerIdRef.current container.innerHTML = '' const applet = new window.GGBApplet( { appName: payload?.app_name || 'geometry', width, height, perspective, preventFocus: true, showToolBar: editable, showAlgebraInput: editable, showMenuBar: false, showResetIcon: !editable, enableLabelDrags: editable, enableShiftDragZoom: true, useBrowserForJS: false, // The api passed in here lets us drive the applet // imperatively without going through a global. We feed each // command separately so one bad line doesn't abort the rest. appletOnLoad: ( api: GeoGebraCommandAPI & { setAxesVisible?: (x: boolean, y: boolean) => void setGridVisible?: (visible: boolean) => void setCoordSystem?: (xMin: number, xMax: number, yMin: number, yMax: number) => void } ) => { if (cancelled) return if (payload?.show_axes !== undefined) api.setAxesVisible?.(payload.show_axes, payload.show_axes) if (payload?.show_grid !== undefined) api.setGridVisible?.(payload.show_grid) const view = payload?.view if (view && api.setCoordSystem) { api.setCoordSystem(view.x_min, view.x_max, view.y_min, view.y_max) } const failures: string[] = [] for (const cmd of commands) { try { if (!runGeoGebraCommand(api, cmd)) { failures.push(cmd) console.warn('[ggb] evalCommand returned false', { cmd }) } } catch (err) { failures.push(cmd) console.warn('[ggb] evalCommand failed', { cmd, err }) } } setLoading(false) if (!cancelled && failures.length > 0) { setCommandErrors(failures) } }, }, true ) applet.inject(containerIdRef.current) } catch (err) { if (!cancelled) { setError(err instanceof Error ? err.message : String(err)) setLoading(false) } } } void mount() return () => { cancelled = true if (containerAtMount) { containerAtMount.innerHTML = '' } } }, [script, payload, width, height, editable, perspective]) return (
{title ? (
{title}
) : null} {error ? (
{t('Failed to load GeoGebra')}: {error}
) : ( <> {commandErrors.length > 0 ? (
{t('GeoGebra rejected {{count}} command(s)', { count: commandErrors.length, })} :
    {commandErrors.map(cmd => (
  • {cmd}
  • ))}
) : null}
{loading ? (
{t('Loading GeoGebra...')}
) : null}
)}
) } export default Geogebra