feat(app): add diagnostics and update runtime
This commit is contained in:
79
src/main/diagnostics.ts
Normal file
79
src/main/diagnostics.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { app, clipboard, shell } from 'electron'
|
||||
import { existsSync, statSync } from 'node:fs'
|
||||
import type { DiagnosticsInfo } from '@shared/types'
|
||||
import {
|
||||
getGsiBaseUrl,
|
||||
getGsiPort,
|
||||
isGsiServerRunning
|
||||
} from './games/gsi-server'
|
||||
import { listGamesStatus } from './games/registry'
|
||||
import { getLogDir } from './logger'
|
||||
import { isMeetingActiveSync } from './meeting-detect'
|
||||
import { getState, getStorePath } from './store'
|
||||
import { getUpdaterStatus } from './updater'
|
||||
|
||||
function fileSize(path: string): number | null {
|
||||
try {
|
||||
if (!path || !existsSync(path)) return null
|
||||
return statSync(path).size
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDiagnosticsInfo(): Promise<DiagnosticsInfo> {
|
||||
const state = getState()
|
||||
const store = getStorePath()
|
||||
const games = await listGamesStatus()
|
||||
|
||||
return {
|
||||
generatedAt: Date.now(),
|
||||
app: {
|
||||
version: app.getVersion(),
|
||||
isPackaged: app.isPackaged,
|
||||
platform: process.platform,
|
||||
arch: process.arch
|
||||
},
|
||||
runtime: {
|
||||
electron: process.versions.electron ?? 'unknown',
|
||||
chrome: process.versions.chrome ?? 'unknown',
|
||||
node: process.versions.node
|
||||
},
|
||||
paths: {
|
||||
userData: app.getPath('userData'),
|
||||
store,
|
||||
logs: getLogDir()
|
||||
},
|
||||
store: {
|
||||
bytes: fileSize(store),
|
||||
exercises: state.exercises.length,
|
||||
meals: state.meals.length,
|
||||
challenges: state.challenges.length,
|
||||
history: state.history?.length ?? 0
|
||||
},
|
||||
updater: getUpdaterStatus(),
|
||||
games,
|
||||
gsi: {
|
||||
running: isGsiServerRunning(),
|
||||
port: getGsiPort(),
|
||||
baseUrl: getGsiBaseUrl()
|
||||
},
|
||||
meetingActive: isMeetingActiveSync()
|
||||
}
|
||||
}
|
||||
|
||||
export async function openLogsFolder(): Promise<{
|
||||
ok: boolean
|
||||
error?: string
|
||||
}> {
|
||||
const dir = getLogDir()
|
||||
if (!dir) return { ok: false, error: 'logs-unavailable' }
|
||||
const error = await shell.openPath(dir)
|
||||
return error ? { ok: false, error } : { ok: true }
|
||||
}
|
||||
|
||||
export async function copyDiagnosticsToClipboard(): Promise<DiagnosticsInfo> {
|
||||
const info = await getDiagnosticsInfo()
|
||||
clipboard.writeText(JSON.stringify(info, null, 2))
|
||||
return info
|
||||
}
|
||||
@@ -144,3 +144,7 @@ export function getGsiBaseUrl(): string {
|
||||
export function getGsiPort(): number {
|
||||
return PORT
|
||||
}
|
||||
|
||||
export function isGsiServerRunning(): boolean {
|
||||
return server !== null
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { startGamesRegistry, stopGamesRegistry } from './games/registry'
|
||||
import { initUpdater, stopUpdater } from './updater'
|
||||
import { IPC } from '@shared/ipc'
|
||||
import { log } from './logger'
|
||||
import { installSecurityHardening } from './security'
|
||||
|
||||
const APP_ID = 'com.anril.exercise-reminder'
|
||||
|
||||
@@ -45,6 +46,7 @@ if (!gotLock) {
|
||||
app.on('second-instance', () => showMainWindow())
|
||||
|
||||
app.whenReady().then(() => {
|
||||
installSecurityHardening()
|
||||
registerIpc()
|
||||
createTray()
|
||||
|
||||
|
||||
160
src/main/ipc.ts
160
src/main/ipc.ts
@@ -10,7 +10,12 @@ import {
|
||||
import type { IpcMainEvent, IpcMainInvokeEvent } from 'electron'
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
import { IPC } from '@shared/ipc'
|
||||
import type { Exercise, GameId, Settings } from '@shared/types'
|
||||
import type {
|
||||
Exercise,
|
||||
GameId,
|
||||
RendererErrorReport,
|
||||
Settings
|
||||
} from '@shared/types'
|
||||
import {
|
||||
addChallenge,
|
||||
addExercise,
|
||||
@@ -68,6 +73,13 @@ import {
|
||||
validateSnoozeMinutes
|
||||
} from './validate'
|
||||
import { log } from './logger'
|
||||
import {
|
||||
copyDiagnosticsToClipboard,
|
||||
getDiagnosticsInfo,
|
||||
openLogsFolder
|
||||
} from './diagnostics'
|
||||
|
||||
const MAX_RENDERER_ERROR_FIELD = 8_000
|
||||
|
||||
/**
|
||||
* Враппер вокруг `ipcMain.handle`: ловит любое исключение в обработчике,
|
||||
@@ -105,10 +117,7 @@ function safeOn<F extends (event: IpcMainEvent, ...args: never[]) => void>(
|
||||
): void {
|
||||
ipcMain.on(channel, (event, ...args) => {
|
||||
try {
|
||||
const call = fn as unknown as (
|
||||
e: IpcMainEvent,
|
||||
...a: unknown[]
|
||||
) => void
|
||||
const call = fn as unknown as (e: IpcMainEvent, ...a: unknown[]) => void
|
||||
call(event, ...args)
|
||||
} catch (err) {
|
||||
log.error(`[ipc] ${channel} (on) threw`, err)
|
||||
@@ -116,6 +125,21 @@ function safeOn<F extends (event: IpcMainEvent, ...args: never[]) => void>(
|
||||
})
|
||||
}
|
||||
|
||||
function cleanRendererError(raw: unknown): RendererErrorReport | null {
|
||||
if (typeof raw !== 'object' || raw === null) return null
|
||||
const r = raw as Record<string, unknown>
|
||||
const message = typeof r.message === 'string' ? r.message.trim() : ''
|
||||
if (!message) return null
|
||||
const clean = (v: unknown): string | undefined =>
|
||||
typeof v === 'string' ? v.slice(0, MAX_RENDERER_ERROR_FIELD) : undefined
|
||||
return {
|
||||
message: message.slice(0, MAX_RENDERER_ERROR_FIELD),
|
||||
stack: clean(r.stack),
|
||||
componentStack: clean(r.componentStack),
|
||||
source: clean(r.source)
|
||||
}
|
||||
}
|
||||
|
||||
export function registerIpc(): void {
|
||||
safeHandle(IPC.getState, () => {
|
||||
// Без history (см. getStateForRenderer) и с актуальным значением
|
||||
@@ -136,17 +160,14 @@ export function registerIpc(): void {
|
||||
return ex
|
||||
})
|
||||
|
||||
safeHandle(
|
||||
IPC.updateExercise,
|
||||
(_e, idRaw: unknown, patchRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const patch = validateExercisePatch(patchRaw)
|
||||
if (!id || !patch) return null
|
||||
const ex = updateExercise(id, patch)
|
||||
broadcastState()
|
||||
return ex
|
||||
}
|
||||
)
|
||||
safeHandle(IPC.updateExercise, (_e, idRaw: unknown, patchRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const patch = validateExercisePatch(patchRaw)
|
||||
if (!id || !patch) return null
|
||||
const ex = updateExercise(id, patch)
|
||||
broadcastState()
|
||||
return ex
|
||||
})
|
||||
|
||||
safeHandle(IPC.deleteExercise, (_e, idRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
@@ -156,21 +177,18 @@ export function registerIpc(): void {
|
||||
return ok
|
||||
})
|
||||
|
||||
safeHandle(
|
||||
IPC.toggleExercise,
|
||||
(_e, idRaw: unknown, enabledRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
if (!id || typeof enabledRaw !== 'boolean') return null
|
||||
const patch: Partial<Exercise> = { enabled: enabledRaw }
|
||||
if (enabledRaw) {
|
||||
const ex = getState().exercises.find((e) => e.id === id)
|
||||
if (ex) patch.nextFireAt = Date.now() + ex.intervalMinutes * 60_000
|
||||
}
|
||||
const ex = updateExercise(id, patch)
|
||||
broadcastState()
|
||||
return ex
|
||||
safeHandle(IPC.toggleExercise, (_e, idRaw: unknown, enabledRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
if (!id || typeof enabledRaw !== 'boolean') return null
|
||||
const patch: Partial<Exercise> = { enabled: enabledRaw }
|
||||
if (enabledRaw) {
|
||||
const ex = getState().exercises.find((e) => e.id === id)
|
||||
if (ex) patch.nextFireAt = Date.now() + ex.intervalMinutes * 60_000
|
||||
}
|
||||
)
|
||||
const ex = updateExercise(id, patch)
|
||||
broadcastState()
|
||||
return ex
|
||||
})
|
||||
|
||||
safeHandle(IPC.markDone, (_e, idRaw: unknown, repsRaw?: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
@@ -299,6 +317,19 @@ export function registerIpc(): void {
|
||||
|
||||
safeHandle(IPC.getMeetingActive, () => isMeetingActiveSync())
|
||||
|
||||
safeHandle(IPC.getDiagnostics, () => getDiagnosticsInfo())
|
||||
|
||||
safeHandle(IPC.openLogsFolder, () => openLogsFolder())
|
||||
|
||||
safeHandle(IPC.copyDiagnostics, () => copyDiagnosticsToClipboard())
|
||||
|
||||
safeHandle(IPC.reportRendererError, (_e, raw: unknown) => {
|
||||
const report = cleanRendererError(raw)
|
||||
if (!report) return false
|
||||
log.error('[renderer] error reported', report)
|
||||
return true
|
||||
})
|
||||
|
||||
safeHandle(IPC.quit, () => app.quit())
|
||||
safeHandle(IPC.reminderClose, () => hideReminderWindow())
|
||||
|
||||
@@ -369,17 +400,14 @@ export function registerIpc(): void {
|
||||
broadcastState()
|
||||
return c
|
||||
})
|
||||
safeHandle(
|
||||
IPC.updateChallenge,
|
||||
(_e, idRaw: unknown, patchRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const patch = validateChallengePatch(patchRaw)
|
||||
if (!id || !patch) return null
|
||||
const c = updateChallenge(id, patch)
|
||||
broadcastState()
|
||||
return c
|
||||
}
|
||||
)
|
||||
safeHandle(IPC.updateChallenge, (_e, idRaw: unknown, patchRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const patch = validateChallengePatch(patchRaw)
|
||||
if (!id || !patch) return null
|
||||
const c = updateChallenge(id, patch)
|
||||
broadcastState()
|
||||
return c
|
||||
})
|
||||
safeHandle(IPC.deleteChallenge, (_e, idRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
if (!id) return false
|
||||
@@ -387,31 +415,25 @@ export function registerIpc(): void {
|
||||
broadcastState()
|
||||
return ok
|
||||
})
|
||||
safeHandle(
|
||||
IPC.toggleChallenge,
|
||||
(_e, idRaw: unknown, enabledRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
if (!id || typeof enabledRaw !== 'boolean') return null
|
||||
const c = updateChallenge(id, { enabled: enabledRaw })
|
||||
broadcastState()
|
||||
return c
|
||||
}
|
||||
)
|
||||
safeHandle(IPC.toggleChallenge, (_e, idRaw: unknown, enabledRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
if (!id || typeof enabledRaw !== 'boolean') return null
|
||||
const c = updateChallenge(id, { enabled: enabledRaw })
|
||||
broadcastState()
|
||||
return c
|
||||
})
|
||||
|
||||
safeHandle(IPC.closeMatchSummary, () => hideReminderWindow())
|
||||
|
||||
safeHandle(
|
||||
IPC.markChallengeDone,
|
||||
(_e, idRaw: unknown, repsRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const reps = validateActualReps(repsRaw)
|
||||
if (!id || reps === undefined || reps <= 0) return false
|
||||
markChallengeDone(id, reps)
|
||||
broadcastState()
|
||||
broadcastHistoryChanged()
|
||||
return true
|
||||
}
|
||||
)
|
||||
safeHandle(IPC.markChallengeDone, (_e, idRaw: unknown, repsRaw: unknown) => {
|
||||
const id = validateId(idRaw)
|
||||
const reps = validateActualReps(repsRaw)
|
||||
if (!id || reps === undefined || reps <= 0) return false
|
||||
markChallengeDone(id, reps)
|
||||
broadcastState()
|
||||
broadcastHistoryChanged()
|
||||
return true
|
||||
})
|
||||
|
||||
// Dev helper: simulate a match end with given stats. NEVER registered in
|
||||
// packaged builds — a compromised renderer (XSS, malicious npm dep) could
|
||||
@@ -448,17 +470,13 @@ export function registerIpc(): void {
|
||||
// renderer не получает прямого доступа к ФС.
|
||||
safeHandle(IPC.exportState, async (event) => {
|
||||
const win = BrowserWindow.fromWebContents(event.sender) ?? undefined
|
||||
const stamp = new Date()
|
||||
.toISOString()
|
||||
.replace(/[:T]/g, '-')
|
||||
.slice(0, 16)
|
||||
const stamp = new Date().toISOString().replace(/[:T]/g, '-').slice(0, 16)
|
||||
const defaultPath = `laude-backup-${stamp}.json`
|
||||
// Native-диалоги OS читают локаль из системы. Title — единственная
|
||||
// строка которую мы контролируем; локализуем по settings.language.
|
||||
const lang = getState().settings.language ?? 'ru'
|
||||
const result = await dialog.showSaveDialog(win!, {
|
||||
title:
|
||||
lang === 'en' ? 'Save backup' : 'Сохранить резервную копию',
|
||||
title: lang === 'en' ? 'Save backup' : 'Сохранить резервную копию',
|
||||
defaultPath,
|
||||
filters: [{ name: 'JSON', extensions: ['json'] }]
|
||||
})
|
||||
@@ -480,7 +498,9 @@ export function registerIpc(): void {
|
||||
const lang = getState().settings.language ?? 'ru'
|
||||
const result = await dialog.showOpenDialog(win!, {
|
||||
title:
|
||||
lang === 'en' ? 'Restore from backup' : 'Восстановить из резервной копии',
|
||||
lang === 'en'
|
||||
? 'Restore from backup'
|
||||
: 'Восстановить из резервной копии',
|
||||
properties: ['openFile'],
|
||||
filters: [{ name: 'JSON', extensions: ['json'] }]
|
||||
})
|
||||
|
||||
@@ -121,5 +121,6 @@ export const log = {
|
||||
|
||||
/** Путь к логам (для диагностики). Возвращает пустую строку до initLogger(). */
|
||||
export function getLogDir(): string {
|
||||
ensurePaths()
|
||||
return logDir
|
||||
}
|
||||
|
||||
37
src/main/security.ts
Normal file
37
src/main/security.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { app, session } from 'electron'
|
||||
import { log } from './logger'
|
||||
|
||||
/**
|
||||
* Renderer permissions are denied by default. The app does not need camera,
|
||||
* microphone, geolocation, USB/HID/serial, direct notification permission or
|
||||
* browser-driven openExternal; all trusted privileged actions go through
|
||||
* validated IPC handlers in main.
|
||||
*/
|
||||
export function installSecurityHardening(): void {
|
||||
session.defaultSession.setPermissionRequestHandler(
|
||||
(_webContents, permission, callback, details) => {
|
||||
log.warn('[security] denied permission request', {
|
||||
permission,
|
||||
requestingUrl: 'requestingUrl' in details ? details.requestingUrl : ''
|
||||
})
|
||||
callback(false)
|
||||
}
|
||||
)
|
||||
|
||||
session.defaultSession.setPermissionCheckHandler(
|
||||
(_webContents, permission, requestingOrigin) => {
|
||||
log.warn('[security] denied permission check', {
|
||||
permission,
|
||||
requestingOrigin
|
||||
})
|
||||
return false
|
||||
}
|
||||
)
|
||||
|
||||
app.on('web-contents-created', (_event, contents) => {
|
||||
contents.on('will-attach-webview', (event) => {
|
||||
event.preventDefault()
|
||||
log.warn('[security] blocked webview attach')
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -47,7 +47,7 @@ let cache: PersistedState | null = null
|
||||
let storePath = ''
|
||||
let pendingWrite: NodeJS.Timeout | null = null
|
||||
|
||||
function getStorePath(): string {
|
||||
export function getStorePath(): string {
|
||||
if (!storePath) {
|
||||
const dir = app.getPath('userData')
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
||||
@@ -197,7 +197,8 @@ function sanitizeMeal(raw: unknown, now = Date.now()): Meal | null {
|
||||
const meal: Meal = {
|
||||
...base,
|
||||
id,
|
||||
nextFireAt: finiteMs(raw.nextFireAt) ?? nextMealOccurrence(base.time, base.days, now)
|
||||
nextFireAt:
|
||||
finiteMs(raw.nextFireAt) ?? nextMealOccurrence(base.time, base.days, now)
|
||||
}
|
||||
const lastDoneAt = finiteMs(raw.lastDoneAt)
|
||||
if (lastDoneAt !== undefined) meal.lastDoneAt = lastDoneAt
|
||||
|
||||
@@ -56,12 +56,8 @@ describe('validateMealInput', () => {
|
||||
})
|
||||
|
||||
it('реджектит кривое время', () => {
|
||||
expect(
|
||||
validateMealInput({ name: 'X', time: '99:99', days: [] })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateMealInput({ name: 'X', time: 'noon', days: [] })
|
||||
).toBeNull()
|
||||
expect(validateMealInput({ name: 'X', time: '99:99', days: [] })).toBeNull()
|
||||
expect(validateMealInput({ name: 'X', time: 'noon', days: [] })).toBeNull()
|
||||
})
|
||||
|
||||
it('реджектит дни вне диапазона и дедупит', () => {
|
||||
@@ -124,8 +120,12 @@ describe('validateExerciseInput', () => {
|
||||
})
|
||||
|
||||
it('rejects missing required fields', () => {
|
||||
expect(validateExerciseInput({ ...validExercise, name: undefined })).toBeNull()
|
||||
expect(validateExerciseInput({ ...validExercise, reps: undefined })).toBeNull()
|
||||
expect(
|
||||
validateExerciseInput({ ...validExercise, name: undefined })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateExerciseInput({ ...validExercise, reps: undefined })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateExerciseInput({ ...validExercise, intervalMinutes: undefined })
|
||||
).toBeNull()
|
||||
@@ -221,7 +221,9 @@ describe('validateExercisePatch', () => {
|
||||
it('accepts partial patches', () => {
|
||||
expect(validateExercisePatch({ reps: 12 })).toEqual({ reps: 12 })
|
||||
expect(validateExercisePatch({ name: 'New' })).toEqual({ name: 'New' })
|
||||
expect(validateExercisePatch({ enabled: false })).toEqual({ enabled: false })
|
||||
expect(validateExercisePatch({ enabled: false })).toEqual({
|
||||
enabled: false
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects patch with a single invalid field', () => {
|
||||
@@ -273,7 +275,14 @@ describe('validateChallengeInput', () => {
|
||||
})
|
||||
|
||||
it('accepts all valid stats', () => {
|
||||
const stats = ['deaths', 'kills', 'assists', 'last_hits', 'denies', 'duration_min']
|
||||
const stats = [
|
||||
'deaths',
|
||||
'kills',
|
||||
'assists',
|
||||
'last_hits',
|
||||
'denies',
|
||||
'duration_min'
|
||||
]
|
||||
for (const stat of stats) {
|
||||
expect(validateChallengeInput({ ...valid, stat })).not.toBeNull()
|
||||
}
|
||||
@@ -288,11 +297,15 @@ describe('validateChallengeInput', () => {
|
||||
})
|
||||
|
||||
it('accepts zero multiplier (legitimate "disable" semantics)', () => {
|
||||
expect(validateChallengeInput({ ...valid, multiplier: 0 })?.multiplier).toBe(0)
|
||||
expect(
|
||||
validateChallengeInput({ ...valid, multiplier: 0 })?.multiplier
|
||||
).toBe(0)
|
||||
})
|
||||
|
||||
it('accepts fractional multiplier (e.g. 0.5×)', () => {
|
||||
expect(validateChallengeInput({ ...valid, multiplier: 0.5 })?.multiplier).toBe(0.5)
|
||||
expect(
|
||||
validateChallengeInput({ ...valid, multiplier: 0.5 })?.multiplier
|
||||
).toBe(0.5)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -399,7 +412,9 @@ describe('validateSettingsPatch', () => {
|
||||
|
||||
it('rejects non-strings', () => {
|
||||
expect(validateSettingsPatch({ lastSeenVersion: 42 })).toBeNull()
|
||||
expect(validateSettingsPatch({ lastSeenVersion: ['1', '0', '0'] })).toBeNull()
|
||||
expect(
|
||||
validateSettingsPatch({ lastSeenVersion: ['1', '0', '0'] })
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -426,6 +441,12 @@ describe('validateSettingsPatch', () => {
|
||||
expect(
|
||||
validateSettingsPatch({ quietHours: { ...baseQh, from: '2500' } })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateSettingsPatch({ quietHours: { ...baseQh, from: '25:00' } })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateSettingsPatch({ quietHours: { ...baseQh, to: '09:99' } })
|
||||
).toBeNull()
|
||||
expect(
|
||||
validateSettingsPatch({ quietHours: { ...baseQh, to: 'bedtime' } })
|
||||
).toBeNull()
|
||||
@@ -479,7 +500,9 @@ describe('validateSettingsPatch', () => {
|
||||
describe('validateId', () => {
|
||||
it('accepts reasonable id strings', () => {
|
||||
expect(validateId('abc')).toBe('abc')
|
||||
expect(validateId('uuid-v4-style-thing-123')).toBe('uuid-v4-style-thing-123')
|
||||
expect(validateId('uuid-v4-style-thing-123')).toBe(
|
||||
'uuid-v4-style-thing-123'
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects non-strings', () => {
|
||||
|
||||
@@ -43,7 +43,6 @@ const VALID_CATEGORIES: ReminderCategory[] = [
|
||||
'eyes',
|
||||
'posture'
|
||||
]
|
||||
const HHMM_RE = /^\d{1,2}:\d{2}$/
|
||||
|
||||
function isObj(v: unknown): v is Record<string, unknown> {
|
||||
return typeof v === 'object' && v !== null && !Array.isArray(v)
|
||||
@@ -130,7 +129,11 @@ export function validateExerciseInput(
|
||||
// dailyGoal: undefined = не задан (нет soft-cap'a), null от UI приводим к
|
||||
// undefined; иначе — должен пройти int-range, иначе reject (нельзя
|
||||
// отправить из renderer'а NaN/негатив и тихо обнулить).
|
||||
if (raw.dailyGoal !== undefined && raw.dailyGoal !== null && dailyGoal === undefined) {
|
||||
if (
|
||||
raw.dailyGoal !== undefined &&
|
||||
raw.dailyGoal !== null &&
|
||||
dailyGoal === undefined
|
||||
) {
|
||||
return null
|
||||
}
|
||||
if (
|
||||
@@ -438,8 +441,8 @@ export function validateSettingsPatch(raw: unknown): Partial<Settings> | null {
|
||||
enabled === undefined ||
|
||||
from === undefined ||
|
||||
to === undefined ||
|
||||
!HHMM_RE.test(from) ||
|
||||
!HHMM_RE.test(to)
|
||||
validHHMM(from) === undefined ||
|
||||
validHHMM(to) === undefined
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -3,12 +3,14 @@ import { IPC } from '@shared/ipc'
|
||||
import type {
|
||||
AppState,
|
||||
Challenge,
|
||||
DiagnosticsInfo,
|
||||
Exercise,
|
||||
GameId,
|
||||
GameStatus,
|
||||
HistoryEntry,
|
||||
MatchSummary,
|
||||
Meal,
|
||||
RendererErrorReport,
|
||||
Settings,
|
||||
Tick,
|
||||
UpdaterStatus
|
||||
@@ -64,6 +66,14 @@ const api = {
|
||||
getAppVersion: (): Promise<string> => ipcRenderer.invoke(IPC.getAppVersion),
|
||||
getMeetingActive: (): Promise<boolean> =>
|
||||
ipcRenderer.invoke(IPC.getMeetingActive),
|
||||
getDiagnostics: (): Promise<DiagnosticsInfo> =>
|
||||
ipcRenderer.invoke(IPC.getDiagnostics),
|
||||
openLogsFolder: (): Promise<{ ok: boolean; error?: string }> =>
|
||||
ipcRenderer.invoke(IPC.openLogsFolder),
|
||||
copyDiagnostics: (): Promise<DiagnosticsInfo> =>
|
||||
ipcRenderer.invoke(IPC.copyDiagnostics),
|
||||
reportRendererError: (report: RendererErrorReport): Promise<boolean> =>
|
||||
ipcRenderer.invoke(IPC.reportRendererError, report),
|
||||
|
||||
pauseAll: (): Promise<void> => ipcRenderer.invoke(IPC.pauseAll),
|
||||
resumeAll: (): Promise<void> => ipcRenderer.invoke(IPC.resumeAll),
|
||||
@@ -164,8 +174,7 @@ const api = {
|
||||
on(IPC.evtMaximizeChanged, h),
|
||||
onMeetingChanged: (h: Handler<boolean>): Unsub =>
|
||||
on(IPC.evtMeetingChanged, h),
|
||||
onHistoryChanged: (h: Handler<void>): Unsub =>
|
||||
on(IPC.evtHistoryChanged, h)
|
||||
onHistoryChanged: (h: Handler<void>): Unsub => on(IPC.evtHistoryChanged, h)
|
||||
}
|
||||
|
||||
contextBridge.exposeInMainWorld('api', api)
|
||||
|
||||
@@ -23,9 +23,13 @@ export class ErrorBoundary extends Component<Props, State> {
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo): void {
|
||||
// No remote telemetry — log to the local console so a curious user
|
||||
// (or dev tools session) can capture it.
|
||||
console.error('[ErrorBoundary]', error, info.componentStack)
|
||||
void window.api?.reportRendererError?.({
|
||||
source: 'ErrorBoundary',
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
componentStack: info.componentStack ?? undefined
|
||||
})
|
||||
}
|
||||
|
||||
reset = (): void => this.setState({ error: null })
|
||||
|
||||
@@ -224,6 +224,7 @@ export const ru: Dict = {
|
||||
'settings.section.language': 'Язык',
|
||||
'settings.section.updates': 'Обновления',
|
||||
'settings.section.data': 'Данные',
|
||||
'settings.section.diagnostics': 'Диагностика',
|
||||
'settings.data.export.label': 'Экспортировать всё',
|
||||
'settings.data.export.hint':
|
||||
'Сохрани резервную копию упражнений, истории, челленджей и настроек в JSON-файл.',
|
||||
@@ -238,6 +239,20 @@ export const ru: Dict = {
|
||||
'Все текущие упражнения, история и настройки будут заменены содержимым файла. Продолжить?',
|
||||
'settings.data.import.ok': 'Восстановлено',
|
||||
'settings.data.import.err': 'Файл не подошёл — это не наша резервная копия?',
|
||||
'settings.diagnostics.app.label': 'Приложение',
|
||||
'settings.diagnostics.data.label': 'Данные',
|
||||
'settings.diagnostics.data.legend': 'упр/еда/чел/ист',
|
||||
'settings.diagnostics.gsi.label': 'Dota GSI',
|
||||
'settings.diagnostics.hint':
|
||||
'Технический снимок без токенов: версии, пути, статусы и счетчики.',
|
||||
'settings.diagnostics.loading': 'Загружаем…',
|
||||
'settings.diagnostics.err': 'Не удалось собрать диагностику',
|
||||
'settings.diagnostics.refresh': 'Обновить диагностику',
|
||||
'settings.diagnostics.copy.btn': 'Копировать',
|
||||
'settings.diagnostics.copy.ok': 'Диагностика скопирована',
|
||||
'settings.diagnostics.logs.btn': 'Логи',
|
||||
'settings.diagnostics.logs.ok': 'Папка логов открыта',
|
||||
'settings.diagnostics.logs.err': 'Не удалось открыть папку логов',
|
||||
'settings.section.about': 'О приложении',
|
||||
'settings.version.label': 'Версия',
|
||||
'settings.version.hint': 'Текущая установленная версия приложения.',
|
||||
@@ -623,6 +638,7 @@ export const en: Dict = {
|
||||
'settings.section.language': 'Language',
|
||||
'settings.section.updates': 'Updates',
|
||||
'settings.section.data': 'Data',
|
||||
'settings.section.diagnostics': 'Diagnostics',
|
||||
'settings.data.export.label': 'Export everything',
|
||||
'settings.data.export.hint':
|
||||
'Save a backup of exercises, history, challenges and settings to a JSON file.',
|
||||
@@ -637,6 +653,20 @@ export const en: Dict = {
|
||||
'All current exercises, history and settings will be replaced with the file contents. Continue?',
|
||||
'settings.data.import.ok': 'Restored',
|
||||
'settings.data.import.err': "Couldn't read the file — not our backup?",
|
||||
'settings.diagnostics.app.label': 'Application',
|
||||
'settings.diagnostics.data.label': 'Data',
|
||||
'settings.diagnostics.data.legend': 'ex/meal/ch/hist',
|
||||
'settings.diagnostics.gsi.label': 'Dota GSI',
|
||||
'settings.diagnostics.hint':
|
||||
'Technical snapshot without tokens: versions, paths, statuses and counts.',
|
||||
'settings.diagnostics.loading': 'Loading…',
|
||||
'settings.diagnostics.err': 'Could not collect diagnostics',
|
||||
'settings.diagnostics.refresh': 'Refresh diagnostics',
|
||||
'settings.diagnostics.copy.btn': 'Copy',
|
||||
'settings.diagnostics.copy.ok': 'Diagnostics copied',
|
||||
'settings.diagnostics.logs.btn': 'Logs',
|
||||
'settings.diagnostics.logs.ok': 'Logs folder opened',
|
||||
'settings.diagnostics.logs.err': 'Could not open logs folder',
|
||||
'settings.section.about': 'About',
|
||||
'settings.version.label': 'Version',
|
||||
'settings.version.hint': 'Currently installed app version.',
|
||||
|
||||
32
src/renderer/src/lib/reporting.ts
Normal file
32
src/renderer/src/lib/reporting.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
function errorFields(err: unknown): { message: string; stack?: string } {
|
||||
if (err instanceof Error) {
|
||||
return {
|
||||
message: err.message || err.name,
|
||||
stack: err.stack
|
||||
}
|
||||
}
|
||||
if (typeof err === 'string') return { message: err }
|
||||
try {
|
||||
return { message: JSON.stringify(err) }
|
||||
} catch {
|
||||
return { message: String(err) }
|
||||
}
|
||||
}
|
||||
|
||||
function report(source: string, err: unknown): void {
|
||||
const { message, stack } = errorFields(err)
|
||||
if (!message) return
|
||||
void window.api
|
||||
?.reportRendererError?.({ source, message, stack })
|
||||
.catch(() => undefined)
|
||||
}
|
||||
|
||||
export function installRendererErrorReporting(): void {
|
||||
window.addEventListener('error', (event) => {
|
||||
report('window.error', event.error ?? event.message)
|
||||
})
|
||||
|
||||
window.addEventListener('unhandledrejection', (event) => {
|
||||
report('window.unhandledrejection', event.reason)
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,9 @@ import './styles/globals.css'
|
||||
import App from './App'
|
||||
import ReminderApp from './ReminderApp'
|
||||
import { ThemeProvider } from './providers/ThemeProvider'
|
||||
import { installRendererErrorReporting } from './lib/reporting'
|
||||
|
||||
installRendererErrorReporting()
|
||||
|
||||
const params = new URLSearchParams(window.location.search)
|
||||
const which = params.get('window') ?? 'main'
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { Copy, FolderOpen, RefreshCw } from 'lucide-react'
|
||||
import { useAppStore } from '../store/appStore'
|
||||
import { Switch } from '../components/ui/Switch'
|
||||
import { Button } from '../components/ui/Button'
|
||||
import { Card, Row, SectionHeader } from '../components/ui/Card'
|
||||
import { UpdaterCard } from '../components/UpdaterCard'
|
||||
import { WhatsNewModal } from '../components/WhatsNewModal'
|
||||
@@ -8,14 +10,16 @@ import { ConfirmModal } from '../components/ui/ConfirmModal'
|
||||
import { Skeleton } from '../components/ui/Skeleton'
|
||||
import { Spinner } from '../components/ui/Spinner'
|
||||
import { RELEASE_NOTES } from '@shared/release-notes'
|
||||
import { useT } from '../i18n'
|
||||
import { translate, useT } from '../i18n'
|
||||
import type {
|
||||
DiagnosticsInfo,
|
||||
Language,
|
||||
NotificationMode,
|
||||
QuietHours,
|
||||
Settings as SettingsType,
|
||||
Theme
|
||||
} from '@shared/types'
|
||||
import { parseHHMM } from '@shared/types'
|
||||
|
||||
export default function SettingsPage(): JSX.Element {
|
||||
const settings = useAppStore((s) => s.state?.settings)
|
||||
@@ -192,6 +196,11 @@ export default function SettingsPage(): JSX.Element {
|
||||
<DataCard />
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
<SectionHeader title={t('settings.section.diagnostics')} />
|
||||
<DiagnosticsCard />
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
<SectionHeader title={t('settings.section.about')} />
|
||||
<AboutCard />
|
||||
@@ -201,6 +210,153 @@ export default function SettingsPage(): JSX.Element {
|
||||
)
|
||||
}
|
||||
|
||||
function DiagnosticsCard(): JSX.Element {
|
||||
const { t, lang } = useT()
|
||||
const [info, setInfo] = useState<DiagnosticsInfo | null>(null)
|
||||
const [busy, setBusy] = useState<'refresh' | 'copy' | 'logs' | null>(null)
|
||||
const [toast, setToast] = useState<string | null>(null)
|
||||
|
||||
const refresh = useCallback(async (): Promise<void> => {
|
||||
setBusy('refresh')
|
||||
try {
|
||||
setInfo(await window.api.getDiagnostics())
|
||||
} catch {
|
||||
setToast(translate(lang, 'settings.diagnostics.err'))
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}, [lang])
|
||||
|
||||
useEffect(() => {
|
||||
void refresh()
|
||||
}, [refresh])
|
||||
|
||||
useEffect(() => {
|
||||
if (!toast) return
|
||||
const id = setTimeout(() => setToast(null), 4000)
|
||||
return () => clearTimeout(id)
|
||||
}, [toast])
|
||||
|
||||
async function copy(): Promise<void> {
|
||||
setBusy('copy')
|
||||
try {
|
||||
setInfo(await window.api.copyDiagnostics())
|
||||
setToast(t('settings.diagnostics.copy.ok'))
|
||||
} catch {
|
||||
setToast(t('settings.diagnostics.err'))
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function openLogs(): Promise<void> {
|
||||
setBusy('logs')
|
||||
try {
|
||||
const r = await window.api.openLogsFolder()
|
||||
setToast(
|
||||
r.ok
|
||||
? t('settings.diagnostics.logs.ok')
|
||||
: t('settings.diagnostics.logs.err')
|
||||
)
|
||||
} catch {
|
||||
setToast(t('settings.diagnostics.logs.err'))
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
const appLine = info
|
||||
? `v${info.app.version} · Electron ${info.runtime.electron}`
|
||||
: t('settings.diagnostics.loading')
|
||||
const dataLine = info
|
||||
? `${info.store.exercises}/${info.store.meals}/${info.store.challenges}/${info.store.history}`
|
||||
: '—'
|
||||
const gsiLine = info
|
||||
? `${info.gsi.running ? 'live' : 'off'} · ${info.gsi.baseUrl}`
|
||||
: '—'
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<Row>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[15px] font-semibold leading-tight">
|
||||
{t('settings.diagnostics.app.label')}
|
||||
</div>
|
||||
<div className="text-[13px] text-text/65 mt-1 leading-snug">
|
||||
{appLine}
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="plain"
|
||||
onClick={refresh}
|
||||
disabled={busy !== null}
|
||||
title={t('settings.diagnostics.refresh')}
|
||||
aria-label={t('settings.diagnostics.refresh')}
|
||||
>
|
||||
<RefreshCw size={16} />
|
||||
</Button>
|
||||
</Row>
|
||||
<Row>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[15px] font-semibold leading-tight">
|
||||
{t('settings.diagnostics.data.label')}
|
||||
</div>
|
||||
<div className="text-[13px] text-text/65 mt-1 leading-snug">
|
||||
{dataLine}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-[12px] text-text/50 font-semibold whitespace-nowrap">
|
||||
{t('settings.diagnostics.data.legend')}
|
||||
</div>
|
||||
</Row>
|
||||
<Row>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-[15px] font-semibold leading-tight">
|
||||
{t('settings.diagnostics.gsi.label')}
|
||||
</div>
|
||||
<div className="text-[13px] text-text/65 mt-1 leading-snug truncate">
|
||||
{gsiLine}
|
||||
</div>
|
||||
</div>
|
||||
</Row>
|
||||
<Row last className="flex-wrap justify-end">
|
||||
<div className="flex-1 min-w-[180px] text-[13px] text-text/65 leading-snug">
|
||||
{t('settings.diagnostics.hint')}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="tinted"
|
||||
onClick={openLogs}
|
||||
disabled={busy !== null}
|
||||
>
|
||||
<FolderOpen size={16} />
|
||||
{t('settings.diagnostics.logs.btn')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="filled"
|
||||
onClick={copy}
|
||||
disabled={busy !== null}
|
||||
>
|
||||
<Copy size={16} />
|
||||
{t('settings.diagnostics.copy.btn')}
|
||||
</Button>
|
||||
</div>
|
||||
</Row>
|
||||
{toast && (
|
||||
<div className="px-4 py-2.5 text-[13px] text-text/75 bg-accent/8 truncate font-medium">
|
||||
{toast}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function AboutCard(): JSX.Element {
|
||||
const { t } = useT()
|
||||
const [open, setOpen] = useState(false)
|
||||
@@ -399,11 +555,10 @@ function QuietTimesRow({
|
||||
}): JSX.Element {
|
||||
const { t } = useT()
|
||||
// Local mirror of from/to so typing doesn't fire an IPC + disk write per
|
||||
// keystroke. We commit on blur (or when validation passes during typing).
|
||||
// The HH:MM regex catches the moment the user has typed a full time.
|
||||
// keystroke. We commit on blur and only send values accepted by the shared
|
||||
// HH:MM parser.
|
||||
const [from, setFrom] = useState(qh.from)
|
||||
const [to, setTo] = useState(qh.to)
|
||||
const HHMM = /^\d{1,2}:\d{2}$/
|
||||
|
||||
// Sync from props when an external state change happens (lang switch,
|
||||
// pause toggle), but only if user isn't mid-edit.
|
||||
@@ -417,7 +572,7 @@ function QuietTimesRow({
|
||||
const commit = (next: { from?: string; to?: string }): void => {
|
||||
const f = next.from ?? from
|
||||
const tt = next.to ?? to
|
||||
if (!HHMM.test(f) || !HHMM.test(tt)) return
|
||||
if (parseHHMM(f) === null || parseHHMM(tt) === null) return
|
||||
if (f === qh.from && tt === qh.to) return
|
||||
onChange({ ...qh, from: f, to: tt })
|
||||
}
|
||||
|
||||
@@ -19,6 +19,10 @@ export const IPC = {
|
||||
getAccentColor: 'system:accentColor',
|
||||
getOsTheme: 'system:osTheme',
|
||||
getAppVersion: 'system:appVersion',
|
||||
getDiagnostics: 'system:diagnostics',
|
||||
openLogsFolder: 'system:openLogsFolder',
|
||||
copyDiagnostics: 'system:copyDiagnostics',
|
||||
reportRendererError: 'system:reportRendererError',
|
||||
|
||||
pauseAll: 'app:pauseAll',
|
||||
resumeAll: 'app:resumeAll',
|
||||
|
||||
@@ -300,7 +300,7 @@ export const DEFAULT_SETTINGS: Settings = {
|
||||
const HHMM_RE = /^(\d{1,2}):(\d{2})$/
|
||||
|
||||
/** Parse `HH:MM` into minutes-since-midnight, or `null` if malformed. */
|
||||
function parseHHMM(s: string): number | null {
|
||||
export function parseHHMM(s: string): number | null {
|
||||
const m = HHMM_RE.exec(s)
|
||||
if (!m) return null
|
||||
const h = Number(m[1])
|
||||
@@ -455,3 +455,45 @@ export type UpdaterStatus =
|
||||
}
|
||||
| { kind: 'downloaded'; version: string }
|
||||
| { kind: 'error'; message: string }
|
||||
|
||||
export type DiagnosticsInfo = {
|
||||
generatedAt: number
|
||||
app: {
|
||||
version: string
|
||||
isPackaged: boolean
|
||||
platform: string
|
||||
arch: string
|
||||
}
|
||||
runtime: {
|
||||
electron: string
|
||||
chrome: string
|
||||
node: string
|
||||
}
|
||||
paths: {
|
||||
userData: string
|
||||
store: string
|
||||
logs: string
|
||||
}
|
||||
store: {
|
||||
bytes: number | null
|
||||
exercises: number
|
||||
meals: number
|
||||
challenges: number
|
||||
history: number
|
||||
}
|
||||
updater: UpdaterStatus
|
||||
games: GameStatus[]
|
||||
gsi: {
|
||||
running: boolean
|
||||
port: number
|
||||
baseUrl: string
|
||||
}
|
||||
meetingActive: boolean
|
||||
}
|
||||
|
||||
export type RendererErrorReport = {
|
||||
message: string
|
||||
stack?: string
|
||||
componentStack?: string
|
||||
source?: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user