first commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { app, BrowserWindow, shell, ipcMain } from 'electron'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import path from 'node:path'
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
// ─── Paths ──────────────────────────────────────────────────────────────────
|
||||
// dist-electron/main.js is the compiled output location.
|
||||
// dist/ is the Vite renderer build output.
|
||||
const RENDERER_DIST = path.join(__dirname, '../dist')
|
||||
// vite-plugin-electron outputs preload as .mjs when "type":"module" is set in package.json
|
||||
const PRELOAD_PATH = path.join(__dirname, 'preload.mjs')
|
||||
|
||||
// ─── Dev server URL ─────────────────────────────────────────────────────────
|
||||
// vite-plugin-electron sets this env var when running in development mode.
|
||||
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
|
||||
|
||||
// ─── Window reference ────────────────────────────────────────────────────────
|
||||
let mainWindow
|
||||
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1280,
|
||||
height: 800,
|
||||
minWidth: 900,
|
||||
minHeight: 600,
|
||||
title: 'CTM Concierge',
|
||||
show: false, // show after ready-to-show to prevent visual flash
|
||||
backgroundColor: '#0f1117',
|
||||
webPreferences: {
|
||||
// ── Security defaults ──────────────────────────────────────────────
|
||||
contextIsolation: true, // Isolates renderer from Electron internals
|
||||
nodeIntegration: false, // Renderer cannot access Node.js APIs directly
|
||||
sandbox: true, // Extra process isolation
|
||||
webSecurity: true,
|
||||
// ── Preload ────────────────────────────────────────────────────────
|
||||
preload: PRELOAD_PATH,
|
||||
},
|
||||
})
|
||||
|
||||
// Gracefully show window once the DOM is ready to avoid white flash
|
||||
mainWindow.once('ready-to-show', () => {
|
||||
mainWindow.show()
|
||||
})
|
||||
|
||||
// Open external links in the system browser, not Electron
|
||||
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
||||
if (url.startsWith('https:') || url.startsWith('http:')) {
|
||||
shell.openExternal(url)
|
||||
}
|
||||
return { action: 'deny' }
|
||||
})
|
||||
|
||||
// ── Load the app ─────────────────────────────────────────────────────────
|
||||
if (VITE_DEV_SERVER_URL) {
|
||||
// Development: load from Vite HMR dev server
|
||||
mainWindow.loadURL(VITE_DEV_SERVER_URL)
|
||||
mainWindow.webContents.openDevTools()
|
||||
} else {
|
||||
// Production: load compiled static files
|
||||
mainWindow.loadFile(path.join(RENDERER_DIST, 'index.html'))
|
||||
}
|
||||
}
|
||||
|
||||
// ─── App lifecycle ───────────────────────────────────────────────────────────
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
|
||||
// macOS: re-create window when dock icon is clicked and no windows are open
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// Quit when all windows are closed (except on macOS)
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
mainWindow = null
|
||||
}
|
||||
})
|
||||
|
||||
// ─── IPC Handlers ────────────────────────────────────────────────────────────
|
||||
// Register main-process IPC handlers here.
|
||||
// Example: ipcMain.handle('channel-name', async (event, ...args) => { ... })
|
||||
|
||||
ipcMain.handle('app:get-version', () => app.getVersion())
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* preload.js
|
||||
*
|
||||
* Runs in a privileged context but is sandboxed from the renderer.
|
||||
* Use contextBridge to expose a safe, narrow API surface to the renderer.
|
||||
* Never expose raw ipcRenderer — always whitelist specific channels.
|
||||
*/
|
||||
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
|
||||
// ─── Allowed IPC channels ─────────────────────────────────────────────────
|
||||
// Only messages on these channels will be forwarded.
|
||||
const ALLOWED_SEND_CHANNELS = [
|
||||
'patient:update-status',
|
||||
'patient:fetch-list',
|
||||
'schedule:fetch',
|
||||
'app:ready',
|
||||
]
|
||||
|
||||
const ALLOWED_RECEIVE_CHANNELS = [
|
||||
'patient:status-updated',
|
||||
'patient:list-response',
|
||||
'schedule:response',
|
||||
'app:get-version',
|
||||
]
|
||||
|
||||
// ─── Exposed API ──────────────────────────────────────────────────────────
|
||||
contextBridge.exposeInMainWorld('api', {
|
||||
/**
|
||||
* Send a one-way message to the main process.
|
||||
* @param {string} channel - Must be in ALLOWED_SEND_CHANNELS
|
||||
* @param {*} data
|
||||
*/
|
||||
send(channel, data) {
|
||||
if (ALLOWED_SEND_CHANNELS.includes(channel)) {
|
||||
ipcRenderer.send(channel, data)
|
||||
} else {
|
||||
console.warn(`[preload] Blocked send on unknown channel: "${channel}"`)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Invoke a main-process handler and await its response.
|
||||
* @param {string} channel - Must be in ALLOWED_SEND_CHANNELS
|
||||
* @param {...*} args
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
invoke(channel, ...args) {
|
||||
if (ALLOWED_SEND_CHANNELS.includes(channel) || ALLOWED_RECEIVE_CHANNELS.includes(channel)) {
|
||||
return ipcRenderer.invoke(channel, ...args)
|
||||
}
|
||||
return Promise.reject(new Error(`[preload] Blocked invoke on unknown channel: "${channel}"`))
|
||||
},
|
||||
|
||||
/**
|
||||
* Subscribe to messages from the main process.
|
||||
* @param {string} channel - Must be in ALLOWED_RECEIVE_CHANNELS
|
||||
* @param {Function} callback - Called with (event, ...args) on each message
|
||||
* @returns {Function} Unsubscribe function — call it to remove the listener
|
||||
*/
|
||||
receive(channel, callback) {
|
||||
if (ALLOWED_RECEIVE_CHANNELS.includes(channel)) {
|
||||
const handler = (_event, ...args) => callback(...args)
|
||||
ipcRenderer.on(channel, handler)
|
||||
// Return cleanup function so React effects can unsubscribe
|
||||
return () => ipcRenderer.removeListener(channel, handler)
|
||||
}
|
||||
console.warn(`[preload] Blocked receive on unknown channel: "${channel}"`)
|
||||
return () => {}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user