-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
154 lines (130 loc) · 4.12 KB
/
index.js
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
import { app, BrowserWindow, ipcMain } from "electron";
import path from "path";
import { fileURLToPath } from "url";
import Store from "electron-store";
// Fix for __dirname in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const store = new Store();
app.disableHardwareAcceleration();
let mainWindow = null;
let loginWindow = null;
function createLoginWindow() {
loginWindow = new BrowserWindow({
width: 600,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
icon: path.join(__dirname, "build", "assets", "icon.icns"),
resizable: false,
});
const loginPath = path.join(__dirname, "login.html");
console.log("Loading login page from:", loginPath);
loginWindow.loadFile(loginPath);
}
function createMainWindow(serverHost) {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
icon: path.join(__dirname, "build", "assets", "icon.icns"),
});
const url = serverHost.startsWith("http")
? serverHost
: `https://${serverHost}`;
console.log("Loading URL:", url);
mainWindow.loadURL(url);
mainWindow.webContents.on("did-finish-load", () => {
mainWindow.webContents.executeJavaScript(`
if (!document.getElementById('reset-server-button')) {
const button = document.createElement('button');
button.id = 'reset-server-button';
button.innerHTML = 'Sign out';
button.style.position = 'fixed';
button.style.bottom = '20px';
button.style.right = '20px';
button.style.zIndex = '10000';
button.style.padding = '8px 16px';
button.style.backgroundColor = '#ff4444';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.style.fontFamily = 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif';
button.onclick = () => {
if (confirm('Are you sure you want to sign out from the server?')) {
require('electron').ipcRenderer.send('reset-server');
}
};
document.body.appendChild(button);
}
`);
});
// Add hover effect
mainWindow.webContents.executeJavaScript(`
const style = document.createElement('style');
style.textContent = \`
#reset-server-button:hover {
background-color: #ff6666 !important;
}
\`;
document.head.appendChild(style);
`);
app.setName("LibreChat UI");
if (loginWindow && !loginWindow.isDestroyed()) {
loginWindow.close();
}
// Log when button is created
console.log("Reset button should be created");
}
function initialize() {
const savedHost = store.get("serverHost");
console.log("Saved host:", savedHost);
if (savedHost) {
createMainWindow(savedHost);
} else {
createLoginWindow();
}
}
app.whenReady().then(() => {
console.log("App is ready");
initialize();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
initialize();
}
});
ipcMain.on("submit-server-host", (event, serverHost) => {
console.log("Saving server host:", serverHost);
store.set("serverHost", serverHost);
createMainWindow(serverHost);
});
ipcMain.on("reset-server", () => {
console.log("Resetting server configuration");
store.delete("serverHost");
if (mainWindow) {
mainWindow.close();
}
createLoginWindow();
});
// Error handling
process.on("uncaughtException", (error) => {
console.error("An uncaught error occurred:", error);
});
app.on("render-process-gone", (event, webContents, details) => {
console.error("Render process gone:", details);
});
app.on("child-process-gone", (event, details) => {
console.error("Child process gone:", details);
});