Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

3.3 KB
Raw Blame
import React from 'react'
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App'
import DashboardSignInGate from './components/DashboardSignInGate'
import { ThemeProvider } from './contexts/ThemeContext'
import {
  clearStaleAssetRecovery,
  isStaleAssetError,
  recoverFromStaleAsset,
} from './utils/staleAssetRecovery'
import './index.css'
import './pixel-workspace.css'
import './wallpaper-themes.css'

const RECOVERY_CLEAR_DELAY_MS = 30_000

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props)
    this.state = { hasError: false, error: null }
  }
  componentDidMount() {
    this.recoveryTimer = globalThis.setTimeout(clearStaleAssetRecovery, RECOVERY_CLEAR_DELAY_MS)
  }
  componentWillUnmount() {
    globalThis.clearTimeout(this.recoveryTimer)
  }
  static getDerivedStateFromError(error) {
    return { hasError: true, error }
  }
  componentDidCatch(error, info) {
    console.error('Dashboard crash:', error, info.componentStack)
    if (recoverFromStaleAsset(error)) return
    this.setState({ stack: info.componentStack })
  }
  render() {
    if (this.state.hasError) {
      const staleAsset = isStaleAssetError(this.state.error)
      return (
        <div style={{ display: 'grid', placeItems: 'center', padding: '2rem', color: '#dedfe3', background: '#111212', minHeight: '100vh', fontFamily: 'Inter, ui-sans-serif, system-ui, sans-serif' }}>
          <div style={{ width: 'min(32rem, 100%)', padding: '2rem', background: '#1d1e1f', border: '1px solid #2e2e32', borderRadius: '12px' }}>
            <h1 style={{ color: '#fff', margin: '0 0 0.75rem', fontSize: '1.5rem' }}>
              {staleAsset ? 'Dashboard updated' : 'Something went wrong'}
            </h1>
            <p style={{ lineHeight: 1.6, margin: 0 }}>
              {staleAsset
                ? 'ODS refreshed its dashboard files. Reload once to continue with the latest version.'
                : 'The dashboard could not finish loading this screen. Your ODS services are still running.'}
            </p>
            <button onClick={() => globalThis.location.reload()}
              style={{ marginTop: '1.25rem', padding: '0.65rem 1rem', background: '#b4b8c0', color: '#171819', border: 'none', borderRadius: '8px', cursor: 'pointer', fontWeight: 600, fontFamily: 'inherit' }}>
              Reload dashboard
            </button>
            {!staleAsset && this.state.error && (
              <details style={{ marginTop: '1.25rem', color: '#989da6', fontSize: '0.8rem' }}>
                <summary style={{ cursor: 'pointer' }}>Technical details</summary>
                <pre style={{ whiteSpace: 'pre-wrap', overflowWrap: 'anywhere', fontFamily: '"JetBrains Mono", monospace' }}>{this.state.error.toString()}</pre>
              </details>
            )}
          </div>
        </div>
      )
    }
    return this.props.children
  }
}

globalThis.addEventListener?.('vite:preloadError', (event) => {
  if (recoverFromStaleAsset(event.payload)) event.preventDefault()
})

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <ErrorBoundary>
      <ThemeProvider>
        <BrowserRouter>
          <DashboardSignInGate>
            <App />
          </DashboardSignInGate>
        </BrowserRouter>
      </ThemeProvider>
    </ErrorBoundary>
  </React.StrictMode>
)