File size: 4,919 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use client";

import { useState, useEffect, useCallback, useSyncExternalStore } from "react";

/**
 * Code Review Fixes Applied:
 * #7  useIsElectron β€” useSyncExternalStore for zero re-renders
 * #11 Import AppInfo type instead of inline duplication
 * #12 useDataDir β€” add error state (was swallowed silently)
 */

// ── Fix #7: Module-level detection (no state, no re-renders) ──

function getIsElectronSnapshot(): boolean {
  return typeof window !== "undefined" && window.electronAPI?.isElectron === true;
}

function getServerSnapshot(): boolean {
  return false; // SSR always returns false
}

const noop = () => () => {};

/**
 * Check if running in Electron β€” zero re-renders via useSyncExternalStore
 */
export function useIsElectron(): boolean {
  return useSyncExternalStore(noop, getIsElectronSnapshot, getServerSnapshot);
}

/**
 * App info shape from Electron main process
 * Fix #11: Single source of truth (matches electron/types.d.ts)
 */
interface AppInfo {
  name: string;
  version: string;
  platform: string;
  isDev: boolean;
  port: number;
}

/**
 * Get Electron app information
 */
export function useElectronAppInfo() {
  const hasApi = getIsElectronSnapshot();
  const [appInfo, setAppInfo] = useState<AppInfo | null>(null);
  const [loading, setLoading] = useState(hasApi);
  const [error, setError] = useState<Error | null>(null);

  useEffect(() => {
    if (typeof window === "undefined" || !window.electronAPI) return;

    window.electronAPI
      .getAppInfo()
      .then((info) => {
        setAppInfo(info);
        setLoading(false);
      })
      .catch((err) => {
        setError(err);
        setLoading(false);
      });
  }, []);

  return { appInfo, loading, error };
}

/**
 * Get the data directory path
 * Fix #12: Now exposes error state (was swallowed silently)
 */
export function useDataDir() {
  const hasApi = getIsElectronSnapshot();
  const [dataDir, setDataDir] = useState<string | null>(null);
  const [loading, setLoading] = useState(hasApi);
  const [error, setError] = useState<Error | null>(null);

  useEffect(() => {
    if (typeof window === "undefined" || !window.electronAPI) return;

    window.electronAPI
      .getDataDir()
      .then((dir) => {
        setDataDir(dir);
        setLoading(false);
      })
      .catch((err) => {
        setError(err instanceof Error ? err : new Error(String(err)));
        setLoading(false);
      });
  }, []);

  return { dataDir, loading, error };
}

/**
 * Window controls for Electron
 */
export function useWindowControls() {
  const isElectron = useIsElectron();

  const minimize = useCallback(() => {
    if (isElectron && window.electronAPI) {
      window.electronAPI.minimizeWindow();
    }
  }, [isElectron]);

  const maximize = useCallback(() => {
    if (isElectron && window.electronAPI) {
      window.electronAPI.maximizeWindow();
    }
  }, [isElectron]);

  const close = useCallback(() => {
    if (isElectron && window.electronAPI) {
      window.electronAPI.closeWindow();
    }
  }, [isElectron]);

  return { isElectron, minimize, maximize, close };
}

/**
 * Open external URL in default browser
 */
export function useOpenExternal() {
  const isElectron = useIsElectron();

  const openExternal = useCallback(
    async (url: string) => {
      if (isElectron && window.electronAPI) {
        await window.electronAPI.openExternal(url);
      } else {
        window.open(url, "_blank", "noopener,noreferrer");
      }
    },
    [isElectron]
  );

  return { openExternal };
}

/**
 * Server controls for Electron
 */
export function useServerControls() {
  const isElectron = useIsElectron();
  const [restarting, setRestarting] = useState(false);

  const restart = useCallback(async () => {
    if (!isElectron || !window.electronAPI) {
      return { success: false };
    }

    setRestarting(true);
    try {
      const result = await window.electronAPI.restartServer();
      return result;
    } finally {
      setRestarting(false);
    }
  }, [isElectron]);

  return { isElectron, restart, restarting };
}

/**
 * Listen for server status updates
 * Fix #6: Uses disposer returned by preload for precise cleanup
 */
export function useServerStatus(onStatus: (status: { status: string; port: number }) => void) {
  const isElectron = useIsElectron();

  useEffect(() => {
    if (!isElectron || !window.electronAPI) return;

    const dispose = window.electronAPI.onServerStatus(onStatus);
    return dispose;
  }, [isElectron, onStatus]);
}

/**
 * Listen for port changes
 * Fix #6: Uses disposer returned by preload for precise cleanup
 */
export function usePortChanged(onPortChanged: (port: number) => void) {
  const isElectron = useIsElectron();

  useEffect(() => {
    if (!isElectron || !window.electronAPI) return;

    const dispose = window.electronAPI.onPortChanged(onPortChanged);
    return dispose;
  }, [isElectron, onPortChanged]);
}