import { app, BrowserWindow, session } from "electron";
import { get_setting } from "./src/setting";
import { create_server, start_server } from "./src/server";
import { getAdminCookieValue, loginTokenName } from "./src/login";
const get_loading_html = (content?:string)=> `
react-sample
${content || "Loading..."}
${content === undefined ? '' : ""}
`;
const setting = get_setting();
if (!setting.cli) {
let window: BrowserWindow | null = null;
const createWindow = async () => {
window = new BrowserWindow({
width: 800,
height: 600,
center: true,
useContentSize: true,
});
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html()).toString('base64'));
await session.defaultSession.cookies.set({
url:`http://localhost:${setting.port}`,
name:loginTokenName,
value:getAdminCookieValue(),
httpOnly: true,
secure: false,
sameSite:"strict"
});
try{
const server = await create_server();
start_server(server);
await window.loadURL(`http://localhost:${setting.port}`);
}
catch(e){
if(e instanceof Error){
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html("Error : "+e.message)).toString('base64'));
}
else{
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html("Error : "+e)).toString('base64'));
}
}
window.on("closed", () => {
window = null;
});
};
const isPrimary = app.requestSingleInstanceLock();
if (!isPrimary) {
app.quit(); //exit window
app.exit();
}
app.on("second-instance", () => {
if (window != null) {
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
});
app.on("ready", (event, info) => {
createWindow();
});
app.on("window-all-closed", () => { // quit when all windows are closed
if (process.platform != "darwin") app.quit(); // (except leave MacOS app active until Cmd+Q)
});
app.on("activate", () => { // re-recreate window when dock icon is clicked and no other windows open
if (window == null) createWindow();
});
} else {
(async () => {
const server = await create_server();
start_server(server);
})();
}