Skip to content

Self-improving AI Employees for everyone

3.1 KB
Raw Blame
/**
 * The app shell: everything that belongs to the app rather than to one
 * screen. It owns the frame both screens fill, the boot-time effects both
 * screens rely on, the app-level dialogs, and the switch between Normal
 * mode (Home) and Dev mode (the workflow editor).
 *
 * The effects live in <ShellEffects/> and the dialogs in <ShellDialogs/> so
 * their re-renders (a theme change, a dialog opening) never re-render the
 * screen. Nothing here subscribes to the main WebSocket context, whose value
 * changes on every console, chat and terminal line.
 */

import { Suspense, lazy, useState } from 'react';
import { TooltipProvider } from '@/components/ui/tooltip';
import { useSoundSync } from '../hooks/useSound';
import { useSavedGraphSync } from '../hooks/useWorkflowOpsListener';
import { useShellDialogsStore } from '../stores/shellDialogsStore';
import { useWorkflowSettingsStore } from '../stores/workflowSettingsStore';
import { ShellModeSwitch } from './ShellModeSwitch';
import { useCurrentWorkflowSync } from './useCurrentWorkflowSync';
import { useModeShortcut } from './useModeShortcut';
import { useUIDefaultsOnce } from './useUIDefaultsOnce';

const SettingsPanel = lazy(() => import('../components/ui/SettingsPanel'));
const CredentialsModal = lazy(() => import('../components/CredentialsModal'));

function ShellEffects() {
  // Mirror the sound preference into the WebAudio engine and re-read
  // --sound-pack whenever the theme changes.
  useSoundSync();
  useCurrentWorkflowSync();
  // Server-saved canvas changes reach the editor's copy on either screen.
  useSavedGraphSync();
  useUIDefaultsOnce();
  useModeShortcut();
  return null;
}

/** Latches true the first time `open` is true, so a dialog's chunk loads
 *  only when it is first opened and it stays mounted for its close
 *  animation afterwards. */
function useOpenedOnce(open: boolean): boolean {
  const [opened, setOpened] = useState(open);
  if (open && !opened) setOpened(true);
  return opened || open;
}

function ShellDialogs() {
  const settingsOpen = useShellDialogsStore((s) => s.settingsOpen);
  const credentialsOpen = useShellDialogsStore((s) => s.credentialsOpen);
  const closeSettings = useShellDialogsStore((s) => s.closeSettings);
  const closeCredentials = useShellDialogsStore((s) => s.closeCredentials);
  const settings = useWorkflowSettingsStore((s) => s.settings);
  const setSettings = useWorkflowSettingsStore((s) => s.setSettings);
  const settingsMounted = useOpenedOnce(settingsOpen);
  const credentialsMounted = useOpenedOnce(credentialsOpen);

  return (
    <Suspense fallback={null}>
      {settingsMounted && (
        <SettingsPanel
          isOpen={settingsOpen}
          onClose={closeSettings}
          settings={settings}
          onSettingsChange={setSettings}
        />
      )}
      {credentialsMounted && <CredentialsModal visible={credentialsOpen} onClose={closeCredentials} />}
    </Suspense>
  );
}

export default function AppShell() {
  return (
    <TooltipProvider>
      <ShellEffects />
      <div className="flex h-screen w-screen flex-col bg-bg-app font-body">
        <ShellModeSwitch />
      </div>
      <ShellDialogs />
    </TooltipProvider>
  );
}