File size: 2,325 Bytes
077865a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Built to build/preload.cjs (CommonJS β€” the reliable preload format).
// Runs in the renderer before any page script: seeds the dashboard session
// token into localStorage so AuthGate's very first /api/auth/status call is
// authenticated. The token arrives via additionalArguments (process.argv),
// which avoids templating strings into executeJavaScript.
import { contextBridge, ipcRenderer } from 'electron';

const TOKEN_KEY = 'freellmapi_dashboard_token';
const arg = process.argv.find((a) => a.startsWith('--freeapi-token='));
if (arg) {
  try {
    window.localStorage.setItem(TOKEN_KEY, arg.slice('--freeapi-token='.length));
  } catch {
    // localStorage unavailable β€” the dashboard will show its login screen.
  }
}

// Lets the client adapt its chrome (drag region, traffic-light padding,
// no Sign out) when running inside the desktop shell.
contextBridge.exposeInMainWorld('__FREEAPI_DESKTOP__', true);

// `desktop` class on <html> activates the client's translucent backdrop
// (html.desktop in index.css). CAREFUL: for an http:// load the preload
// runs before the page's document is parsed β€” documentElement is null or
// a placeholder that the parser replaces β€” so the early add is best-effort
// (no-flash when it sticks) and MUST NOT throw, or the theme observer
// below would never register. The client re-adds the class itself at
// module load (App.tsx), so the effect never depends on the early add.
function applyDesktopClass() {
  document.documentElement?.classList.add('desktop');
}
try {
  applyDesktopClass();
} catch {
  // Document not ready β€” DOMContentLoaded below covers it.
}

// Mirror the dashboard's theme to the main process so the tray popover
// matches. The dashboard expresses its theme as the `dark` class on
// documentElement (set by the early script in index.html and toggled by
// the navbar) β€” observe that class rather than reaching across worlds
// into localStorage.
function reportTheme() {
  ipcRenderer.send(
    'freeapi:theme-changed',
    document.documentElement.classList.contains('dark') ? 'dark' : 'light',
  );
}
window.addEventListener('DOMContentLoaded', () => {
  applyDesktopClass();
  reportTheme();
  new MutationObserver(reportTheme).observe(document.documentElement, {
    attributes: true,
    attributeFilter: ['class'],
  });
});