Compare commits
14 Commits
v0.5.0
...
36085f225f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36085f225f | ||
|
|
03ab4eebf5 | ||
|
|
a64f03b3cc | ||
|
|
e96ca06587 | ||
|
|
2503b27d42 | ||
|
|
ec6735f3f4 | ||
|
|
85897aa7dc | ||
|
|
f0dc5b2cc3 | ||
|
|
f3367e09de | ||
|
|
d6f94ee1c9 | ||
|
|
6160ece8d4 | ||
|
|
3f038e59e8 | ||
|
|
33e237948e | ||
|
|
f861af5db1 |
18
.editorconfig
Normal file
18
.editorconfig
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.{md,markdown}]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[*.{ps1,psm1,psd1}]
|
||||||
|
end_of_line = crlf
|
||||||
|
|
||||||
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
63
.eslintrc.cjs
Normal file
63
.eslintrc.cjs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* ESLint focuses on correctness, NOT style — Prettier owns formatting.
|
||||||
|
* Stylistic rules that fight Prettier are off.
|
||||||
|
*/
|
||||||
|
module.exports = {
|
||||||
|
root: true,
|
||||||
|
env: { browser: true, node: true, es2022: true },
|
||||||
|
parser: '@typescript-eslint/parser',
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 2022,
|
||||||
|
sourceType: 'module',
|
||||||
|
ecmaFeatures: { jsx: true }
|
||||||
|
},
|
||||||
|
plugins: ['@typescript-eslint', 'react', 'react-hooks'],
|
||||||
|
settings: { react: { version: 'detect' } },
|
||||||
|
extends: [
|
||||||
|
'eslint:recommended',
|
||||||
|
'plugin:@typescript-eslint/recommended',
|
||||||
|
'plugin:react/recommended',
|
||||||
|
'plugin:react-hooks/recommended'
|
||||||
|
],
|
||||||
|
rules: {
|
||||||
|
// React 17+ JSX transform — no need to import React in scope.
|
||||||
|
'react/react-in-jsx-scope': 'off',
|
||||||
|
'react/prop-types': 'off', // we use TS
|
||||||
|
|
||||||
|
// Hooks correctness — high signal.
|
||||||
|
'react-hooks/rules-of-hooks': 'error',
|
||||||
|
'react-hooks/exhaustive-deps': 'warn',
|
||||||
|
|
||||||
|
// TS — pragmatic, not strict.
|
||||||
|
'@typescript-eslint/no-explicit-any': 'warn',
|
||||||
|
'@typescript-eslint/no-unused-vars': [
|
||||||
|
'warn',
|
||||||
|
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' }
|
||||||
|
],
|
||||||
|
'@typescript-eslint/no-non-null-assertion': 'off',
|
||||||
|
|
||||||
|
// Vanilla — common bugs.
|
||||||
|
'no-console': ['warn', { allow: ['warn', 'error', 'info'] }],
|
||||||
|
'no-debugger': 'error',
|
||||||
|
'prefer-const': 'warn',
|
||||||
|
eqeqeq: ['error', 'always', { null: 'ignore' }]
|
||||||
|
},
|
||||||
|
ignorePatterns: [
|
||||||
|
'node_modules',
|
||||||
|
'out',
|
||||||
|
'release',
|
||||||
|
'dist',
|
||||||
|
'*.tsbuildinfo',
|
||||||
|
'src/preload/index.d.ts'
|
||||||
|
],
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
files: ['**/*.test.ts', '**/*.test.tsx'],
|
||||||
|
env: { node: true }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
files: ['*.config.js', '*.config.ts', 'electron.vite.config.ts'],
|
||||||
|
rules: { '@typescript-eslint/no-var-requires': 'off' }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
name: CI
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [main]
|
|
||||||
pull_request:
|
|
||||||
branches: [main]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
quality:
|
|
||||||
name: Typecheck + Tests
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: '20'
|
|
||||||
cache: 'npm'
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
|
|
||||||
- name: Typecheck (main + preload + shared)
|
|
||||||
run: npm run typecheck:node
|
|
||||||
|
|
||||||
- name: Typecheck (renderer)
|
|
||||||
run: npm run typecheck:web
|
|
||||||
|
|
||||||
- name: Run unit tests
|
|
||||||
run: npm run test:run
|
|
||||||
|
|
||||||
build:
|
|
||||||
name: Build (Windows)
|
|
||||||
runs-on: windows-latest
|
|
||||||
needs: quality
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: '20'
|
|
||||||
cache: 'npm'
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
|
|
||||||
- name: Build production bundle (no installer)
|
|
||||||
run: npm run build
|
|
||||||
|
|
||||||
- name: Smoke-test unpacked build
|
|
||||||
run: npm run dist:dir
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Upload unpacked artifact
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: exercise-reminder-unpacked
|
|
||||||
path: release/win-unpacked/
|
|
||||||
retention-days: 7
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
name: Release
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- 'v*.*.*'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
release:
|
|
||||||
name: Build installer + publish release
|
|
||||||
runs-on: windows-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup Node.js
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: '20'
|
|
||||||
cache: 'npm'
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
|
|
||||||
- name: Verify version matches tag
|
|
||||||
shell: pwsh
|
|
||||||
run: |
|
|
||||||
$tag = "${{ gitea.ref_name }}"
|
|
||||||
$expected = $tag.TrimStart('v')
|
|
||||||
$actual = (Get-Content package.json | ConvertFrom-Json).version
|
|
||||||
if ($expected -ne $actual) {
|
|
||||||
Write-Error "Tag $tag does not match package.json version $actual"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
Write-Host "Version match: $actual"
|
|
||||||
|
|
||||||
- name: Typecheck
|
|
||||||
run: npm run typecheck
|
|
||||||
|
|
||||||
- name: Run unit tests
|
|
||||||
run: npm run test:run
|
|
||||||
|
|
||||||
- name: Build NSIS installer
|
|
||||||
run: npm run dist
|
|
||||||
env:
|
|
||||||
# electron-builder uses this when --publish flag is set; we publish
|
|
||||||
# to a Gitea release manually below to avoid hard-coupling.
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Generate release notes from commits
|
|
||||||
id: notes
|
|
||||||
shell: pwsh
|
|
||||||
run: |
|
|
||||||
$tag = "${{ gitea.ref_name }}"
|
|
||||||
$prev = git describe --tags --abbrev=0 "$tag^" 2>$null
|
|
||||||
if ($prev) {
|
|
||||||
$log = git log --pretty=format:"- %s" "$prev..$tag"
|
|
||||||
} else {
|
|
||||||
$log = git log --pretty=format:"- %s" "$tag"
|
|
||||||
}
|
|
||||||
$notes = "### Изменения`n`n$log`n`n---`n`nУстановщик ниже — запустить и следовать мастеру. Если приложение уже стояло — обновится поверх с сохранением настроек."
|
|
||||||
$encoded = $notes -replace "`r?`n", "%0A"
|
|
||||||
"notes=$encoded" | Out-File -FilePath $env:GITEA_OUTPUT -Append
|
|
||||||
|
|
||||||
- name: Create Gitea release with artifacts
|
|
||||||
uses: akkuman/gitea-release-action@v1
|
|
||||||
with:
|
|
||||||
server_url: ${{ gitea.server_url }}
|
|
||||||
token: ${{ secrets.GITEA_TOKEN }}
|
|
||||||
name: 'Exercise Reminder ${{ gitea.ref_name }}'
|
|
||||||
body: ${{ steps.notes.outputs.notes }}
|
|
||||||
files: |
|
|
||||||
release/Exercise-Reminder-Setup-*.exe
|
|
||||||
release/Exercise-Reminder-Setup-*.exe.blockmap
|
|
||||||
release/latest.yml
|
|
||||||
8
.prettierignore
Normal file
8
.prettierignore
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
node_modules
|
||||||
|
out
|
||||||
|
release
|
||||||
|
dist
|
||||||
|
package-lock.json
|
||||||
|
*.tsbuildinfo
|
||||||
|
resources/**/*.ico
|
||||||
|
resources/**/*.png
|
||||||
18
.prettierrc.json
Normal file
18
.prettierrc.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "none",
|
||||||
|
"printWidth": 80,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"arrowParens": "always",
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"endOfLine": "lf",
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"files": ["*.md", "*.markdown"],
|
||||||
|
"options": {
|
||||||
|
"proseWrap": "preserve"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
210
CHANGELOG.md
Normal file
210
CHANGELOG.md
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
Все заметные изменения проекта документируются здесь.
|
||||||
|
Формат основан на [Keep a Changelog](https://keepachangelog.com/ru/1.1.0/),
|
||||||
|
проект следует [Semantic Versioning](https://semver.org/lang/ru/).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.5.3] — 2026-05-19
|
||||||
|
|
||||||
|
Полировка кастомного тайтлбара и размера окна.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Maximize/Restore.** Средняя кнопка тайтлбара (иконка квадрата)
|
||||||
|
раньше была «спрятать в трей» — выглядела как нативная Windows
|
||||||
|
maximize и сбивала с толку. Теперь это настоящий toggle на
|
||||||
|
весь экран: иконка свапается `Square` ↔ `Copy` в зависимости
|
||||||
|
от состояния, aria-label локализован.
|
||||||
|
- **Double-click по тайтлбару** тоже toggleMaximize — стандартный
|
||||||
|
Windows-жест.
|
||||||
|
- **CLAUDE.md** в корне — контекст проекта для будущих сессий
|
||||||
|
Claude Code (стек, архитектура, команды, тех. долг).
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Drag-зона тайтлбара.** Окно не двигалось, если хватать его
|
||||||
|
рядом с кнопками свернуть/закрыть. Класс `titlebar-nodrag` стоял
|
||||||
|
на обёртке кластера с `flex-1 basis-0`, поэтому пустое место
|
||||||
|
слева от иконок тоже было no-drag. Перенесли `no-drag` на сами
|
||||||
|
кнопки — теперь тащить можно отовсюду, кроме самих квадратиков.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- **Минимальный размер окна** 900×600 → 1100×700. Гарантирует
|
||||||
|
срабатывание Tailwind `lg:` (4 hero-stat в один ряд, heatmap
|
||||||
|
и сетка упражнений помещаются без горизонтального скролла).
|
||||||
|
|
||||||
|
## [0.5.2] — 2026-05-19
|
||||||
|
|
||||||
|
Большая внутренняя итерация: тройной независимый аудит (~220 находок),
|
||||||
|
закрыты топ-приоритеты. Тестов 53, ESLint и Prettier чистые, typecheck OK.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Prettier + ESLint + EditorConfig.** Конфиги, скрипты
|
||||||
|
`npm run format` / `format:check` / `lint`, CI-готовые правила. Вся
|
||||||
|
`src/` единообразно отформатирована.
|
||||||
|
- **Error Boundary** на двух уровнях: вокруг всего App и вокруг
|
||||||
|
роутов. Крах одной страницы (например, malformed history в
|
||||||
|
HistoryHeatmap) больше не блэнкит окно — показывается локализованный
|
||||||
|
fallback с кнопкой «Попробовать снова». Stack trace только в dev.
|
||||||
|
- **IPC validation layer** (`src/main/validate.ts`) — hand-rolled
|
||||||
|
схемы для всех renderer-supplied payload (intervalMinutes ∈ [1,1440],
|
||||||
|
reps ∈ [1,9999], multiplier ∈ [0,1000], string-cap 200 chars,
|
||||||
|
enum-валидация для theme/lang/notify-mode/stat, regex для HH:MM,
|
||||||
|
дедупликация quietHours.days). Compromised renderer больше не может
|
||||||
|
засунуть `reps: NaN` или `intervalMinutes: -1` в стор.
|
||||||
|
- **Schema migrations framework.** `__schemaVersion` в persisted-state,
|
||||||
|
`MIGRATIONS` map для будущих структурных правок.
|
||||||
|
- **Modal focus trap + focus restore + aria-labelledby.** Tab/Shift-Tab
|
||||||
|
больше не вываливаются на нижний слой; на закрытии фокус
|
||||||
|
возвращается на триггер.
|
||||||
|
- **Sidebar mobile drawer:** Esc закрывает, focus trap внутри, focus
|
||||||
|
restore на гамбургер, `role="dialog"` + `aria-modal`.
|
||||||
|
- **Tray menu i18n** — пункты меню следуют `settings.language`.
|
||||||
|
- **Bilingual heatmap.** Title, легенда, weekday-лейблы и tooltip
|
||||||
|
с плюрализацией (1 повтор / 2 повтора / 5 повторов) — всё через
|
||||||
|
i18n. 7 новых ключей `weekday.short.*`.
|
||||||
|
- CHANGELOG.md по формату Keep a Changelog.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Critical: данные больше не теряются на corrupt JSON.** Раньше
|
||||||
|
`catch → makeInitial()` молча затирал упражнения/историю. Теперь
|
||||||
|
файл уезжает в `app-state.json.corrupt-<timestamp>`.
|
||||||
|
- **Atomic write через `.tmp` + rename + retry** на EBUSY/EPERM
|
||||||
|
(антивирус, OneDrive). Раньше обрыв питания мог дать truncate.
|
||||||
|
- **HIGH security: GSI server теперь верифицирует auth.token**
|
||||||
|
через `timingSafeEqual` против per-install токена. Раньше
|
||||||
|
эндпоинт был полностью неаутентифицирован — любой локальный
|
||||||
|
процесс мог подделать match-end.
|
||||||
|
- **HIGH security: `shell.openExternal` allowlist** —
|
||||||
|
только `http/https/mailto`. Раньше `file:`/`javascript:`/`steam:`
|
||||||
|
уходили в OS handler.
|
||||||
|
- **HIGH security: dev IPC `simulateMatchEnd`** убран из production
|
||||||
|
билдов (gate на `!app.isPackaged` + `import.meta.env.MODE`).
|
||||||
|
- **HIGH security: GSI server reject `Origin`/`Sec-Fetch-Site`** —
|
||||||
|
блокирует CSRF от browser-вкладок. Body cap 256 KB (OOM-вектор
|
||||||
|
закрыт). Require `application/json`. Generic 400 без error-echo.
|
||||||
|
- **`isQuietAt` wrap-around + day filter.** С `22:00 → 07:00,
|
||||||
|
days=[Mon..Fri]` теперь правильно проверяется день *начала* окна
|
||||||
|
(старт Fri 22:00 → активно ночью Sat 02:00).
|
||||||
|
- **DST drift в `history.ts`.** Календарная арифметика (`setDate`)
|
||||||
|
вместо ms-арифметики — на границе DST дни больше не дублируются.
|
||||||
|
- **Scheduler:** `broadcastState()` после fire, защита от
|
||||||
|
двойной регистрации `powerMonitor` listeners.
|
||||||
|
- **Settings IPC chatter.** QuietTimesRow держит локальное состояние,
|
||||||
|
IPC летит только на `onBlur`. Раньше скрабинг времени давал ~5
|
||||||
|
IPC, каждый переписывал `app-state.json`.
|
||||||
|
- **Dashboard** «До следующего» показывает `—` при паузе вместо
|
||||||
|
обманчиво тикающего таймера.
|
||||||
|
- **HistoryHeatmap** percentile-bucketing (p25/p50/p85) вместо
|
||||||
|
относительной шкалы — outlier-день больше не схлопывает все
|
||||||
|
нормальные дни в самый слабый бакет.
|
||||||
|
- **ReminderApp:** Enter теперь корректно передаёт adjusted reps
|
||||||
|
(раньше всегда planned). `key={exercise.id+nextFireAt}` сбрасывает
|
||||||
|
степпер на новом fire. Степпер capped at 5× planned. Space не
|
||||||
|
работает когда фокус на кнопке. Esc закрывает MatchSummary.
|
||||||
|
- **`i18n.translate`** — split/join вместо regex (var-значения с
|
||||||
|
регулярными метасимволами теперь интерполируются буквально).
|
||||||
|
- **`icon.tsx`** lookup сужен до `ICON_CHOICES` — произвольное имя
|
||||||
|
больше не зарезолвится в `Lucide.default`.
|
||||||
|
- **UpdaterCard NaN guard** на download-progress (electron-updater
|
||||||
|
даёт undefined в ранних событиях).
|
||||||
|
- **`format.ts`** guard от NaN/Infinity в `formatCountdown`.
|
||||||
|
- **`updateExercise`/`updateChallenge`** стрипают `id` из patch —
|
||||||
|
рендер не может перезаписать identity.
|
||||||
|
- **clearHistory(undefined)** теперь no-op (нужен явный boundary).
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
- `.gitea/workflows/*.yml` — без runners оставляли queued runs.
|
||||||
|
Релизим через `release.ps1`. has_actions на репо выключен.
|
||||||
|
|
||||||
|
## [0.5.1] — 2026-05-18
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Auto-update архитектурно переписан.** Раньше `publish.url` включал
|
||||||
|
`${version}` и запекался в каждый билд — установленные копии видели
|
||||||
|
только свой собственный релиз. Введён фиксированный
|
||||||
|
`…/releases/download/update-channel`, который никогда не меняется.
|
||||||
|
- Hourly auto-проверка работает в silent-режиме: транзитные сетевые
|
||||||
|
ошибки (504, TLS drops) больше не показывают красный баннер
|
||||||
|
«Ошибка проверки». Только ручной клик «Проверить» поднимает ошибку.
|
||||||
|
- Boot-check ретраит 3 раза с backoff 30s/2m/5m.
|
||||||
|
- В `Up to date` показывается «проверено N мин назад».
|
||||||
|
- `release.ps1` теперь публикует в три-четыре места одной командой:
|
||||||
|
vX.Y.Z, update-channel, и переданные `-BridgeTags` для миграции
|
||||||
|
пользователей со старых версий.
|
||||||
|
- `upload-release-assets.ps1` ретраит curl до 4 раз с backoff на 504 /
|
||||||
|
TLS-сбрасывание; до ретрая проверяет, не залился ли файл на самом
|
||||||
|
деле (Gitea часто принимает body, но таймаутит ответ).
|
||||||
|
- Скрипты — ASCII-only (PS5.1 без BOM падает на em-dash).
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
- `.gitea/workflows/*.yml` — Gitea Actions без настроенных runners
|
||||||
|
оставляли queued runs в репозитории. Релизим через `release.ps1`.
|
||||||
|
|
||||||
|
## [0.5.0] — 2026-05-18
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **История + стрики.** Каждое выполненное упражнение пишется в
|
||||||
|
`app-state.json` (cap 10k записей, trim oldest 10% на overflow).
|
||||||
|
Heatmap-календарь 12 недель на Dashboard, ежедневный счётчик
|
||||||
|
«сделано сегодня», серия дней подряд (с grace-периодом за вчера).
|
||||||
|
- **Тихие часы.** Окно времени, в которое напоминания подавляются.
|
||||||
|
Поддержка wrap-around (22:00 → 08:00) и фильтра по дням недели.
|
||||||
|
- **Частичное выполнение.** Степпер `−/+` в окне напоминания: можно
|
||||||
|
отметить «сделал 5 из 10», в историю запишется честное число.
|
||||||
|
- README.md на русском — описание, фичи, установка, dev-команды,
|
||||||
|
архитектура, стек.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `markDone(id, actualReps?)` принимает фактическое число повторений.
|
||||||
|
|
||||||
|
### Tests
|
||||||
|
|
||||||
|
- `+18` тестов (5 для тихих часов, 13 для истории/стриков). Всего 51.
|
||||||
|
|
||||||
|
## [0.4.0] — 2026-05-17
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Английская локализация.** Самописная i18n: плоский словарь
|
||||||
|
~200 ключей × 2 языка + хук `useT()` + плюрализация (CLDR rules
|
||||||
|
для RU: one/few/many).
|
||||||
|
- Селектор языка в Settings, переключение мгновенное.
|
||||||
|
|
||||||
|
## [0.3.x] — 2026-05-17
|
||||||
|
|
||||||
|
Серия мелких релизов с дизайн-итерациями (Apple iOS / macOS aesthetic):
|
||||||
|
шрифты Plus Jakarta Sans + Bricolage Grotesque, светлая/тёмная/системная
|
||||||
|
тема, vibrancy sidebar, iOS-grouped lists, spring-анимации.
|
||||||
|
|
||||||
|
## [0.2.0] — 2026-05-16
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Dota 2 Game State Integration: локальный HTTP-сервер парсит callbacks
|
||||||
|
от Steam, после Победа/Поражение показывает «причитающиеся»
|
||||||
|
повторения (например `10 смертей × 3 = 30 приседаний`).
|
||||||
|
|
||||||
|
## [0.1.x] — 2026-05-15 .. 2026-05-16
|
||||||
|
|
||||||
|
Первые публичные сборки: ядро напоминаний (упражнения, интервалы,
|
||||||
|
иконки), системный трей, автозапуск с Windows, native-уведомления,
|
||||||
|
NSIS-инсталлятор, auto-update через electron-updater.
|
||||||
|
|
||||||
|
[Unreleased]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/compare/v0.5.3...HEAD
|
||||||
|
[0.5.3]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.5.3
|
||||||
|
[0.5.2]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.5.2
|
||||||
|
[0.5.1]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.5.1
|
||||||
|
[0.5.0]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.5.0
|
||||||
|
[0.4.0]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.4.0
|
||||||
|
[0.2.0]: https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/tag/v0.2.0
|
||||||
168
CLAUDE.md
Normal file
168
CLAUDE.md
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
Контекст проекта для Claude Code. Читается при старте каждой сессии.
|
||||||
|
|
||||||
|
## TL;DR
|
||||||
|
|
||||||
|
**Laude / Exercise Reminder** — Windows desktop приложение на Electron 33, которое напоминает делать упражнения и опционально парсит статистику матчей Dota 2 (через GSI) в количество повторений. Текущая версия — **0.5.3**. Один разработчик (AnRil), один remote — self-hosted Gitea.
|
||||||
|
|
||||||
|
## Стек
|
||||||
|
|
||||||
|
- **Runtime**: Electron 33 (main + preload + renderer)
|
||||||
|
- **Build**: electron-vite 2 + Vite 5 + electron-builder 25 (NSIS, x64 only)
|
||||||
|
- **UI**: React 18 + TypeScript 5 + Tailwind 3 + framer-motion + react-router (HashRouter) + zustand 5
|
||||||
|
- **Auto-update**: electron-updater 6, generic provider, фиксированный канал
|
||||||
|
- **Тесты**: Vitest 4 (53 теста, все зелёные)
|
||||||
|
- **Lint/format**: ESLint 8 (flat-ish .eslintrc.cjs) + Prettier 3 + EditorConfig
|
||||||
|
- **Иконки**: lucide-react (whitelisted lookup через `ICON_CHOICES`)
|
||||||
|
- **Шрифты**: Plus Jakarta Sans, Bricolage Grotesque, JetBrains Mono (Google Fonts CDN)
|
||||||
|
|
||||||
|
## Архитектура (важное)
|
||||||
|
|
||||||
|
### Процессы
|
||||||
|
- **main** (`src/main/`) — Node, scheduler, GSI HTTP-сервер, IPC, окна, tray, updater, persistence
|
||||||
|
- **preload** (`src/preload/index.ts`) — contextBridge → `window.api`, dev-only методы вырезаны на проде (`import.meta.env.MODE !== 'production'`)
|
||||||
|
- **renderer** (`src/renderer/src/`) — React, zustand-store, страницы Dashboard/Games/Settings/About, ReminderApp в отдельном окне
|
||||||
|
|
||||||
|
### Persistence
|
||||||
|
- Единственный JSON-файл: `%APPDATA%\Exercise Reminder\app-state.json`
|
||||||
|
- **Атомарная запись**: tmp + rename + retry на EBUSY/EPERM (антивирус, OneDrive)
|
||||||
|
- **Не теряет данные**: corrupt JSON → quarantine в `app-state.json.corrupt-<ts>`, не silent wipe
|
||||||
|
- **Schema migrations**: `__schemaVersion` поле + `MIGRATIONS: Record<number, (s)=>s>` map в `src/main/store.ts`
|
||||||
|
- **Debounced writes**: pendingWrite с `.unref()`
|
||||||
|
|
||||||
|
### IPC
|
||||||
|
- Типизированные каналы — `src/shared/ipc.ts`
|
||||||
|
- **Validation layer** — `src/main/validate.ts` (hand-rolled, без zod):
|
||||||
|
- `intervalMinutes ∈ [1, 1440]`, `reps ∈ [1, 9999]`, `multiplier ∈ [0, 1000]`
|
||||||
|
- string cap 200 chars, enum-валидация для theme/lang/notify-mode/stat
|
||||||
|
- HH:MM regex для quietHours, dedup days
|
||||||
|
- Strip `id` из updateExercise/updateChallenge patch
|
||||||
|
- **Dev-only**: `dev:simulateMatchEnd` gated на `!app.isPackaged`
|
||||||
|
|
||||||
|
### Auto-update (КРИТИЧНО)
|
||||||
|
- **Фиксированный URL канала**: `…/releases/download/update-channel/latest.yml` — никогда не меняется
|
||||||
|
- **НЕ** `…/releases/download/v${version}/…` (старая схема ломалась: установленная копия видела только свой релиз)
|
||||||
|
- Hourly silent auto-check (транзитные сетевые ошибки не показывают красный баннер; только ручной клик показывает ошибку)
|
||||||
|
- Boot-check: 3 ретрая с backoff 30s/2m/5m
|
||||||
|
- `lastCheckedAt` → UI «проверено N мин назад»
|
||||||
|
- Релиз через `scripts/release.ps1` публикует одной командой в:
|
||||||
|
1. `vX.Y.Z` (постоянный архивный тег)
|
||||||
|
2. `update-channel` (rolling — клиенты проверяют отсюда)
|
||||||
|
3. Опциональные `-BridgeTags` для миграции старых пользователей
|
||||||
|
|
||||||
|
### Безопасность
|
||||||
|
- **GSI server** (`src/main/games/gsi-server.ts`): per-install token verify через `timingSafeEqual`, reject Origin/Sec-Fetch-Site (CSRF), 256KB body cap, require `application/json`, generic 400
|
||||||
|
- **shell.openExternal allowlist**: только `http:`/`https:`/`mailto:` (`src/main/windows.ts`)
|
||||||
|
- **will-navigate** блокирует non-file:// и non-dev URL
|
||||||
|
- **Modal focus trap** + focus restore, aria-labelledby
|
||||||
|
|
||||||
|
### Quiet hours
|
||||||
|
- `isQuietAt(time, settings)` в `src/shared/types.ts`
|
||||||
|
- Wrap-around (22:00 → 07:00) корректно — при wrap-active проверяется день *начала* окна
|
||||||
|
- Тесты в `src/shared/quiet-hours.test.ts`
|
||||||
|
|
||||||
|
### История / стрики
|
||||||
|
- `src/renderer/src/lib/history.ts` — DST-safe через `shiftDays()` (calendar `setDate`, не ms-арифметика)
|
||||||
|
- Cap 10k записей, trim oldest 10% на overflow
|
||||||
|
- HistoryHeatmap: percentile-based bucketing (p25/p50/p85), а не flat ratio (защищает от outlier-дней)
|
||||||
|
|
||||||
|
### i18n
|
||||||
|
- Самописная микро-система: `src/renderer/src/i18n/dict.ts` (плоский словарь ~200 ключей × 2 языка)
|
||||||
|
- Хук `useT()`, плюрализация CLDR rules для RU (one/few/many)
|
||||||
|
- Интерполяция через split/join (не regex — защита от regex-инъекций в значениях var)
|
||||||
|
- Tray menu тоже локализован (`TRAY_STRINGS` в `src/main/tray.ts`)
|
||||||
|
|
||||||
|
## Команды
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev # electron-vite dev
|
||||||
|
npm run typecheck # tsc по node + web
|
||||||
|
npm run test:run # vitest один раз
|
||||||
|
npm run lint # eslint --max-warnings 0
|
||||||
|
npm run format # prettier --write
|
||||||
|
npm run dist # сборка + NSIS installer → release/
|
||||||
|
|
||||||
|
# Релиз (всё в одном)
|
||||||
|
npm run release -- -Bump patch
|
||||||
|
# или -Bump minor / -Bump major / -Version 1.2.3
|
||||||
|
# опционально: -BridgeTags v0.4.0,v0.4.1
|
||||||
|
```
|
||||||
|
|
||||||
|
## Скрипты релиза
|
||||||
|
|
||||||
|
- `scripts/release.ps1` — bump → typecheck → test → build → tag → push → upload в Gitea (vX.Y.Z + update-channel + bridges)
|
||||||
|
- `scripts/upload-release-assets.ps1` — curl.exe с retry/backoff (15s/45s/2m/5m × 4) на 504/TLS, проверяет уже-залилось через list assets перед ретраем
|
||||||
|
- **PowerShell 5.1 gotchas**:
|
||||||
|
- Default reads CP1251 → файлы скриптов **ASCII-only**, без em-dash/кириллицы в коде
|
||||||
|
- `Set-Content -Encoding utf8` добавляет BOM → ломает PostCSS. Для UTF-8 без BOM использовать `[System.IO.File]::WriteAllText` + `new UTF8Encoding($false)`
|
||||||
|
- Никогда `-i` флаги (rebase -i, add -i) — нет interactive input
|
||||||
|
|
||||||
|
## Gitea remote
|
||||||
|
|
||||||
|
- URL: `https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude` (Punycode для `президент.рф`)
|
||||||
|
- User: `anril`
|
||||||
|
- Auth: см. `~/.claude/projects/.../memory/gitea_remote.md`
|
||||||
|
- **Actions выключены** (`has_actions: false`) — релизим через PowerShell, runners не настроены
|
||||||
|
- `.gitea/workflows/` пустая (раньше там лежали yml → queued runs копились)
|
||||||
|
|
||||||
|
## Файлы, которые часто правлю
|
||||||
|
|
||||||
|
| Файл | Что |
|
||||||
|
|---|---|
|
||||||
|
| `package.json` | version, publish.url, scripts, deps |
|
||||||
|
| `src/main/store.ts` | persistence, migrations, validation, atomic writes |
|
||||||
|
| `src/main/ipc.ts` | IPC handlers с валидацией |
|
||||||
|
| `src/main/scheduler.ts` | таймеры упражнений, powerMonitor |
|
||||||
|
| `src/main/games/dota2.ts` + `gsi-server.ts` | GSI приём матчей |
|
||||||
|
| `src/main/updater.ts` | auto-update logic, silent retries |
|
||||||
|
| `src/shared/types.ts` | shared типы, дефолты, isQuietAt |
|
||||||
|
| `src/shared/ipc.ts` | IPC channel types |
|
||||||
|
| `src/renderer/src/i18n/dict.ts` | словари |
|
||||||
|
| `src/renderer/src/pages/Dashboard.tsx` | главная |
|
||||||
|
| `src/renderer/src/ReminderApp.tsx` | окно напоминания |
|
||||||
|
|
||||||
|
## Тесты (53)
|
||||||
|
|
||||||
|
```
|
||||||
|
src/shared/types.test.ts (4)
|
||||||
|
src/shared/quiet-hours.test.ts (7)
|
||||||
|
src/renderer/src/lib/format.test.ts (8)
|
||||||
|
src/renderer/src/lib/history.test.ts (13)
|
||||||
|
src/main/games/vdf.test.ts (11)
|
||||||
|
src/renderer/src/i18n/i18n.test.ts (10)
|
||||||
|
```
|
||||||
|
|
||||||
|
Покрываются: helpers, история/стрики (DST), тихие часы (wrap+filter), VDF-парсер Steam, i18n с плюрализацией, дефолты.
|
||||||
|
|
||||||
|
## Технический долг (не для пользователя)
|
||||||
|
|
||||||
|
- `sandbox: true` на BrowserWindow — нужен smoke-тест preload в sandbox-режиме
|
||||||
|
- Self-host Google Fonts (сейчас внешняя CSP-зависимость)
|
||||||
|
- ReminderApp race: первое напоминание может прийти без озвучки до загрузки settings
|
||||||
|
- Мажорные апдейты (React 18→19, Electron 33→42, Tailwind 3→4) — каждый ломающий, отдельная итерация
|
||||||
|
- Code-signing NSIS — ~$300/год, уберёт SmartScreen warning
|
||||||
|
- Скриншоты в README (есть TODO в самом README)
|
||||||
|
|
||||||
|
## Стиль кода
|
||||||
|
|
||||||
|
- Prettier: semi:false, singleQuote, trailingComma:none, printWidth:80
|
||||||
|
- ESLint: eslint:recommended + ts + react + react-hooks (без style rules — это Prettier)
|
||||||
|
- TypeScript strict, никакого `any` в новом коде
|
||||||
|
- Комментарии на русском там, где объясняют **почему**, не **что**
|
||||||
|
- Коммиты на русском, формат `тип(scope): кратко` (feat/fix/docs/refactor/test/chore)
|
||||||
|
- Co-Authored-By футер в коммитах от Claude
|
||||||
|
|
||||||
|
## Управление контекстом
|
||||||
|
|
||||||
|
- **Ужимать контекст при достижении 250k токенов** — вызывать `/compact` (или эквивалент) когда суммарный контекст подходит к 250 000 токенов. Не дожидаться authentic переполнения и автоматического сжатия от рантайма — сделать это контролируемо, чтобы важный контекст (открытые правки, недокоммиченные решения, текущая ветка задачи) попал в summary, а не выпал.
|
||||||
|
|
||||||
|
## Чего НЕ делать
|
||||||
|
|
||||||
|
- Не пушить в `update-channel` руками — только через `release.ps1`
|
||||||
|
- Не добавлять `.gitea/workflows/*.yml` — has_actions выключен, runs зависнут
|
||||||
|
- Не использовать regex в i18n-интерполяции — только split/join
|
||||||
|
- Не silent wipe corrupt JSON — quarantine с timestamp
|
||||||
|
- Не возвращать ms-арифметику в history.ts — DST сломается
|
||||||
|
- Не убирать validation layer из IPC — compromised renderer может засунуть NaN/негативы
|
||||||
|
- Не амендить коммиты без явной просьбы пользователя
|
||||||
14
README.md
14
README.md
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Windows desktop приложение, которое напоминает делать упражнения во время работы за компьютером. Опционально подключается к Dota 2 и после каждого матча превращает статистику (смерти, убийства, ассисты) в количество повторений.
|
Windows desktop приложение, которое напоминает делать упражнения во время работы за компьютером. Опционально подключается к Dota 2 и после каждого матча превращает статистику (смерти, убийства, ассисты) в количество повторений.
|
||||||
|
|
||||||
[](https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/latest)
|
[](https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/latest)
|
||||||
[]()
|
[]()
|
||||||
[]()
|
[]()
|
||||||
|
|
||||||
## Что внутри
|
## Что внутри
|
||||||
@@ -15,7 +15,7 @@ Windows desktop приложение, которое напоминает дел
|
|||||||
- **Игровая интеграция (Dota 2)** — Game State Integration читает статистику матча, после Победа/Поражение показывает экран с «причитающимися» повторениями (например `10 смертей × 3 = 30 приседаний`).
|
- **Игровая интеграция (Dota 2)** — Game State Integration читает статистику матча, после Победа/Поражение показывает экран с «причитающимися» повторениями (например `10 смертей × 3 = 30 приседаний`).
|
||||||
- **Apple-style интерфейс** — Plus Jakarta Sans + Bricolage Grotesque, iOS-палитра, vibrancy sidebar, spring-анимации, светлая/тёмная/системная тема.
|
- **Apple-style интерфейс** — Plus Jakarta Sans + Bricolage Grotesque, iOS-палитра, vibrancy sidebar, spring-анимации, светлая/тёмная/системная тема.
|
||||||
- **Два языка** — русский и английский, переключение мгновенное.
|
- **Два языка** — русский и английский, переключение мгновенное.
|
||||||
- **Auto-update** — приложение само скачивает новые версии из Gitea release (проверка каждый час).
|
- **Auto-update** — приложение само скачивает новые версии из фиксированного `update-channel` (проверка каждый час, силент-ретрай при сетевых сбоях).
|
||||||
|
|
||||||
## Скриншоты
|
## Скриншоты
|
||||||
|
|
||||||
@@ -67,14 +67,16 @@ npm run release -- -Bump patch # bump версии + tag + push + upload в G
|
|||||||
|
|
||||||
```
|
```
|
||||||
src/shared/types.test.ts (4)
|
src/shared/types.test.ts (4)
|
||||||
|
src/shared/quiet-hours.test.ts (5)
|
||||||
src/renderer/src/lib/format.test.ts (8)
|
src/renderer/src/lib/format.test.ts (8)
|
||||||
|
src/renderer/src/lib/history.test.ts (13)
|
||||||
src/main/games/vdf.test.ts (11)
|
src/main/games/vdf.test.ts (11)
|
||||||
src/renderer/src/i18n/i18n.test.ts (10)
|
src/renderer/src/i18n/i18n.test.ts (10)
|
||||||
─────────────────────────────────────
|
─────────────────────────────────────────
|
||||||
33 ✓
|
51 ✓
|
||||||
```
|
```
|
||||||
|
|
||||||
Покрытие: чистые helpers (форматирование, парсер VDF для Steam-конфигов), i18n с плюрализацией для RU/EN, дефолты shared-типов.
|
Покрытие: чистые helpers (форматирование, история/стрики, тихие часы, парсер VDF для Steam-конфигов), i18n с плюрализацией для RU/EN, дефолты shared-типов.
|
||||||
|
|
||||||
## Лицензия
|
## Лицензия
|
||||||
|
|
||||||
|
|||||||
208
RELEASING.md
208
RELEASING.md
@@ -1,146 +1,142 @@
|
|||||||
# Релиз и автообновления
|
# Релиз и автообновления
|
||||||
|
|
||||||
Документ описывает три способа выпустить новую версию. Все опираются на
|
Документ описывает, как выпускать новые версии и как устроена система
|
||||||
один и тот же артефакт — NSIS-инсталлятор `Exercise-Reminder-Setup-X.Y.Z.exe`,
|
авто-обновлений.
|
||||||
который сам решает: устанавливать заново или обновлять существующую копию.
|
|
||||||
|
|
||||||
## TL;DR
|
## TL;DR
|
||||||
|
|
||||||
```pwsh
|
```pwsh
|
||||||
$env:GITEA_TOKEN = '<token из Gitea Settings → Applications>'
|
$env:GITEA_TOKEN = '<token из Gitea Settings → Applications>'
|
||||||
npm run release -- -Bump patch # 0.2.0 → 0.2.1
|
npm run release -- -Bump patch # 0.5.1 → 0.5.2
|
||||||
# или
|
npm run release -- -Bump minor -BridgeTags v0.5.0 # 0.5.x → 0.6.0 + bridge
|
||||||
npm run release -- -Version 0.3.0
|
npm run release -- -Version 1.0.0
|
||||||
```
|
```
|
||||||
|
|
||||||
Скрипт сделает всё сам: бамп версии, коммит, тег, push, тесты, сборка
|
Скрипт делает всё сам: бамп версии, коммит, тег, push, тесты, сборка
|
||||||
инсталлятора, создание Gitea release с заметками из коммитов, загрузка
|
инсталлятора, загрузка в Gitea releases.
|
||||||
артефактов.
|
|
||||||
|
|
||||||
После публикации релиза установленные у пользователей копии в течение
|
## Архитектура auto-update
|
||||||
~6 часов проверят `latest.yml` на Gitea и предложат обновление через UI.
|
|
||||||
|
|
||||||
---
|
### Где лежат артефакты
|
||||||
|
|
||||||
## Как работает auto-update
|
Каждый выпуск публикует три файла:
|
||||||
|
|
||||||
1. На каждом релизе вместе с `.exe` публикуется `latest.yml` —
|
```
|
||||||
манифест с версией, размером, sha512 хешем.
|
Exercise-Reminder-Setup-X.Y.Z.exe # NSIS-инсталлятор (~80 MB)
|
||||||
2. Приложение (через `electron-updater`) каждые 6 часов делает HTTP
|
Exercise-Reminder-Setup-X.Y.Z.exe.blockmap # для differential update (~90 KB)
|
||||||
GET на `<gitea>/AnRil/laude/releases/download/v<current>/latest.yml`.
|
latest.yml # манифест: версия + хеш + размер
|
||||||
3. Если версия в манифесте выше текущей — статус становится
|
```
|
||||||
`available`, в Settings → Обновления появляется кнопка «Скачать».
|
|
||||||
4. После скачивания — статус `downloaded`, кнопка «Перезапустить».
|
|
||||||
5. При перезапуске NSIS установщик из дельты или полный накатывается
|
|
||||||
поверх существующей инсталляции. Данные в `%APPDATA%\Exercise Reminder\`
|
|
||||||
сохраняются.
|
|
||||||
|
|
||||||
**Важно:** репозиторий `laude` приватный. Чтобы auto-update работал на
|
И они одновременно публикуются в **три-четыре места** на Gitea:
|
||||||
машинах конечных пользователей, либо:
|
|
||||||
- сделать репозиторий публичным, либо
|
|
||||||
- сделать публичными только релизы (Gitea: Release Settings),
|
|
||||||
- либо подписывать запросы токеном (нужен код в `updater.ts`,
|
|
||||||
использующий `autoUpdater.requestHeaders`).
|
|
||||||
|
|
||||||
## Способ 1 — скрипт релиза (рекомендованный сейчас)
|
| Release tag | Назначение |
|
||||||
|
| ----------------- | ------------------------------------------------------------ |
|
||||||
|
| `vX.Y.Z` | Архив + changelog для людей |
|
||||||
|
| `update-channel` | **Фиксированный URL для auto-updater** (никогда не меняется) |
|
||||||
|
| `vN.M.K` (bridge) | Мост: чтобы клиенты на старых версиях нашли обновление |
|
||||||
|
|
||||||
Самый прямой путь, не зависит от Gitea Actions runners.
|
### Что приложение запекает в бинарник
|
||||||
|
|
||||||
|
В `package.json` → `build.publish.url`:
|
||||||
|
|
||||||
|
```
|
||||||
|
https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/download/update-channel
|
||||||
|
```
|
||||||
|
|
||||||
|
Этот URL **никогда не меняется**. Все версии (и сегодняшние, и будущие)
|
||||||
|
проверяют один и тот же `update-channel/latest.yml`.
|
||||||
|
|
||||||
|
### Цикл проверки
|
||||||
|
|
||||||
|
1. При запуске и каждый час `electron-updater` делает GET на
|
||||||
|
`…/update-channel/latest.yml`.
|
||||||
|
2. Если в манифесте версия выше текущей — Settings → Обновления показывает
|
||||||
|
«Доступно vX.Y.Z». По клику качается `.exe` (или differential по
|
||||||
|
`.blockmap`).
|
||||||
|
3. После скачивания — кнопка «Перезапустить». NSIS обновляет инсталляцию
|
||||||
|
поверх с сохранением `%APPDATA%\Exercise Reminder\app-state.json`.
|
||||||
|
|
||||||
|
### Bridge-теги (миграционный период)
|
||||||
|
|
||||||
|
До v0.5.1 publish.url был `…/releases/download/v${version}` — у каждой
|
||||||
|
версии свой адрес. Установленные ранее копии запекли старый URL.
|
||||||
|
Чтобы они нашли обновление, новые артефакты также заливаются в их
|
||||||
|
старые releases (флаг `-BridgeTags`).
|
||||||
|
|
||||||
|
После того как все клиенты получили v0.5.1 или выше, аргумент
|
||||||
|
`-BridgeTags` можно перестать использовать — все будущие версии берут
|
||||||
|
обновления через `update-channel`.
|
||||||
|
|
||||||
|
### Поведение при ошибках
|
||||||
|
|
||||||
|
- Hourly auto-check работает в **silent**-режиме: сетевые ошибки
|
||||||
|
логируются в консоль, но **не** показываются как красный баннер.
|
||||||
|
Следующая попытка через час.
|
||||||
|
- Boot-check ретраит 3 раза с backoff 30s/2m/5m перед тем как сдаться.
|
||||||
|
- Только ручной клик «Проверить обновления» показывает ошибку, если
|
||||||
|
она есть.
|
||||||
|
|
||||||
|
## Команды
|
||||||
|
|
||||||
```pwsh
|
```pwsh
|
||||||
# Один раз — получить токен в Gitea (Settings → Applications)
|
# Один раз — токен из Gitea Settings -> Applications (write:repository).
|
||||||
# и сохранить в переменную окружения. Право — write:repository.
|
|
||||||
[Environment]::SetEnvironmentVariable('GITEA_TOKEN', '<token>', 'User')
|
[Environment]::SetEnvironmentVariable('GITEA_TOKEN', '<token>', 'User')
|
||||||
|
|
||||||
# Релиз
|
# Релиз
|
||||||
npm run release -- -Bump patch # patch (0.2.0 → 0.2.1)
|
npm run release -- -Bump patch # patch (0.5.1 -> 0.5.2)
|
||||||
npm run release -- -Bump minor # minor (0.2.0 → 0.3.0)
|
npm run release -- -Bump minor # minor (0.5.x -> 0.6.0)
|
||||||
npm run release -- -Bump major # major (0.2.0 → 1.0.0)
|
npm run release -- -Bump major # major
|
||||||
npm run release -- -Version 1.2.3 # точная версия
|
npm run release -- -Version 1.2.3 # точная версия
|
||||||
npm run release -- -DryRun # посмотреть план без действий
|
npm run release -- -BridgeTags v0.4.0,v0.5.0 # дополнительные мосты
|
||||||
|
npm run release -- -DryRun # план без действий
|
||||||
```
|
```
|
||||||
|
|
||||||
Что делает скрипт:
|
Что делает `release.ps1`:
|
||||||
1. Проверяет что нет незакоммиченных изменений
|
|
||||||
2. Бампит версию в `package.json`, коммитит
|
|
||||||
3. Прогоняет `npm run typecheck` и `npm run test:run`
|
|
||||||
4. Собирает `npm run dist` (NSIS + блокмап + latest.yml)
|
|
||||||
5. Создаёт тег `vX.Y.Z`, пушит main и тег в origin
|
|
||||||
6. Через Gitea API создаёт release с заметками из git log
|
|
||||||
7. Загружает три файла как assets: `.exe`, `.exe.blockmap`, `latest.yml`
|
|
||||||
|
|
||||||
## Способ 2 — Gitea Actions (если есть runners)
|
1. Проверяет чистоту дерева.
|
||||||
|
2. Бампит `package.json`, коммитит как `chore(release): vX.Y.Z`.
|
||||||
Workflows лежат в `.gitea/workflows/`:
|
3. `npm run typecheck` + `npm run test:run`.
|
||||||
|
4. `npm run dist` → NSIS-инсталлятор + blockmap + latest.yml в `release/`.
|
||||||
- **`ci.yml`** — на push в main и на PR. Запускает typecheck +
|
5. `git tag vX.Y.Z` и push main + tag в origin.
|
||||||
unit-тесты + smoke-сборку (без NSIS). Кладёт распакованную сборку
|
6. Через `upload-release-assets.ps1` заливает артефакты в каждый тег
|
||||||
как artifact на 7 дней.
|
из списка: `vX.Y.Z`, `update-channel`, и все `-BridgeTags`.
|
||||||
- **`release.yml`** — на push тега `v*.*.*`. Сверяет тег с версией
|
7. Каждая заливка ретраит до 4 раз с backoff 15s/45s/2m/5m на 504.
|
||||||
в `package.json`, прогоняет тесты, собирает NSIS-инсталлятор,
|
|
||||||
создаёт Gitea release с заметками, загружает артефакты.
|
|
||||||
|
|
||||||
Чтобы release workflow работал — в репозитории нужен secret
|
|
||||||
`GITEA_TOKEN` (Gitea Repo Settings → Secrets). Этот же токен может быть
|
|
||||||
переиспользован из `Способа 1`.
|
|
||||||
|
|
||||||
Для запуска release workflow:
|
|
||||||
```bash
|
|
||||||
git tag v0.3.0
|
|
||||||
git push origin v0.3.0
|
|
||||||
```
|
|
||||||
|
|
||||||
## Способ 3 — руками
|
|
||||||
|
|
||||||
Если что-то сломалось в автоматизации:
|
|
||||||
|
|
||||||
```pwsh
|
|
||||||
npm run typecheck
|
|
||||||
npm run test:run
|
|
||||||
npm run dist
|
|
||||||
# В release/ появятся:
|
|
||||||
# Exercise-Reminder-Setup-X.Y.Z.exe
|
|
||||||
# Exercise-Reminder-Setup-X.Y.Z.exe.blockmap
|
|
||||||
# latest.yml
|
|
||||||
```
|
|
||||||
|
|
||||||
Затем в Gitea UI: Releases → Draft new release → загрузить три файла.
|
|
||||||
|
|
||||||
## Тестирование auto-update
|
## Тестирование auto-update
|
||||||
|
|
||||||
Удобный способ проверить, что цикл работает:
|
1. Установить какую-нибудь старую версию через `.exe` из её release.
|
||||||
|
2. Релизнуть свежую версию.
|
||||||
|
3. В установленной копии: Settings → Обновления → Проверить.
|
||||||
|
4. Должно показать «Доступна vX.Y.Z» с кнопкой «Скачать».
|
||||||
|
5. Скачать → Перезапустить → проверить версию.
|
||||||
|
|
||||||
1. Релизнуть `0.x.0` через `npm run release`.
|
Для `npm run dev` auto-updater отключён — статус сразу `unsupported`.
|
||||||
2. Установить полученный `.exe` на машину.
|
|
||||||
3. Релизнуть `0.x.1` (любой бамп).
|
|
||||||
4. На установленной копии открыть Settings → Обновления → Проверить.
|
|
||||||
Должно показать «Доступно обновление v0.x.1».
|
|
||||||
5. Скачать → Перезапустить → проверить версию в окне «О программе»
|
|
||||||
(или в Settings).
|
|
||||||
|
|
||||||
Для dev-режима (`npm run dev`) auto-updater отключён — статус сразу
|
|
||||||
становится `unsupported` с пояснением.
|
|
||||||
|
|
||||||
## Откат релиза
|
## Откат релиза
|
||||||
|
|
||||||
Если опубликовали плохой релиз:
|
|
||||||
|
|
||||||
1. Удалить release в Gitea UI (или через API).
|
1. Удалить release в Gitea UI (или через API).
|
||||||
2. Удалить тег: `git push origin :refs/tags/vX.Y.Z` и локально
|
2. `git push origin :refs/tags/vX.Y.Z` и `git tag -d vX.Y.Z`.
|
||||||
`git tag -d vX.Y.Z`.
|
3. `git revert <bump-hash>` (бамп уже запушен).
|
||||||
3. Откатить bump-коммит: `git revert <hash>` или `git reset --hard HEAD~1`
|
4. Если артефакты успели уехать в `update-channel` — перезалить туда
|
||||||
(если ещё не пушили дальше).
|
предыдущую версию: `pwsh scripts/upload-release-assets.ps1 -Tag update-channel -AssetVersion <previous>`.
|
||||||
4. Релизнуть тот же номер заново — auto-updater на клиентах увидит
|
|
||||||
тот же манифест и не предложит обновление (если sha512 совпадёт).
|
На практике лучше выпустить hotfix-патч `X.Y.Z+1`, чем откатывать.
|
||||||
Если содержание поменялось — увидит и предложит обновиться. На
|
|
||||||
практике лучше выпустить hotfix-патч `X.Y.Z+1`, чем переписывать
|
## Gitea Actions
|
||||||
существующий релиз.
|
|
||||||
|
Раньше в `.gitea/workflows/` лежали `ci.yml` и `release.yml`. Они
|
||||||
|
требуют Gitea Actions runners (отдельная служба, у нас не настроена),
|
||||||
|
поэтому каждая push-операция оставляла зависший workflow run в Actions
|
||||||
|
tab. Workflows удалены, has_actions на репозитории выключен,
|
||||||
|
Actions tab возвращает 404. Если когда-нибудь захочется CI — добавить
|
||||||
|
обратно `.gitea/workflows/*.yml` + поднять runners.
|
||||||
|
|
||||||
## Что попадает в установщик
|
## Что попадает в установщик
|
||||||
|
|
||||||
См. `build` секцию `package.json`:
|
См. `build.files` в `package.json`:
|
||||||
|
|
||||||
- `out/**/*` — собранный код (main + preload + renderer)
|
- `out/**/*` — собранный код (main + preload + renderer)
|
||||||
- `resources/**/*` — иконки
|
- `resources/**/*` — иконки
|
||||||
|
|
||||||
Никаких node_modules, исходников, тестов, README — `electron-builder`
|
Без `node_modules`, без исходников, без тестов — `electron-builder`
|
||||||
сам распаковывает и упаковывает только необходимое.
|
сам выбирает только необходимое.
|
||||||
|
|||||||
2708
package-lock.json
generated
2708
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "laude",
|
"name": "laude",
|
||||||
"version": "0.5.0",
|
"version": "0.5.3",
|
||||||
"description": "Exercise reminder — Windows desktop app",
|
"description": "Exercise reminder — Windows desktop app",
|
||||||
"main": "out/main/index.js",
|
"main": "out/main/index.js",
|
||||||
"author": "AnRil",
|
"author": "AnRil",
|
||||||
@@ -14,6 +14,9 @@
|
|||||||
"typecheck": "npm run typecheck:node && npm run typecheck:web",
|
"typecheck": "npm run typecheck:node && npm run typecheck:web",
|
||||||
"test": "vitest",
|
"test": "vitest",
|
||||||
"test:run": "vitest run",
|
"test:run": "vitest run",
|
||||||
|
"format": "prettier --write \"src/**/*.{ts,tsx,css}\" \"*.{json,md}\" \".github/**/*.yml\"",
|
||||||
|
"format:check": "prettier --check \"src/**/*.{ts,tsx,css}\" \"*.{json,md}\"",
|
||||||
|
"lint": "eslint src --ext .ts,.tsx --max-warnings 0",
|
||||||
"dist": "electron-vite build && electron-builder --win --x64",
|
"dist": "electron-vite build && electron-builder --win --x64",
|
||||||
"dist:dir": "electron-vite build && electron-builder --win --x64 --dir",
|
"dist:dir": "electron-vite build && electron-builder --win --x64 --dir",
|
||||||
"publish": "electron-vite build && electron-builder --win --x64 --publish always",
|
"publish": "electron-vite build && electron-builder --win --x64 --publish always",
|
||||||
@@ -33,12 +36,18 @@
|
|||||||
"@types/node": "^22.19.19",
|
"@types/node": "^22.19.19",
|
||||||
"@types/react": "^18.3.12",
|
"@types/react": "^18.3.12",
|
||||||
"@types/react-dom": "^18.3.1",
|
"@types/react-dom": "^18.3.1",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^8.15.0",
|
||||||
|
"@typescript-eslint/parser": "^8.15.0",
|
||||||
"@vitejs/plugin-react": "^4.3.3",
|
"@vitejs/plugin-react": "^4.3.3",
|
||||||
"autoprefixer": "^10.4.20",
|
"autoprefixer": "^10.4.20",
|
||||||
"electron": "^33.2.0",
|
"electron": "^33.2.0",
|
||||||
"electron-builder": "^25.1.8",
|
"electron-builder": "^25.1.8",
|
||||||
"electron-vite": "^2.3.0",
|
"electron-vite": "^2.3.0",
|
||||||
|
"eslint": "^8.57.1",
|
||||||
|
"eslint-plugin-react": "^7.37.2",
|
||||||
|
"eslint-plugin-react-hooks": "^5.0.0",
|
||||||
"postcss": "^8.4.49",
|
"postcss": "^8.4.49",
|
||||||
|
"prettier": "^3.4.1",
|
||||||
"tailwindcss": "^3.4.15",
|
"tailwindcss": "^3.4.15",
|
||||||
"typescript": "^5.6.3",
|
"typescript": "^5.6.3",
|
||||||
"vite": "^5.4.11",
|
"vite": "^5.4.11",
|
||||||
@@ -88,7 +97,7 @@
|
|||||||
},
|
},
|
||||||
"publish": {
|
"publish": {
|
||||||
"provider": "generic",
|
"provider": "generic",
|
||||||
"url": "https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/download/v${version}",
|
"url": "https://xn--90adajar8af4h.xn--p1ai/git/AnRil/laude/releases/download/update-channel",
|
||||||
"channel": "latest"
|
"channel": "latest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,67 +1,78 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Локальный релиз: бамп версии → коммит → тег → push → сборка → upload в Gitea release.
|
Локальный релиз: бамп версии -> коммит -> тег -> push -> сборка -> upload в Gitea.
|
||||||
|
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Один скрипт от и до. Если Gitea Actions не настроено, это рабочая альтернатива.
|
Single-command release flow.
|
||||||
|
|
||||||
|
Каждый релиз публикует артефакты в ТРИ места:
|
||||||
|
1. Тег vX.Y.Z (исторический архив + changelog)
|
||||||
|
2. Тег update-channel (фиксированный URL для auto-updater)
|
||||||
|
3. Bridge-теги, указанные в -BridgeTags (для миграции пользователей со
|
||||||
|
старых версий, у которых запечён старый publish.url).
|
||||||
|
|
||||||
|
После того как все пользователи получили версию с новым (фиксированным)
|
||||||
|
publish.url, аргумент -BridgeTags можно перестать указывать.
|
||||||
|
|
||||||
.PARAMETER Bump
|
.PARAMETER Bump
|
||||||
Какую часть semver инкрементировать: patch (по умолчанию), minor, major.
|
Какую часть semver инкрементировать: patch (по умолчанию), minor, major.
|
||||||
Альтернатива — указать -Version явно.
|
|
||||||
|
|
||||||
.PARAMETER Version
|
.PARAMETER Version
|
||||||
Точная версия (напр. "0.3.0"). Если задана, -Bump игнорируется.
|
Точная версия (напр. "0.5.1"). Если задана, -Bump игнорируется.
|
||||||
|
|
||||||
.PARAMETER SkipBuild
|
.PARAMETER SkipBuild
|
||||||
Пропустить сборку (если уже собрано вручную, .exe лежит в release/).
|
Пропустить сборку (если уже собрано вручную, .exe лежит в release/).
|
||||||
|
|
||||||
|
.PARAMETER BridgeTags
|
||||||
|
Список старых тегов, в которые нужно ТАКЖЕ перезалить новые артефакты,
|
||||||
|
чтобы пользователи на этих версиях нашли апдейт. Например: v0.4.0,v0.5.0.
|
||||||
|
|
||||||
.PARAMETER DryRun
|
.PARAMETER DryRun
|
||||||
Показать что произойдёт, но ничего не делать.
|
Показать что произойдёт, ничего не делая.
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
pwsh scripts/release.ps1 -Bump minor
|
pwsh scripts/release.ps1 -Bump patch
|
||||||
pwsh scripts/release.ps1 -Version 0.3.0
|
pwsh scripts/release.ps1 -Version 0.5.1 -BridgeTags v0.4.0,v0.5.0
|
||||||
pwsh scripts/release.ps1 -Bump patch -DryRun
|
|
||||||
|
|
||||||
.NOTES
|
.NOTES
|
||||||
Требует переменную окружения GITEA_TOKEN с правом write:repository
|
Требует GITEA_TOKEN с правом write:repository.
|
||||||
(создаётся в Gitea: Settings → Applications → Generate New Token).
|
Канал 'update-channel' должен существовать на Gitea (создаётся однократно).
|
||||||
#>
|
#>
|
||||||
param(
|
param(
|
||||||
[ValidateSet('patch', 'minor', 'major')]
|
[ValidateSet('patch', 'minor', 'major')]
|
||||||
[string]$Bump = 'patch',
|
[string]$Bump = 'patch',
|
||||||
[string]$Version,
|
[string]$Version,
|
||||||
[switch]$SkipBuild,
|
[switch]$SkipBuild,
|
||||||
|
[string[]]$BridgeTags = @(),
|
||||||
[switch]$DryRun
|
[switch]$DryRun
|
||||||
)
|
)
|
||||||
|
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
# --- Config ---------------------------------------------------------------
|
|
||||||
$repoOwner = 'AnRil'
|
$repoOwner = 'AnRil'
|
||||||
$repoName = 'laude'
|
$repoName = 'laude'
|
||||||
$giteaHost = 'xn--90adajar8af4h.xn--p1ai/git'
|
$giteaHost = 'xn--90adajar8af4h.xn--p1ai/git'
|
||||||
$apiBase = "https://$giteaHost/api/v1"
|
$channelTag = 'update-channel'
|
||||||
|
|
||||||
# --- Pre-flight checks ---------------------------------------------------
|
# --- Pre-flight ----------------------------------------------------------
|
||||||
$root = Resolve-Path (Join-Path $PSScriptRoot '..')
|
$root = Resolve-Path (Join-Path $PSScriptRoot '..')
|
||||||
Set-Location $root
|
Set-Location $root
|
||||||
|
|
||||||
if (-not $env:GITEA_TOKEN -and -not $DryRun) {
|
if (-not $env:GITEA_TOKEN -and -not $DryRun) {
|
||||||
Write-Error 'GITEA_TOKEN не задан. Создай в Gitea Settings → Applications и export GITEA_TOKEN=...'
|
Write-Error 'GITEA_TOKEN not set.'
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
$status = git status --porcelain
|
$status = git status --porcelain
|
||||||
if ($status) {
|
if ($status) {
|
||||||
Write-Error "Есть незакоммиченные изменения. Сначала закоммить или stash."
|
Write-Error "Uncommitted changes. Commit or stash first."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
$branch = git rev-parse --abbrev-ref HEAD
|
$branch = git rev-parse --abbrev-ref HEAD
|
||||||
if ($branch -ne 'main') {
|
if ($branch -ne 'main') {
|
||||||
Write-Warning "Текущая ветка не main, а $branch. Продолжить? (Ctrl+C для отмены)"
|
Write-Warning "Branch is $branch, not main. Press Enter to continue or Ctrl+C to cancel."
|
||||||
Read-Host 'Press Enter'
|
Read-Host
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Compute next version ------------------------------------------------
|
# --- Compute next version ------------------------------------------------
|
||||||
@@ -83,109 +94,75 @@ if ($Version) {
|
|||||||
|
|
||||||
$tag = "v$next"
|
$tag = "v$next"
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "→ Release plan" -ForegroundColor Cyan
|
Write-Host "Release plan" -ForegroundColor Cyan
|
||||||
Write-Host " current : v$current"
|
Write-Host " current : v$current"
|
||||||
Write-Host " next : $tag"
|
Write-Host " next : $tag"
|
||||||
Write-Host " bump : $Bump"
|
Write-Host " publish into : $tag, $channelTag$(if ($BridgeTags) { ', ' + ($BridgeTags -join ', ') })"
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|
||||||
if ($DryRun) {
|
if ($DryRun) {
|
||||||
Write-Host '(dry run — exiting)' -ForegroundColor Yellow
|
Write-Host '(dry run - exiting)' -ForegroundColor Yellow
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Bump version in package.json ---------------------------------------
|
# --- Bump package.json --------------------------------------------------
|
||||||
Write-Host "→ Bumping package.json to $next…" -ForegroundColor Cyan
|
# IMPORTANT: read+write as UTF-8 WITHOUT BOM. PS5.1 defaults will (a) read the
|
||||||
$pkgJson = (Get-Content package.json -Raw) -replace "`"version`":\s*`"$current`"", "`"version`": `"$next`""
|
# file as CP1251 and mangle non-ASCII chars like em-dash, and (b) write back
|
||||||
Set-Content -Path package.json -Value $pkgJson -NoNewline -Encoding utf8
|
# with a BOM that breaks PostCSS / electron-builder reads of package.json.
|
||||||
|
Write-Host "Bumping package.json to $next..." -ForegroundColor Cyan
|
||||||
|
$pkgPath = Join-Path $root 'package.json'
|
||||||
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
||||||
|
$pkgJson = [System.IO.File]::ReadAllText($pkgPath, $utf8NoBom)
|
||||||
|
$pkgJson = $pkgJson -replace "`"version`":\s*`"$current`"", "`"version`": `"$next`""
|
||||||
|
[System.IO.File]::WriteAllText($pkgPath, $pkgJson, $utf8NoBom)
|
||||||
|
|
||||||
git add package.json
|
git add package.json
|
||||||
git commit -m "chore(release): $tag"
|
git commit -m "chore(release): $tag"
|
||||||
|
|
||||||
# --- Build (typecheck + tests + dist) ------------------------------------
|
# --- Quality gates ------------------------------------------------------
|
||||||
if (-not $SkipBuild) {
|
if (-not $SkipBuild) {
|
||||||
Write-Host "→ Running typecheck…" -ForegroundColor Cyan
|
Write-Host "Typecheck..." -ForegroundColor Cyan
|
||||||
npm run typecheck
|
npm run typecheck
|
||||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||||
|
|
||||||
Write-Host "→ Running tests…" -ForegroundColor Cyan
|
Write-Host "Tests..." -ForegroundColor Cyan
|
||||||
npm run test:run
|
npm run test:run
|
||||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||||
|
|
||||||
Write-Host "→ Building installer (npm run dist)…" -ForegroundColor Cyan
|
Write-Host "Building installer..." -ForegroundColor Cyan
|
||||||
npm run dist
|
npm run dist
|
||||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Verify artifacts exist ---------------------------------------------
|
# --- Verify artifacts ---------------------------------------------------
|
||||||
$installer = Join-Path 'release' "Exercise-Reminder-Setup-$next.exe"
|
$installer = Join-Path 'release' "Exercise-Reminder-Setup-$next.exe"
|
||||||
$blockmap = "$installer.blockmap"
|
$blockmap = "$installer.blockmap"
|
||||||
$manifest = Join-Path 'release' 'latest.yml'
|
$manifest = Join-Path 'release' 'latest.yml'
|
||||||
foreach ($f in @($installer, $blockmap, $manifest)) {
|
foreach ($f in @($installer, $blockmap, $manifest)) {
|
||||||
if (-not (Test-Path $f)) {
|
if (-not (Test-Path $f)) {
|
||||||
Write-Error "Не найден артефакт: $f"
|
Write-Error "Artifact missing: $f"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Tag + push ----------------------------------------------------------
|
# --- Tag + push ---------------------------------------------------------
|
||||||
Write-Host "→ Tagging $tag and pushing…" -ForegroundColor Cyan
|
Write-Host "Tagging $tag and pushing..." -ForegroundColor Cyan
|
||||||
git tag -a $tag -m "Release $tag"
|
git tag -a $tag -m "Release $tag"
|
||||||
git push origin main
|
git push origin main
|
||||||
git push origin $tag
|
git push origin $tag
|
||||||
|
|
||||||
# --- Create release via Gitea API ----------------------------------------
|
# --- Upload to all target releases --------------------------------------
|
||||||
Write-Host "→ Creating Gitea release $tag…" -ForegroundColor Cyan
|
$uploadScript = Join-Path $PSScriptRoot 'upload-release-assets.ps1'
|
||||||
$headers = @{
|
|
||||||
Authorization = "token $env:GITEA_TOKEN"
|
$targets = @($tag, $channelTag) + $BridgeTags
|
||||||
Accept = 'application/json'
|
foreach ($target in $targets) {
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host "==> Uploading $next artifacts into release '$target'" -ForegroundColor Cyan
|
||||||
|
& powershell -ExecutionPolicy Bypass -File $uploadScript -Tag $target -AssetVersion $next
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Error "Upload to '$target' failed (exit $LASTEXITCODE)"
|
||||||
|
exit $LASTEXITCODE
|
||||||
}
|
}
|
||||||
|
|
||||||
# Release notes from commits since previous tag
|
|
||||||
$prev = git describe --tags --abbrev=0 "$tag^" 2>$null
|
|
||||||
if ($prev) {
|
|
||||||
$log = git log --pretty=format:"- %s" "$prev..$tag" | Out-String
|
|
||||||
} else {
|
|
||||||
$log = git log --pretty=format:"- %s" "$tag" | Out-String
|
|
||||||
}
|
|
||||||
$body = @"
|
|
||||||
### Изменения
|
|
||||||
|
|
||||||
$log
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Установщик ниже** — запустить и следовать мастеру. Если приложение уже стояло — обновится поверх, настройки сохранятся.
|
|
||||||
"@
|
|
||||||
|
|
||||||
$releaseBody = @{
|
|
||||||
tag_name = $tag
|
|
||||||
name = "Exercise Reminder $tag"
|
|
||||||
body = $body
|
|
||||||
draft = $false
|
|
||||||
prerelease = $false
|
|
||||||
} | ConvertTo-Json -Depth 5
|
|
||||||
|
|
||||||
$release = Invoke-RestMethod `
|
|
||||||
-Uri "$apiBase/repos/$repoOwner/$repoName/releases" `
|
|
||||||
-Method Post `
|
|
||||||
-Headers $headers `
|
|
||||||
-Body $releaseBody `
|
|
||||||
-ContentType 'application/json'
|
|
||||||
|
|
||||||
Write-Host " Release id: $($release.id)" -ForegroundColor DarkGray
|
|
||||||
|
|
||||||
# --- Upload assets -------------------------------------------------------
|
|
||||||
foreach ($asset in @($installer, $blockmap, $manifest)) {
|
|
||||||
$name = Split-Path $asset -Leaf
|
|
||||||
Write-Host "→ Uploading $name…" -ForegroundColor Cyan
|
|
||||||
$uri = "$apiBase/repos/$repoOwner/$repoName/releases/$($release.id)/assets?name=$([uri]::EscapeDataString($name))"
|
|
||||||
Invoke-RestMethod `
|
|
||||||
-Uri $uri `
|
|
||||||
-Method Post `
|
|
||||||
-Headers $headers `
|
|
||||||
-InFile $asset `
|
|
||||||
-ContentType 'application/octet-stream' | Out-Null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$releaseUrl = "https://$giteaHost/$repoOwner/$repoName/releases/tag/$tag"
|
$releaseUrl = "https://$giteaHost/$repoOwner/$repoName/releases/tag/$tag"
|
||||||
@@ -193,4 +170,4 @@ Write-Host ""
|
|||||||
Write-Host "Release published" -ForegroundColor Green
|
Write-Host "Release published" -ForegroundColor Green
|
||||||
Write-Host " $releaseUrl"
|
Write-Host " $releaseUrl"
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "Auto-updater подхватит обновление на установленных копиях в течение ~6 часов."
|
Write-Host "Auto-updater will pick up the new version within ~1 hour on all installed copies."
|
||||||
|
|||||||
@@ -1,22 +1,31 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Upload pre-built NSIS artifacts to an existing Gitea release.
|
Upload pre-built NSIS artifacts to a Gitea release.
|
||||||
|
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Use when the tag v* is already pushed (e.g. release.ps1 succeeded up to
|
Uploads installer + blockmap + latest.yml to the release identified by -Tag.
|
||||||
push but failed on upload, or release was created manually without assets).
|
If the release does not exist it is created (only for semver-looking tags;
|
||||||
If a release for the tag does not exist yet, it is created. If it exists,
|
for non-semver tags like 'update-channel' the release must exist already).
|
||||||
same-name assets are replaced.
|
Same-named existing assets are replaced.
|
||||||
|
|
||||||
.PARAMETER Tag
|
.PARAMETER Tag
|
||||||
Version tag, e.g. v0.3.0. Defaults to v<package.json version>.
|
Release tag to upload INTO. May be a version tag (v0.5.1) or a channel
|
||||||
|
tag (update-channel). Defaults to v<package.json version>.
|
||||||
|
|
||||||
|
.PARAMETER AssetVersion
|
||||||
|
Version of the artifacts being uploaded (e.g. 0.5.1). Defaults to the
|
||||||
|
numeric part of -Tag. Specify explicitly when uploading version-X.Y.Z
|
||||||
|
artifacts into a non-version tag (channel or bridge).
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
pwsh scripts/upload-release-assets.ps1
|
pwsh scripts/upload-release-assets.ps1
|
||||||
pwsh scripts/upload-release-assets.ps1 -Tag v0.3.0
|
pwsh scripts/upload-release-assets.ps1 -Tag v0.5.0
|
||||||
|
pwsh scripts/upload-release-assets.ps1 -Tag update-channel -AssetVersion 0.5.1
|
||||||
|
pwsh scripts/upload-release-assets.ps1 -Tag v0.4.0 -AssetVersion 0.5.1
|
||||||
#>
|
#>
|
||||||
param(
|
param(
|
||||||
[string]$Tag
|
[string]$Tag,
|
||||||
|
[string]$AssetVersion
|
||||||
)
|
)
|
||||||
|
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
@@ -35,10 +44,18 @@ $root = Resolve-Path (Join-Path $PSScriptRoot '..')
|
|||||||
Set-Location $root
|
Set-Location $root
|
||||||
|
|
||||||
if (-not $Tag) {
|
if (-not $Tag) {
|
||||||
$version = (Get-Content package.json | ConvertFrom-Json).version
|
$pkgVersion = (Get-Content package.json | ConvertFrom-Json).version
|
||||||
$Tag = "v$version"
|
$Tag = "v$pkgVersion"
|
||||||
}
|
}
|
||||||
$version = $Tag.TrimStart('v')
|
if (-not $AssetVersion) {
|
||||||
|
# Derive from tag when possible (vX.Y.Z -> X.Y.Z); otherwise read package.json.
|
||||||
|
if ($Tag -match '^v\d+\.\d+\.\d+') {
|
||||||
|
$AssetVersion = $Tag.TrimStart('v')
|
||||||
|
} else {
|
||||||
|
$AssetVersion = (Get-Content package.json | ConvertFrom-Json).version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$version = $AssetVersion
|
||||||
|
|
||||||
$installer = Join-Path 'release' "Exercise-Reminder-Setup-$version.exe"
|
$installer = Join-Path 'release' "Exercise-Reminder-Setup-$version.exe"
|
||||||
$blockmap = "$installer.blockmap"
|
$blockmap = "$installer.blockmap"
|
||||||
@@ -66,6 +83,10 @@ try {
|
|||||||
Write-Host " Found release id=$($release.id)" -ForegroundColor DarkGray
|
Write-Host " Found release id=$($release.id)" -ForegroundColor DarkGray
|
||||||
} catch {
|
} catch {
|
||||||
if ($_.Exception.Response.StatusCode.value__ -eq 404) {
|
if ($_.Exception.Response.StatusCode.value__ -eq 404) {
|
||||||
|
if ($Tag -notmatch '^v\d+\.\d+\.\d+') {
|
||||||
|
Write-Error "Release '$Tag' not found and tag is not semver. Create it manually on Gitea (e.g. 'update-channel' is a one-time setup)."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
Write-Host " Not found, creating new release..." -ForegroundColor DarkGray
|
Write-Host " Not found, creating new release..." -ForegroundColor DarkGray
|
||||||
|
|
||||||
$prev = $null
|
$prev = $null
|
||||||
@@ -80,7 +101,7 @@ try {
|
|||||||
if ($prev) {
|
if ($prev) {
|
||||||
$log = (& git log --pretty=format:"- %s" "$prev..$Tag") -join "`n"
|
$log = (& git log --pretty=format:"- %s" "$prev..$Tag") -join "`n"
|
||||||
} else {
|
} else {
|
||||||
# No prior tag — list last 10 commits up to this tag.
|
# No prior tag - list last 10 commits up to this tag.
|
||||||
$log = (& git log --pretty=format:"- %s" -n 10 "$Tag") -join "`n"
|
$log = (& git log --pretty=format:"- %s" -n 10 "$Tag") -join "`n"
|
||||||
}
|
}
|
||||||
$body = "### Changes`n`n$log`n`n---`n`nInstaller below: run it; if app is already installed, it updates in place and keeps your settings."
|
$body = "### Changes`n`n$log`n`n---`n`nInstaller below: run it; if app is already installed, it updates in place and keeps your settings."
|
||||||
@@ -136,21 +157,66 @@ if ($curlCmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$maxRetries = 4
|
||||||
|
$backoffs = @(15, 45, 120, 300) # seconds between attempts
|
||||||
|
|
||||||
foreach ($asset in @($installer, $blockmap, $manifest)) {
|
foreach ($asset in @($installer, $blockmap, $manifest)) {
|
||||||
$name = Split-Path $asset -Leaf
|
$name = Split-Path $asset -Leaf
|
||||||
$size = (Get-Item $asset).Length
|
$size = (Get-Item $asset).Length
|
||||||
Write-Host ("Uploading {0} ({1:N1} MB)..." -f $name, ($size / 1MB)) -ForegroundColor Cyan
|
|
||||||
$uri = "$apiBase/repos/$repoOwner/$repoName/releases/$($release.id)/assets?name=$([uri]::EscapeDataString($name))"
|
$uri = "$apiBase/repos/$repoOwner/$repoName/releases/$($release.id)/assets?name=$([uri]::EscapeDataString($name))"
|
||||||
# -f: fail on HTTP errors; -s -S: silent but show errors; --data-binary @file
|
|
||||||
|
$attempt = 0
|
||||||
|
$uploaded = $false
|
||||||
|
while (-not $uploaded -and $attempt -le $maxRetries) {
|
||||||
|
if ($attempt -gt 0) {
|
||||||
|
$wait = $backoffs[[Math]::Min($attempt - 1, $backoffs.Length - 1)]
|
||||||
|
Write-Host (" Retrying in {0}s (attempt {1}/{2})..." -f $wait, ($attempt + 1), ($maxRetries + 1)) -ForegroundColor Yellow
|
||||||
|
Start-Sleep -Seconds $wait
|
||||||
|
|
||||||
|
# Re-check whether prior attempt actually succeeded server-side before
|
||||||
|
# 504-ing the client. If asset is already there, treat as success.
|
||||||
|
try {
|
||||||
|
$check = Invoke-RestMethod `
|
||||||
|
-Uri "$apiBase/repos/$repoOwner/$repoName/releases/$($release.id)/assets" `
|
||||||
|
-Method Get -Headers $headers
|
||||||
|
$existing = $check | Where-Object { $_.name -eq $name }
|
||||||
|
if ($existing -and $existing.size -eq $size) {
|
||||||
|
Write-Host " Asset already present server-side ($($existing.size) bytes) - skipping retry." -ForegroundColor DarkGray
|
||||||
|
$uploaded = $true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
# If asset is present but with wrong size (half-uploaded), delete first.
|
||||||
|
if ($existing) {
|
||||||
|
Write-Host " Removing partial asset id=$($existing.id) ($($existing.size) bytes) before retry..." -ForegroundColor DarkGray
|
||||||
|
Invoke-RestMethod `
|
||||||
|
-Uri "$apiBase/repos/$repoOwner/$repoName/releases/$($release.id)/assets/$($existing.id)" `
|
||||||
|
-Method Delete -Headers $headers | Out-Null
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
# If the list call itself fails, just proceed with the retry.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host ("Uploading {0} ({1:N1} MB)..." -f $name, ($size / 1MB)) -ForegroundColor Cyan
|
||||||
& $curl `
|
& $curl `
|
||||||
--fail-with-body `
|
--fail-with-body `
|
||||||
--silent --show-error `
|
--silent --show-error `
|
||||||
|
--connect-timeout 30 `
|
||||||
|
--max-time 900 `
|
||||||
-H "Authorization: token $env:GITEA_TOKEN" `
|
-H "Authorization: token $env:GITEA_TOKEN" `
|
||||||
-H "Content-Type: application/octet-stream" `
|
-H "Content-Type: application/octet-stream" `
|
||||||
--data-binary "@$asset" `
|
--data-binary "@$asset" `
|
||||||
$uri
|
$uri
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -eq 0) {
|
||||||
Write-Error "Upload failed for $name (curl exit $LASTEXITCODE)"
|
$uploaded = $true
|
||||||
|
} else {
|
||||||
|
Write-Host " curl exit $LASTEXITCODE - will retry." -ForegroundColor Yellow
|
||||||
|
$attempt++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $uploaded) {
|
||||||
|
Write-Error "Upload failed for $name after $($maxRetries + 1) attempts."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,5 +17,8 @@ export function isAutostartEnabled(): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function wasStartedHidden(): boolean {
|
export function wasStartedHidden(): boolean {
|
||||||
return process.argv.includes(HIDDEN_FLAG) || app.getLoginItemSettings().wasOpenedAsHidden
|
return (
|
||||||
|
process.argv.includes(HIDDEN_FLAG) ||
|
||||||
|
app.getLoginItemSettings().wasOpenedAsHidden
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
|
import {
|
||||||
|
existsSync,
|
||||||
|
mkdirSync,
|
||||||
|
readFileSync,
|
||||||
|
unlinkSync,
|
||||||
|
writeFileSync
|
||||||
|
} from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import { randomBytes } from 'node:crypto'
|
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import type { GameProvider, ProviderEventHandler } from './provider'
|
import type { GameProvider, ProviderEventHandler } from './provider'
|
||||||
import { findGameInstall } from './steam'
|
import { findGameInstall } from './steam'
|
||||||
@@ -21,6 +27,7 @@ const LAUNCH_OPTION = '-gamestateintegration'
|
|||||||
|
|
||||||
type DotaGsi = {
|
type DotaGsi = {
|
||||||
provider?: { name?: string }
|
provider?: { name?: string }
|
||||||
|
auth?: { token?: string }
|
||||||
map?: {
|
map?: {
|
||||||
game_state?: string
|
game_state?: string
|
||||||
win_team?: 'radiant' | 'dire' | 'none'
|
win_team?: 'radiant' | 'dire' | 'none'
|
||||||
@@ -38,6 +45,19 @@ type DotaGsi = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant-time string equality. Avoids early-exit timing oracles that could
|
||||||
|
* leak the token byte-by-byte to a local attacker who can measure response
|
||||||
|
* latency on the loopback HTTP server. (Practical risk is tiny; correctness
|
||||||
|
* matters anyway.)
|
||||||
|
*/
|
||||||
|
function safeEqualStrings(a: string, b: string): boolean {
|
||||||
|
const A = Buffer.from(a, 'utf-8')
|
||||||
|
const B = Buffer.from(b, 'utf-8')
|
||||||
|
if (A.length !== B.length) return false
|
||||||
|
return timingSafeEqual(A, B)
|
||||||
|
}
|
||||||
|
|
||||||
function tokenStorePath(): string {
|
function tokenStorePath(): string {
|
||||||
return join(app.getPath('userData'), 'dota2-gsi-token.txt')
|
return join(app.getPath('userData'), 'dota2-gsi-token.txt')
|
||||||
}
|
}
|
||||||
@@ -115,7 +135,10 @@ export class Dota2Provider implements GameProvider {
|
|||||||
if (present) launchOptionStatus = 'applied'
|
if (present) launchOptionStatus = 'applied'
|
||||||
else {
|
else {
|
||||||
steamRunning = await isSteamRunning()
|
steamRunning = await isSteamRunning()
|
||||||
launchOptionStatus = steamRunning ? 'queued' : 'queued'
|
// Either Steam is open (we can't write while it runs -> 'queued') or
|
||||||
|
// closed (apply on next ensureLaunchOption call -> still queued until
|
||||||
|
// the watcher tick actually writes). 'queued' is correct for both.
|
||||||
|
launchOptionStatus = 'queued'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -134,7 +157,8 @@ export class Dota2Provider implements GameProvider {
|
|||||||
async install(): Promise<void> {
|
async install(): Promise<void> {
|
||||||
if (!this.installPath) {
|
if (!this.installPath) {
|
||||||
const status = await this.detect()
|
const status = await this.detect()
|
||||||
if (!status.installPath) throw new Error('Dota 2 не найдена в Steam-библиотеках')
|
if (!status.installPath)
|
||||||
|
throw new Error('Dota 2 не найдена в Steam-библиотеках')
|
||||||
}
|
}
|
||||||
const dir = cfgDir(this.installPath!)
|
const dir = cfgDir(this.installPath!)
|
||||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
||||||
@@ -157,7 +181,13 @@ export class Dota2Provider implements GameProvider {
|
|||||||
|
|
||||||
async start(emit: ProviderEventHandler): Promise<void> {
|
async start(emit: ProviderEventHandler): Promise<void> {
|
||||||
this.emit = emit
|
this.emit = emit
|
||||||
this.unregister = registerGsiRoute(ROUTE, (payload) => this.handle(payload as DotaGsi))
|
// Defensive double-register guard: free any previous registration first.
|
||||||
|
this.unregister?.()
|
||||||
|
this.unregister = registerGsiRoute(ROUTE, (payload) => {
|
||||||
|
// Runtime shape check — payload comes from a network socket.
|
||||||
|
if (typeof payload !== 'object' || payload === null) return
|
||||||
|
this.handle(payload as DotaGsi)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
@@ -169,10 +199,34 @@ export class Dota2Provider implements GameProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private handle(g: DotaGsi): void {
|
private handle(g: DotaGsi): void {
|
||||||
// Track latest snapshot so we have stats when the transition fires.
|
// Verify the per-install token. Dota always sends auth.token; anything
|
||||||
if (g.player || g.map) this.latest = { ...this.latest, ...g, player: { ...this.latest?.player, ...g.player }, map: { ...this.latest?.map, ...g.map } }
|
// without it (or with the wrong one) is some other process on localhost
|
||||||
|
// trying to fake a match-end event.
|
||||||
|
const incoming = g.auth?.token
|
||||||
|
if (
|
||||||
|
typeof incoming !== 'string' ||
|
||||||
|
!safeEqualStrings(incoming, this.token)
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const state = g.map?.game_state ?? this.latest?.map?.game_state
|
// Narrow the shape before spread-merging. A payload like `{player:"x"}`
|
||||||
|
// would otherwise let `{...this.latest?.player, ...g.player}` throw.
|
||||||
|
const playerObj =
|
||||||
|
typeof g.player === 'object' && g.player !== null ? g.player : undefined
|
||||||
|
const mapObj =
|
||||||
|
typeof g.map === 'object' && g.map !== null ? g.map : undefined
|
||||||
|
|
||||||
|
if (playerObj || mapObj) {
|
||||||
|
this.latest = {
|
||||||
|
...this.latest,
|
||||||
|
...g,
|
||||||
|
player: { ...this.latest?.player, ...playerObj },
|
||||||
|
map: { ...this.latest?.map, ...mapObj }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = mapObj?.game_state ?? this.latest?.map?.game_state
|
||||||
if (!state) return
|
if (!state) return
|
||||||
|
|
||||||
const prev = this.prevState
|
const prev = this.prevState
|
||||||
@@ -209,6 +263,11 @@ export class Dota2Provider implements GameProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Reset stale state so the NEXT match starts from a clean slate even if
|
||||||
|
// the user re-enters the same lobby or Dota's GSI restarts mid-session.
|
||||||
|
this.latest = undefined
|
||||||
|
this.prevState = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,63 @@
|
|||||||
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'
|
import {
|
||||||
|
createServer,
|
||||||
|
type IncomingMessage,
|
||||||
|
type Server,
|
||||||
|
type ServerResponse
|
||||||
|
} from 'node:http'
|
||||||
|
|
||||||
export type GsiHandler = (payload: unknown, headers: Record<string, string | string[] | undefined>) => void
|
export type GsiHandler = (
|
||||||
|
payload: unknown,
|
||||||
|
headers: Record<string, string | string[] | undefined>
|
||||||
|
) => void
|
||||||
|
|
||||||
const PORT = 4701
|
const PORT = 4701
|
||||||
|
/**
|
||||||
|
* Hard cap on incoming POST body. Real Dota GSI payloads are ~8 KB; anything
|
||||||
|
* larger is either a bug or a malicious local client trying to OOM us.
|
||||||
|
*/
|
||||||
|
const MAX_BODY_BYTES = 256 * 1024
|
||||||
|
|
||||||
let server: Server | null = null
|
let server: Server | null = null
|
||||||
const handlers: Map<string, GsiHandler> = new Map()
|
const handlers: Map<string, GsiHandler> = new Map()
|
||||||
|
|
||||||
function getBody(req: IncomingMessage): Promise<Buffer> {
|
function readBody(req: IncomingMessage): Promise<Buffer> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
let received = 0
|
||||||
const chunks: Buffer[] = []
|
const chunks: Buffer[] = []
|
||||||
req.on('data', (c) => chunks.push(c as Buffer))
|
req.on('data', (c: Buffer) => {
|
||||||
|
received += c.length
|
||||||
|
if (received > MAX_BODY_BYTES) {
|
||||||
|
// Drop the connection so we don't keep buffering.
|
||||||
|
req.destroy(new Error('body too large'))
|
||||||
|
reject(new Error('body too large'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
chunks.push(c)
|
||||||
|
})
|
||||||
req.on('end', () => resolve(Buffer.concat(chunks)))
|
req.on('end', () => resolve(Buffer.concat(chunks)))
|
||||||
req.on('error', reject)
|
req.on('error', reject)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
async function onRequest(
|
||||||
|
req: IncomingMessage,
|
||||||
|
res: ServerResponse
|
||||||
|
): Promise<void> {
|
||||||
|
// Reject browser-originated requests outright. Legitimate Dota GSI POSTs
|
||||||
|
// never include an Origin header; any value here means a webpage is poking
|
||||||
|
// our localhost endpoint via cross-origin fetch, which we never want.
|
||||||
|
if (req.headers.origin) {
|
||||||
|
res.statusCode = 403
|
||||||
|
res.end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Same intent for Sec-Fetch-Site: browsers always set it, Dota never does.
|
||||||
|
if (req.headers['sec-fetch-site']) {
|
||||||
|
res.statusCode = 403
|
||||||
|
res.end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const route = (req.url ?? '/').split('?')[0]
|
const route = (req.url ?? '/').split('?')[0]
|
||||||
const handler = handlers.get(route)
|
const handler = handlers.get(route)
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
@@ -29,17 +70,38 @@ async function onRequest(req: IncomingMessage, res: ServerResponse): Promise<voi
|
|||||||
res.end()
|
res.end()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Require JSON content-type. Browsers' "simple" requests in no-cors mode
|
||||||
|
// can only send text/plain or form-encoded — locking to application/json
|
||||||
|
// shrinks the cross-origin attack surface further.
|
||||||
|
const ct = String(req.headers['content-type'] ?? '').toLowerCase()
|
||||||
|
if (!ct.includes('application/json')) {
|
||||||
|
res.statusCode = 415
|
||||||
|
res.end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let payload: unknown
|
||||||
try {
|
try {
|
||||||
const body = await getBody(req)
|
const body = await readBody(req)
|
||||||
const text = body.toString('utf-8')
|
const text = body.toString('utf-8')
|
||||||
const payload = text.length > 0 ? JSON.parse(text) : {}
|
payload = text.length > 0 ? JSON.parse(text) : {}
|
||||||
|
} catch (err) {
|
||||||
|
// Log the real reason locally; do not echo it to the client.
|
||||||
|
console.warn('[gsi] bad request:', err instanceof Error ? err.message : err)
|
||||||
|
res.statusCode = 400
|
||||||
|
res.end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
handler(payload, req.headers)
|
handler(payload, req.headers)
|
||||||
res.statusCode = 200
|
res.statusCode = 200
|
||||||
res.setHeader('Content-Type', 'text/plain')
|
res.setHeader('Content-Type', 'text/plain')
|
||||||
res.end('ok')
|
res.end('ok')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('[gsi] handler threw:', err)
|
||||||
res.statusCode = 500
|
res.statusCode = 500
|
||||||
res.end(String(err))
|
res.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,16 +114,26 @@ export async function startGsiServer(): Promise<void> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopGsiServer(): void {
|
export async function stopGsiServer(): Promise<void> {
|
||||||
if (server) {
|
if (!server) return
|
||||||
server.close()
|
const s = server
|
||||||
server = null
|
server = null
|
||||||
}
|
// Free pending sockets so close() can resolve quickly even while Dota holds
|
||||||
|
// a long-poll connection open.
|
||||||
|
s.closeAllConnections?.()
|
||||||
|
await new Promise<void>((resolve) => s.close(() => resolve()))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function registerGsiRoute(route: string, handler: GsiHandler): () => void {
|
export function registerGsiRoute(
|
||||||
|
route: string,
|
||||||
|
handler: GsiHandler
|
||||||
|
): () => void {
|
||||||
handlers.set(route, handler)
|
handlers.set(route, handler)
|
||||||
return () => handlers.delete(route)
|
return () => {
|
||||||
|
// Only delete if we're still the registered handler — protects against
|
||||||
|
// double-register + unregister races where a newer handler took our slot.
|
||||||
|
if (handlers.get(route) === handler) handlers.delete(route)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getGsiBaseUrl(): string {
|
export function getGsiBaseUrl(): string {
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ export type MatchEndPayload = {
|
|||||||
stats: Partial<Record<GameStat, number>>
|
stats: Partial<Record<GameStat, number>>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ProviderEventHandler = (event: { type: 'match_end'; payload: MatchEndPayload }) => void
|
export type ProviderEventHandler = (event: {
|
||||||
|
type: 'match_end'
|
||||||
|
payload: MatchEndPayload
|
||||||
|
}) => void
|
||||||
|
|
||||||
export interface GameProvider {
|
export interface GameProvider {
|
||||||
readonly id: GameId
|
readonly id: GameId
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { startGsiServer, stopGsiServer } from './gsi-server'
|
|||||||
import { onLaunchOptionsApplied } from './steam-launch-options'
|
import { onLaunchOptionsApplied } from './steam-launch-options'
|
||||||
import { IPC } from '@shared/ipc'
|
import { IPC } from '@shared/ipc'
|
||||||
import type {
|
import type {
|
||||||
Challenge,
|
|
||||||
ChallengeResult,
|
ChallengeResult,
|
||||||
GameId,
|
GameId,
|
||||||
GameStatus,
|
GameStatus,
|
||||||
@@ -21,7 +20,10 @@ const providers: Record<GameId, GameProvider> = {
|
|||||||
|
|
||||||
let running = false
|
let running = false
|
||||||
|
|
||||||
async function onMatchEnd(gameId: GameId, payload: MatchEndPayload): Promise<void> {
|
async function onMatchEnd(
|
||||||
|
gameId: GameId,
|
||||||
|
payload: MatchEndPayload
|
||||||
|
): Promise<void> {
|
||||||
const provider = providers[gameId]
|
const provider = providers[gameId]
|
||||||
const challenges = getChallenges().filter(
|
const challenges = getChallenges().filter(
|
||||||
(c) => c.gameId === gameId && c.enabled
|
(c) => c.gameId === gameId && c.enabled
|
||||||
@@ -136,7 +138,10 @@ export function broadcastGames(games: GameStatus[]): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Simulate a match-end for debugging (called from IPC in dev).
|
// Simulate a match-end for debugging (called from IPC in dev).
|
||||||
export function simulateMatchEnd(id: GameId, stats: Partial<Record<string, number>>): void {
|
export function simulateMatchEnd(
|
||||||
|
id: GameId,
|
||||||
|
stats: Partial<Record<string, number>>
|
||||||
|
): void {
|
||||||
void onMatchEnd(id, {
|
void onMatchEnd(id, {
|
||||||
durationMs: (stats.duration_min ?? 35) * 60_000,
|
durationMs: (stats.duration_min ?? 35) * 60_000,
|
||||||
won: stats.won === 1,
|
won: stats.won === 1,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
existsSync,
|
existsSync,
|
||||||
readFileSync,
|
readFileSync,
|
||||||
readdirSync,
|
readdirSync,
|
||||||
|
renameSync,
|
||||||
writeFileSync
|
writeFileSync
|
||||||
} from 'node:fs'
|
} from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
@@ -53,7 +54,10 @@ function findKey(node: VdfNode, target: string): string | undefined {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
function findCaseInsensitive(node: VdfNode, ...keys: string[]): VdfNode | undefined {
|
function findCaseInsensitive(
|
||||||
|
node: VdfNode,
|
||||||
|
...keys: string[]
|
||||||
|
): VdfNode | undefined {
|
||||||
let cur: VdfNode = node
|
let cur: VdfNode = node
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
const found: string | undefined = findKey(cur, key)
|
const found: string | undefined = findKey(cur, key)
|
||||||
@@ -80,7 +84,11 @@ function findOrCreatePath(node: VdfNode, ...keys: string[]): VdfNode {
|
|||||||
return cur
|
return cur
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAppNode(parsed: VdfNode, appId: string, create: boolean): VdfNode | undefined {
|
function getAppNode(
|
||||||
|
parsed: VdfNode,
|
||||||
|
appId: string,
|
||||||
|
create: boolean
|
||||||
|
): VdfNode | undefined {
|
||||||
if (create) {
|
if (create) {
|
||||||
const apps = findOrCreatePath(
|
const apps = findOrCreatePath(
|
||||||
parsed,
|
parsed,
|
||||||
@@ -116,13 +124,28 @@ function writeBackup(path: string): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function atomicWrite(path: string, contents: string): void {
|
function atomicWrite(path: string, contents: string): void {
|
||||||
// Write to temp then rename (atomic on Windows for same directory).
|
// Write to temp then rename (atomic on Windows for same directory). Retry a
|
||||||
|
// few times on transient EBUSY/EPERM (AV scanners and OneDrive sometimes
|
||||||
|
// hold a handle briefly during a Steam config rewrite).
|
||||||
const tmp = path + '.exr.tmp'
|
const tmp = path + '.exr.tmp'
|
||||||
|
const delays = [0, 50, 200]
|
||||||
|
let lastErr: unknown
|
||||||
|
for (const delay of delays) {
|
||||||
|
if (delay > 0) {
|
||||||
|
const until = Date.now() + delay
|
||||||
|
while (Date.now() < until) {
|
||||||
|
/* spin */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
writeFileSync(tmp, contents, 'utf-8')
|
writeFileSync(tmp, contents, 'utf-8')
|
||||||
// fs.renameSync replaces destination atomically on Windows
|
renameSync(tmp, path)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
return
|
||||||
const fs = require('node:fs') as typeof import('node:fs')
|
} catch (e) {
|
||||||
fs.renameSync(tmp, path)
|
lastErr = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw lastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
function modifyLaunchOptions(
|
function modifyLaunchOptions(
|
||||||
@@ -183,7 +206,9 @@ export async function isLaunchOptionPresent(
|
|||||||
const parsed = parseVdf(raw)
|
const parsed = parseVdf(raw)
|
||||||
const app = getAppNode(parsed, appId, false)
|
const app = getAppNode(parsed, appId, false)
|
||||||
if (!app) continue
|
if (!app) continue
|
||||||
const loKey = Object.keys(app).find((k) => k.toLowerCase() === 'launchoptions')
|
const loKey = Object.keys(app).find(
|
||||||
|
(k) => k.toLowerCase() === 'launchoptions'
|
||||||
|
)
|
||||||
if (!loKey) continue
|
if (!loKey) continue
|
||||||
const value = String(app[loKey] ?? '')
|
const value = String(app[loKey] ?? '')
|
||||||
if (value.includes(option)) return true
|
if (value.includes(option)) return true
|
||||||
@@ -194,7 +219,10 @@ export async function isLaunchOptionPresent(
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
async function applyOptionToAllConfigs(appId: string, option: string): Promise<void> {
|
async function applyOptionToAllConfigs(
|
||||||
|
appId: string,
|
||||||
|
option: string
|
||||||
|
): Promise<void> {
|
||||||
const paths = await getLocalConfigPaths()
|
const paths = await getLocalConfigPaths()
|
||||||
for (const p of paths) {
|
for (const p of paths) {
|
||||||
modifyLaunchOptions(p, appId, (current) => {
|
modifyLaunchOptions(p, appId, (current) => {
|
||||||
@@ -204,7 +232,10 @@ async function applyOptionToAllConfigs(appId: string, option: string): Promise<v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function removeOptionFromAllConfigs(appId: string, option: string): Promise<void> {
|
async function removeOptionFromAllConfigs(
|
||||||
|
appId: string,
|
||||||
|
option: string
|
||||||
|
): Promise<void> {
|
||||||
const paths = await getLocalConfigPaths()
|
const paths = await getLocalConfigPaths()
|
||||||
for (const p of paths) {
|
for (const p of paths) {
|
||||||
modifyLaunchOptions(p, appId, (current) => {
|
modifyLaunchOptions(p, appId, (current) => {
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ import { parseVdf, type VdfNode } from './vdf'
|
|||||||
|
|
||||||
const execAsync = promisify(exec)
|
const execAsync = promisify(exec)
|
||||||
|
|
||||||
async function regQuery(key: string, valueName: string): Promise<string | undefined> {
|
async function regQuery(
|
||||||
|
key: string,
|
||||||
|
valueName: string
|
||||||
|
): Promise<string | undefined> {
|
||||||
try {
|
try {
|
||||||
const { stdout } = await execAsync(
|
const { stdout } = await execAsync(`reg query "${key}" /v ${valueName}`, {
|
||||||
`reg query "${key}" /v ${valueName}`,
|
windowsHide: true
|
||||||
{ windowsHide: true }
|
})
|
||||||
)
|
|
||||||
const m = stdout.match(/REG_SZ\s+(.+)/)
|
const m = stdout.match(/REG_SZ\s+(.+)/)
|
||||||
return m?.[1]?.trim()
|
return m?.[1]?.trim()
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -4,7 +4,10 @@
|
|||||||
export type VdfNode = { [key: string]: string | VdfNode }
|
export type VdfNode = { [key: string]: string | VdfNode }
|
||||||
|
|
||||||
class Cursor {
|
class Cursor {
|
||||||
constructor(public src: string, public pos: number = 0) {}
|
constructor(
|
||||||
|
public src: string,
|
||||||
|
public pos: number = 0
|
||||||
|
) {}
|
||||||
peek(): string {
|
peek(): string {
|
||||||
return this.src[this.pos] ?? ''
|
return this.src[this.pos] ?? ''
|
||||||
}
|
}
|
||||||
@@ -51,7 +54,12 @@ function readToken(c: Cursor): string {
|
|||||||
}
|
}
|
||||||
if (c.peek() === '{' || c.peek() === '}') return c.next()
|
if (c.peek() === '{' || c.peek() === '}') return c.next()
|
||||||
let out = ''
|
let out = ''
|
||||||
while (!c.eof() && !/\s/.test(c.peek()) && c.peek() !== '{' && c.peek() !== '}') {
|
while (
|
||||||
|
!c.eof() &&
|
||||||
|
!/\s/.test(c.peek()) &&
|
||||||
|
c.peek() !== '{' &&
|
||||||
|
c.peek() !== '}'
|
||||||
|
) {
|
||||||
out += c.next()
|
out += c.next()
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { app, BrowserWindow, nativeTheme, systemPreferences } from 'electron'
|
import { app, BrowserWindow, nativeTheme, systemPreferences } from 'electron'
|
||||||
import { createMainWindow, createReminderWindow, showMainWindow } from './windows'
|
import {
|
||||||
|
createMainWindow,
|
||||||
|
createReminderWindow,
|
||||||
|
showMainWindow
|
||||||
|
} from './windows'
|
||||||
import { registerIpc } from './ipc'
|
import { registerIpc } from './ipc'
|
||||||
import { startScheduler, stopScheduler } from './scheduler'
|
import { startScheduler, stopScheduler } from './scheduler'
|
||||||
import { createTray } from './tray'
|
import { createTray } from './tray'
|
||||||
@@ -27,8 +31,7 @@ if (!gotLock) {
|
|||||||
registerIpc()
|
registerIpc()
|
||||||
createTray()
|
createTray()
|
||||||
|
|
||||||
const hidden =
|
const hidden = wasStartedHidden() || getState().settings.startMinimized
|
||||||
wasStartedHidden() || getState().settings.startMinimized
|
|
||||||
createMainWindow(!hidden)
|
createMainWindow(!hidden)
|
||||||
// Pre-create the reminder window so first-trigger is instant (no load lag).
|
// Pre-create the reminder window so first-trigger is instant (no load lag).
|
||||||
createReminderWindow()
|
createReminderWindow()
|
||||||
@@ -51,7 +54,8 @@ if (!gotLock) {
|
|||||||
try {
|
try {
|
||||||
const color = '#' + systemPreferences.getAccentColor()
|
const color = '#' + systemPreferences.getAccentColor()
|
||||||
for (const win of BrowserWindow.getAllWindows()) {
|
for (const win of BrowserWindow.getAllWindows()) {
|
||||||
if (!win.isDestroyed()) win.webContents.send(IPC.evtAccentChanged, color)
|
if (!win.isDestroyed())
|
||||||
|
win.webContents.send(IPC.evtAccentChanged, color)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// ignore
|
// ignore
|
||||||
|
|||||||
149
src/main/ipc.ts
149
src/main/ipc.ts
@@ -1,6 +1,13 @@
|
|||||||
import { ipcMain, nativeTheme, systemPreferences, BrowserWindow, app, shell } from 'electron'
|
import {
|
||||||
|
ipcMain,
|
||||||
|
nativeTheme,
|
||||||
|
systemPreferences,
|
||||||
|
BrowserWindow,
|
||||||
|
app,
|
||||||
|
shell
|
||||||
|
} from 'electron'
|
||||||
import { IPC } from '@shared/ipc'
|
import { IPC } from '@shared/ipc'
|
||||||
import type { Challenge, Exercise, GameId, Settings } from '@shared/types'
|
import type { Exercise, GameId, Settings } from '@shared/types'
|
||||||
import {
|
import {
|
||||||
addChallenge,
|
addChallenge,
|
||||||
addExercise,
|
addExercise,
|
||||||
@@ -21,6 +28,7 @@ import { broadcastState } from './state-actions'
|
|||||||
import { setAutostart, isAutostartEnabled } from './autostart'
|
import { setAutostart, isAutostartEnabled } from './autostart'
|
||||||
import { setPaused, forceCheck } from './scheduler'
|
import { setPaused, forceCheck } from './scheduler'
|
||||||
import { hideReminderWindow, getMainWindow } from './windows'
|
import { hideReminderWindow, getMainWindow } from './windows'
|
||||||
|
import { refreshMenu } from './tray'
|
||||||
import {
|
import {
|
||||||
broadcastGames,
|
broadcastGames,
|
||||||
installGame,
|
installGame,
|
||||||
@@ -35,6 +43,16 @@ import {
|
|||||||
getUpdaterStatus,
|
getUpdaterStatus,
|
||||||
quitAndInstall
|
quitAndInstall
|
||||||
} from './updater'
|
} from './updater'
|
||||||
|
import {
|
||||||
|
validateActualReps,
|
||||||
|
validateChallengeInput,
|
||||||
|
validateChallengePatch,
|
||||||
|
validateExerciseInput,
|
||||||
|
validateExercisePatch,
|
||||||
|
validateId,
|
||||||
|
validateSettingsPatch,
|
||||||
|
validateSnoozeMinutes
|
||||||
|
} from './validate'
|
||||||
|
|
||||||
export function registerIpc(): void {
|
export function registerIpc(): void {
|
||||||
ipcMain.handle(IPC.getState, () => {
|
ipcMain.handle(IPC.getState, () => {
|
||||||
@@ -43,60 +61,78 @@ export function registerIpc(): void {
|
|||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(IPC.addExercise, (_e, input: unknown) => {
|
||||||
|
const safe = validateExerciseInput(input)
|
||||||
|
if (!safe) return null
|
||||||
|
const ex = addExercise(safe)
|
||||||
|
broadcastState()
|
||||||
|
return ex
|
||||||
|
})
|
||||||
|
|
||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
IPC.addExercise,
|
IPC.updateExercise,
|
||||||
(_e, input: Omit<Exercise, 'id' | 'nextFireAt' | 'lastDoneAt'>) => {
|
(_e, idRaw: unknown, patchRaw: unknown) => {
|
||||||
const ex = addExercise(input)
|
const id = validateId(idRaw)
|
||||||
|
const patch = validateExercisePatch(patchRaw)
|
||||||
|
if (!id || !patch) return null
|
||||||
|
const ex = updateExercise(id, patch)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ex
|
return ex
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
ipcMain.handle(IPC.updateExercise, (_e, id: string, patch: Partial<Exercise>) => {
|
ipcMain.handle(IPC.deleteExercise, (_e, idRaw: unknown) => {
|
||||||
const ex = updateExercise(id, patch)
|
const id = validateId(idRaw)
|
||||||
broadcastState()
|
if (!id) return false
|
||||||
return ex
|
|
||||||
})
|
|
||||||
|
|
||||||
ipcMain.handle(IPC.deleteExercise, (_e, id: string) => {
|
|
||||||
const ok = deleteExercise(id)
|
const ok = deleteExercise(id)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ok
|
return ok
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle(IPC.toggleExercise, (_e, id: string, enabled: boolean) => {
|
ipcMain.handle(
|
||||||
const patch: Partial<Exercise> = { enabled }
|
IPC.toggleExercise,
|
||||||
if (enabled) {
|
(_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)
|
const ex = getState().exercises.find((e) => e.id === id)
|
||||||
if (ex) patch.nextFireAt = Date.now() + ex.intervalMinutes * 60_000
|
if (ex) patch.nextFireAt = Date.now() + ex.intervalMinutes * 60_000
|
||||||
}
|
}
|
||||||
const ex = updateExercise(id, patch)
|
const ex = updateExercise(id, patch)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ex
|
return ex
|
||||||
})
|
|
||||||
|
|
||||||
ipcMain.handle(
|
|
||||||
IPC.markDone,
|
|
||||||
(_e, id: string, actualReps?: number) => {
|
|
||||||
const ex = markDone(id, actualReps)
|
|
||||||
broadcastState()
|
|
||||||
return ex
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
ipcMain.handle(IPC.snooze, (_e, id: string, minutes: number) => {
|
ipcMain.handle(IPC.markDone, (_e, idRaw: unknown, repsRaw?: unknown) => {
|
||||||
|
const id = validateId(idRaw)
|
||||||
|
if (!id) return null
|
||||||
|
const ex = markDone(id, validateActualReps(repsRaw))
|
||||||
|
broadcastState()
|
||||||
|
return ex
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(IPC.snooze, (_e, idRaw: unknown, minRaw: unknown) => {
|
||||||
|
const id = validateId(idRaw)
|
||||||
|
const minutes = validateSnoozeMinutes(minRaw)
|
||||||
|
if (!id || minutes === null) return null
|
||||||
const ex = snooze(id, minutes)
|
const ex = snooze(id, minutes)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ex
|
return ex
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle(IPC.skip, (_e, id: string) => {
|
ipcMain.handle(IPC.skip, (_e, idRaw: unknown) => {
|
||||||
|
const id = validateId(idRaw)
|
||||||
|
if (!id) return null
|
||||||
const ex = skip(id)
|
const ex = skip(id)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ex
|
return ex
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle(IPC.updateSettings, (_e, patch: Partial<Settings>) => {
|
ipcMain.handle(IPC.updateSettings, (_e, patchRaw: unknown) => {
|
||||||
|
const patch = validateSettingsPatch(patchRaw)
|
||||||
|
if (!patch) return null
|
||||||
if (patch.startWithWindows !== undefined) {
|
if (patch.startWithWindows !== undefined) {
|
||||||
setAutostart(patch.startWithWindows)
|
setAutostart(patch.startWithWindows)
|
||||||
}
|
}
|
||||||
@@ -106,9 +142,21 @@ export function registerIpc(): void {
|
|||||||
}
|
}
|
||||||
const settings = updateSettings(merged)
|
const settings = updateSettings(merged)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
|
// Language change reflects in the tray menu next time it's opened.
|
||||||
|
if (patch.language !== undefined) refreshMenu()
|
||||||
return settings
|
return settings
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(IPC.pauseAll, () => {
|
||||||
|
setPaused(true)
|
||||||
|
refreshMenu()
|
||||||
|
})
|
||||||
|
ipcMain.handle(IPC.resumeAll, () => {
|
||||||
|
setPaused(false)
|
||||||
|
forceCheck()
|
||||||
|
refreshMenu()
|
||||||
|
})
|
||||||
|
|
||||||
ipcMain.handle(IPC.getAccentColor, () => {
|
ipcMain.handle(IPC.getAccentColor, () => {
|
||||||
try {
|
try {
|
||||||
return '#' + systemPreferences.getAccentColor()
|
return '#' + systemPreferences.getAccentColor()
|
||||||
@@ -121,12 +169,6 @@ export function registerIpc(): void {
|
|||||||
nativeTheme.shouldUseDarkColors ? 'dark' : 'light'
|
nativeTheme.shouldUseDarkColors ? 'dark' : 'light'
|
||||||
)
|
)
|
||||||
|
|
||||||
ipcMain.handle(IPC.pauseAll, () => setPaused(true))
|
|
||||||
ipcMain.handle(IPC.resumeAll, () => {
|
|
||||||
setPaused(false)
|
|
||||||
forceCheck()
|
|
||||||
})
|
|
||||||
|
|
||||||
ipcMain.handle(IPC.quit, () => app.quit())
|
ipcMain.handle(IPC.quit, () => app.quit())
|
||||||
ipcMain.handle(IPC.reminderClose, () => hideReminderWindow())
|
ipcMain.handle(IPC.reminderClose, () => hideReminderWindow())
|
||||||
|
|
||||||
@@ -134,6 +176,17 @@ export function registerIpc(): void {
|
|||||||
BrowserWindow.fromWebContents(event.sender)?.minimize()
|
BrowserWindow.fromWebContents(event.sender)?.minimize()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ipcMain.on(IPC.toggleMaximizeMain, (event) => {
|
||||||
|
const win = BrowserWindow.fromWebContents(event.sender)
|
||||||
|
if (!win) return
|
||||||
|
if (win.isMaximized()) win.unmaximize()
|
||||||
|
else win.maximize()
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.handle(IPC.isMaximizedMain, (event) => {
|
||||||
|
return BrowserWindow.fromWebContents(event.sender)?.isMaximized() ?? false
|
||||||
|
})
|
||||||
|
|
||||||
ipcMain.on(IPC.closeMain, () => {
|
ipcMain.on(IPC.closeMain, () => {
|
||||||
const main = getMainWindow()
|
const main = getMainWindow()
|
||||||
if (!main) return
|
if (!main) return
|
||||||
@@ -179,39 +232,55 @@ export function registerIpc(): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Challenges
|
// Challenges
|
||||||
ipcMain.handle(IPC.addChallenge, (_e, input: Omit<Challenge, 'id'>) => {
|
ipcMain.handle(IPC.addChallenge, (_e, input: unknown) => {
|
||||||
const c = addChallenge(input)
|
const safe = validateChallengeInput(input)
|
||||||
|
if (!safe) return null
|
||||||
|
const c = addChallenge(safe)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return c
|
return c
|
||||||
})
|
})
|
||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
IPC.updateChallenge,
|
IPC.updateChallenge,
|
||||||
(_e, id: string, patch: Partial<Challenge>) => {
|
(_e, idRaw: unknown, patchRaw: unknown) => {
|
||||||
|
const id = validateId(idRaw)
|
||||||
|
const patch = validateChallengePatch(patchRaw)
|
||||||
|
if (!id || !patch) return null
|
||||||
const c = updateChallenge(id, patch)
|
const c = updateChallenge(id, patch)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
ipcMain.handle(IPC.deleteChallenge, (_e, id: string) => {
|
ipcMain.handle(IPC.deleteChallenge, (_e, idRaw: unknown) => {
|
||||||
|
const id = validateId(idRaw)
|
||||||
|
if (!id) return false
|
||||||
const ok = deleteChallenge(id)
|
const ok = deleteChallenge(id)
|
||||||
broadcastState()
|
broadcastState()
|
||||||
return ok
|
return ok
|
||||||
})
|
})
|
||||||
ipcMain.handle(IPC.toggleChallenge, (_e, id: string, enabled: boolean) => {
|
ipcMain.handle(
|
||||||
const c = updateChallenge(id, { enabled })
|
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()
|
broadcastState()
|
||||||
return c
|
return c
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
ipcMain.handle(IPC.closeMatchSummary, () => hideReminderWindow())
|
ipcMain.handle(IPC.closeMatchSummary, () => hideReminderWindow())
|
||||||
|
|
||||||
// Dev helper: simulate a match end with given stats.
|
// Dev helper: simulate a match end with given stats. NEVER registered in
|
||||||
|
// packaged builds — a compromised renderer (XSS, malicious npm dep) could
|
||||||
|
// otherwise fabricate arbitrary match-end events at will.
|
||||||
|
if (!app.isPackaged) {
|
||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
'dev:simulateMatchEnd',
|
'dev:simulateMatchEnd',
|
||||||
(_e, id: GameId, stats: Record<string, number>) => {
|
(_e, id: GameId, stats: Record<string, number>) => {
|
||||||
simulateMatchEnd(id, stats)
|
simulateMatchEnd(id, stats)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Auto-updater
|
// Auto-updater
|
||||||
ipcMain.handle(IPC.updaterStatus, () => getUpdaterStatus())
|
ipcMain.handle(IPC.updaterStatus, () => getUpdaterStatus())
|
||||||
|
|||||||
@@ -4,10 +4,17 @@ import type { Tick } from '@shared/types'
|
|||||||
import { isQuietAt } from '@shared/types'
|
import { isQuietAt } from '@shared/types'
|
||||||
import { getExercises, getSettings, updateExercise } from './store'
|
import { getExercises, getSettings, updateExercise } from './store'
|
||||||
import { fireReminder } from './notifications'
|
import { fireReminder } from './notifications'
|
||||||
|
import { broadcastState } from './state-actions'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TICK_MS drives the per-second countdown UI; CHECK_MS gates the (cheaper)
|
||||||
|
* "is anything due to fire?" pass so we don't iterate exercises every second.
|
||||||
|
*/
|
||||||
const TICK_MS = 1000
|
const TICK_MS = 1000
|
||||||
const CHECK_MS = 5000
|
const CHECK_MS = 5000
|
||||||
|
|
||||||
let tickHandle: NodeJS.Timeout | null = null
|
let tickHandle: NodeJS.Timeout | null = null
|
||||||
|
let powerListenersArmed = false
|
||||||
let lastCheckAt = 0
|
let lastCheckAt = 0
|
||||||
let paused = false
|
let paused = false
|
||||||
|
|
||||||
@@ -16,22 +23,29 @@ function checkDueExercises(): void {
|
|||||||
const settings = getSettings()
|
const settings = getSettings()
|
||||||
if (!settings.globalEnabled) return
|
if (!settings.globalEnabled) return
|
||||||
|
|
||||||
// Inside the quiet window: defer all due fires to the next minute boundary.
|
// Inside the quiet window: defer all due fires until it closes. The next
|
||||||
// The next tick after the window closes will pick them up.
|
// CHECK_MS pass after the window ends will pick them up.
|
||||||
if (isQuietAt(settings.quietHours, new Date())) return
|
if (isQuietAt(settings.quietHours, new Date())) return
|
||||||
|
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const exercises = getExercises()
|
const exercises = getExercises()
|
||||||
|
let anyFired = false
|
||||||
for (const ex of exercises) {
|
for (const ex of exercises) {
|
||||||
if (!ex.enabled) continue
|
if (!ex.enabled) continue
|
||||||
if (ex.nextFireAt <= now) {
|
if (ex.nextFireAt <= now) {
|
||||||
const updated = updateExercise(ex.id, {
|
const updated = updateExercise(ex.id, {
|
||||||
nextFireAt: now + ex.intervalMinutes * 60_000
|
nextFireAt: now + ex.intervalMinutes * 60_000
|
||||||
})
|
})
|
||||||
if (updated) fireReminder(updated, settings.notificationMode)
|
if (updated) {
|
||||||
|
anyFired = true
|
||||||
|
fireReminder(updated, settings.notificationMode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Push fresh state so the renderer's Dashboard/Exercises pages don't show
|
||||||
|
// stale `nextFireAt` until the next state-changing IPC arrives.
|
||||||
|
if (anyFired) broadcastState()
|
||||||
|
}
|
||||||
|
|
||||||
function broadcastTicks(): void {
|
function broadcastTicks(): void {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
@@ -61,6 +75,11 @@ export function startScheduler(): void {
|
|||||||
// Run an immediate tick so renderer hydrates quickly.
|
// Run an immediate tick so renderer hydrates quickly.
|
||||||
tick()
|
tick()
|
||||||
|
|
||||||
|
// Only attach powerMonitor listeners once per process — startScheduler may
|
||||||
|
// be invoked again after stopScheduler in dev hot-reload paths and we don't
|
||||||
|
// want the same handler firing N times after a resume.
|
||||||
|
if (!powerListenersArmed) {
|
||||||
|
powerListenersArmed = true
|
||||||
powerMonitor.on('resume', () => {
|
powerMonitor.on('resume', () => {
|
||||||
lastCheckAt = 0
|
lastCheckAt = 0
|
||||||
tick()
|
tick()
|
||||||
@@ -70,6 +89,7 @@ export function startScheduler(): void {
|
|||||||
tick()
|
tick()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function stopScheduler(): void {
|
export function stopScheduler(): void {
|
||||||
if (tickHandle) {
|
if (tickHandle) {
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
import {
|
||||||
|
existsSync,
|
||||||
|
mkdirSync,
|
||||||
|
readFileSync,
|
||||||
|
renameSync,
|
||||||
|
unlinkSync,
|
||||||
|
writeFileSync
|
||||||
|
} from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import { randomUUID } from 'node:crypto'
|
import { randomUUID } from 'node:crypto'
|
||||||
import {
|
import {
|
||||||
@@ -14,9 +21,15 @@ import {
|
|||||||
Settings
|
Settings
|
||||||
} from '@shared/types'
|
} from '@shared/types'
|
||||||
|
|
||||||
/** Keep at most this many entries (~3 years if ~10/day). Trim oldest. */
|
/**
|
||||||
|
* Keep at most this many history entries (≈2.7 years at 10/day).
|
||||||
|
* When the cap is hit, drop oldest 10% so we don't trim on every write.
|
||||||
|
*/
|
||||||
const HISTORY_MAX = 10_000
|
const HISTORY_MAX = 10_000
|
||||||
|
|
||||||
|
const WRITE_DEBOUNCE_MS = 1500
|
||||||
|
const WRITE_RETRY_DELAYS = [50, 200, 800] // ms backoff on transient EBUSY/EPERM
|
||||||
|
|
||||||
let cache: AppState | null = null
|
let cache: AppState | null = null
|
||||||
let storePath = ''
|
let storePath = ''
|
||||||
let pendingWrite: NodeJS.Timeout | null = null
|
let pendingWrite: NodeJS.Timeout | null = null
|
||||||
@@ -66,26 +79,122 @@ function makeInitial(): AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Quarantine a corrupt state file so the user can recover it manually. */
|
||||||
|
function quarantineCorrupt(p: string, reason: string): void {
|
||||||
|
try {
|
||||||
|
const stamp = new Date()
|
||||||
|
.toISOString()
|
||||||
|
.replace(/[:.]/g, '-')
|
||||||
|
.replace(/Z$/, '')
|
||||||
|
const dest = `${p}.corrupt-${stamp}`
|
||||||
|
renameSync(p, dest)
|
||||||
|
console.error(
|
||||||
|
`[store] app-state.json was unreadable (${reason}); ` +
|
||||||
|
`moved to ${dest} and starting fresh.`
|
||||||
|
)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[store] failed to quarantine corrupt state file:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidParsed(v: unknown): v is Record<string, unknown> {
|
||||||
|
return typeof v === 'object' && v !== null && !Array.isArray(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current persisted-state schema version. Bump this and add a migration to
|
||||||
|
* MIGRATIONS whenever the on-disk shape changes in a non-additive way.
|
||||||
|
*
|
||||||
|
* Additive changes (new optional fields, new entries in `gamesEnabled`) do
|
||||||
|
* NOT need a version bump — DEFAULT_SETTINGS spread + the `?? []` guards in
|
||||||
|
* `coerce()` handle them gracefully.
|
||||||
|
*/
|
||||||
|
const CURRENT_SCHEMA_VERSION = 1
|
||||||
|
|
||||||
|
type StoredState = Record<string, unknown> & { __schemaVersion?: number }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrations are applied in order until the stored version matches CURRENT.
|
||||||
|
* Each fn returns the next-version state. The receiver may freely mutate.
|
||||||
|
*
|
||||||
|
* Note: the v0→v1 migration is a no-op — v1 is the inaugural schema. The
|
||||||
|
* machinery exists so future structural changes (e.g. splitting
|
||||||
|
* `quietHours.days` into a per-window record) have a single explicit place
|
||||||
|
* to live.
|
||||||
|
*/
|
||||||
|
const MIGRATIONS: Record<number, (s: StoredState) => StoredState> = {
|
||||||
|
0: (s) => s
|
||||||
|
}
|
||||||
|
|
||||||
|
function runMigrations(s: StoredState): StoredState {
|
||||||
|
let version = typeof s.__schemaVersion === 'number' ? s.__schemaVersion : 0
|
||||||
|
let cursor = s
|
||||||
|
while (version < CURRENT_SCHEMA_VERSION) {
|
||||||
|
const fn = MIGRATIONS[version]
|
||||||
|
if (!fn) {
|
||||||
|
console.warn(
|
||||||
|
`[store] no migration from v${version}; skipping ahead and hoping for the best.`
|
||||||
|
)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
cursor = fn(cursor)
|
||||||
|
version += 1
|
||||||
|
}
|
||||||
|
cursor.__schemaVersion = CURRENT_SCHEMA_VERSION
|
||||||
|
return cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Coerce a (possibly partial) migrated state into a fully-formed AppState. */
|
||||||
|
function coerce(s: StoredState): AppState {
|
||||||
|
return {
|
||||||
|
exercises: Array.isArray(s.exercises) ? (s.exercises as Exercise[]) : [],
|
||||||
|
settings: {
|
||||||
|
...DEFAULT_SETTINGS,
|
||||||
|
...(isValidParsed(s.settings) ? (s.settings as Partial<Settings>) : {})
|
||||||
|
},
|
||||||
|
challenges: Array.isArray(s.challenges)
|
||||||
|
? (s.challenges as Challenge[])
|
||||||
|
: [],
|
||||||
|
gamesEnabled: isValidParsed(s.gamesEnabled)
|
||||||
|
? (s.gamesEnabled as Partial<Record<GameId, boolean>>)
|
||||||
|
: {},
|
||||||
|
history: Array.isArray(s.history) ? (s.history as HistoryEntry[]) : []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function load(): AppState {
|
function load(): AppState {
|
||||||
const p = getStorePath()
|
const p = getStorePath()
|
||||||
if (!existsSync(p)) {
|
if (!existsSync(p)) {
|
||||||
const initial = makeInitial()
|
const initial = makeInitial()
|
||||||
writeFileSync(p, JSON.stringify(initial, null, 2), 'utf-8')
|
atomicWrite(
|
||||||
|
p,
|
||||||
|
JSON.stringify(
|
||||||
|
{ __schemaVersion: CURRENT_SCHEMA_VERSION, ...initial },
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
)
|
||||||
|
)
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
|
let raw: string
|
||||||
try {
|
try {
|
||||||
const raw = readFileSync(p, 'utf-8')
|
raw = readFileSync(p, 'utf-8')
|
||||||
const parsed = JSON.parse(raw) as Partial<AppState>
|
} catch (e) {
|
||||||
return {
|
console.error('[store] cannot read state file:', e)
|
||||||
exercises: parsed.exercises ?? [],
|
return makeInitial() // do not quarantine — we can't read it anyway
|
||||||
settings: { ...DEFAULT_SETTINGS, ...(parsed.settings ?? {}) },
|
|
||||||
challenges: parsed.challenges ?? [],
|
|
||||||
gamesEnabled: parsed.gamesEnabled ?? {},
|
|
||||||
history: parsed.history ?? []
|
|
||||||
}
|
}
|
||||||
} catch {
|
let parsed: unknown
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(raw)
|
||||||
|
} catch (e) {
|
||||||
|
quarantineCorrupt(p, `JSON parse error: ${String(e)}`)
|
||||||
return makeInitial()
|
return makeInitial()
|
||||||
}
|
}
|
||||||
|
if (!isValidParsed(parsed)) {
|
||||||
|
quarantineCorrupt(p, `expected object, got ${typeof parsed}`)
|
||||||
|
return makeInitial()
|
||||||
|
}
|
||||||
|
return coerce(runMigrations(parsed))
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendHistory(
|
function appendHistory(
|
||||||
@@ -98,11 +207,10 @@ function appendHistory(
|
|||||||
const entry: HistoryEntry = { ts: Date.now(), exerciseId, action }
|
const entry: HistoryEntry = { ts: Date.now(), exerciseId, action }
|
||||||
if (actualReps !== undefined) entry.actualReps = actualReps
|
if (actualReps !== undefined) entry.actualReps = actualReps
|
||||||
state.history.push(entry)
|
state.history.push(entry)
|
||||||
// Cap size — trim oldest 10% when over limit, so we don't trim every write.
|
|
||||||
if (state.history.length > HISTORY_MAX) {
|
if (state.history.length > HISTORY_MAX) {
|
||||||
state.history = state.history.slice(-Math.floor(HISTORY_MAX * 0.9))
|
state.history = state.history.slice(-Math.floor(HISTORY_MAX * 0.9))
|
||||||
}
|
}
|
||||||
scheduleWrite()
|
// Caller schedules the write; appendHistory itself is internal.
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getHistory(sinceMs?: number): HistoryEntry[] {
|
export function getHistory(sinceMs?: number): HistoryEntry[] {
|
||||||
@@ -115,17 +223,53 @@ export function clearHistory(beforeTs?: number): number {
|
|||||||
const state = getState()
|
const state = getState()
|
||||||
const before = state.history?.length ?? 0
|
const before = state.history?.length ?? 0
|
||||||
if (beforeTs == null) {
|
if (beforeTs == null) {
|
||||||
state.history = []
|
// Refuse a full wipe via IPC — callers must pass an explicit boundary.
|
||||||
} else {
|
// (Settings UI passes 0 to wipe everything; that's an opt-in.)
|
||||||
state.history = (state.history ?? []).filter((e) => e.ts >= beforeTs)
|
return 0
|
||||||
}
|
}
|
||||||
|
state.history = (state.history ?? []).filter((e) => e.ts >= beforeTs)
|
||||||
scheduleWrite()
|
scheduleWrite()
|
||||||
return before - (state.history?.length ?? 0)
|
return before - (state.history?.length ?? 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Atomically write to `path` via a sibling .tmp file + rename. Retries a few
|
||||||
|
* times on transient EBUSY/EPERM (AV/OneDrive holding the file).
|
||||||
|
*/
|
||||||
|
function atomicWrite(path: string, contents: string): void {
|
||||||
|
const tmp = `${path}.tmp`
|
||||||
|
let lastErr: unknown
|
||||||
|
for (let i = 0; i <= WRITE_RETRY_DELAYS.length; i++) {
|
||||||
|
try {
|
||||||
|
writeFileSync(tmp, contents, 'utf-8')
|
||||||
|
renameSync(tmp, path)
|
||||||
|
return
|
||||||
|
} catch (e) {
|
||||||
|
lastErr = e
|
||||||
|
// best-effort cleanup of the stale .tmp
|
||||||
|
try {
|
||||||
|
if (existsSync(tmp)) unlinkSync(tmp)
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
const delay = WRITE_RETRY_DELAYS[i]
|
||||||
|
if (delay === undefined) break
|
||||||
|
// Synchronous sleep — write path is short and called outside the hot loop.
|
||||||
|
const until = Date.now() + delay
|
||||||
|
while (Date.now() < until) {
|
||||||
|
/* spin */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.error('[store] atomic write failed after retries:', lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
function flush(): void {
|
function flush(): void {
|
||||||
if (!cache) return
|
if (!cache) return
|
||||||
writeFileSync(getStorePath(), JSON.stringify(cache, null, 2), 'utf-8')
|
// Persist the schema version alongside the state so future migrations know
|
||||||
|
// where to pick up from. The renderer never reads this key.
|
||||||
|
const payload = { __schemaVersion: CURRENT_SCHEMA_VERSION, ...cache }
|
||||||
|
atomicWrite(getStorePath(), JSON.stringify(payload, null, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
function scheduleWrite(): void {
|
function scheduleWrite(): void {
|
||||||
@@ -133,7 +277,10 @@ function scheduleWrite(): void {
|
|||||||
pendingWrite = setTimeout(() => {
|
pendingWrite = setTimeout(() => {
|
||||||
pendingWrite = null
|
pendingWrite = null
|
||||||
flush()
|
flush()
|
||||||
}, 1500)
|
}, WRITE_DEBOUNCE_MS)
|
||||||
|
// Don't keep the event loop alive solely for a pending write — `before-quit`
|
||||||
|
// calls `flushNow()` and we explicitly want the process to exit on schedule.
|
||||||
|
pendingWrite.unref?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getState(): AppState {
|
export function getState(): AppState {
|
||||||
@@ -178,9 +325,15 @@ export function updateExercise(
|
|||||||
const idx = state.exercises.findIndex((e) => e.id === id)
|
const idx = state.exercises.findIndex((e) => e.id === id)
|
||||||
if (idx === -1) return undefined
|
if (idx === -1) return undefined
|
||||||
const prev = state.exercises[idx]
|
const prev = state.exercises[idx]
|
||||||
const merged: Exercise = { ...prev, ...patch }
|
// Drop `id` from the patch even though the type forbids it — runtime callers
|
||||||
|
// (IPC) can still smuggle it through. We never let the id change.
|
||||||
|
const { id: _ignoredId, ...safePatch } = patch as Partial<Exercise>
|
||||||
|
const merged: Exercise = { ...prev, ...safePatch }
|
||||||
// If interval changed, reschedule from now.
|
// If interval changed, reschedule from now.
|
||||||
if (patch.intervalMinutes !== undefined && patch.intervalMinutes !== prev.intervalMinutes) {
|
if (
|
||||||
|
patch.intervalMinutes !== undefined &&
|
||||||
|
patch.intervalMinutes !== prev.intervalMinutes
|
||||||
|
) {
|
||||||
merged.nextFireAt = Date.now() + merged.intervalMinutes * 60_000
|
merged.nextFireAt = Date.now() + merged.intervalMinutes * 60_000
|
||||||
}
|
}
|
||||||
state.exercises[idx] = merged
|
state.exercises[idx] = merged
|
||||||
@@ -258,7 +411,9 @@ export function updateChallenge(
|
|||||||
const state = getState()
|
const state = getState()
|
||||||
const idx = state.challenges.findIndex((c) => c.id === id)
|
const idx = state.challenges.findIndex((c) => c.id === id)
|
||||||
if (idx === -1) return undefined
|
if (idx === -1) return undefined
|
||||||
state.challenges[idx] = { ...state.challenges[idx], ...patch }
|
// Same id-strip as updateExercise.
|
||||||
|
const { id: _ignoredId, ...safePatch } = patch as Partial<Challenge>
|
||||||
|
state.challenges[idx] = { ...state.challenges[idx], ...safePatch }
|
||||||
scheduleWrite()
|
scheduleWrite()
|
||||||
return state.challenges[idx]
|
return state.challenges[idx]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,45 @@ import { join } from 'node:path'
|
|||||||
import { showMainWindow } from './windows'
|
import { showMainWindow } from './windows'
|
||||||
import { isPaused, setPaused, forceCheck } from './scheduler'
|
import { isPaused, setPaused, forceCheck } from './scheduler'
|
||||||
import { snoozeAll } from './state-actions'
|
import { snoozeAll } from './state-actions'
|
||||||
|
import { getSettings } from './store'
|
||||||
|
import type { Language } from '@shared/types'
|
||||||
|
|
||||||
let tray: Tray | null = null
|
let tray: Tray | null = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimal tray-side localisation. The renderer's full i18n dict lives in
|
||||||
|
* `src/renderer/src/i18n/dict.ts` and isn't reachable from the main process
|
||||||
|
* tsconfig, so we keep the 5 strings the tray actually uses here.
|
||||||
|
*/
|
||||||
|
const TRAY_STRINGS: Record<Language, Record<string, string>> = {
|
||||||
|
ru: {
|
||||||
|
open: 'Открыть',
|
||||||
|
pause: 'Пауза напоминаний',
|
||||||
|
resume: 'Возобновить напоминания',
|
||||||
|
snooze15: 'Отложить все на 15 мин',
|
||||||
|
quit: 'Выход'
|
||||||
|
},
|
||||||
|
en: {
|
||||||
|
open: 'Open',
|
||||||
|
pause: 'Pause reminders',
|
||||||
|
resume: 'Resume reminders',
|
||||||
|
snooze15: 'Snooze all 15 min',
|
||||||
|
quit: 'Quit'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function trayLabel(key: string): string {
|
||||||
|
// getSettings reads from cache; if the store hasn't loaded yet (very early
|
||||||
|
// boot) it lazily reads from disk. Defaults to 'ru' if anything goes wrong.
|
||||||
|
let lang: Language = 'ru'
|
||||||
|
try {
|
||||||
|
lang = getSettings().language ?? 'ru'
|
||||||
|
} catch {
|
||||||
|
/* keep default */
|
||||||
|
}
|
||||||
|
return TRAY_STRINGS[lang]?.[key] ?? TRAY_STRINGS.ru[key] ?? key
|
||||||
|
}
|
||||||
|
|
||||||
function resolveTrayIcon(): Electron.NativeImage {
|
function resolveTrayIcon(): Electron.NativeImage {
|
||||||
// Try resources/, fallback to a transparent 16x16 if missing during dev.
|
// Try resources/, fallback to a transparent 16x16 if missing during dev.
|
||||||
const candidates = [
|
const candidates = [
|
||||||
@@ -35,10 +71,10 @@ export function refreshMenu(): void {
|
|||||||
if (!tray) return
|
if (!tray) return
|
||||||
const paused = isPaused()
|
const paused = isPaused()
|
||||||
const menu = Menu.buildFromTemplate([
|
const menu = Menu.buildFromTemplate([
|
||||||
{ label: 'Открыть', click: () => showMainWindow() },
|
{ label: trayLabel('open'), click: () => showMainWindow() },
|
||||||
{ type: 'separator' },
|
{ type: 'separator' },
|
||||||
{
|
{
|
||||||
label: paused ? 'Возобновить напоминания' : 'Пауза напоминаний',
|
label: paused ? trayLabel('resume') : trayLabel('pause'),
|
||||||
click: () => {
|
click: () => {
|
||||||
setPaused(!paused)
|
setPaused(!paused)
|
||||||
refreshMenu()
|
refreshMenu()
|
||||||
@@ -46,12 +82,12 @@ export function refreshMenu(): void {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Отложить все на 15 мин',
|
label: trayLabel('snooze15'),
|
||||||
click: () => snoozeAll(15)
|
click: () => snoozeAll(15)
|
||||||
},
|
},
|
||||||
{ type: 'separator' },
|
{ type: 'separator' },
|
||||||
{
|
{
|
||||||
label: 'Выход',
|
label: trayLabel('quit'),
|
||||||
click: () => {
|
click: () => {
|
||||||
app.quit()
|
app.quit()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,32 @@ import { IPC } from '@shared/ipc'
|
|||||||
import type { UpdaterStatus } from '@shared/types'
|
import type { UpdaterStatus } from '@shared/types'
|
||||||
|
|
||||||
let currentStatus: UpdaterStatus = { kind: 'idle' }
|
let currentStatus: UpdaterStatus = { kind: 'idle' }
|
||||||
|
let lastCheckedAt: number | undefined
|
||||||
let wired = false
|
let wired = false
|
||||||
let checkInterval: NodeJS.Timeout | null = null
|
let checkInterval: NodeJS.Timeout | null = null
|
||||||
|
|
||||||
|
// User-initiated checks surface errors. Background checks stay quiet to avoid
|
||||||
|
// the red banner on transient network blips (504s, DNS, captive portals).
|
||||||
|
let silentMode = false
|
||||||
|
|
||||||
const CHECK_INTERVAL_MS = 60 * 60 * 1000 // every hour
|
const CHECK_INTERVAL_MS = 60 * 60 * 1000 // every hour
|
||||||
|
const BOOT_DELAY_MS = 5_000
|
||||||
|
// Boot retry: if the first check fails (e.g. network not yet up), retry a few
|
||||||
|
// times with exponential backoff before giving up until the hourly tick.
|
||||||
|
const BOOT_RETRY_DELAYS = [30_000, 120_000, 300_000] // 30s, 2min, 5min
|
||||||
|
|
||||||
export function getUpdaterStatus(): UpdaterStatus {
|
export function getUpdaterStatus(): UpdaterStatus {
|
||||||
return currentStatus
|
return currentStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
function setStatus(s: UpdaterStatus): void {
|
function setStatus(s: UpdaterStatus): void {
|
||||||
|
// Preserve lastCheckedAt across status transitions where applicable.
|
||||||
|
if (s.kind === 'not-available' || s.kind === 'idle') {
|
||||||
|
if (lastCheckedAt && !('lastCheckedAt' in s)) {
|
||||||
|
const withTs = s as { lastCheckedAt?: number }
|
||||||
|
withTs.lastCheckedAt = lastCheckedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
currentStatus = s
|
currentStatus = s
|
||||||
for (const win of BrowserWindow.getAllWindows()) {
|
for (const win of BrowserWindow.getAllWindows()) {
|
||||||
if (!win.isDestroyed()) win.webContents.send(IPC.evtUpdaterStatus, s)
|
if (!win.isDestroyed()) win.webContents.send(IPC.evtUpdaterStatus, s)
|
||||||
@@ -38,9 +54,14 @@ export function initUpdater(): void {
|
|||||||
autoUpdater.autoInstallOnAppQuit = true
|
autoUpdater.autoInstallOnAppQuit = true
|
||||||
autoUpdater.allowDowngrade = false
|
autoUpdater.allowDowngrade = false
|
||||||
|
|
||||||
autoUpdater.on('checking-for-update', () => setStatus({ kind: 'checking' }))
|
autoUpdater.on('checking-for-update', () => {
|
||||||
|
// Don't replace the prior status with "checking" during silent polls — the
|
||||||
|
// UI would briefly flicker for users opening Settings during a tick.
|
||||||
|
if (!silentMode) setStatus({ kind: 'checking' })
|
||||||
|
})
|
||||||
|
|
||||||
autoUpdater.on('update-available', (info) => {
|
autoUpdater.on('update-available', (info) => {
|
||||||
|
lastCheckedAt = Date.now()
|
||||||
setStatus({
|
setStatus({
|
||||||
kind: 'available',
|
kind: 'available',
|
||||||
version: info.version,
|
version: info.version,
|
||||||
@@ -50,7 +71,12 @@ export function initUpdater(): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
autoUpdater.on('update-not-available', () => {
|
autoUpdater.on('update-not-available', () => {
|
||||||
setStatus({ kind: 'not-available', currentVersion: app.getVersion() })
|
lastCheckedAt = Date.now()
|
||||||
|
setStatus({
|
||||||
|
kind: 'not-available',
|
||||||
|
currentVersion: app.getVersion(),
|
||||||
|
lastCheckedAt
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
autoUpdater.on('download-progress', (p) => {
|
autoUpdater.on('download-progress', (p) => {
|
||||||
@@ -68,23 +94,43 @@ export function initUpdater(): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
autoUpdater.on('error', (err) => {
|
autoUpdater.on('error', (err) => {
|
||||||
setStatus({
|
const message = err instanceof Error ? err.message : String(err)
|
||||||
kind: 'error',
|
if (silentMode) {
|
||||||
message: err instanceof Error ? err.message : String(err)
|
// Background check failed — keep previous status, don't show red banner.
|
||||||
})
|
// Will retry on the next hourly tick.
|
||||||
|
console.warn('[updater] silent check failed:', message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setStatus({ kind: 'error', message })
|
||||||
})
|
})
|
||||||
|
|
||||||
// First check on boot (slight delay so window has time to subscribe).
|
// First check on boot with retry-on-failure.
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
void checkForUpdates()
|
void bootCheckWithRetry()
|
||||||
}, 5_000)
|
}, BOOT_DELAY_MS)
|
||||||
|
|
||||||
// Periodic re-check
|
// Periodic re-check (silent).
|
||||||
checkInterval = setInterval(() => {
|
checkInterval = setInterval(() => {
|
||||||
void checkForUpdates()
|
void checkForUpdates({ silent: true })
|
||||||
}, CHECK_INTERVAL_MS)
|
}, CHECK_INTERVAL_MS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function bootCheckWithRetry(): Promise<void> {
|
||||||
|
for (let attempt = 0; attempt <= BOOT_RETRY_DELAYS.length; attempt++) {
|
||||||
|
await checkForUpdates({ silent: true })
|
||||||
|
if (
|
||||||
|
currentStatus.kind === 'available' ||
|
||||||
|
currentStatus.kind === 'not-available' ||
|
||||||
|
currentStatus.kind === 'downloaded'
|
||||||
|
) {
|
||||||
|
return // success
|
||||||
|
}
|
||||||
|
const delay = BOOT_RETRY_DELAYS[attempt]
|
||||||
|
if (delay === undefined) return // exhausted retries
|
||||||
|
await new Promise((r) => setTimeout(r, delay))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function stopUpdater(): void {
|
export function stopUpdater(): void {
|
||||||
if (checkInterval) {
|
if (checkInterval) {
|
||||||
clearInterval(checkInterval)
|
clearInterval(checkInterval)
|
||||||
@@ -92,15 +138,22 @@ export function stopUpdater(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkForUpdates(): Promise<UpdaterStatus> {
|
export async function checkForUpdates(
|
||||||
|
opts: { silent?: boolean } = {}
|
||||||
|
): Promise<UpdaterStatus> {
|
||||||
if (!app.isPackaged) return currentStatus
|
if (!app.isPackaged) return currentStatus
|
||||||
|
silentMode = opts.silent ?? false
|
||||||
try {
|
try {
|
||||||
await autoUpdater.checkForUpdates()
|
await autoUpdater.checkForUpdates()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setStatus({
|
const message = err instanceof Error ? err.message : String(err)
|
||||||
kind: 'error',
|
if (silentMode) {
|
||||||
message: err instanceof Error ? err.message : String(err)
|
console.warn('[updater] silent check failed (sync):', message)
|
||||||
})
|
} else {
|
||||||
|
setStatus({ kind: 'error', message })
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
silentMode = false
|
||||||
}
|
}
|
||||||
return currentStatus
|
return currentStatus
|
||||||
}
|
}
|
||||||
|
|||||||
408
src/main/validate.test.ts
Normal file
408
src/main/validate.test.ts
Normal file
@@ -0,0 +1,408 @@
|
|||||||
|
/**
|
||||||
|
* Тесты для IPC validation layer.
|
||||||
|
*
|
||||||
|
* Этот слой — security-boundary между renderer и main. Если он сломается,
|
||||||
|
* compromised renderer сможет писать в стор NaN, отрицательные, Infinity,
|
||||||
|
* сверхдлинные строки или undefined-enum'ы. Поэтому покрытие важно для:
|
||||||
|
*
|
||||||
|
* 1. Тип-проверок (строка/число/булево/массив)
|
||||||
|
* 2. Range-checks (reps ∈ [1,9999], minutes ∈ [1,1440] и т.д.)
|
||||||
|
* 3. Enum allowlist (theme/lang/notify-mode/stat)
|
||||||
|
* 4. Edge cases: NaN, Infinity, MAX_SAFE_INTEGER, 0, отрицательные, длина строк
|
||||||
|
* 5. Partial-patch semantics (отсутствие поля ≠ невалидное значение)
|
||||||
|
* 6. Сложный nested case: quietHours с HH:MM regex и dedup days
|
||||||
|
*/
|
||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import {
|
||||||
|
validateExerciseInput,
|
||||||
|
validateExercisePatch,
|
||||||
|
validateChallengeInput,
|
||||||
|
validateChallengePatch,
|
||||||
|
validateSettingsPatch,
|
||||||
|
validateId,
|
||||||
|
validateActualReps,
|
||||||
|
validateSnoozeMinutes
|
||||||
|
} from './validate'
|
||||||
|
|
||||||
|
const validExercise = {
|
||||||
|
name: 'Push-ups',
|
||||||
|
reps: 10,
|
||||||
|
intervalMinutes: 30,
|
||||||
|
icon: 'Dumbbell',
|
||||||
|
enabled: true
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('validateExerciseInput', () => {
|
||||||
|
it('accepts a fully-formed valid input', () => {
|
||||||
|
expect(validateExerciseInput(validExercise)).toEqual(validExercise)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-objects', () => {
|
||||||
|
expect(validateExerciseInput(null)).toBeNull()
|
||||||
|
expect(validateExerciseInput(undefined)).toBeNull()
|
||||||
|
expect(validateExerciseInput('string')).toBeNull()
|
||||||
|
expect(validateExerciseInput(42)).toBeNull()
|
||||||
|
expect(validateExerciseInput([])).toBeNull() // arrays not allowed
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects missing required fields', () => {
|
||||||
|
expect(validateExerciseInput({ ...validExercise, name: undefined })).toBeNull()
|
||||||
|
expect(validateExerciseInput({ ...validExercise, reps: undefined })).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, intervalMinutes: undefined })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects out-of-range reps', () => {
|
||||||
|
expect(validateExerciseInput({ ...validExercise, reps: 0 })).toBeNull()
|
||||||
|
expect(validateExerciseInput({ ...validExercise, reps: -1 })).toBeNull()
|
||||||
|
expect(validateExerciseInput({ ...validExercise, reps: 10_000 })).toBeNull()
|
||||||
|
expect(validateExerciseInput({ ...validExercise, reps: NaN })).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, reps: Infinity })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('truncates reps with Math.trunc (5.7 → 5)', () => {
|
||||||
|
const r = validateExerciseInput({ ...validExercise, reps: 5.7 })
|
||||||
|
expect(r?.reps).toBe(5)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects out-of-range intervalMinutes (> 24h)', () => {
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, intervalMinutes: 0 })
|
||||||
|
).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, intervalMinutes: 1441 })
|
||||||
|
).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, intervalMinutes: -1 })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects empty name', () => {
|
||||||
|
expect(validateExerciseInput({ ...validExercise, name: '' })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects name longer than MAX_STR_LEN (200)', () => {
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, name: 'x'.repeat(201) })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts name exactly at MAX_STR_LEN', () => {
|
||||||
|
const r = validateExerciseInput({ ...validExercise, name: 'x'.repeat(200) })
|
||||||
|
expect(r?.name).toHaveLength(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('defaults icon to Activity if missing', () => {
|
||||||
|
const { icon: _ignored, ...rest } = validExercise
|
||||||
|
void _ignored
|
||||||
|
expect(validateExerciseInput(rest)?.icon).toBe('Activity')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('defaults enabled to true if missing', () => {
|
||||||
|
const { enabled: _ignored, ...rest } = validExercise
|
||||||
|
void _ignored
|
||||||
|
expect(validateExerciseInput(rest)?.enabled).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Дизайн validateExerciseInput: required-поля (name/reps/intervalMinutes)
|
||||||
|
// строгие — невалидное значение reject'ит весь input. Optional-поля
|
||||||
|
// (icon/enabled) lenient — невалидное молча подменяется дефолтом. Это
|
||||||
|
// фиксирует контракт: malicious renderer не сможет создать запись с
|
||||||
|
// reps=-1, но если он пришлёт `enabled: 'yes'`, получит просто enabled=true.
|
||||||
|
it('coerces invalid enabled to true (lenient default for optional fields)', () => {
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, enabled: 'yes' })?.enabled
|
||||||
|
).toBe(true)
|
||||||
|
expect(
|
||||||
|
validateExerciseInput({ ...validExercise, enabled: 1 })?.enabled
|
||||||
|
).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
// А вот в patch optional-поля строгие — нет defaults, есть `if (v ===
|
||||||
|
// undefined) return null`. Это правильнее: если renderer пришёл с патчем,
|
||||||
|
// в котором есть поле, оно должно быть валидным.
|
||||||
|
it('strict patch: rejects invalid enabled in patch (unlike input)', () => {
|
||||||
|
expect(validateExercisePatch({ enabled: 'yes' })).toBeNull()
|
||||||
|
expect(validateExercisePatch({ enabled: 1 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-string name', () => {
|
||||||
|
expect(validateExerciseInput({ ...validExercise, name: 42 })).toBeNull()
|
||||||
|
expect(validateExerciseInput({ ...validExercise, name: null })).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateExercisePatch', () => {
|
||||||
|
it('accepts an empty patch (no-op update)', () => {
|
||||||
|
expect(validateExercisePatch({})).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
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 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects patch with a single invalid field', () => {
|
||||||
|
// Patch is all-or-nothing: one bad field rejects the whole patch.
|
||||||
|
expect(validateExercisePatch({ name: 'OK', reps: -1 })).toBeNull()
|
||||||
|
expect(validateExercisePatch({ name: '', reps: 10 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-object', () => {
|
||||||
|
expect(validateExercisePatch(null)).toBeNull()
|
||||||
|
expect(validateExercisePatch([])).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts nextFireAt and lastDoneAt with valid ranges', () => {
|
||||||
|
expect(validateExercisePatch({ nextFireAt: 0 })).toEqual({ nextFireAt: 0 })
|
||||||
|
expect(validateExercisePatch({ lastDoneAt: 1_000_000_000_000 })).toEqual({
|
||||||
|
lastDoneAt: 1_000_000_000_000
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects negative timestamps', () => {
|
||||||
|
expect(validateExercisePatch({ nextFireAt: -1 })).toBeNull()
|
||||||
|
expect(validateExercisePatch({ lastDoneAt: -1 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects NaN/Infinity timestamps', () => {
|
||||||
|
expect(validateExercisePatch({ nextFireAt: NaN })).toBeNull()
|
||||||
|
expect(validateExercisePatch({ nextFireAt: Infinity })).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateChallengeInput', () => {
|
||||||
|
const valid = {
|
||||||
|
name: 'Deaths → squats',
|
||||||
|
gameId: 'dota2',
|
||||||
|
stat: 'deaths' as const,
|
||||||
|
multiplier: 3,
|
||||||
|
exerciseName: 'Приседания',
|
||||||
|
icon: 'Activity',
|
||||||
|
enabled: true
|
||||||
|
}
|
||||||
|
|
||||||
|
it('accepts valid input', () => {
|
||||||
|
expect(validateChallengeInput(valid)).toEqual(valid)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects unknown stat', () => {
|
||||||
|
expect(validateChallengeInput({ ...valid, stat: 'pizza' })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts all valid stats', () => {
|
||||||
|
const stats = ['deaths', 'kills', 'assists', 'last_hits', 'denies', 'duration_min']
|
||||||
|
for (const stat of stats) {
|
||||||
|
expect(validateChallengeInput({ ...valid, stat })).not.toBeNull()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects negative multiplier', () => {
|
||||||
|
expect(validateChallengeInput({ ...valid, multiplier: -1 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects multiplier > 1000', () => {
|
||||||
|
expect(validateChallengeInput({ ...valid, multiplier: 1001 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts zero multiplier (legitimate "disable" semantics)', () => {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateChallengePatch', () => {
|
||||||
|
it('accepts empty patch', () => {
|
||||||
|
expect(validateChallengePatch({})).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects unknown stat in patch', () => {
|
||||||
|
expect(validateChallengePatch({ stat: 'mana' })).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateSettingsPatch', () => {
|
||||||
|
it('accepts empty patch', () => {
|
||||||
|
expect(validateSettingsPatch({})).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts each boolean toggle independently', () => {
|
||||||
|
expect(validateSettingsPatch({ globalEnabled: false })).toEqual({
|
||||||
|
globalEnabled: false
|
||||||
|
})
|
||||||
|
expect(validateSettingsPatch({ soundEnabled: true })).toEqual({
|
||||||
|
soundEnabled: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects unknown theme', () => {
|
||||||
|
expect(validateSettingsPatch({ theme: 'sepia' })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts all valid themes', () => {
|
||||||
|
expect(validateSettingsPatch({ theme: 'light' })?.theme).toBe('light')
|
||||||
|
expect(validateSettingsPatch({ theme: 'dark' })?.theme).toBe('dark')
|
||||||
|
expect(validateSettingsPatch({ theme: 'system' })?.theme).toBe('system')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects unknown language', () => {
|
||||||
|
expect(validateSettingsPatch({ language: 'fr' })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects unknown notification mode', () => {
|
||||||
|
expect(validateSettingsPatch({ notificationMode: 'sms' })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects out-of-range snoozeMinutes', () => {
|
||||||
|
expect(validateSettingsPatch({ snoozeMinutes: 0 })).toBeNull()
|
||||||
|
expect(validateSettingsPatch({ snoozeMinutes: 1441 })).toBeNull()
|
||||||
|
expect(validateSettingsPatch({ snoozeMinutes: -5 })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('quietHours subobject', () => {
|
||||||
|
const baseQh = {
|
||||||
|
enabled: true,
|
||||||
|
from: '22:00',
|
||||||
|
to: '08:00',
|
||||||
|
days: [0, 1, 2, 3, 4, 5, 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
it('accepts a valid quietHours', () => {
|
||||||
|
expect(validateSettingsPatch({ quietHours: baseQh })?.quietHours).toEqual(
|
||||||
|
baseQh
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-object quietHours', () => {
|
||||||
|
expect(validateSettingsPatch({ quietHours: 'always' })).toBeNull()
|
||||||
|
expect(validateSettingsPatch({ quietHours: null })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects malformed HH:MM', () => {
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, from: '2500' } })
|
||||||
|
).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, to: 'bedtime' } })
|
||||||
|
).toBeNull()
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, from: '8' } })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts HH:MM with 1-digit hour (9:30)', () => {
|
||||||
|
// Regex is /^\d{1,2}:\d{2}$/ — допускаем «9:30», парсер сам разберётся.
|
||||||
|
const r = validateSettingsPatch({
|
||||||
|
quietHours: { ...baseQh, from: '9:30' }
|
||||||
|
})
|
||||||
|
expect(r?.quietHours?.from).toBe('9:30')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dedupes days array', () => {
|
||||||
|
const r = validateSettingsPatch({
|
||||||
|
quietHours: { ...baseQh, days: [1, 2, 2, 3, 1] }
|
||||||
|
})
|
||||||
|
expect(r?.quietHours?.days).toEqual([1, 2, 3])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects out-of-range day (7)', () => {
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, days: [0, 7] } })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects negative day', () => {
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, days: [-1] } })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-array days', () => {
|
||||||
|
expect(
|
||||||
|
validateSettingsPatch({ quietHours: { ...baseQh, days: 'all' } })
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts empty days array (window effectively disabled)', () => {
|
||||||
|
const r = validateSettingsPatch({
|
||||||
|
quietHours: { ...baseQh, days: [] }
|
||||||
|
})
|
||||||
|
expect(r?.quietHours?.days).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
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')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-strings', () => {
|
||||||
|
expect(validateId(42)).toBeNull()
|
||||||
|
expect(validateId(null)).toBeNull()
|
||||||
|
expect(validateId(undefined)).toBeNull()
|
||||||
|
expect(validateId({})).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects empty string', () => {
|
||||||
|
expect(validateId('')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects strings longer than 64 chars', () => {
|
||||||
|
expect(validateId('x'.repeat(65))).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateActualReps', () => {
|
||||||
|
it('returns undefined for undefined/null (means: use planned reps)', () => {
|
||||||
|
expect(validateActualReps(undefined)).toBeUndefined()
|
||||||
|
expect(validateActualReps(null)).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts zero (partial completion = "did 0 of 10")', () => {
|
||||||
|
expect(validateActualReps(0)).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('accepts large values up to cap', () => {
|
||||||
|
expect(validateActualReps(100_000)).toBe(100_000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects negative', () => {
|
||||||
|
expect(validateActualReps(-1)).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects values above cap', () => {
|
||||||
|
expect(validateActualReps(100_001)).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects NaN/Infinity', () => {
|
||||||
|
expect(validateActualReps(NaN)).toBeUndefined()
|
||||||
|
expect(validateActualReps(Infinity)).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('validateSnoozeMinutes', () => {
|
||||||
|
it('accepts valid minutes', () => {
|
||||||
|
expect(validateSnoozeMinutes(15)).toBe(15)
|
||||||
|
expect(validateSnoozeMinutes(1)).toBe(1)
|
||||||
|
expect(validateSnoozeMinutes(1440)).toBe(1440)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects 0 and above 24h', () => {
|
||||||
|
expect(validateSnoozeMinutes(0)).toBeNull()
|
||||||
|
expect(validateSnoozeMinutes(1441)).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects non-numbers', () => {
|
||||||
|
expect(validateSnoozeMinutes('15')).toBeNull()
|
||||||
|
expect(validateSnoozeMinutes(null)).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
311
src/main/validate.ts
Normal file
311
src/main/validate.ts
Normal file
@@ -0,0 +1,311 @@
|
|||||||
|
/**
|
||||||
|
* Hand-rolled runtime validators for IPC payloads.
|
||||||
|
*
|
||||||
|
* TypeScript types are erased at compile time — a compromised or buggy
|
||||||
|
* renderer can still send arbitrary JSON across the IPC boundary. These
|
||||||
|
* helpers enforce shape, type and range BEFORE the data hits the store.
|
||||||
|
*
|
||||||
|
* Philosophy: be lenient with unknown fields (drop them silently), strict
|
||||||
|
* about known fields (reject the call if a known field is the wrong type
|
||||||
|
* or out of range). Never throw to the renderer; return a sanitised value
|
||||||
|
* or `null` and the caller decides what to do.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type {
|
||||||
|
Challenge,
|
||||||
|
Exercise,
|
||||||
|
GameStat,
|
||||||
|
Settings,
|
||||||
|
Theme,
|
||||||
|
Language,
|
||||||
|
NotificationMode
|
||||||
|
} from '@shared/types'
|
||||||
|
|
||||||
|
const MAX_STR_LEN = 200
|
||||||
|
const VALID_THEMES: Theme[] = ['system', 'light', 'dark']
|
||||||
|
const VALID_LANGS: Language[] = ['ru', 'en']
|
||||||
|
const VALID_NOTIFY: NotificationMode[] = ['toast', 'modal', 'both']
|
||||||
|
const VALID_STATS: GameStat[] = [
|
||||||
|
'deaths',
|
||||||
|
'kills',
|
||||||
|
'assists',
|
||||||
|
'last_hits',
|
||||||
|
'denies',
|
||||||
|
'duration_min'
|
||||||
|
]
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeStr(v: unknown, max = MAX_STR_LEN): string | undefined {
|
||||||
|
if (typeof v !== 'string') return undefined
|
||||||
|
if (v.length === 0 || v.length > max) return undefined
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
function intInRange(v: unknown, min: number, max: number): number | undefined {
|
||||||
|
if (typeof v !== 'number' || !Number.isFinite(v)) return undefined
|
||||||
|
const n = Math.trunc(v)
|
||||||
|
if (n < min || n > max) return undefined
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
function numInRange(v: unknown, min: number, max: number): number | undefined {
|
||||||
|
if (typeof v !== 'number' || !Number.isFinite(v)) return undefined
|
||||||
|
if (v < min || v > max) return undefined
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
function bool(v: unknown): boolean | undefined {
|
||||||
|
return typeof v === 'boolean' ? v : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function oneOf<T extends string>(
|
||||||
|
v: unknown,
|
||||||
|
allowed: readonly T[]
|
||||||
|
): T | undefined {
|
||||||
|
return typeof v === 'string' && (allowed as readonly string[]).includes(v)
|
||||||
|
? (v as T)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Exercise validators
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
export function validateExerciseInput(
|
||||||
|
raw: unknown
|
||||||
|
): Omit<Exercise, 'id' | 'nextFireAt' | 'lastDoneAt'> | null {
|
||||||
|
if (!isObj(raw)) return null
|
||||||
|
const name = safeStr(raw.name)
|
||||||
|
const reps = intInRange(raw.reps, 1, 9999)
|
||||||
|
const intervalMinutes = intInRange(raw.intervalMinutes, 1, 24 * 60)
|
||||||
|
const icon = safeStr(raw.icon, 64) ?? 'Activity'
|
||||||
|
const enabled = bool(raw.enabled) ?? true
|
||||||
|
if (
|
||||||
|
name === undefined ||
|
||||||
|
reps === undefined ||
|
||||||
|
intervalMinutes === undefined
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return { name, reps, intervalMinutes, icon, enabled }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateExercisePatch(
|
||||||
|
raw: unknown
|
||||||
|
): Partial<Omit<Exercise, 'id'>> | null {
|
||||||
|
if (!isObj(raw)) return null
|
||||||
|
const out: Partial<Omit<Exercise, 'id'>> = {}
|
||||||
|
if ('name' in raw) {
|
||||||
|
const v = safeStr(raw.name)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.name = v
|
||||||
|
}
|
||||||
|
if ('reps' in raw) {
|
||||||
|
const v = intInRange(raw.reps, 1, 9999)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.reps = v
|
||||||
|
}
|
||||||
|
if ('intervalMinutes' in raw) {
|
||||||
|
const v = intInRange(raw.intervalMinutes, 1, 24 * 60)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.intervalMinutes = v
|
||||||
|
}
|
||||||
|
if ('icon' in raw) {
|
||||||
|
const v = safeStr(raw.icon, 64)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.icon = v
|
||||||
|
}
|
||||||
|
if ('enabled' in raw) {
|
||||||
|
const v = bool(raw.enabled)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.enabled = v
|
||||||
|
}
|
||||||
|
// Allow scheduler-controlled fields to be patched (used by store.markDone
|
||||||
|
// through this same boundary), but range-check them.
|
||||||
|
if ('nextFireAt' in raw) {
|
||||||
|
const v = numInRange(raw.nextFireAt, 0, Number.MAX_SAFE_INTEGER)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.nextFireAt = v
|
||||||
|
}
|
||||||
|
if ('lastDoneAt' in raw) {
|
||||||
|
const v = numInRange(raw.lastDoneAt, 0, Number.MAX_SAFE_INTEGER)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.lastDoneAt = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Challenge validators
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
export function validateChallengeInput(
|
||||||
|
raw: unknown
|
||||||
|
): Omit<Challenge, 'id'> | null {
|
||||||
|
if (!isObj(raw)) return null
|
||||||
|
const name = safeStr(raw.name)
|
||||||
|
const gameId = safeStr(raw.gameId, 32)
|
||||||
|
const stat = oneOf(raw.stat, VALID_STATS)
|
||||||
|
const multiplier = numInRange(raw.multiplier, 0, 1000)
|
||||||
|
const exerciseName = safeStr(raw.exerciseName)
|
||||||
|
const icon = safeStr(raw.icon, 64) ?? 'Activity'
|
||||||
|
const enabled = bool(raw.enabled) ?? true
|
||||||
|
if (
|
||||||
|
name === undefined ||
|
||||||
|
gameId === undefined ||
|
||||||
|
stat === undefined ||
|
||||||
|
multiplier === undefined ||
|
||||||
|
exerciseName === undefined
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
gameId: gameId as Challenge['gameId'],
|
||||||
|
stat,
|
||||||
|
multiplier,
|
||||||
|
exerciseName,
|
||||||
|
icon,
|
||||||
|
enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateChallengePatch(
|
||||||
|
raw: unknown
|
||||||
|
): Partial<Omit<Challenge, 'id'>> | null {
|
||||||
|
if (!isObj(raw)) return null
|
||||||
|
const out: Partial<Omit<Challenge, 'id'>> = {}
|
||||||
|
if ('name' in raw) {
|
||||||
|
const v = safeStr(raw.name)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.name = v
|
||||||
|
}
|
||||||
|
if ('exerciseName' in raw) {
|
||||||
|
const v = safeStr(raw.exerciseName)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.exerciseName = v
|
||||||
|
}
|
||||||
|
if ('stat' in raw) {
|
||||||
|
const v = oneOf(raw.stat, VALID_STATS)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.stat = v
|
||||||
|
}
|
||||||
|
if ('multiplier' in raw) {
|
||||||
|
const v = numInRange(raw.multiplier, 0, 1000)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.multiplier = v
|
||||||
|
}
|
||||||
|
if ('icon' in raw) {
|
||||||
|
const v = safeStr(raw.icon, 64)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.icon = v
|
||||||
|
}
|
||||||
|
if ('enabled' in raw) {
|
||||||
|
const v = bool(raw.enabled)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.enabled = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Settings validators
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
export function validateSettingsPatch(raw: unknown): Partial<Settings> | null {
|
||||||
|
if (!isObj(raw)) return null
|
||||||
|
const out: Partial<Settings> = {}
|
||||||
|
if ('globalEnabled' in raw) {
|
||||||
|
const v = bool(raw.globalEnabled)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.globalEnabled = v
|
||||||
|
}
|
||||||
|
if ('startWithWindows' in raw) {
|
||||||
|
const v = bool(raw.startWithWindows)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.startWithWindows = v
|
||||||
|
}
|
||||||
|
if ('startMinimized' in raw) {
|
||||||
|
const v = bool(raw.startMinimized)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.startMinimized = v
|
||||||
|
}
|
||||||
|
if ('minimizeToTray' in raw) {
|
||||||
|
const v = bool(raw.minimizeToTray)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.minimizeToTray = v
|
||||||
|
}
|
||||||
|
if ('soundEnabled' in raw) {
|
||||||
|
const v = bool(raw.soundEnabled)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.soundEnabled = v
|
||||||
|
}
|
||||||
|
if ('notificationMode' in raw) {
|
||||||
|
const v = oneOf(raw.notificationMode, VALID_NOTIFY)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.notificationMode = v
|
||||||
|
}
|
||||||
|
if ('theme' in raw) {
|
||||||
|
const v = oneOf(raw.theme, VALID_THEMES)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.theme = v
|
||||||
|
}
|
||||||
|
if ('language' in raw) {
|
||||||
|
const v = oneOf(raw.language, VALID_LANGS)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.language = v
|
||||||
|
}
|
||||||
|
if ('snoozeMinutes' in raw) {
|
||||||
|
const v = intInRange(raw.snoozeMinutes, 1, 24 * 60)
|
||||||
|
if (v === undefined) return null
|
||||||
|
out.snoozeMinutes = v
|
||||||
|
}
|
||||||
|
if ('quietHours' in raw) {
|
||||||
|
const qh = raw.quietHours
|
||||||
|
if (!isObj(qh)) return null
|
||||||
|
const enabled = bool(qh.enabled)
|
||||||
|
const from = safeStr(qh.from, 8)
|
||||||
|
const to = safeStr(qh.to, 8)
|
||||||
|
if (
|
||||||
|
enabled === undefined ||
|
||||||
|
from === undefined ||
|
||||||
|
to === undefined ||
|
||||||
|
!HHMM_RE.test(from) ||
|
||||||
|
!HHMM_RE.test(to)
|
||||||
|
) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (!Array.isArray(qh.days)) return null
|
||||||
|
const days: number[] = []
|
||||||
|
for (const d of qh.days) {
|
||||||
|
const n = intInRange(d, 0, 6)
|
||||||
|
if (n === undefined) return null
|
||||||
|
if (!days.includes(n)) days.push(n)
|
||||||
|
}
|
||||||
|
out.quietHours = { enabled, from, to, days }
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Misc tiny validators
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
export function validateId(raw: unknown): string | null {
|
||||||
|
// UUIDs from store.ts via randomUUID(); accept any reasonable string id.
|
||||||
|
const v = safeStr(raw, 64)
|
||||||
|
return v ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateActualReps(raw: unknown): number | undefined {
|
||||||
|
if (raw === undefined || raw === null) return undefined
|
||||||
|
return intInRange(raw, 0, 100000) ?? undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateSnoozeMinutes(raw: unknown): number | null {
|
||||||
|
return intInRange(raw, 1, 24 * 60) ?? null
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { BrowserWindow, shell, screen, app, nativeImage } from 'electron'
|
import { BrowserWindow, shell, screen, app, nativeImage } from 'electron'
|
||||||
|
import { IPC } from '../shared/ipc'
|
||||||
import { existsSync } from 'node:fs'
|
import { existsSync } from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
|
|
||||||
@@ -24,6 +25,48 @@ function windowIcon(): Electron.NativeImage | undefined {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allowlist of schemes safe to hand to the OS via shell.openExternal.
|
||||||
|
* The renderer is hostile-by-default — XSS or a malicious dep could ask us
|
||||||
|
* to open `file:`, `javascript:`, `ms-msdt:`, `steam://install/...` etc.
|
||||||
|
* (Custom URI handlers have historically been RCE vectors.)
|
||||||
|
*/
|
||||||
|
const ALLOWED_EXTERNAL_SCHEMES = new Set(['http:', 'https:', 'mailto:'])
|
||||||
|
|
||||||
|
function isSafeExternalUrl(url: string): boolean {
|
||||||
|
try {
|
||||||
|
return ALLOWED_EXTERNAL_SCHEMES.has(new URL(url).protocol)
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function installSafeNavigation(win: BrowserWindow): void {
|
||||||
|
// Any popup attempt: open externally only if scheme is in our allowlist.
|
||||||
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
||||||
|
if (isSafeExternalUrl(url)) {
|
||||||
|
void shell.openExternal(url)
|
||||||
|
} else {
|
||||||
|
console.warn(
|
||||||
|
'[windows] blocked openExternal for non-allowlisted URL:',
|
||||||
|
url
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return { action: 'deny' }
|
||||||
|
})
|
||||||
|
// Renderer must never navigate the BrowserWindow to a third-party origin.
|
||||||
|
// We always load file:// or the dev URL; anything else is suspect.
|
||||||
|
win.webContents.on('will-navigate', (event, url) => {
|
||||||
|
const devUrl = process.env['ELECTRON_RENDERER_URL']
|
||||||
|
const allow =
|
||||||
|
url.startsWith('file://') || (devUrl && url.startsWith(devUrl))
|
||||||
|
if (!allow) {
|
||||||
|
event.preventDefault()
|
||||||
|
console.warn('[windows] blocked will-navigate to:', url)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function loadRoute(win: BrowserWindow, route: 'main' | 'reminder'): void {
|
function loadRoute(win: BrowserWindow, route: 'main' | 'reminder'): void {
|
||||||
const devUrl = process.env['ELECTRON_RENDERER_URL']
|
const devUrl = process.env['ELECTRON_RENDERER_URL']
|
||||||
if (devUrl) {
|
if (devUrl) {
|
||||||
@@ -48,8 +91,13 @@ export function createMainWindow(showImmediately = true): BrowserWindow {
|
|||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
width: 1100,
|
width: 1100,
|
||||||
height: 720,
|
height: 720,
|
||||||
minWidth: 900,
|
// Минимум подобран так, чтобы:
|
||||||
minHeight: 600,
|
// - срабатывал Tailwind `lg:` (≥1024px) → 4 hero-stat в один ряд, а не 2×2
|
||||||
|
// - сайдбар (256px) + контент (max-w-5xl, padding lg:px-10) помещались без
|
||||||
|
// горизонтального скролла heatmap'а и карточек упражнений
|
||||||
|
// - по вертикали оставался запас на header + stats + heatmap без обрезки
|
||||||
|
minWidth: 1100,
|
||||||
|
minHeight: 700,
|
||||||
show: false,
|
show: false,
|
||||||
frame: false,
|
frame: false,
|
||||||
backgroundColor: '#0f1117',
|
backgroundColor: '#0f1117',
|
||||||
@@ -68,10 +116,17 @@ export function createMainWindow(showImmediately = true): BrowserWindow {
|
|||||||
if (showImmediately) win.show()
|
if (showImmediately) win.show()
|
||||||
})
|
})
|
||||||
|
|
||||||
win.webContents.setWindowOpenHandler(({ url }) => {
|
// Сообщаем рендереру об изменении max-состояния, чтобы он мог менять
|
||||||
shell.openExternal(url)
|
// иконку (квадрат ↔ «двойной квадрат») в кастомном тайтлбаре.
|
||||||
return { action: 'deny' }
|
const emitMaxState = (maximized: boolean): void => {
|
||||||
})
|
if (!win.isDestroyed()) {
|
||||||
|
win.webContents.send(IPC.evtMaximizeChanged, maximized)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
win.on('maximize', () => emitMaxState(true))
|
||||||
|
win.on('unmaximize', () => emitMaxState(false))
|
||||||
|
|
||||||
|
installSafeNavigation(win)
|
||||||
|
|
||||||
loadRoute(win, 'main')
|
loadRoute(win, 'main')
|
||||||
mainWindow = win
|
mainWindow = win
|
||||||
@@ -123,6 +178,7 @@ export function createReminderWindow(): BrowserWindow {
|
|||||||
})
|
})
|
||||||
|
|
||||||
win.setAlwaysOnTop(true, 'screen-saver')
|
win.setAlwaysOnTop(true, 'screen-saver')
|
||||||
|
installSafeNavigation(win)
|
||||||
loadRoute(win, 'reminder')
|
loadRoute(win, 'reminder')
|
||||||
|
|
||||||
win.on('closed', () => {
|
win.on('closed', () => {
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ type Unsub = () => void
|
|||||||
type Handler<T> = (payload: T) => void
|
type Handler<T> = (payload: T) => void
|
||||||
|
|
||||||
function on<T>(channel: string, handler: Handler<T>): Unsub {
|
function on<T>(channel: string, handler: Handler<T>): Unsub {
|
||||||
const listener = (_e: Electron.IpcRendererEvent, payload: T): void => handler(payload)
|
const listener = (_e: Electron.IpcRendererEvent, payload: T): void =>
|
||||||
|
handler(payload)
|
||||||
ipcRenderer.on(channel, listener)
|
ipcRenderer.on(channel, listener)
|
||||||
return () => ipcRenderer.removeListener(channel, listener)
|
return () => ipcRenderer.removeListener(channel, listener)
|
||||||
}
|
}
|
||||||
@@ -44,7 +45,8 @@ const api = {
|
|||||||
ipcRenderer.invoke(IPC.updateSettings, patch),
|
ipcRenderer.invoke(IPC.updateSettings, patch),
|
||||||
|
|
||||||
getAccentColor: (): Promise<string> => ipcRenderer.invoke(IPC.getAccentColor),
|
getAccentColor: (): Promise<string> => ipcRenderer.invoke(IPC.getAccentColor),
|
||||||
getOsTheme: (): Promise<'light' | 'dark'> => ipcRenderer.invoke(IPC.getOsTheme),
|
getOsTheme: (): Promise<'light' | 'dark'> =>
|
||||||
|
ipcRenderer.invoke(IPC.getOsTheme),
|
||||||
|
|
||||||
pauseAll: (): Promise<void> => ipcRenderer.invoke(IPC.pauseAll),
|
pauseAll: (): Promise<void> => ipcRenderer.invoke(IPC.pauseAll),
|
||||||
resumeAll: (): Promise<void> => ipcRenderer.invoke(IPC.resumeAll),
|
resumeAll: (): Promise<void> => ipcRenderer.invoke(IPC.resumeAll),
|
||||||
@@ -52,6 +54,9 @@ const api = {
|
|||||||
reminderClose: (): Promise<void> => ipcRenderer.invoke(IPC.reminderClose),
|
reminderClose: (): Promise<void> => ipcRenderer.invoke(IPC.reminderClose),
|
||||||
|
|
||||||
minimizeMain: (): void => ipcRenderer.send(IPC.minimizeMain),
|
minimizeMain: (): void => ipcRenderer.send(IPC.minimizeMain),
|
||||||
|
toggleMaximizeMain: (): void => ipcRenderer.send(IPC.toggleMaximizeMain),
|
||||||
|
isMaximizedMain: (): Promise<boolean> =>
|
||||||
|
ipcRenderer.invoke(IPC.isMaximizedMain),
|
||||||
closeMain: (): void => ipcRenderer.send(IPC.closeMain),
|
closeMain: (): void => ipcRenderer.send(IPC.closeMain),
|
||||||
hideMain: (): void => ipcRenderer.send(IPC.hideMain),
|
hideMain: (): void => ipcRenderer.send(IPC.hideMain),
|
||||||
|
|
||||||
@@ -69,17 +74,31 @@ const api = {
|
|||||||
// Challenges
|
// Challenges
|
||||||
addChallenge: (input: Omit<Challenge, 'id'>): Promise<Challenge> =>
|
addChallenge: (input: Omit<Challenge, 'id'>): Promise<Challenge> =>
|
||||||
ipcRenderer.invoke(IPC.addChallenge, input),
|
ipcRenderer.invoke(IPC.addChallenge, input),
|
||||||
updateChallenge: (id: string, patch: Partial<Challenge>): Promise<Challenge> =>
|
updateChallenge: (
|
||||||
ipcRenderer.invoke(IPC.updateChallenge, id, patch),
|
id: string,
|
||||||
|
patch: Partial<Challenge>
|
||||||
|
): Promise<Challenge> => ipcRenderer.invoke(IPC.updateChallenge, id, patch),
|
||||||
deleteChallenge: (id: string): Promise<boolean> =>
|
deleteChallenge: (id: string): Promise<boolean> =>
|
||||||
ipcRenderer.invoke(IPC.deleteChallenge, id),
|
ipcRenderer.invoke(IPC.deleteChallenge, id),
|
||||||
toggleChallenge: (id: string, enabled: boolean): Promise<Challenge> =>
|
toggleChallenge: (id: string, enabled: boolean): Promise<Challenge> =>
|
||||||
ipcRenderer.invoke(IPC.toggleChallenge, id, enabled),
|
ipcRenderer.invoke(IPC.toggleChallenge, id, enabled),
|
||||||
|
|
||||||
closeMatchSummary: (): Promise<void> => ipcRenderer.invoke(IPC.closeMatchSummary),
|
closeMatchSummary: (): Promise<void> =>
|
||||||
|
ipcRenderer.invoke(IPC.closeMatchSummary),
|
||||||
|
|
||||||
simulateMatchEnd: (id: GameId, stats: Record<string, number>): Promise<void> =>
|
// Dev-only: synthesize a match-end event from the renderer. The channel is
|
||||||
ipcRenderer.invoke('dev:simulateMatchEnd', id, stats),
|
// not registered in production builds (see src/main/ipc.ts), so this
|
||||||
|
// function will reject in shipped binaries even though it's exposed.
|
||||||
|
// Gated at the preload level too so the bundler can dead-code-eliminate it.
|
||||||
|
...(import.meta.env.MODE !== 'production'
|
||||||
|
? {
|
||||||
|
simulateMatchEnd: (
|
||||||
|
id: GameId,
|
||||||
|
stats: Record<string, number>
|
||||||
|
): Promise<void> =>
|
||||||
|
ipcRenderer.invoke('dev:simulateMatchEnd', id, stats)
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
|
||||||
// Auto-updater
|
// Auto-updater
|
||||||
updaterStatus: (): Promise<UpdaterStatus> =>
|
updaterStatus: (): Promise<UpdaterStatus> =>
|
||||||
@@ -99,11 +118,15 @@ const api = {
|
|||||||
onFire: (h: Handler<Exercise>): Unsub => on(IPC.evtFire, h),
|
onFire: (h: Handler<Exercise>): Unsub => on(IPC.evtFire, h),
|
||||||
onMatchEnd: (h: Handler<MatchSummary>): Unsub => on(IPC.evtMatchEnd, h),
|
onMatchEnd: (h: Handler<MatchSummary>): Unsub => on(IPC.evtMatchEnd, h),
|
||||||
onStateChanged: (h: Handler<AppState>): Unsub => on(IPC.evtStateChanged, h),
|
onStateChanged: (h: Handler<AppState>): Unsub => on(IPC.evtStateChanged, h),
|
||||||
onThemeChanged: (h: Handler<'light' | 'dark'>): Unsub => on(IPC.evtThemeChanged, h),
|
onThemeChanged: (h: Handler<'light' | 'dark'>): Unsub =>
|
||||||
|
on(IPC.evtThemeChanged, h),
|
||||||
onAccentChanged: (h: Handler<string>): Unsub => on(IPC.evtAccentChanged, h),
|
onAccentChanged: (h: Handler<string>): Unsub => on(IPC.evtAccentChanged, h),
|
||||||
onGamesChanged: (h: Handler<GameStatus[]>): Unsub => on(IPC.evtGamesChanged, h),
|
onGamesChanged: (h: Handler<GameStatus[]>): Unsub =>
|
||||||
|
on(IPC.evtGamesChanged, h),
|
||||||
onUpdaterStatus: (h: Handler<UpdaterStatus>): Unsub =>
|
onUpdaterStatus: (h: Handler<UpdaterStatus>): Unsub =>
|
||||||
on(IPC.evtUpdaterStatus, h)
|
on(IPC.evtUpdaterStatus, h),
|
||||||
|
onMaximizeChanged: (h: Handler<boolean>): Unsub =>
|
||||||
|
on(IPC.evtMaximizeChanged, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('api', api)
|
contextBridge.exposeInMainWorld('api', api)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { HashRouter, Route, Routes, useLocation } from 'react-router-dom'
|
|||||||
import { AnimatePresence, motion } from 'framer-motion'
|
import { AnimatePresence, motion } from 'framer-motion'
|
||||||
import { Sidebar } from './components/Sidebar'
|
import { Sidebar } from './components/Sidebar'
|
||||||
import { Titlebar } from './components/Titlebar'
|
import { Titlebar } from './components/Titlebar'
|
||||||
|
import { ErrorBoundary } from './components/ErrorBoundary'
|
||||||
import Dashboard from './pages/Dashboard'
|
import Dashboard from './pages/Dashboard'
|
||||||
import Exercises from './pages/Exercises'
|
import Exercises from './pages/Exercises'
|
||||||
import GamesPage from './pages/Games'
|
import GamesPage from './pages/Games'
|
||||||
@@ -10,16 +11,26 @@ import ChallengesPage from './pages/Challenges'
|
|||||||
import SettingsPage from './pages/Settings'
|
import SettingsPage from './pages/Settings'
|
||||||
import { subscribeToBackend, useAppStore } from './store/appStore'
|
import { subscribeToBackend, useAppStore } from './store/appStore'
|
||||||
|
|
||||||
|
// Module-level guard so React 18 StrictMode's double-invocation of mount
|
||||||
|
// effects (in dev only) doesn't subscribe to backend IPC twice.
|
||||||
|
let backendSubscribed = false
|
||||||
|
|
||||||
export default function App(): JSX.Element {
|
export default function App(): JSX.Element {
|
||||||
const hydrated = useAppStore((s) => s.hydrated)
|
const hydrated = useAppStore((s) => s.hydrated)
|
||||||
const [mobileNavOpen, setMobileNavOpen] = useState(false)
|
const [mobileNavOpen, setMobileNavOpen] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (backendSubscribed) return undefined
|
||||||
|
backendSubscribed = true
|
||||||
const unsub = subscribeToBackend()
|
const unsub = subscribeToBackend()
|
||||||
return unsub
|
return () => {
|
||||||
|
backendSubscribed = false
|
||||||
|
unsub()
|
||||||
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<ErrorBoundary>
|
||||||
<HashRouter>
|
<HashRouter>
|
||||||
<div className="h-screen w-screen flex flex-col bg-bg">
|
<div className="h-screen w-screen flex flex-col bg-bg">
|
||||||
<Titlebar onMenuClick={() => setMobileNavOpen(true)} />
|
<Titlebar onMenuClick={() => setMobileNavOpen(true)} />
|
||||||
@@ -30,14 +41,18 @@ export default function App(): JSX.Element {
|
|||||||
/>
|
/>
|
||||||
<main className="flex-1 overflow-hidden min-w-0">
|
<main className="flex-1 overflow-hidden min-w-0">
|
||||||
{hydrated ? (
|
{hydrated ? (
|
||||||
|
<ErrorBoundary>
|
||||||
<RoutedPages onNav={() => setMobileNavOpen(false)} />
|
<RoutedPages onNav={() => setMobileNavOpen(false)} />
|
||||||
|
</ErrorBoundary>
|
||||||
) : (
|
) : (
|
||||||
<div className="p-8 text-text/45">Загрузка…</div>
|
// Neutral placeholder — settings (and lang) aren't loaded yet.
|
||||||
|
<div className="p-8 text-text/45" />
|
||||||
)}
|
)}
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
|
</ErrorBoundary>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,24 +54,16 @@ export default function ReminderApp(): JSX.Element {
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
// ESC closes the match summary view too — keyboard parity with exercise mode.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (mode.kind !== 'exercise') return
|
if (mode.kind !== 'match') return
|
||||||
const ex = mode.exercise
|
|
||||||
const snoozeMin = settings?.snoozeMinutes ?? 5
|
|
||||||
function onKey(e: KeyboardEvent): void {
|
function onKey(e: KeyboardEvent): void {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Escape') close()
|
||||||
window.api.markDone(ex.id).then(close)
|
|
||||||
} else if (e.key === ' ' || e.code === 'Space') {
|
|
||||||
e.preventDefault()
|
|
||||||
window.api.snooze(ex.id, snoozeMin).then(close)
|
|
||||||
} else if (e.key === 'Escape') {
|
|
||||||
window.api.skip(ex.id).then(close)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
window.addEventListener('keydown', onKey)
|
window.addEventListener('keydown', onKey)
|
||||||
return () => window.removeEventListener('keydown', onKey)
|
return () => window.removeEventListener('keydown', onKey)
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [mode, settings?.snoozeMinutes])
|
}, [mode.kind])
|
||||||
|
|
||||||
function close(): void {
|
function close(): void {
|
||||||
setMode({ kind: 'idle' })
|
setMode({ kind: 'idle' })
|
||||||
@@ -83,7 +75,10 @@ export default function ReminderApp(): JSX.Element {
|
|||||||
if (mode.kind === 'idle') return <div className="reminder-shell" />
|
if (mode.kind === 'idle') return <div className="reminder-shell" />
|
||||||
if (mode.kind === 'exercise') {
|
if (mode.kind === 'exercise') {
|
||||||
return (
|
return (
|
||||||
|
// key={exercise.id} forces a fresh component (and fresh stepper state)
|
||||||
|
// when a new reminder arrives while the previous modal is still open.
|
||||||
<ExerciseReminder
|
<ExerciseReminder
|
||||||
|
key={mode.exercise.id + ':' + mode.exercise.nextFireAt}
|
||||||
exercise={mode.exercise}
|
exercise={mode.exercise}
|
||||||
snoozeMinutes={settings?.snoozeMinutes ?? 5}
|
snoozeMinutes={settings?.snoozeMinutes ?? 5}
|
||||||
lang={lang}
|
lang={lang}
|
||||||
@@ -97,11 +92,17 @@ export default function ReminderApp(): JSX.Element {
|
|||||||
done={mode.done}
|
done={mode.done}
|
||||||
lang={lang}
|
lang={lang}
|
||||||
onMarkDone={(id) =>
|
onMarkDone={(id) =>
|
||||||
setMode({
|
// Functional update so a second rapid click can't race against a stale
|
||||||
|
// `mode.done` captured in this closure.
|
||||||
|
setMode((m) =>
|
||||||
|
m.kind === 'match'
|
||||||
|
? {
|
||||||
kind: 'match',
|
kind: 'match',
|
||||||
summary: mode.summary,
|
summary: m.summary,
|
||||||
done: new Set([...mode.done, id])
|
done: new Set([...m.done, id])
|
||||||
})
|
}
|
||||||
|
: m
|
||||||
|
)
|
||||||
}
|
}
|
||||||
onClose={close}
|
onClose={close}
|
||||||
/>
|
/>
|
||||||
@@ -124,14 +125,13 @@ function ExerciseReminder({
|
|||||||
|
|
||||||
const [actualReps, setActualReps] = useState(exercise.reps)
|
const [actualReps, setActualReps] = useState(exercise.reps)
|
||||||
const adjusted = actualReps !== exercise.reps
|
const adjusted = actualReps !== exercise.reps
|
||||||
|
// Cap the stepper at 5× planned so a stuck "+" button can't log nonsense.
|
||||||
|
const REP_CAP = Math.max(50, exercise.reps * 5)
|
||||||
|
|
||||||
async function done(): Promise<void> {
|
async function done(): Promise<void> {
|
||||||
// Only pass actualReps when user adjusted — otherwise leave undefined
|
// Only pass actualReps when user adjusted — otherwise leave undefined
|
||||||
// so history records the full planned value cleanly.
|
// so history records the full planned value cleanly.
|
||||||
await window.api.markDone(
|
await window.api.markDone(exercise.id, adjusted ? actualReps : undefined)
|
||||||
exercise.id,
|
|
||||||
adjusted ? actualReps : undefined
|
|
||||||
)
|
|
||||||
onClose()
|
onClose()
|
||||||
}
|
}
|
||||||
async function snooze(): Promise<void> {
|
async function snooze(): Promise<void> {
|
||||||
@@ -143,7 +143,38 @@ function ExerciseReminder({
|
|||||||
onClose()
|
onClose()
|
||||||
}
|
}
|
||||||
const dec = (): void => setActualReps((n) => Math.max(0, n - 1))
|
const dec = (): void => setActualReps((n) => Math.max(0, n - 1))
|
||||||
const inc = (): void => setActualReps((n) => n + 1)
|
const inc = (): void => setActualReps((n) => Math.min(REP_CAP, n + 1))
|
||||||
|
|
||||||
|
// Keyboard shortcuts live INSIDE the component so they have access to the
|
||||||
|
// current `actualReps` — pressing Enter respects the stepper's adjustment.
|
||||||
|
useEffect(() => {
|
||||||
|
function onKey(e: KeyboardEvent): void {
|
||||||
|
// Don't hijack Space when a button is focused (default activation).
|
||||||
|
const targetTag = (e.target as HTMLElement | null)?.tagName
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
e.preventDefault()
|
||||||
|
void done()
|
||||||
|
} else if (
|
||||||
|
(e.key === ' ' || e.code === 'Space') &&
|
||||||
|
targetTag !== 'BUTTON'
|
||||||
|
) {
|
||||||
|
e.preventDefault()
|
||||||
|
void snooze()
|
||||||
|
} else if (e.key === 'Escape') {
|
||||||
|
e.preventDefault()
|
||||||
|
void skip()
|
||||||
|
} else if (e.key === 'ArrowUp' || e.key === '+') {
|
||||||
|
e.preventDefault()
|
||||||
|
inc()
|
||||||
|
} else if (e.key === 'ArrowDown' || e.key === '-') {
|
||||||
|
e.preventDefault()
|
||||||
|
dec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.addEventListener('keydown', onKey)
|
||||||
|
return () => window.removeEventListener('keydown', onKey)
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [actualReps, snoozeMinutes])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="reminder-shell flex flex-col h-full">
|
<div className="reminder-shell flex flex-col h-full">
|
||||||
@@ -181,7 +212,7 @@ function ExerciseReminder({
|
|||||||
<button
|
<button
|
||||||
onClick={dec}
|
onClick={dec}
|
||||||
className="w-9 h-9 grid place-items-center rounded-full bg-surface-2 text-text/65 hover:text-text hover:bg-hairline/25 active:scale-90 transition-all"
|
className="w-9 h-9 grid place-items-center rounded-full bg-surface-2 text-text/65 hover:text-text hover:bg-hairline/25 active:scale-90 transition-all"
|
||||||
aria-label="−"
|
aria-label={t('reminder.aria.decrement')}
|
||||||
>
|
>
|
||||||
<Minus size={16} strokeWidth={2.5} />
|
<Minus size={16} strokeWidth={2.5} />
|
||||||
</button>
|
</button>
|
||||||
@@ -201,14 +232,17 @@ function ExerciseReminder({
|
|||||||
<button
|
<button
|
||||||
onClick={inc}
|
onClick={inc}
|
||||||
className="w-9 h-9 grid place-items-center rounded-full bg-surface-2 text-text/65 hover:text-text hover:bg-hairline/25 active:scale-90 transition-all"
|
className="w-9 h-9 grid place-items-center rounded-full bg-surface-2 text-text/65 hover:text-text hover:bg-hairline/25 active:scale-90 transition-all"
|
||||||
aria-label="+"
|
aria-label={t('reminder.aria.increment')}
|
||||||
>
|
>
|
||||||
<Plus size={16} strokeWidth={2.5} />
|
<Plus size={16} strokeWidth={2.5} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{adjusted && (
|
{adjusted && (
|
||||||
<div className="text-[12px] text-accent mt-2 font-medium">
|
<div className="text-[12px] text-accent mt-2 font-medium">
|
||||||
{t('reminder.partial', { actual: actualReps, planned: exercise.reps })}
|
{t('reminder.partial', {
|
||||||
|
actual: actualReps,
|
||||||
|
planned: exercise.reps
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -320,7 +354,8 @@ function MatchSummaryView({
|
|||||||
<p className="text-[13px] text-text/65 mt-1.5 font-medium">
|
<p className="text-[13px] text-text/65 mt-1.5 font-medium">
|
||||||
<span className="font-mono-num font-bold text-text">{minutes}</span>{' '}
|
<span className="font-mono-num font-bold text-text">{minutes}</span>{' '}
|
||||||
{t('fmt.m')} ·{' '}
|
{t('fmt.m')} ·{' '}
|
||||||
{tn('match.summary.challenges', summary.results.length)}{' · '}
|
{tn('match.summary.challenges', summary.results.length)}
|
||||||
|
{' · '}
|
||||||
{allDone ? (
|
{allDone ? (
|
||||||
<span className="text-success font-bold">
|
<span className="text-success font-bold">
|
||||||
{t('match.summary.all_done')}
|
{t('match.summary.all_done')}
|
||||||
|
|||||||
61
src/renderer/src/components/ErrorBoundary.tsx
Normal file
61
src/renderer/src/components/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { Component, type ErrorInfo, type ReactNode } from 'react'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
children: ReactNode
|
||||||
|
/** Optional render override; receives the captured error. */
|
||||||
|
fallback?: (err: Error, reset: () => void) => ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
error: Error | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Top-level error boundary so a crash in one subtree (e.g. a malformed
|
||||||
|
* history entry crashing HistoryHeatmap) does not blank the whole window.
|
||||||
|
* React class components are still the only way to implement this.
|
||||||
|
*/
|
||||||
|
export class ErrorBoundary extends Component<Props, State> {
|
||||||
|
state: State = { error: null }
|
||||||
|
|
||||||
|
static getDerivedStateFromError(error: Error): State {
|
||||||
|
return { error }
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
reset = (): void => this.setState({ error: null })
|
||||||
|
|
||||||
|
render(): ReactNode {
|
||||||
|
const { error } = this.state
|
||||||
|
if (!error) return this.props.children
|
||||||
|
|
||||||
|
if (this.props.fallback) return this.props.fallback(error, this.reset)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-6 max-w-xl mx-auto text-center">
|
||||||
|
<div className="text-[15px] font-semibold mb-2">
|
||||||
|
Что-то пошло не так
|
||||||
|
</div>
|
||||||
|
<div className="text-[13px] text-text/65 mb-4 break-words">
|
||||||
|
{error.message}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={this.reset}
|
||||||
|
className="h-9 px-4 rounded-xl bg-accent text-white text-[14px] font-semibold active:scale-95 transition-transform"
|
||||||
|
>
|
||||||
|
Попробовать снова
|
||||||
|
</button>
|
||||||
|
{import.meta.env.DEV && error.stack && (
|
||||||
|
<pre className="mt-6 p-3 bg-surface-2 rounded-xl text-left text-[11px] font-mono-num overflow-auto max-h-64">
|
||||||
|
{error.stack}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ import { Check, MoreHorizontal } from 'lucide-react'
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import type { Exercise, Tick } from '@shared/types'
|
import type { Exercise, Tick } from '@shared/types'
|
||||||
import { Icon } from '../lib/icon'
|
import { Icon } from '../lib/icon'
|
||||||
import { formatCountdown, formatInterval } from '../lib/format'
|
import { formatCountdown } from '../lib/format'
|
||||||
import { Switch } from './ui/Switch'
|
import { Switch } from './ui/Switch'
|
||||||
import { useT } from '../i18n'
|
import { useT } from '../i18n'
|
||||||
|
|
||||||
@@ -78,9 +78,7 @@ export function ExerciseCard({
|
|||||||
strokeLinecap="round"
|
strokeLinecap="round"
|
||||||
strokeDasharray={C}
|
strokeDasharray={C}
|
||||||
strokeDashoffset={dashOffset}
|
strokeDashoffset={dashOffset}
|
||||||
className={
|
className={isDue ? 'stroke-accent' : 'stroke-accent/85'}
|
||||||
isDue ? 'stroke-accent' : 'stroke-accent/85'
|
|
||||||
}
|
|
||||||
style={{ transition: 'stroke-dashoffset 0.5s linear' }}
|
style={{ transition: 'stroke-dashoffset 0.5s linear' }}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@@ -159,15 +157,13 @@ export function ExerciseCard({
|
|||||||
isDue ? 'text-accent' : 'text-text'
|
isDue ? 'text-accent' : 'text-text'
|
||||||
].join(' ')}
|
].join(' ')}
|
||||||
>
|
>
|
||||||
{exercise.enabled
|
{exercise.enabled ? formatCountdown(ms, lang) : t('fmt.paused')}
|
||||||
? formatCountdown(ms, lang)
|
|
||||||
: t('fmt.paused')}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Switch
|
<Switch
|
||||||
checked={exercise.enabled}
|
checked={exercise.enabled}
|
||||||
onChange={onToggle}
|
onChange={onToggle}
|
||||||
aria-label={t('btn.done')}
|
aria-label={t('exercise.aria.toggle', { name: exercise.name })}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,43 +1,58 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { dailyRepsRange } from '../lib/history'
|
import { dailyRepsRange } from '../lib/history'
|
||||||
import type { Exercise, HistoryEntry, Language } from '@shared/types'
|
import type { Exercise, HistoryEntry } from '@shared/types'
|
||||||
|
import { translateN, useT } from '../i18n'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
history: HistoryEntry[]
|
history: HistoryEntry[]
|
||||||
exercises: Exercise[]
|
exercises: Exercise[]
|
||||||
days?: number
|
days?: number
|
||||||
lang: Language
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GitHub-style contribution grid: weeks as columns, days-of-week as rows.
|
* GitHub-style contribution grid: weeks as columns, days-of-week as rows.
|
||||||
* Intensity bucket from 0 to 4 based on relative reps within the window.
|
*
|
||||||
|
* Intensity bucket uses percentile-based thresholds (over non-zero days)
|
||||||
|
* rather than a flat ratio against the single max — so one outlier day
|
||||||
|
* doesn't blot out every normal day into the lowest bucket.
|
||||||
*/
|
*/
|
||||||
export function HistoryHeatmap({
|
export function HistoryHeatmap({
|
||||||
history,
|
history,
|
||||||
exercises,
|
exercises,
|
||||||
days = 84, // 12 weeks
|
days = 84 // 12 weeks
|
||||||
lang
|
|
||||||
}: Props): JSX.Element {
|
}: Props): JSX.Element {
|
||||||
|
const { t, lang } = useT()
|
||||||
|
|
||||||
const cells = useMemo(
|
const cells = useMemo(
|
||||||
() => dailyRepsRange(history, exercises, days),
|
() => dailyRepsRange(history, exercises, days),
|
||||||
[history, exercises, days]
|
[history, exercises, days]
|
||||||
)
|
)
|
||||||
const max = cells.reduce((m, c) => Math.max(m, c.reps), 0)
|
|
||||||
|
|
||||||
// Bucket function — 0 for zero, 1-4 for low/med/high/peak.
|
// Percentile-based bucket thresholds over non-zero days. Stable when the
|
||||||
|
// user has one outlier (e.g. a 200-rep "catch up" day) — normal 10-rep
|
||||||
|
// days still spread across buckets 1..4 instead of all collapsing to 1.
|
||||||
|
const thresholds = useMemo(() => {
|
||||||
|
const nz = cells
|
||||||
|
.map((c) => c.reps)
|
||||||
|
.filter((n) => n > 0)
|
||||||
|
.sort((a, b) => a - b)
|
||||||
|
if (nz.length === 0) return null
|
||||||
|
const p = (q: number): number =>
|
||||||
|
nz[Math.min(nz.length - 1, Math.floor(q * nz.length))]
|
||||||
|
return { p25: p(0.25), p50: p(0.5), p85: p(0.85) }
|
||||||
|
}, [cells])
|
||||||
|
|
||||||
function bucket(n: number): number {
|
function bucket(n: number): number {
|
||||||
if (n === 0) return 0
|
if (n === 0 || !thresholds) return 0
|
||||||
if (max === 0) return 0
|
if (n <= thresholds.p25) return 1
|
||||||
const ratio = n / max
|
if (n <= thresholds.p50) return 2
|
||||||
if (ratio < 0.25) return 1
|
if (n <= thresholds.p85) return 3
|
||||||
if (ratio < 0.5) return 2
|
|
||||||
if (ratio < 0.85) return 3
|
|
||||||
return 4
|
return 4
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group cells into columns (weeks). Pad start so first column aligns to
|
// Group cells into columns (weeks). Pad start so the first column aligns
|
||||||
// its actual week (Mon-first).
|
// to its actual weekday (Mon-first).
|
||||||
|
const weeks = useMemo(() => {
|
||||||
const firstDay = cells[0]?.date ?? new Date()
|
const firstDay = cells[0]?.date ?? new Date()
|
||||||
const firstWeekday = (firstDay.getDay() + 6) % 7 // 0 = Mon
|
const firstWeekday = (firstDay.getDay() + 6) % 7 // 0 = Mon
|
||||||
const padded: ({
|
const padded: ({
|
||||||
@@ -45,15 +60,24 @@ export function HistoryHeatmap({
|
|||||||
date: Date
|
date: Date
|
||||||
reps: number
|
reps: number
|
||||||
} | null)[] = [...Array(firstWeekday).fill(null), ...cells]
|
} | null)[] = [...Array(firstWeekday).fill(null), ...cells]
|
||||||
const weeks: (typeof padded)[] = []
|
const out: (typeof padded)[] = []
|
||||||
for (let i = 0; i < padded.length; i += 7) {
|
for (let i = 0; i < padded.length; i += 7) {
|
||||||
weeks.push(padded.slice(i, i + 7))
|
out.push(padded.slice(i, i + 7))
|
||||||
}
|
}
|
||||||
|
return out
|
||||||
|
}, [cells])
|
||||||
|
|
||||||
const dayLabels =
|
// Day labels along the Y axis. Mon-first, only label every other day to
|
||||||
lang === 'en'
|
// keep the column narrow. Pulled from the i18n dict (index = Date.getDay()).
|
||||||
? ['Mon', '', 'Wed', '', 'Fri', '', 'Sun']
|
const dayLabels = [
|
||||||
: ['Пн', '', 'Ср', '', 'Пт', '', 'Вс']
|
t('weekday.short.1'), // Mon
|
||||||
|
'',
|
||||||
|
t('weekday.short.3'), // Wed
|
||||||
|
'',
|
||||||
|
t('weekday.short.5'), // Fri
|
||||||
|
'',
|
||||||
|
t('weekday.short.0') // Sun
|
||||||
|
]
|
||||||
|
|
||||||
const monthLabels = useMemo(() => {
|
const monthLabels = useMemo(() => {
|
||||||
const fmt = new Intl.DateTimeFormat(lang === 'en' ? 'en-US' : 'ru-RU', {
|
const fmt = new Intl.DateTimeFormat(lang === 'en' ? 'en-US' : 'ru-RU', {
|
||||||
@@ -65,7 +89,7 @@ export function HistoryHeatmap({
|
|||||||
})
|
})
|
||||||
}, [weeks, lang])
|
}, [weeks, lang])
|
||||||
|
|
||||||
// Compress repeated month labels (only show on first week of the month)
|
// Show a month label only on the first week that lands inside it.
|
||||||
const monthLabelsCompressed = monthLabels.map((label, i) =>
|
const monthLabelsCompressed = monthLabels.map((label, i) =>
|
||||||
label && label !== monthLabels[i - 1] ? label : ''
|
label && label !== monthLabels[i - 1] ? label : ''
|
||||||
)
|
)
|
||||||
@@ -79,11 +103,16 @@ export function HistoryHeatmap({
|
|||||||
[lang]
|
[lang]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Pluralised "{n} reps" / "{n} повторов" for the cell tooltip.
|
||||||
|
// Outside React state — needed inside the cell-render closure.
|
||||||
|
const repsLabel = (n: number): string =>
|
||||||
|
translateN(lang, 'heatmap.tooltip.reps', n)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="bg-surface rounded-2xl p-5 shadow-card dark:ring-0.5 dark:ring-hairline/30">
|
<div className="bg-surface rounded-2xl p-5 shadow-card dark:ring-0.5 dark:ring-hairline/30">
|
||||||
<div className="flex items-center gap-2 mb-3">
|
<div className="flex items-center gap-2 mb-3">
|
||||||
<div className="text-[14px] text-text/75 font-semibold">
|
<div className="text-[14px] text-text/75 font-semibold">
|
||||||
{lang === 'en' ? 'Activity, last 12 weeks' : 'Активность за 12 недель'}
|
{t('heatmap.title')}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -115,9 +144,7 @@ export function HistoryHeatmap({
|
|||||||
<div key={wi} className="flex flex-col gap-[3px]">
|
<div key={wi} className="flex flex-col gap-[3px]">
|
||||||
{w.map((c, di) => {
|
{w.map((c, di) => {
|
||||||
if (!c) {
|
if (!c) {
|
||||||
return (
|
return <div key={di} className="w-[12px] h-[12px]" />
|
||||||
<div key={di} className="w-[12px] h-[12px]" />
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
const b = bucket(c.reps)
|
const b = bucket(c.reps)
|
||||||
const tone =
|
const tone =
|
||||||
@@ -133,7 +160,7 @@ export function HistoryHeatmap({
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={di}
|
key={di}
|
||||||
title={`${dateFmt.format(c.date)} · ${c.reps} ${lang === 'en' ? 'reps' : 'повторов'}`}
|
title={`${dateFmt.format(c.date)} · ${repsLabel(c.reps)}`}
|
||||||
className={[
|
className={[
|
||||||
'w-[12px] h-[12px] rounded-[3px] transition-colors',
|
'w-[12px] h-[12px] rounded-[3px] transition-colors',
|
||||||
tone
|
tone
|
||||||
@@ -149,7 +176,7 @@ export function HistoryHeatmap({
|
|||||||
|
|
||||||
{/* Legend */}
|
{/* Legend */}
|
||||||
<div className="flex items-center justify-end gap-1.5 mt-3 text-[10px] text-text/45 font-medium">
|
<div className="flex items-center justify-end gap-1.5 mt-3 text-[10px] text-text/45 font-medium">
|
||||||
<span>{lang === 'en' ? 'Less' : 'Меньше'}</span>
|
<span>{t('heatmap.legend.less')}</span>
|
||||||
{[0, 1, 2, 3, 4].map((b) => (
|
{[0, 1, 2, 3, 4].map((b) => (
|
||||||
<div
|
<div
|
||||||
key={b}
|
key={b}
|
||||||
@@ -167,7 +194,7 @@ export function HistoryHeatmap({
|
|||||||
].join(' ')}
|
].join(' ')}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
<span>{lang === 'en' ? 'More' : 'Больше'}</span>
|
<span>{t('heatmap.legend.more')}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
|
import { useEffect, useRef } from 'react'
|
||||||
import { NavLink } from 'react-router-dom'
|
import { NavLink } from 'react-router-dom'
|
||||||
import { AnimatePresence, motion } from 'framer-motion'
|
import { AnimatePresence, motion } from 'framer-motion'
|
||||||
import {
|
import { Sun, Dumbbell, Joystick, Flame, Settings2, X } from 'lucide-react'
|
||||||
Sun,
|
|
||||||
Dumbbell,
|
|
||||||
Joystick,
|
|
||||||
Flame,
|
|
||||||
Settings2,
|
|
||||||
X
|
|
||||||
} from 'lucide-react'
|
|
||||||
import { useT } from '../i18n'
|
import { useT } from '../i18n'
|
||||||
|
|
||||||
type Item = {
|
type Item = {
|
||||||
@@ -51,6 +45,53 @@ export function Sidebar({
|
|||||||
onMobileClose
|
onMobileClose
|
||||||
}: Props): JSX.Element {
|
}: Props): JSX.Element {
|
||||||
const { t } = useT()
|
const { t } = useT()
|
||||||
|
const drawerRef = useRef<HTMLElement | null>(null)
|
||||||
|
const lastFocusedRef = useRef<HTMLElement | null>(null)
|
||||||
|
|
||||||
|
// Esc closes + focus trap while the mobile drawer is open. Mirrors the
|
||||||
|
// pattern used in Modal.tsx.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mobileOpen) return undefined
|
||||||
|
lastFocusedRef.current = document.activeElement as HTMLElement | null
|
||||||
|
|
||||||
|
const onKeyDown = (e: KeyboardEvent): void => {
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
e.preventDefault()
|
||||||
|
onMobileClose?.()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (e.key !== 'Tab') return
|
||||||
|
const root = drawerRef.current
|
||||||
|
if (!root) return
|
||||||
|
const focusables = root.querySelectorAll<HTMLElement>(
|
||||||
|
'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
||||||
|
)
|
||||||
|
if (focusables.length === 0) return
|
||||||
|
const first = focusables[0]
|
||||||
|
const last = focusables[focusables.length - 1]
|
||||||
|
const active = document.activeElement as HTMLElement | null
|
||||||
|
if (e.shiftKey) {
|
||||||
|
if (active === first || !root.contains(active)) {
|
||||||
|
e.preventDefault()
|
||||||
|
last.focus()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (active === last || !root.contains(active)) {
|
||||||
|
e.preventDefault()
|
||||||
|
first.focus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('keydown', onKeyDown, true)
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('keydown', onKeyDown, true)
|
||||||
|
// Return focus to the trigger (Titlebar's hamburger) so keyboard users
|
||||||
|
// pick up where they left off.
|
||||||
|
const target = lastFocusedRef.current
|
||||||
|
if (target && document.body.contains(target)) target.focus()
|
||||||
|
}
|
||||||
|
}, [mobileOpen, onMobileClose])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<aside className="hidden md:flex w-64 shrink-0 vibrancy hairline-b border-r-0 flex-col">
|
<aside className="hidden md:flex w-64 shrink-0 vibrancy hairline-b border-r-0 flex-col">
|
||||||
@@ -72,8 +113,13 @@ export function Sidebar({
|
|||||||
initial={{ opacity: 0 }}
|
initial={{ opacity: 0 }}
|
||||||
animate={{ opacity: 1 }}
|
animate={{ opacity: 1 }}
|
||||||
exit={{ opacity: 0 }}
|
exit={{ opacity: 0 }}
|
||||||
|
aria-hidden="true"
|
||||||
/>
|
/>
|
||||||
<motion.aside
|
<motion.aside
|
||||||
|
ref={drawerRef}
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-label={t('sidebar.aria.nav')}
|
||||||
className="relative w-72 max-w-[85vw] h-full vibrancy flex flex-col"
|
className="relative w-72 max-w-[85vw] h-full vibrancy flex flex-col"
|
||||||
initial={{ x: '-100%' }}
|
initial={{ x: '-100%' }}
|
||||||
animate={{ x: 0 }}
|
animate={{ x: 0 }}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Minus, X, Square, Menu } from 'lucide-react'
|
import { Minus, X, Square, Copy, Menu } from 'lucide-react'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
import { useT } from '../i18n'
|
import { useT } from '../i18n'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@@ -10,8 +11,29 @@ export function Titlebar({ title, onMenuClick }: Props): JSX.Element {
|
|||||||
const { t } = useT()
|
const { t } = useT()
|
||||||
const effectiveTitle = title ?? t('titlebar.app_title')
|
const effectiveTitle = title ?? t('titlebar.app_title')
|
||||||
|
|
||||||
|
// Локально отслеживаем maximize-state, чтобы свапать иконку (квадрат ↔
|
||||||
|
// «двойной квадрат», как в нативной винде). Стартовое значение спрашиваем
|
||||||
|
// у main; дальше подписываемся на evtMaximizeChanged.
|
||||||
|
const [maximized, setMaximized] = useState(false)
|
||||||
|
useEffect(() => {
|
||||||
|
void window.api.isMaximizedMain().then(setMaximized)
|
||||||
|
const unsub = window.api.onMaximizeChanged((v) => setMaximized(v))
|
||||||
|
return unsub
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Double-click по тайтлбару — стандартный Windows-жест для toggle maximize.
|
||||||
|
// Игнорируем клики по элементам с no-drag (кнопки/меню) — у них своя логика.
|
||||||
|
function onDoubleClick(e: React.MouseEvent<HTMLDivElement>): void {
|
||||||
|
const target = e.target as HTMLElement
|
||||||
|
if (target.closest('.titlebar-nodrag')) return
|
||||||
|
window.api.toggleMaximizeMain()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="titlebar-drag relative h-10 px-2 sm:px-3 flex items-center justify-between vibrancy hairline-b">
|
<div
|
||||||
|
onDoubleClick={onDoubleClick}
|
||||||
|
className="titlebar-drag relative h-10 px-2 sm:px-3 flex items-center justify-between vibrancy hairline-b"
|
||||||
|
>
|
||||||
<div className="flex items-center gap-1 min-w-0 flex-1 basis-0">
|
<div className="flex items-center gap-1 min-w-0 flex-1 basis-0">
|
||||||
{onMenuClick && (
|
{onMenuClick && (
|
||||||
<button
|
<button
|
||||||
@@ -28,7 +50,10 @@ export function Titlebar({ title, onMenuClick }: Props): JSX.Element {
|
|||||||
{effectiveTitle}
|
{effectiveTitle}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="titlebar-nodrag flex items-center justify-end gap-0.5 min-w-0 flex-1 basis-0">
|
{/* no-drag навешен на сами кнопки, не на обёртку: иначе из-за
|
||||||
|
flex-1 basis-0 весь кластер (включая пустое место слева от кнопок)
|
||||||
|
становится no-drag, и окно нельзя ухватить рядом с кнопками. */}
|
||||||
|
<div className="flex items-center justify-end gap-0.5 min-w-0 flex-1 basis-0">
|
||||||
<WinBtn
|
<WinBtn
|
||||||
onClick={() => window.api.minimizeMain()}
|
onClick={() => window.api.minimizeMain()}
|
||||||
label={t('titlebar.minimize_aria')}
|
label={t('titlebar.minimize_aria')}
|
||||||
@@ -36,10 +61,18 @@ export function Titlebar({ title, onMenuClick }: Props): JSX.Element {
|
|||||||
<Minus size={13} strokeWidth={2} />
|
<Minus size={13} strokeWidth={2} />
|
||||||
</WinBtn>
|
</WinBtn>
|
||||||
<WinBtn
|
<WinBtn
|
||||||
onClick={() => window.api.hideMain()}
|
onClick={() => window.api.toggleMaximizeMain()}
|
||||||
label={t('titlebar.tray_aria')}
|
label={
|
||||||
|
maximized
|
||||||
|
? t('titlebar.restore_aria')
|
||||||
|
: t('titlebar.maximize_aria')
|
||||||
|
}
|
||||||
>
|
>
|
||||||
|
{maximized ? (
|
||||||
|
<Copy size={11} strokeWidth={2} />
|
||||||
|
) : (
|
||||||
<Square size={11} strokeWidth={2} />
|
<Square size={11} strokeWidth={2} />
|
||||||
|
)}
|
||||||
</WinBtn>
|
</WinBtn>
|
||||||
<WinBtn
|
<WinBtn
|
||||||
onClick={() => window.api.closeMain()}
|
onClick={() => window.api.closeMain()}
|
||||||
@@ -69,7 +102,7 @@ function WinBtn({
|
|||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
aria-label={label}
|
aria-label={label}
|
||||||
className={[
|
className={[
|
||||||
'w-9 h-7 grid place-items-center rounded-md transition-colors text-text/55',
|
'titlebar-nodrag w-9 h-7 grid place-items-center rounded-md transition-colors text-text/55',
|
||||||
danger
|
danger
|
||||||
? 'hover:bg-destructive hover:text-white'
|
? 'hover:bg-destructive hover:text-white'
|
||||||
: 'hover:bg-text/[0.08] hover:text-text'
|
: 'hover:bg-text/[0.08] hover:text-text'
|
||||||
|
|||||||
@@ -10,9 +10,18 @@ import {
|
|||||||
import { motion } from 'framer-motion'
|
import { motion } from 'framer-motion'
|
||||||
import { Button } from './ui/Button'
|
import { Button } from './ui/Button'
|
||||||
import { Card } from './ui/Card'
|
import { Card } from './ui/Card'
|
||||||
import { useT } from '../i18n'
|
import { useT, type TFn } from '../i18n'
|
||||||
import type { UpdaterStatus } from '@shared/types'
|
import type { UpdaterStatus } from '@shared/types'
|
||||||
|
|
||||||
|
function formatChecked(ts: number, t: TFn): string {
|
||||||
|
const diffMs = Date.now() - ts
|
||||||
|
const diffMin = Math.max(0, Math.round(diffMs / 60_000))
|
||||||
|
if (diffMin < 1) return t('updater.checked.just_now')
|
||||||
|
if (diffMin < 60) return t('updater.checked.minutes_ago', { n: diffMin })
|
||||||
|
const diffH = Math.round(diffMin / 60)
|
||||||
|
return t('updater.checked.hours_ago', { n: diffH })
|
||||||
|
}
|
||||||
|
|
||||||
export function UpdaterCard(): JSX.Element {
|
export function UpdaterCard(): JSX.Element {
|
||||||
const [status, setStatus] = useState<UpdaterStatus>({ kind: 'idle' })
|
const [status, setStatus] = useState<UpdaterStatus>({ kind: 'idle' })
|
||||||
const [busy, setBusy] = useState(false)
|
const [busy, setBusy] = useState(false)
|
||||||
@@ -85,23 +94,25 @@ function Body({
|
|||||||
<Cell
|
<Cell
|
||||||
tone="info"
|
tone="info"
|
||||||
icon={
|
icon={
|
||||||
<RefreshCw
|
<RefreshCw size={16} strokeWidth={2.4} className="animate-spin" />
|
||||||
size={16}
|
|
||||||
strokeWidth={2.4}
|
|
||||||
className="animate-spin"
|
|
||||||
/>
|
|
||||||
}
|
}
|
||||||
title={t('updater.checking')}
|
title={t('updater.checking')}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (status.kind === 'not-available') {
|
if (status.kind === 'not-available') {
|
||||||
|
const subtitle = status.lastCheckedAt
|
||||||
|
? t('updater.up_to_date.subtitle_checked', {
|
||||||
|
v: status.currentVersion,
|
||||||
|
when: formatChecked(status.lastCheckedAt, t)
|
||||||
|
})
|
||||||
|
: t('updater.up_to_date.subtitle', { v: status.currentVersion })
|
||||||
return (
|
return (
|
||||||
<Cell
|
<Cell
|
||||||
tone="success"
|
tone="success"
|
||||||
icon={<CheckCircle2 size={16} strokeWidth={2.4} />}
|
icon={<CheckCircle2 size={16} strokeWidth={2.4} />}
|
||||||
title={t('updater.up_to_date')}
|
title={t('updater.up_to_date')}
|
||||||
subtitle={t('updater.up_to_date.subtitle', { v: status.currentVersion })}
|
subtitle={subtitle}
|
||||||
action={
|
action={
|
||||||
<Button variant="tinted" size="sm" onClick={onCheck} disabled={busy}>
|
<Button variant="tinted" size="sm" onClick={onCheck} disabled={busy}>
|
||||||
<RefreshCw size={13} strokeWidth={2.5} /> {t('btn.check')}
|
<RefreshCw size={13} strokeWidth={2.5} /> {t('btn.check')}
|
||||||
@@ -130,8 +141,16 @@ function Body({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (status.kind === 'downloading') {
|
if (status.kind === 'downloading') {
|
||||||
const pct = Math.max(0, Math.min(100, status.percent || 0))
|
// electron-updater fires early `download-progress` events where some
|
||||||
const mb = (n: number): string => (n / 1024 / 1024).toFixed(1)
|
// fields are undefined; guard against NaN/Infinity so we never render
|
||||||
|
// "NaN%" or "NaN MB/s".
|
||||||
|
const rawPct = Number.isFinite(status.percent) ? status.percent : 0
|
||||||
|
const pct = Math.max(0, Math.min(100, rawPct))
|
||||||
|
const mb = (n: number): string =>
|
||||||
|
Number.isFinite(n) ? (n / 1024 / 1024).toFixed(1) : '0.0'
|
||||||
|
const speed = Number.isFinite(status.bytesPerSecond)
|
||||||
|
? (status.bytesPerSecond / 1024 / 1024).toFixed(2)
|
||||||
|
: '0.00'
|
||||||
return (
|
return (
|
||||||
<div className="px-4 py-4">
|
<div className="px-4 py-4">
|
||||||
<div className="flex items-center gap-3 mb-3">
|
<div className="flex items-center gap-3 mb-3">
|
||||||
@@ -146,7 +165,7 @@ function Body({
|
|||||||
{t('updater.downloading.subtitle', {
|
{t('updater.downloading.subtitle', {
|
||||||
got: mb(status.transferred),
|
got: mb(status.transferred),
|
||||||
total: mb(status.total),
|
total: mb(status.total),
|
||||||
speed: (status.bytesPerSecond / 1024 / 1024).toFixed(2)
|
speed
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -24,15 +24,13 @@ const legacyMap: Record<LegacyVariant, Variant> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const variantClasses: Record<Variant, string> = {
|
const variantClasses: Record<Variant, string> = {
|
||||||
filled:
|
filled: 'bg-accent text-white hover:brightness-105 active:brightness-95',
|
||||||
'bg-accent text-white hover:brightness-105 active:brightness-95',
|
|
||||||
tinted:
|
tinted:
|
||||||
'bg-accent/12 text-accent hover:bg-accent/18 active:bg-accent/22 dark:bg-accent/20 dark:hover:bg-accent/25',
|
'bg-accent/12 text-accent hover:bg-accent/18 active:bg-accent/22 dark:bg-accent/20 dark:hover:bg-accent/25',
|
||||||
plain: 'text-accent hover:bg-accent/10 active:bg-accent/15',
|
plain: 'text-accent hover:bg-accent/10 active:bg-accent/15',
|
||||||
destructive:
|
destructive:
|
||||||
'bg-destructive/12 text-destructive hover:bg-destructive/18 active:bg-destructive/22 dark:bg-destructive/20',
|
'bg-destructive/12 text-destructive hover:bg-destructive/18 active:bg-destructive/22 dark:bg-destructive/20',
|
||||||
success:
|
success: 'bg-success text-white hover:brightness-105 active:brightness-95'
|
||||||
'bg-success text-white hover:brightness-105 active:brightness-95'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sizeClasses: Record<Size, string> = {
|
const sizeClasses: Record<Size, string> = {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { AnimatePresence, motion } from 'framer-motion'
|
import { AnimatePresence, motion } from 'framer-motion'
|
||||||
import { X } from 'lucide-react'
|
import { X } from 'lucide-react'
|
||||||
import { ReactNode, useEffect } from 'react'
|
import { ReactNode, useEffect, useId, useRef } from 'react'
|
||||||
|
import { useT } from '../../i18n'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
open: boolean
|
open: boolean
|
||||||
@@ -17,9 +18,25 @@ const sizeClass = {
|
|||||||
lg: 'max-w-3xl'
|
lg: 'max-w-3xl'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** All elements inside `root` that can receive keyboard focus. */
|
||||||
|
function getFocusable(root: HTMLElement): HTMLElement[] {
|
||||||
|
return Array.from(
|
||||||
|
root.querySelectorAll<HTMLElement>(
|
||||||
|
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
||||||
|
)
|
||||||
|
).filter((el) => el.offsetParent !== null || el === document.activeElement)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* iOS-style centred sheet. Spring-snap on enter, soft fade-out.
|
* iOS-style centred sheet. Spring-snap on enter, soft fade-out.
|
||||||
* Backdrop uses heavy blur for proper iOS modal feel.
|
*
|
||||||
|
* Accessibility:
|
||||||
|
* - role="dialog" + aria-modal="true" + aria-labelledby on the title <h2>
|
||||||
|
* - Focus is trapped inside the dialog while open; Tab/Shift-Tab cycle
|
||||||
|
* through focusable children and never escape to the underlying page.
|
||||||
|
* - On open the first focusable element is focused.
|
||||||
|
* - On close, focus returns to whatever was focused when the modal opened.
|
||||||
|
* - Esc closes (parent handles confirm-on-dirty if it wants).
|
||||||
*/
|
*/
|
||||||
export function Modal({
|
export function Modal({
|
||||||
open,
|
open,
|
||||||
@@ -29,6 +46,12 @@ export function Modal({
|
|||||||
footer,
|
footer,
|
||||||
size = 'md'
|
size = 'md'
|
||||||
}: Props): JSX.Element {
|
}: Props): JSX.Element {
|
||||||
|
const { t } = useT()
|
||||||
|
const titleId = useId()
|
||||||
|
const sheetRef = useRef<HTMLDivElement | null>(null)
|
||||||
|
const lastFocusedRef = useRef<HTMLElement | null>(null)
|
||||||
|
|
||||||
|
// Esc closes.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) return
|
if (!open) return
|
||||||
const onKey = (e: KeyboardEvent): void => {
|
const onKey = (e: KeyboardEvent): void => {
|
||||||
@@ -38,6 +61,60 @@ export function Modal({
|
|||||||
return () => window.removeEventListener('keydown', onKey)
|
return () => window.removeEventListener('keydown', onKey)
|
||||||
}, [open, onClose])
|
}, [open, onClose])
|
||||||
|
|
||||||
|
// Focus trap + focus restore.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return
|
||||||
|
const previouslyFocused = document.activeElement as HTMLElement | null
|
||||||
|
lastFocusedRef.current = previouslyFocused
|
||||||
|
|
||||||
|
// Defer focus to the next frame — framer-motion's enter animation may
|
||||||
|
// still be mounting children when this effect runs.
|
||||||
|
const raf = requestAnimationFrame(() => {
|
||||||
|
const root = sheetRef.current
|
||||||
|
if (!root) return
|
||||||
|
const focusables = getFocusable(root)
|
||||||
|
const first = focusables.find(
|
||||||
|
(el) => !el.hasAttribute('data-modal-close')
|
||||||
|
)
|
||||||
|
;(first ?? focusables[0])?.focus()
|
||||||
|
})
|
||||||
|
|
||||||
|
const onKeyDown = (e: KeyboardEvent): void => {
|
||||||
|
if (e.key !== 'Tab') return
|
||||||
|
const root = sheetRef.current
|
||||||
|
if (!root) return
|
||||||
|
const focusables = getFocusable(root)
|
||||||
|
if (focusables.length === 0) {
|
||||||
|
e.preventDefault()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const first = focusables[0]
|
||||||
|
const last = focusables[focusables.length - 1]
|
||||||
|
const active = document.activeElement as HTMLElement | null
|
||||||
|
if (e.shiftKey) {
|
||||||
|
if (active === first || !root.contains(active)) {
|
||||||
|
e.preventDefault()
|
||||||
|
last.focus()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (active === last || !root.contains(active)) {
|
||||||
|
e.preventDefault()
|
||||||
|
first.focus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('keydown', onKeyDown, true)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelAnimationFrame(raf)
|
||||||
|
document.removeEventListener('keydown', onKeyDown, true)
|
||||||
|
// Restore focus to the trigger (button/row) that opened the modal,
|
||||||
|
// unless it was unmounted while the modal was open.
|
||||||
|
const target = lastFocusedRef.current
|
||||||
|
if (target && document.body.contains(target)) target.focus()
|
||||||
|
}
|
||||||
|
}, [open])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimatePresence>
|
<AnimatePresence>
|
||||||
{open && (
|
{open && (
|
||||||
@@ -50,8 +127,10 @@ export function Modal({
|
|||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
>
|
>
|
||||||
<motion.div
|
<motion.div
|
||||||
|
ref={sheetRef}
|
||||||
role="dialog"
|
role="dialog"
|
||||||
aria-modal="true"
|
aria-modal="true"
|
||||||
|
aria-labelledby={titleId}
|
||||||
className={[
|
className={[
|
||||||
'relative w-full bg-surface rounded-3xl shadow-sheet flex flex-col overflow-hidden',
|
'relative w-full bg-surface rounded-3xl shadow-sheet flex flex-col overflow-hidden',
|
||||||
sizeClass[size]
|
sizeClass[size]
|
||||||
@@ -64,13 +143,17 @@ export function Modal({
|
|||||||
>
|
>
|
||||||
{/* Header — iOS large modal title */}
|
{/* Header — iOS large modal title */}
|
||||||
<div className="flex items-center justify-between px-5 pt-5 pb-3">
|
<div className="flex items-center justify-between px-5 pt-5 pb-3">
|
||||||
<h2 className="font-display text-[20px] font-semibold tracking-tight">
|
<h2
|
||||||
|
id={titleId}
|
||||||
|
className="font-display text-[20px] font-semibold tracking-tight"
|
||||||
|
>
|
||||||
{title}
|
{title}
|
||||||
</h2>
|
</h2>
|
||||||
<button
|
<button
|
||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
|
data-modal-close=""
|
||||||
className="w-7 h-7 grid place-items-center rounded-full bg-surface-2 hover:bg-hairline/25 text-text/60 hover:text-text transition-colors active:scale-90"
|
className="w-7 h-7 grid place-items-center rounded-full bg-surface-2 hover:bg-hairline/25 text-text/60 hover:text-text transition-colors active:scale-90"
|
||||||
aria-label="Закрыть"
|
aria-label={t('btn.close')}
|
||||||
>
|
>
|
||||||
<X size={14} strokeWidth={2.5} />
|
<X size={14} strokeWidth={2.5} />
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export const ru: Dict = {
|
|||||||
'sidebar.status_tracking': 'Активность отслеживается',
|
'sidebar.status_tracking': 'Активность отслеживается',
|
||||||
'titlebar.menu_aria': 'Меню',
|
'titlebar.menu_aria': 'Меню',
|
||||||
'titlebar.minimize_aria': 'Свернуть',
|
'titlebar.minimize_aria': 'Свернуть',
|
||||||
|
'titlebar.maximize_aria': 'Развернуть',
|
||||||
|
'titlebar.restore_aria': 'Восстановить размер',
|
||||||
'titlebar.tray_aria': 'В трей',
|
'titlebar.tray_aria': 'В трей',
|
||||||
'titlebar.close_aria': 'Закрыть',
|
'titlebar.close_aria': 'Закрыть',
|
||||||
'titlebar.app_title': 'Exercise Reminder',
|
'titlebar.app_title': 'Exercise Reminder',
|
||||||
@@ -116,8 +118,7 @@ export const ru: Dict = {
|
|||||||
// Games
|
// Games
|
||||||
'games.kicker': 'Трекинг матчей',
|
'games.kicker': 'Трекинг матчей',
|
||||||
'games.title': 'Игры',
|
'games.title': 'Игры',
|
||||||
'games.subtitle':
|
'games.subtitle': 'Подключи игру — челленджи сработают сразу после матча',
|
||||||
'Подключи игру — челленджи сработают сразу после матча',
|
|
||||||
'games.subtitle.live': '{n} live',
|
'games.subtitle.live': '{n} live',
|
||||||
'games.section.supported': 'Поддерживаемые',
|
'games.section.supported': 'Поддерживаемые',
|
||||||
'games.scanning': 'Сканируем установленные игры…',
|
'games.scanning': 'Сканируем установленные игры…',
|
||||||
@@ -185,6 +186,11 @@ export const ru: Dict = {
|
|||||||
'updater.checking': 'Проверяем обновления…',
|
'updater.checking': 'Проверяем обновления…',
|
||||||
'updater.up_to_date': 'Последняя версия',
|
'updater.up_to_date': 'Последняя версия',
|
||||||
'updater.up_to_date.subtitle': 'Текущая: v{v}',
|
'updater.up_to_date.subtitle': 'Текущая: v{v}',
|
||||||
|
'updater.up_to_date.subtitle_checked': 'Текущая: v{v} · проверено {when}',
|
||||||
|
'updater.last_checked': 'проверено {when}',
|
||||||
|
'updater.checked.just_now': 'только что',
|
||||||
|
'updater.checked.minutes_ago': '{n} мин назад',
|
||||||
|
'updater.checked.hours_ago': '{n} ч назад',
|
||||||
'updater.available.title': 'Доступна v{v}',
|
'updater.available.title': 'Доступна v{v}',
|
||||||
'updater.downloading.title': 'Загружаем обновление',
|
'updater.downloading.title': 'Загружаем обновление',
|
||||||
'updater.downloading.subtitle': '{got} / {total} МБ · {speed} МБ/с',
|
'updater.downloading.subtitle': '{got} / {total} МБ · {speed} МБ/с',
|
||||||
@@ -200,6 +206,32 @@ export const ru: Dict = {
|
|||||||
'reminder.reps': 'раз',
|
'reminder.reps': 'раз',
|
||||||
'reminder.next_in': 'Следующее через {interval}',
|
'reminder.next_in': 'Следующее через {interval}',
|
||||||
'reminder.partial': 'Засчитаем {actual} из {planned}',
|
'reminder.partial': 'Засчитаем {actual} из {planned}',
|
||||||
|
'reminder.aria.decrement': 'Уменьшить количество повторов',
|
||||||
|
'reminder.aria.increment': 'Увеличить количество повторов',
|
||||||
|
|
||||||
|
// Weekday short labels (Mon..Sun). Used by Settings days-of-week picker,
|
||||||
|
// HistoryHeatmap row axis, and Dashboard date headers. Index 0 = Sunday
|
||||||
|
// to match Date.getDay()'s convention so callers can use the value
|
||||||
|
// directly without re-mapping.
|
||||||
|
'weekday.short.0': 'Вс',
|
||||||
|
'weekday.short.1': 'Пн',
|
||||||
|
'weekday.short.2': 'Вт',
|
||||||
|
'weekday.short.3': 'Ср',
|
||||||
|
'weekday.short.4': 'Чт',
|
||||||
|
'weekday.short.5': 'Пт',
|
||||||
|
'weekday.short.6': 'Сб',
|
||||||
|
|
||||||
|
// History heatmap
|
||||||
|
'heatmap.title': 'Активность за 12 недель',
|
||||||
|
'heatmap.legend.less': 'Меньше',
|
||||||
|
'heatmap.legend.more': 'Больше',
|
||||||
|
'heatmap.tooltip.reps_one': '{n} повтор',
|
||||||
|
'heatmap.tooltip.reps_few': '{n} повтора',
|
||||||
|
'heatmap.tooltip.reps_many': '{n} повторов',
|
||||||
|
|
||||||
|
// Sidebar
|
||||||
|
'sidebar.aria.nav': 'Главная навигация',
|
||||||
|
'exercise.aria.toggle': 'Переключить упражнение «{name}»',
|
||||||
'reminder.btn.done': 'Готово',
|
'reminder.btn.done': 'Готово',
|
||||||
'match.title.won': 'Победа',
|
'match.title.won': 'Победа',
|
||||||
'match.title.lost': 'Поражение',
|
'match.title.lost': 'Поражение',
|
||||||
@@ -235,6 +267,8 @@ export const en: Dict = {
|
|||||||
'sidebar.status_tracking': 'Activity tracking is on',
|
'sidebar.status_tracking': 'Activity tracking is on',
|
||||||
'titlebar.menu_aria': 'Menu',
|
'titlebar.menu_aria': 'Menu',
|
||||||
'titlebar.minimize_aria': 'Minimize',
|
'titlebar.minimize_aria': 'Minimize',
|
||||||
|
'titlebar.maximize_aria': 'Maximize',
|
||||||
|
'titlebar.restore_aria': 'Restore size',
|
||||||
'titlebar.tray_aria': 'To tray',
|
'titlebar.tray_aria': 'To tray',
|
||||||
'titlebar.close_aria': 'Close',
|
'titlebar.close_aria': 'Close',
|
||||||
'titlebar.app_title': 'Exercise Reminder',
|
'titlebar.app_title': 'Exercise Reminder',
|
||||||
@@ -398,6 +432,11 @@ export const en: Dict = {
|
|||||||
'updater.checking': 'Checking for updates…',
|
'updater.checking': 'Checking for updates…',
|
||||||
'updater.up_to_date': 'Up to date',
|
'updater.up_to_date': 'Up to date',
|
||||||
'updater.up_to_date.subtitle': 'Current: v{v}',
|
'updater.up_to_date.subtitle': 'Current: v{v}',
|
||||||
|
'updater.up_to_date.subtitle_checked': 'Current: v{v} · checked {when}',
|
||||||
|
'updater.last_checked': 'checked {when}',
|
||||||
|
'updater.checked.just_now': 'just now',
|
||||||
|
'updater.checked.minutes_ago': '{n}m ago',
|
||||||
|
'updater.checked.hours_ago': '{n}h ago',
|
||||||
'updater.available.title': 'v{v} available',
|
'updater.available.title': 'v{v} available',
|
||||||
'updater.downloading.title': 'Downloading update',
|
'updater.downloading.title': 'Downloading update',
|
||||||
'updater.downloading.subtitle': '{got} / {total} MB · {speed} MB/s',
|
'updater.downloading.subtitle': '{got} / {total} MB · {speed} MB/s',
|
||||||
@@ -413,6 +452,28 @@ export const en: Dict = {
|
|||||||
'reminder.reps': 'reps',
|
'reminder.reps': 'reps',
|
||||||
'reminder.next_in': 'Next in {interval}',
|
'reminder.next_in': 'Next in {interval}',
|
||||||
'reminder.partial': "We'll log {actual} of {planned}",
|
'reminder.partial': "We'll log {actual} of {planned}",
|
||||||
|
'reminder.aria.decrement': 'Decrease rep count',
|
||||||
|
'reminder.aria.increment': 'Increase rep count',
|
||||||
|
|
||||||
|
// Weekday short labels (Mon..Sun). Index 0 = Sunday.
|
||||||
|
'weekday.short.0': 'Sun',
|
||||||
|
'weekday.short.1': 'Mon',
|
||||||
|
'weekday.short.2': 'Tue',
|
||||||
|
'weekday.short.3': 'Wed',
|
||||||
|
'weekday.short.4': 'Thu',
|
||||||
|
'weekday.short.5': 'Fri',
|
||||||
|
'weekday.short.6': 'Sat',
|
||||||
|
|
||||||
|
// History heatmap
|
||||||
|
'heatmap.title': 'Activity, last 12 weeks',
|
||||||
|
'heatmap.legend.less': 'Less',
|
||||||
|
'heatmap.legend.more': 'More',
|
||||||
|
'heatmap.tooltip.reps_one': '{n} rep',
|
||||||
|
'heatmap.tooltip.reps_many': '{n} reps',
|
||||||
|
|
||||||
|
// Sidebar
|
||||||
|
'sidebar.aria.nav': 'Main navigation',
|
||||||
|
'exercise.aria.toggle': 'Toggle exercise "{name}"',
|
||||||
'reminder.btn.done': 'Done',
|
'reminder.btn.done': 'Done',
|
||||||
'match.title.won': 'Victory',
|
'match.title.won': 'Victory',
|
||||||
'match.title.lost': 'Defeat',
|
'match.title.lost': 'Defeat',
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { translate, translateN } from './index'
|
import { translate, translateN } from './index'
|
||||||
|
import { ru, en } from './dict'
|
||||||
|
|
||||||
describe('translate', () => {
|
describe('translate', () => {
|
||||||
it('returns the matching string by key', () => {
|
it('returns the matching string by key', () => {
|
||||||
@@ -30,6 +31,50 @@ describe('translate', () => {
|
|||||||
// @ts-expect-error testing fallback
|
// @ts-expect-error testing fallback
|
||||||
expect(translate('fr', 'btn.save')).toBe('Сохранить')
|
expect(translate('fr', 'btn.save')).toBe('Сохранить')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Регрессия: до v0.5.2 интерполяция шла через regex, и если
|
||||||
|
// var-значение содержало regex-метасимволы ($1, .*, и т.д.), они
|
||||||
|
// интерпретировались как backreferences. Сейчас split/join.
|
||||||
|
it('substitutes regex metacharacters literally (no regex injection)', () => {
|
||||||
|
expect(
|
||||||
|
translate('ru', 'btn.snooze_min', { n: '$1.*' as unknown as number })
|
||||||
|
).toBe('Отложить $1.* мин')
|
||||||
|
expect(
|
||||||
|
translate('en', 'btn.snooze_min', {
|
||||||
|
n: '$$$&\\1' as unknown as number
|
||||||
|
})
|
||||||
|
).toBe('Snooze $$$&\\1m')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('leaves unsubstituted placeholders intact', () => {
|
||||||
|
// {n} остаётся как есть, если var не передан — это сигнал «забыл vars».
|
||||||
|
expect(translate('ru', 'btn.snooze_min')).toContain('{n}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('dictionary parity', () => {
|
||||||
|
// EN не имеет CLDR-категории `few` — только `one`/`many`. Поэтому RU-ключи
|
||||||
|
// вида `*_few` легитимно отсутствуют в EN, исключаем их из парити-чека.
|
||||||
|
const isRuFewOnly = (k: string): boolean => k.endsWith('_few')
|
||||||
|
|
||||||
|
it('every key in ru (except *_few) exists in en', () => {
|
||||||
|
const missing = Object.keys(ru).filter(
|
||||||
|
(k) => !isRuFewOnly(k) && !(k in en)
|
||||||
|
)
|
||||||
|
expect(missing, `missing in en: ${missing.join(', ')}`).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('every key in en exists in ru', () => {
|
||||||
|
const missing = Object.keys(en).filter((k) => !(k in ru))
|
||||||
|
expect(missing, `missing in ru: ${missing.join(', ')}`).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('weekday.short.0..6 exist in both languages', () => {
|
||||||
|
for (const i of [0, 1, 2, 3, 4, 5, 6]) {
|
||||||
|
expect(ru[`weekday.short.${i}`]).toBeTruthy()
|
||||||
|
expect(en[`weekday.short.${i}`]).toBeTruthy()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('translateN (plural)', () => {
|
describe('translateN (plural)', () => {
|
||||||
|
|||||||
@@ -9,21 +9,30 @@ export function getDict(lang: Language): Dict {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type TVars = Record<string, string | number>
|
export type TVars = Record<string, string | number>
|
||||||
|
export type TFn = (key: string, vars?: TVars) => string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Look up a key in the dictionary, substitute `{var}` placeholders.
|
* Look up a key in the dictionary, substitute `{var}` placeholders.
|
||||||
* Returns the key itself if not found — surfaces missing translations.
|
* Returns the key itself if not found — surfaces missing translations.
|
||||||
|
*
|
||||||
|
* Substitution is done by string split/join rather than a regex so that
|
||||||
|
* variable values containing regex metacharacters (e.g. a user-supplied
|
||||||
|
* exercise name with `$1` or `.*`) are interpolated literally.
|
||||||
*/
|
*/
|
||||||
export function translate(
|
export function translate(lang: Language, key: string, vars?: TVars): string {
|
||||||
lang: Language,
|
|
||||||
key: string,
|
|
||||||
vars?: TVars
|
|
||||||
): string {
|
|
||||||
const dict = getDict(lang)
|
const dict = getDict(lang)
|
||||||
let s = dict[key] ?? key
|
let s = dict[key]
|
||||||
|
if (s === undefined) {
|
||||||
|
// Surface missing translations in dev; in prod render the key so the user
|
||||||
|
// sees something deterministic instead of a blank.
|
||||||
|
if (import.meta.env.DEV) {
|
||||||
|
console.warn(`[i18n] missing key "${key}" for lang "${lang}"`)
|
||||||
|
}
|
||||||
|
s = key
|
||||||
|
}
|
||||||
if (vars) {
|
if (vars) {
|
||||||
for (const k of Object.keys(vars)) {
|
for (const k of Object.keys(vars)) {
|
||||||
s = s.replace(new RegExp(`\\{${k}\\}`, 'g'), String(vars[k]))
|
s = s.split(`{${k}}`).join(String(vars[k]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
@@ -36,8 +45,10 @@ export function translate(
|
|||||||
* many → 0, 5-20, 25-30…
|
* many → 0, 5-20, 25-30…
|
||||||
*/
|
*/
|
||||||
function pluralRu(n: number): 'one' | 'few' | 'many' {
|
function pluralRu(n: number): 'one' | 'few' | 'many' {
|
||||||
const mod10 = n % 10
|
// Always pluralize on the absolute value — a "-1" count is the same form as "1".
|
||||||
const mod100 = n % 100
|
const abs = Math.abs(Math.trunc(n))
|
||||||
|
const mod10 = abs % 10
|
||||||
|
const mod100 = abs % 100
|
||||||
if (mod10 === 1 && mod100 !== 11) return 'one'
|
if (mod10 === 1 && mod100 !== 11) return 'one'
|
||||||
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return 'few'
|
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return 'few'
|
||||||
return 'many'
|
return 'many'
|
||||||
@@ -54,12 +65,7 @@ export function translateN(
|
|||||||
n: number,
|
n: number,
|
||||||
vars?: TVars
|
vars?: TVars
|
||||||
): string {
|
): string {
|
||||||
const form =
|
const form = lang === 'ru' ? pluralRu(n) : n === 1 ? 'one' : 'many'
|
||||||
lang === 'ru'
|
|
||||||
? pluralRu(n)
|
|
||||||
: n === 1
|
|
||||||
? 'one'
|
|
||||||
: 'many'
|
|
||||||
return translate(lang, `${keyBase}_${form}`, { n, ...vars })
|
return translate(lang, `${keyBase}_${form}`, { n, ...vars })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,29 @@ describe('formatCountdown', () => {
|
|||||||
expect(formatCountdown(999)).toBe('0с')
|
expect(formatCountdown(999)).toBe('0с')
|
||||||
expect(formatCountdown(500)).toBe('0с')
|
expect(formatCountdown(500)).toBe('0с')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Guard added in v0.5.2 — electron-updater и scheduler могут передать
|
||||||
|
// NaN/Infinity на ранних событиях. Должны вернуть «сейчас», не «NaNс».
|
||||||
|
it('returns "сейчас" for NaN and Infinity (defensive guard)', () => {
|
||||||
|
expect(formatCountdown(NaN)).toBe('сейчас')
|
||||||
|
expect(formatCountdown(Infinity)).toBe('сейчас')
|
||||||
|
expect(formatCountdown(-Infinity)).toBe('сейчас')
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('english locale', () => {
|
||||||
|
it('renders sub-minute with "s"', () => {
|
||||||
|
expect(formatCountdown(45_000, 'en')).toBe('45s')
|
||||||
|
})
|
||||||
|
it('renders minutes+seconds with "m"/"s"', () => {
|
||||||
|
expect(formatCountdown(65_000, 'en')).toBe('1m 05s')
|
||||||
|
})
|
||||||
|
it('renders hours+minutes with "h"/"m"', () => {
|
||||||
|
expect(formatCountdown(3_660_000, 'en')).toBe('1h 01m')
|
||||||
|
})
|
||||||
|
it('returns "now" for zero', () => {
|
||||||
|
expect(formatCountdown(0, 'en')).toBe('now')
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('formatInterval', () => {
|
describe('formatInterval', () => {
|
||||||
@@ -53,4 +76,10 @@ describe('formatInterval', () => {
|
|||||||
expect(formatInterval(90)).toBe('1 ч 30 мин')
|
expect(formatInterval(90)).toBe('1 ч 30 мин')
|
||||||
expect(formatInterval(125)).toBe('2 ч 5 мин')
|
expect(formatInterval(125)).toBe('2 ч 5 мин')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('english locale', () => {
|
||||||
|
expect(formatInterval(30, 'en')).toBe('30 min')
|
||||||
|
expect(formatInterval(60, 'en')).toBe('1 h')
|
||||||
|
expect(formatInterval(90, 'en')).toBe('1 h 30 min')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const SUFFIX = {
|
|||||||
|
|
||||||
export function formatCountdown(ms: number, lang: Language = 'ru'): string {
|
export function formatCountdown(ms: number, lang: Language = 'ru'): string {
|
||||||
const s = SUFFIX[lang] ?? SUFFIX.ru
|
const s = SUFFIX[lang] ?? SUFFIX.ru
|
||||||
if (ms <= 0) return s.now
|
if (!Number.isFinite(ms) || ms <= 0) return s.now
|
||||||
const totalSec = Math.floor(ms / 1000)
|
const totalSec = Math.floor(ms / 1000)
|
||||||
const h = Math.floor(totalSec / 3600)
|
const h = Math.floor(totalSec / 3600)
|
||||||
const m = Math.floor((totalSec % 3600) / 60)
|
const m = Math.floor((totalSec % 3600) / 60)
|
||||||
@@ -17,10 +17,7 @@ export function formatCountdown(ms: number, lang: Language = 'ru'): string {
|
|||||||
return `${sec}${s.s}`
|
return `${sec}${s.s}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatInterval(
|
export function formatInterval(minutes: number, lang: Language = 'ru'): string {
|
||||||
minutes: number,
|
|
||||||
lang: Language = 'ru'
|
|
||||||
): string {
|
|
||||||
const s = SUFFIX[lang] ?? SUFFIX.ru
|
const s = SUFFIX[lang] ?? SUFFIX.ru
|
||||||
if (minutes < 60) return `${minutes} ${s.minLong}`
|
if (minutes < 60) return `${minutes} ${s.minLong}`
|
||||||
const h = Math.floor(minutes / 60)
|
const h = Math.floor(minutes / 60)
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import type { Exercise, HistoryEntry } from '@shared/types'
|
import type { Exercise, HistoryEntry } from '@shared/types'
|
||||||
import { currentStreak, dailyReps, dayKey, dailyRepsRange } from './history'
|
import {
|
||||||
|
currentStreak,
|
||||||
|
dailyReps,
|
||||||
|
dayKey,
|
||||||
|
dailyRepsRange,
|
||||||
|
plannedRepsToday
|
||||||
|
} from './history'
|
||||||
|
|
||||||
const MS_DAY = 24 * 60 * 60 * 1000
|
const MS_DAY = 24 * 60 * 60 * 1000
|
||||||
|
|
||||||
@@ -94,19 +100,12 @@ describe('currentStreak', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('ignores skip and snooze', () => {
|
it('ignores skip and snooze', () => {
|
||||||
const hist = [
|
const hist = [entry('a', day(0), 'skip'), entry('a', day(1), 'snooze')]
|
||||||
entry('a', day(0), 'skip'),
|
|
||||||
entry('a', day(1), 'snooze')
|
|
||||||
]
|
|
||||||
expect(currentStreak(hist)).toBe(0)
|
expect(currentStreak(hist)).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('multiple entries same day count once', () => {
|
it('multiple entries same day count once', () => {
|
||||||
const hist = [
|
const hist = [entry('a', day(0)), entry('b', day(0)), entry('a', day(1))]
|
||||||
entry('a', day(0)),
|
|
||||||
entry('b', day(0)),
|
|
||||||
entry('a', day(1))
|
|
||||||
]
|
|
||||||
expect(currentStreak(hist)).toBe(2)
|
expect(currentStreak(hist)).toBe(2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -124,4 +123,77 @@ describe('dailyRepsRange', () => {
|
|||||||
expect(range.at(-1)?.reps).toBe(10) // today
|
expect(range.at(-1)?.reps).toBe(10) // today
|
||||||
expect(range.at(-2)?.reps).toBe(3) // yesterday, partial
|
expect(range.at(-2)?.reps).toBe(3) // yesterday, partial
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// DST regression: до v0.5.2 dailyRepsRange использовал `ts - i*MS_DAY`.
|
||||||
|
// На границе DST (например в EU last Sunday October — 25 час) арифметика
|
||||||
|
// ms-vs-календарь расходилась, и dayKey() выдавал дубликат/пропуск дня.
|
||||||
|
// Сейчас shiftDays() через setDate(). Простой инвариант: количество
|
||||||
|
// уникальных day-keys всегда == days, и все keys строго возрастают.
|
||||||
|
it('produces unique day keys without gaps (DST-safe)', () => {
|
||||||
|
const range = dailyRepsRange([], [], 90)
|
||||||
|
const keys = range.map((r) => r.key)
|
||||||
|
expect(new Set(keys).size).toBe(90)
|
||||||
|
for (let i = 1; i < keys.length; i++) {
|
||||||
|
expect(keys[i] > keys[i - 1]).toBe(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('last entry is today', () => {
|
||||||
|
const range = dailyRepsRange([], [], 7)
|
||||||
|
expect(range.at(-1)?.key).toBe(dayKey(Date.now()))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('plannedRepsToday', () => {
|
||||||
|
it('returns 0 when no exercises enabled', () => {
|
||||||
|
const exs = [{ ...ex('a', 10), enabled: false }]
|
||||||
|
expect(plannedRepsToday(exs)).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns 0 for empty list', () => {
|
||||||
|
expect(plannedRepsToday([])).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('multiplies reps by approximate fires per day', () => {
|
||||||
|
// 60-min interval × 24 = 24 fires/day × 10 reps = 240
|
||||||
|
const exs = [{ ...ex('a', 10), intervalMinutes: 60 }]
|
||||||
|
expect(plannedRepsToday(exs)).toBe(240)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sums across multiple enabled exercises', () => {
|
||||||
|
const exs = [
|
||||||
|
{ ...ex('a', 10), intervalMinutes: 60 }, // 24 × 10 = 240
|
||||||
|
{ ...ex('b', 5), intervalMinutes: 30 } // 48 × 5 = 240
|
||||||
|
]
|
||||||
|
expect(plannedRepsToday(exs)).toBe(480)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('floor of (1440/interval), minimum 1 fire/day for huge intervals', () => {
|
||||||
|
// 1440-min interval = 1 fire/day; 2000-min interval should still be ≥ 1.
|
||||||
|
const exs = [{ ...ex('a', 7), intervalMinutes: 2000 }]
|
||||||
|
expect(plannedRepsToday(exs)).toBe(7)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('currentStreak edge cases', () => {
|
||||||
|
const today = Date.now()
|
||||||
|
|
||||||
|
it('ignores future-dated entries (clock skew, partial restore)', () => {
|
||||||
|
const tomorrow = today + 24 * 60 * 60 * 1000
|
||||||
|
// future entry shouldn't anchor the streak.
|
||||||
|
expect(currentStreak([entry('a', tomorrow)])).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles entries spread across the same day with mixed actions', () => {
|
||||||
|
const e = (
|
||||||
|
action: 'done' | 'skip' | 'snooze',
|
||||||
|
ts: number
|
||||||
|
): HistoryEntry => entry('a', ts, action)
|
||||||
|
const hist = [
|
||||||
|
e('skip', today),
|
||||||
|
e('done', today), // done is enough — streak counts the day
|
||||||
|
e('snooze', today)
|
||||||
|
]
|
||||||
|
expect(currentStreak(hist)).toBe(1)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import type { Exercise, HistoryEntry } from '@shared/types'
|
import type { Exercise, HistoryEntry } from '@shared/types'
|
||||||
|
|
||||||
const MS_DAY = 24 * 60 * 60 * 1000
|
|
||||||
|
|
||||||
/** YYYY-MM-DD in local time. */
|
/** YYYY-MM-DD in local time. */
|
||||||
export function dayKey(ts: number): string {
|
export function dayKey(ts: number): string {
|
||||||
const d = new Date(ts)
|
const d = new Date(ts)
|
||||||
@@ -16,6 +14,18 @@ export function todayKey(): string {
|
|||||||
return dayKey(Date.now())
|
return dayKey(Date.now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a new Date offset by `dayDelta` calendar days from `base`, with the
|
||||||
|
* time-of-day preserved. Uses `setDate` (calendar arithmetic) rather than
|
||||||
|
* subtracting `n * 24h` of milliseconds — across DST transitions ms arithmetic
|
||||||
|
* drifts by ±1h and `dayKey` can emit the wrong day.
|
||||||
|
*/
|
||||||
|
function shiftDays(base: Date, dayDelta: number): Date {
|
||||||
|
const d = new Date(base.getTime())
|
||||||
|
d.setDate(d.getDate() + dayDelta)
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reps logged on a given local day. Uses `actualReps` if present, otherwise
|
* Reps logged on a given local day. Uses `actualReps` if present, otherwise
|
||||||
* looks up exercise's planned `reps`.
|
* looks up exercise's planned `reps`.
|
||||||
@@ -46,28 +56,27 @@ export function dailyRepsRange(
|
|||||||
): { key: string; date: Date; reps: number }[] {
|
): { key: string; date: Date; reps: number }[] {
|
||||||
const today = new Date()
|
const today = new Date()
|
||||||
today.setHours(0, 0, 0, 0)
|
today.setHours(0, 0, 0, 0)
|
||||||
const buckets = new Map<string, number>()
|
const buckets = new Map<string, { date: Date; reps: number }>()
|
||||||
const byId = new Map(exercises.map((e) => [e.id, e]))
|
const byId = new Map(exercises.map((e) => [e.id, e]))
|
||||||
|
|
||||||
// Seed all days with 0 so heatmap renders contiguous.
|
// Seed all days with 0 so heatmap renders contiguous. Use calendar arithmetic
|
||||||
|
// (setDate) — DST transitions would shift epoch-based math by ±1h, causing
|
||||||
|
// dayKey() to emit duplicate or missing days at the boundary.
|
||||||
for (let i = days - 1; i >= 0; i--) {
|
for (let i = days - 1; i >= 0; i--) {
|
||||||
const d = new Date(today.getTime() - i * MS_DAY)
|
const d = shiftDays(today, -i)
|
||||||
buckets.set(dayKey(d.getTime()), 0)
|
buckets.set(dayKey(d.getTime()), { date: d, reps: 0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
if (e.action !== 'done') continue
|
if (e.action !== 'done') continue
|
||||||
const k = dayKey(e.ts)
|
const k = dayKey(e.ts)
|
||||||
if (!buckets.has(k)) continue
|
const bucket = buckets.get(k)
|
||||||
|
if (!bucket) continue
|
||||||
const reps = e.actualReps ?? byId.get(e.exerciseId)?.reps ?? 0
|
const reps = e.actualReps ?? byId.get(e.exerciseId)?.reps ?? 0
|
||||||
buckets.set(k, (buckets.get(k) ?? 0) + reps)
|
bucket.reps += reps
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.from(buckets, ([key, reps]) => ({
|
return Array.from(buckets, ([key, { date, reps }]) => ({ key, date, reps }))
|
||||||
key,
|
|
||||||
date: new Date(`${key}T00:00:00`),
|
|
||||||
reps
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,21 +93,22 @@ export function currentStreak(entries: HistoryEntry[]): number {
|
|||||||
|
|
||||||
const today = new Date()
|
const today = new Date()
|
||||||
today.setHours(0, 0, 0, 0)
|
today.setHours(0, 0, 0, 0)
|
||||||
|
const yesterday = shiftDays(today, -1)
|
||||||
const todayK = dayKey(today.getTime())
|
const todayK = dayKey(today.getTime())
|
||||||
const yesterdayK = dayKey(today.getTime() - MS_DAY)
|
const yesterdayK = dayKey(yesterday.getTime())
|
||||||
|
|
||||||
// Start from today if active today, else yesterday (grace), else 0.
|
// Start from today if active today, else yesterday (grace), else 0.
|
||||||
let cursor = doneDays.has(todayK)
|
let cursor: Date | null = doneDays.has(todayK)
|
||||||
? today
|
? today
|
||||||
: doneDays.has(yesterdayK)
|
: doneDays.has(yesterdayK)
|
||||||
? new Date(today.getTime() - MS_DAY)
|
? yesterday
|
||||||
: null
|
: null
|
||||||
if (!cursor) return 0
|
if (!cursor) return 0
|
||||||
|
|
||||||
let streak = 0
|
let streak = 0
|
||||||
while (doneDays.has(dayKey(cursor.getTime()))) {
|
while (doneDays.has(dayKey(cursor.getTime()))) {
|
||||||
streak++
|
streak++
|
||||||
cursor = new Date(cursor.getTime() - MS_DAY)
|
cursor = shiftDays(cursor, -1)
|
||||||
}
|
}
|
||||||
return streak
|
return streak
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/renderer/src/lib/icon-choices.test.ts
Normal file
26
src/renderer/src/lib/icon-choices.test.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { ICON_CHOICES } from './icon-choices'
|
||||||
|
import { SAMPLE_EXERCISES } from '@shared/types'
|
||||||
|
|
||||||
|
describe('ICON_CHOICES', () => {
|
||||||
|
// Если иконка SAMPLE_EXERCISES не входит в whitelist, при первом запуске
|
||||||
|
// приложения иконка молча заменится на fallback-Activity. Лучше ловить
|
||||||
|
// расхождение в CI.
|
||||||
|
it('contains every icon used by SAMPLE_EXERCISES', () => {
|
||||||
|
const allowed = new Set<string>(ICON_CHOICES)
|
||||||
|
for (const ex of SAMPLE_EXERCISES) {
|
||||||
|
expect(
|
||||||
|
allowed.has(ex.icon),
|
||||||
|
`icon "${ex.icon}" for sample "${ex.name}" is not in ICON_CHOICES`
|
||||||
|
).toBe(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('has no duplicates', () => {
|
||||||
|
expect(new Set(ICON_CHOICES).size).toBe(ICON_CHOICES.length)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is non-empty', () => {
|
||||||
|
expect(ICON_CHOICES.length).toBeGreaterThan(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
27
src/renderer/src/lib/icon-choices.ts
Normal file
27
src/renderer/src/lib/icon-choices.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* Whitelist of allowed Lucide-icon names. Wrapped in a separate .ts file
|
||||||
|
* (без JSX), чтобы его можно было импортировать из node-tests и из shared/
|
||||||
|
* без подтягивания JSX-зависимости icon.tsx.
|
||||||
|
*/
|
||||||
|
export const ICON_CHOICES = [
|
||||||
|
'Activity',
|
||||||
|
'Dumbbell',
|
||||||
|
'StretchHorizontal',
|
||||||
|
'PersonStanding',
|
||||||
|
'Heart',
|
||||||
|
'Footprints',
|
||||||
|
'Hand',
|
||||||
|
'Eye',
|
||||||
|
'Brain',
|
||||||
|
'Bike',
|
||||||
|
'Waves',
|
||||||
|
'Wind',
|
||||||
|
'Sun',
|
||||||
|
'Coffee',
|
||||||
|
'Apple',
|
||||||
|
'GlassWater',
|
||||||
|
'BookOpen',
|
||||||
|
'Sparkles'
|
||||||
|
] as const
|
||||||
|
|
||||||
|
export type IconName = (typeof ICON_CHOICES)[number]
|
||||||
@@ -1,36 +1,31 @@
|
|||||||
import * as Lucide from 'lucide-react'
|
import * as Lucide from 'lucide-react'
|
||||||
import type { LucideProps } from 'lucide-react'
|
import type { LucideProps } from 'lucide-react'
|
||||||
|
import { ICON_CHOICES, type IconName } from './icon-choices'
|
||||||
|
|
||||||
export const ICON_CHOICES = [
|
// Re-export для обратной совместимости с импортёрами icon.tsx.
|
||||||
'Activity',
|
export { ICON_CHOICES, type IconName }
|
||||||
'Dumbbell',
|
|
||||||
'StretchHorizontal',
|
|
||||||
'PersonStanding',
|
|
||||||
'Heart',
|
|
||||||
'Footprints',
|
|
||||||
'Hand',
|
|
||||||
'Eye',
|
|
||||||
'Brain',
|
|
||||||
'Bike',
|
|
||||||
'Waves',
|
|
||||||
'Wind',
|
|
||||||
'Sun',
|
|
||||||
'Coffee',
|
|
||||||
'Apple',
|
|
||||||
'GlassWater',
|
|
||||||
'BookOpen',
|
|
||||||
'Sparkles'
|
|
||||||
] as const
|
|
||||||
|
|
||||||
export type IconName = (typeof ICON_CHOICES)[number]
|
const ICON_SET = new Set<string>(ICON_CHOICES)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a Lucide icon by name. Restricted to the curated ICON_CHOICES set —
|
||||||
|
* an arbitrary string from a corrupted state file could otherwise resolve to
|
||||||
|
* unrelated Lucide exports (`default`, `createLucideIcon`, etc.) and crash
|
||||||
|
* the renderer.
|
||||||
|
*/
|
||||||
export function Icon({
|
export function Icon({
|
||||||
name,
|
name,
|
||||||
...props
|
...props
|
||||||
}: { name: string } & LucideProps): JSX.Element {
|
}: { name: string } & LucideProps): JSX.Element {
|
||||||
const Cmp = (Lucide as unknown as Record<string, React.ComponentType<LucideProps>>)[
|
if (!ICON_SET.has(name)) {
|
||||||
name
|
if (import.meta.env.DEV) {
|
||||||
]
|
console.warn(`[Icon] unknown icon name "${name}" — falling back`)
|
||||||
|
}
|
||||||
|
return <Lucide.Activity {...props} />
|
||||||
|
}
|
||||||
|
const Cmp = (
|
||||||
|
Lucide as unknown as Record<string, React.ComponentType<LucideProps>>
|
||||||
|
)[name]
|
||||||
if (!Cmp) return <Lucide.Activity {...props} />
|
if (!Cmp) return <Lucide.Activity {...props} />
|
||||||
return <Cmp {...props} />
|
return <Cmp {...props} />
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ const which = params.get('window') ?? 'main'
|
|||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<ThemeProvider>{which === 'reminder' ? <ReminderApp /> : <App />}</ThemeProvider>
|
<ThemeProvider>
|
||||||
|
{which === 'reminder' ? <ReminderApp /> : <App />}
|
||||||
|
</ThemeProvider>
|
||||||
</React.StrictMode>
|
</React.StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,15 +18,20 @@ export default function Dashboard(): JSX.Element {
|
|||||||
const [editing, setEditing] = useState<Exercise | null>(null)
|
const [editing, setEditing] = useState<Exercise | null>(null)
|
||||||
const { t, lang } = useT()
|
const { t, lang } = useT()
|
||||||
|
|
||||||
const exercises = state?.exercises ?? []
|
// Memoise the exercises array reference so downstream useMemos don't fire
|
||||||
|
// on every render — `state?.exercises ?? []` creates a fresh array each time
|
||||||
|
// the parent re-renders even when nothing changed.
|
||||||
|
const exercises = useMemo(() => state?.exercises ?? [], [state?.exercises])
|
||||||
const settings = state?.settings
|
const settings = state?.settings
|
||||||
const gamesEnabled = Object.values(state?.gamesEnabled ?? {}).some(Boolean)
|
const gamesEnabled = Object.values(state?.gamesEnabled ?? {}).some(Boolean)
|
||||||
|
|
||||||
// Local history mirror; reloaded whenever app-state changes.
|
// Local history mirror; reloaded only when exercises change (not on every
|
||||||
|
// tick or settings tweak — those don't affect history). When ticks/settings
|
||||||
|
// change we don't re-fetch.
|
||||||
const [history, setHistory] = useState<HistoryEntry[]>([])
|
const [history, setHistory] = useState<HistoryEntry[]>([])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void window.api.getHistory().then(setHistory)
|
void window.api.getHistory().then(setHistory)
|
||||||
}, [state])
|
}, [exercises])
|
||||||
|
|
||||||
const todayDone = useMemo(
|
const todayDone = useMemo(
|
||||||
() => dailyReps(history, exercises, todayKey()),
|
() => dailyReps(history, exercises, todayKey()),
|
||||||
@@ -34,7 +39,11 @@ export default function Dashboard(): JSX.Element {
|
|||||||
)
|
)
|
||||||
const streak = useMemo(() => currentStreak(history), [history])
|
const streak = useMemo(() => currentStreak(history), [history])
|
||||||
|
|
||||||
|
// `ticks` is intentionally a dep so the countdown re-evaluates each second
|
||||||
|
// even though Date.now() inside isn't a reactive dependency. Reference it
|
||||||
|
// once inside the memo so ESLint sees the dep as used.
|
||||||
const stats = useMemo(() => {
|
const stats = useMemo(() => {
|
||||||
|
void ticks // re-run on tick (Date.now() is the actual driver)
|
||||||
const enabled = exercises.filter((e) => e.enabled)
|
const enabled = exercises.filter((e) => e.enabled)
|
||||||
const next = enabled
|
const next = enabled
|
||||||
.map((e) => ({ id: e.id, ms: e.nextFireAt - Date.now() }))
|
.map((e) => ({ id: e.id, ms: e.nextFireAt - Date.now() }))
|
||||||
@@ -124,10 +133,14 @@ export default function Dashboard(): JSX.Element {
|
|||||||
icon={<Flame size={14} strokeWidth={2.6} />}
|
icon={<Flame size={14} strokeWidth={2.6} />}
|
||||||
/>
|
/>
|
||||||
<HeroStat
|
<HeroStat
|
||||||
tone="info"
|
tone={paused ? 'muted' : 'info'}
|
||||||
label={t('dashboard.stat.next')}
|
label={t('dashboard.stat.next')}
|
||||||
|
// When paused, the countdown freezes — show a dash instead of a
|
||||||
|
// number that keeps ticking down, which is misleading.
|
||||||
value={
|
value={
|
||||||
stats.nextMs === Infinity
|
paused
|
||||||
|
? '—'
|
||||||
|
: stats.nextMs === Infinity
|
||||||
? '—'
|
? '—'
|
||||||
: stats.nextMs <= 0
|
: stats.nextMs <= 0
|
||||||
? t('dashboard.stat.next.now')
|
? t('dashboard.stat.next.now')
|
||||||
@@ -166,11 +179,7 @@ export default function Dashboard(): JSX.Element {
|
|||||||
|
|
||||||
{history.length > 0 && (
|
{history.length > 0 && (
|
||||||
<div className="mb-8">
|
<div className="mb-8">
|
||||||
<HistoryHeatmap
|
<HistoryHeatmap history={history} exercises={exercises} />
|
||||||
history={history}
|
|
||||||
exercises={exercises}
|
|
||||||
lang={lang}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -130,9 +130,7 @@ function ExerciseRow({
|
|||||||
<div
|
<div
|
||||||
className={[
|
className={[
|
||||||
'w-9 h-9 rounded-lg grid place-items-center shrink-0',
|
'w-9 h-9 rounded-lg grid place-items-center shrink-0',
|
||||||
exercise.enabled
|
exercise.enabled ? 'bg-accent text-white' : 'bg-text/15 text-text/45'
|
||||||
? 'bg-accent text-white'
|
|
||||||
: 'bg-text/15 text-text/45'
|
|
||||||
].join(' ')}
|
].join(' ')}
|
||||||
>
|
>
|
||||||
<Icon name={exercise.icon} size={18} strokeWidth={2.2} />
|
<Icon name={exercise.icon} size={18} strokeWidth={2.2} />
|
||||||
|
|||||||
@@ -54,9 +54,7 @@ export default function GamesPage(): JSX.Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const liveCount = games.filter(
|
const liveCount = games.filter((g) => g.enabled && g.integrationActive).length
|
||||||
(g) => g.enabled && g.integrationActive
|
|
||||||
).length
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full overflow-y-auto">
|
<div className="h-full overflow-y-auto">
|
||||||
@@ -170,11 +168,7 @@ function GameCard({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{game.installed && game.integrationActive && (
|
{game.installed && game.integrationActive && (
|
||||||
<Switch
|
<Switch checked={game.enabled} onChange={onToggle} disabled={busy} />
|
||||||
checked={game.enabled}
|
|
||||||
onChange={onToggle}
|
|
||||||
disabled={busy}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -280,8 +274,16 @@ function StatusBadge({
|
|||||||
function DevPanel({ games }: { games: GameStatus[] }): JSX.Element | null {
|
function DevPanel({ games }: { games: GameStatus[] }): JSX.Element | null {
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
const { t } = useT()
|
const { t } = useT()
|
||||||
|
// Never render in packaged builds — the matching IPC handler is also
|
||||||
|
// unregistered there, so the buttons would do nothing anyway.
|
||||||
|
if (!import.meta.env.DEV) return null
|
||||||
const dota = games.find((g) => g.id === 'dota2')
|
const dota = games.find((g) => g.id === 'dota2')
|
||||||
if (!dota?.enabled) return null
|
if (!dota?.enabled) return null
|
||||||
|
// In dev the preload exposes window.api.simulateMatchEnd; the conditional
|
||||||
|
// type still hides it, so we narrow here.
|
||||||
|
const api = window.api as typeof window.api & {
|
||||||
|
simulateMatchEnd?: (id: 'dota2', stats: Record<string, number>) => void
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div className="mt-10">
|
<div className="mt-10">
|
||||||
<button
|
<button
|
||||||
@@ -305,7 +307,7 @@ function DevPanel({ games }: { games: GameStatus[] }): JSX.Element | null {
|
|||||||
).map((p) => (
|
).map((p) => (
|
||||||
<button
|
<button
|
||||||
key={p.label}
|
key={p.label}
|
||||||
onClick={() => window.api.simulateMatchEnd('dota2', p.stats)}
|
onClick={() => api.simulateMatchEnd?.('dota2', p.stats)}
|
||||||
className="text-[12px] px-3 py-1.5 rounded-full bg-surface-2 hover:bg-accent/15 hover:text-accent text-text/70 font-medium transition-colors active:scale-95"
|
className="text-[12px] px-3 py-1.5 rounded-full bg-surface-2 hover:bg-accent/15 hover:text-accent text-text/70 font-medium transition-colors active:scale-95"
|
||||||
>
|
>
|
||||||
{p.label}
|
{p.label}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
import { useAppStore } from '../store/appStore'
|
import { useAppStore } from '../store/appStore'
|
||||||
import { Switch } from '../components/ui/Switch'
|
import { Switch } from '../components/ui/Switch'
|
||||||
import { Card, Row, SectionHeader } from '../components/ui/Card'
|
import { Card, Row, SectionHeader } from '../components/ui/Card'
|
||||||
@@ -204,6 +205,30 @@ function QuietTimesRow({
|
|||||||
last?: boolean
|
last?: boolean
|
||||||
}): JSX.Element {
|
}): JSX.Element {
|
||||||
const { t } = useT()
|
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.
|
||||||
|
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.
|
||||||
|
useEffect(() => {
|
||||||
|
setFrom(qh.from)
|
||||||
|
}, [qh.from])
|
||||||
|
useEffect(() => {
|
||||||
|
setTo(qh.to)
|
||||||
|
}, [qh.to])
|
||||||
|
|
||||||
|
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 (f === qh.from && tt === qh.to) return
|
||||||
|
onChange({ ...qh, from: f, to: tt })
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row last={last} className={disabled ? 'opacity-50' : ''}>
|
<Row last={last} className={disabled ? 'opacity-50' : ''}>
|
||||||
<div className="flex-1 min-w-0">
|
<div className="flex-1 min-w-0">
|
||||||
@@ -217,17 +242,19 @@ function QuietTimesRow({
|
|||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<input
|
<input
|
||||||
type="time"
|
type="time"
|
||||||
value={qh.from}
|
value={from}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onChange={(e) => onChange({ ...qh, from: e.target.value })}
|
onChange={(e) => setFrom(e.target.value)}
|
||||||
|
onBlur={() => commit({ from })}
|
||||||
className="h-9 px-3 rounded-xl bg-surface-2 text-[14px] outline-none focus:ring-2 focus:ring-accent/45 transition-all border-0 font-mono-num"
|
className="h-9 px-3 rounded-xl bg-surface-2 text-[14px] outline-none focus:ring-2 focus:ring-accent/45 transition-all border-0 font-mono-num"
|
||||||
/>
|
/>
|
||||||
<span className="text-text/45 text-[14px]">—</span>
|
<span className="text-text/45 text-[14px]">—</span>
|
||||||
<input
|
<input
|
||||||
type="time"
|
type="time"
|
||||||
value={qh.to}
|
value={to}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onChange={(e) => onChange({ ...qh, to: e.target.value })}
|
onChange={(e) => setTo(e.target.value)}
|
||||||
|
onBlur={() => commit({ to })}
|
||||||
className="h-9 px-3 rounded-xl bg-surface-2 text-[14px] outline-none focus:ring-2 focus:ring-accent/45 transition-all border-0 font-mono-num"
|
className="h-9 px-3 rounded-xl bg-surface-2 text-[14px] outline-none focus:ring-2 focus:ring-accent/45 transition-all border-0 font-mono-num"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -246,17 +273,19 @@ function QuietDaysRow({
|
|||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
last?: boolean
|
last?: boolean
|
||||||
}): JSX.Element {
|
}): JSX.Element {
|
||||||
const { t, lang } = useT()
|
const { t } = useT()
|
||||||
const labels =
|
// Indices match Date.getDay() (0 = Sunday) — same convention as
|
||||||
lang === 'en'
|
// src/shared/types.ts QuietHours.days values.
|
||||||
? ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
const labels = [0, 1, 2, 3, 4, 5, 6].map((i) => t(`weekday.short.${i}`))
|
||||||
: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб']
|
|
||||||
|
|
||||||
function toggle(d: number): void {
|
function toggle(d: number): void {
|
||||||
const set = new Set(qh.days)
|
const set = new Set(qh.days)
|
||||||
if (set.has(d)) set.delete(d)
|
if (set.has(d)) set.delete(d)
|
||||||
else set.add(d)
|
else set.add(d)
|
||||||
onChange({ ...qh, days: Array.from(set).sort() })
|
// Numeric sort — default Array.sort() does lexical and would order
|
||||||
|
// [0,1,10,2] as [0,1,10,2]; even though days are single-digit today the
|
||||||
|
// explicit comparator survives future widening.
|
||||||
|
onChange({ ...qh, days: Array.from(set).sort((a, b) => a - b) })
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { ReactNode, useEffect, useState } from 'react'
|
import { ReactNode, useEffect, useState } from 'react'
|
||||||
import { useAppStore } from '../store/appStore'
|
import { useAppStore } from '../store/appStore'
|
||||||
|
|
||||||
export function ThemeProvider({ children }: { children: ReactNode }): JSX.Element {
|
export function ThemeProvider({
|
||||||
|
children
|
||||||
|
}: {
|
||||||
|
children: ReactNode
|
||||||
|
}): JSX.Element {
|
||||||
const settings = useAppStore((s) => s.state?.settings)
|
const settings = useAppStore((s) => s.state?.settings)
|
||||||
const [osTheme, setOsTheme] = useState<'light' | 'dark'>('dark')
|
const [osTheme, setOsTheme] = useState<'light' | 'dark'>('dark')
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ export const useAppStore = create<Store>((set) => ({
|
|||||||
export function subscribeToBackend(): () => void {
|
export function subscribeToBackend(): () => void {
|
||||||
const store = useAppStore.getState()
|
const store = useAppStore.getState()
|
||||||
store.hydrate()
|
store.hydrate()
|
||||||
const u1 = window.api.onStateChanged((s) => useAppStore.getState().setState(s))
|
const u1 = window.api.onStateChanged((s) =>
|
||||||
|
useAppStore.getState().setState(s)
|
||||||
|
)
|
||||||
const u2 = window.api.onTick((t) => useAppStore.getState().setTicks(t))
|
const u2 = window.api.onTick((t) => useAppStore.getState().setTicks(t))
|
||||||
return () => {
|
return () => {
|
||||||
u1()
|
u1()
|
||||||
|
|||||||
@@ -114,8 +114,8 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.font-mono-num {
|
.font-mono-num {
|
||||||
font-family: 'JetBrains Mono', ui-monospace, 'SF Mono', 'Cascadia Code',
|
font-family:
|
||||||
Menlo, monospace;
|
'JetBrains Mono', ui-monospace, 'SF Mono', 'Cascadia Code', Menlo, monospace;
|
||||||
font-variant-numeric: tabular-nums;
|
font-variant-numeric: tabular-nums;
|
||||||
font-feature-settings: 'ss02', 'ss19', 'zero';
|
font-feature-settings: 'ss02', 'ss19', 'zero';
|
||||||
letter-spacing: -0.01em;
|
letter-spacing: -0.01em;
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ export const IPC = {
|
|||||||
resumeAll: 'app:resumeAll',
|
resumeAll: 'app:resumeAll',
|
||||||
quit: 'app:quit',
|
quit: 'app:quit',
|
||||||
minimizeMain: 'window:minimize',
|
minimizeMain: 'window:minimize',
|
||||||
|
toggleMaximizeMain: 'window:toggleMaximize',
|
||||||
|
isMaximizedMain: 'window:isMaximized',
|
||||||
closeMain: 'window:close',
|
closeMain: 'window:close',
|
||||||
hideMain: 'window:hide',
|
hideMain: 'window:hide',
|
||||||
|
|
||||||
@@ -54,5 +56,6 @@ export const IPC = {
|
|||||||
evtThemeChanged: 'evt:themeChanged',
|
evtThemeChanged: 'evt:themeChanged',
|
||||||
evtAccentChanged: 'evt:accentChanged',
|
evtAccentChanged: 'evt:accentChanged',
|
||||||
evtGamesChanged: 'evt:gamesChanged',
|
evtGamesChanged: 'evt:gamesChanged',
|
||||||
evtUpdaterStatus: 'evt:updaterStatus'
|
evtUpdaterStatus: 'evt:updaterStatus',
|
||||||
|
evtMaximizeChanged: 'evt:maximizeChanged'
|
||||||
} as const
|
} as const
|
||||||
|
|||||||
@@ -58,6 +58,38 @@ describe('isQuietAt', () => {
|
|||||||
expect(isQuietAt(qh, at('2026-05-18T13:30:00'))).toBe(true)
|
expect(isQuietAt(qh, at('2026-05-18T13:30:00'))).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('wrap-around + day filter: window day matters, not current day', () => {
|
||||||
|
// from=22:00, to=07:00, days=[Mon..Fri].
|
||||||
|
const qh: QuietHours = {
|
||||||
|
enabled: true,
|
||||||
|
from: '22:00',
|
||||||
|
to: '07:00',
|
||||||
|
days: [1, 2, 3, 4, 5]
|
||||||
|
}
|
||||||
|
// Friday 23:00 -> window starts today, Friday in filter -> quiet
|
||||||
|
expect(isQuietAt(qh, at('2026-05-15T23:00:00'))).toBe(true)
|
||||||
|
// Saturday 02:00 -> window started Friday 22:00, Friday in filter -> quiet
|
||||||
|
expect(isQuietAt(qh, at('2026-05-16T02:00:00'))).toBe(true)
|
||||||
|
// Saturday 23:00 -> would start today, Saturday not in filter -> noisy
|
||||||
|
expect(isQuietAt(qh, at('2026-05-16T23:00:00'))).toBe(false)
|
||||||
|
// Sunday 02:00 -> started Saturday, Saturday not in filter -> noisy
|
||||||
|
expect(isQuietAt(qh, at('2026-05-17T02:00:00'))).toBe(false)
|
||||||
|
// Monday 01:00 -> would have started Sunday, Sunday not in filter -> noisy
|
||||||
|
expect(isQuietAt(qh, at('2026-05-18T01:00:00'))).toBe(false)
|
||||||
|
// Monday 23:00 -> starts today, Monday in filter -> quiet
|
||||||
|
expect(isQuietAt(qh, at('2026-05-18T23:00:00'))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('malformed from/to falls back to non-quiet', () => {
|
||||||
|
const qh: QuietHours = {
|
||||||
|
enabled: true,
|
||||||
|
from: 'bogus',
|
||||||
|
to: '08:00',
|
||||||
|
days: ALL_DAYS
|
||||||
|
}
|
||||||
|
expect(isQuietAt(qh, at('2026-05-17T05:00:00'))).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
it('zero-length window (from === to) is never quiet', () => {
|
it('zero-length window (from === to) is never quiet', () => {
|
||||||
const qh: QuietHours = {
|
const qh: QuietHours = {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
|||||||
@@ -30,7 +30,11 @@ describe('SAMPLE_EXERCISES', () => {
|
|||||||
expect(ex.icon.length, `icon set for ${ex.name}`).toBeGreaterThan(0)
|
expect(ex.icon.length, `icon set for ${ex.name}`).toBeGreaterThan(0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
// NB: тест «sample icons ⊆ ICON_CHOICES» лежит в
|
||||||
|
// src/renderer/src/lib/icon-choices.test.ts — он тянет renderer-сторону
|
||||||
|
// (ICON_CHOICES), а node-tsconfig сюда не пускает renderer-импорты.
|
||||||
|
|
||||||
describe('STAT_LABELS', () => {
|
describe('STAT_LABELS', () => {
|
||||||
it('has a Russian label for every GameStat in every GAME_STATS bundle', () => {
|
it('has a Russian label for every GameStat in every GAME_STATS bundle', () => {
|
||||||
|
|||||||
@@ -176,40 +176,88 @@ 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 {
|
||||||
|
const m = HHMM_RE.exec(s)
|
||||||
|
if (!m) return null
|
||||||
|
const h = Number(m[1])
|
||||||
|
const min = Number(m[2])
|
||||||
|
if (!Number.isFinite(h) || !Number.isFinite(min)) return null
|
||||||
|
if (h < 0 || h > 23 || min < 0 || min > 59) return null
|
||||||
|
return h * 60 + min
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if `now` falls inside the quiet window. Handles wrap-around
|
* Returns true if `now` falls inside the quiet window. Handles wrap-around
|
||||||
* windows (e.g. 22:00 → 08:00). Exposed from shared so both main scheduler
|
* windows (e.g. 22:00 → 08:00) AND day-of-week filtering correctly: when the
|
||||||
* and renderer settings UI can use the same logic.
|
* window started the previous day (we're in the AM half of a wrap-around),
|
||||||
|
* the day filter is evaluated against the START day, not the current day.
|
||||||
|
*
|
||||||
|
* Example: from=22:00, to=07:00, days=[Mon..Fri]. At Sat 02:00 the window
|
||||||
|
* is active (started Fri 22:00 — Friday is in the filter). At Mon 01:00 the
|
||||||
|
* window is NOT active (would have started Sun 22:00 — Sunday is excluded).
|
||||||
|
*
|
||||||
|
* Malformed `from`/`to` strings (after a corrupt state file) return false.
|
||||||
*/
|
*/
|
||||||
export function isQuietAt(qh: QuietHours, now: Date): boolean {
|
export function isQuietAt(qh: QuietHours, now: Date): boolean {
|
||||||
if (!qh.enabled) return false
|
if (!qh.enabled) return false
|
||||||
const dow = now.getDay() // 0..6
|
const fromMin = parseHHMM(qh.from)
|
||||||
if (qh.days.length > 0 && !qh.days.includes(dow)) return false
|
const toMin = parseHHMM(qh.to)
|
||||||
const [fh, fm] = qh.from.split(':').map(Number)
|
if (fromMin === null || toMin === null) return false
|
||||||
const [th, tm] = qh.to.split(':').map(Number)
|
|
||||||
const cur = now.getHours() * 60 + now.getMinutes()
|
|
||||||
const fromMin = fh * 60 + fm
|
|
||||||
const toMin = th * 60 + tm
|
|
||||||
if (fromMin === toMin) return false
|
if (fromMin === toMin) return false
|
||||||
|
|
||||||
|
const cur = now.getHours() * 60 + now.getMinutes()
|
||||||
|
const todayDow = now.getDay() // 0..6, 0=Sunday
|
||||||
|
const yesterdayDow = (todayDow + 6) % 7
|
||||||
|
|
||||||
|
// Helper: is this day included by the filter?
|
||||||
|
const dayActive = (dow: number): boolean =>
|
||||||
|
qh.days.length === 0 || qh.days.includes(dow)
|
||||||
|
|
||||||
if (fromMin < toMin) {
|
if (fromMin < toMin) {
|
||||||
// Same-day window.
|
// Same-day window — start day is `todayDow`.
|
||||||
|
if (!dayActive(todayDow)) return false
|
||||||
return cur >= fromMin && cur < toMin
|
return cur >= fromMin && cur < toMin
|
||||||
}
|
}
|
||||||
// Wraps midnight: active if after `from` today OR before `to` today.
|
// Wrap-around window. Either:
|
||||||
return cur >= fromMin || cur < toMin
|
// - cur >= fromMin: window started TODAY at fromMin → check todayDow
|
||||||
|
// - cur < toMin: window started YESTERDAY at fromMin → check yesterdayDow
|
||||||
|
if (cur >= fromMin) return dayActive(todayDow)
|
||||||
|
if (cur < toMin) return dayActive(yesterdayDow)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SAMPLE_EXERCISES: Omit<Exercise, 'id' | 'nextFireAt'>[] = [
|
export const SAMPLE_EXERCISES: Omit<Exercise, 'id' | 'nextFireAt'>[] = [
|
||||||
{ name: 'Приседания', reps: 10, icon: 'Activity', intervalMinutes: 30, enabled: true },
|
{
|
||||||
{ name: 'Отжимания', reps: 10, icon: 'Dumbbell', intervalMinutes: 45, enabled: true },
|
name: 'Приседания',
|
||||||
{ name: 'Растяжка спины', reps: 1, icon: 'StretchHorizontal', intervalMinutes: 60, enabled: false }
|
reps: 10,
|
||||||
|
icon: 'Activity',
|
||||||
|
intervalMinutes: 30,
|
||||||
|
enabled: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Отжимания',
|
||||||
|
reps: 10,
|
||||||
|
icon: 'Dumbbell',
|
||||||
|
intervalMinutes: 45,
|
||||||
|
enabled: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Растяжка спины',
|
||||||
|
reps: 1,
|
||||||
|
icon: 'StretchHorizontal',
|
||||||
|
intervalMinutes: 60,
|
||||||
|
enabled: false
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
export type UpdaterStatus =
|
export type UpdaterStatus =
|
||||||
| { kind: 'idle' }
|
| { kind: 'idle'; lastCheckedAt?: number }
|
||||||
| { kind: 'unsupported'; reason: string }
|
| { kind: 'unsupported'; reason: string }
|
||||||
| { kind: 'checking' }
|
| { kind: 'checking' }
|
||||||
| { kind: 'not-available'; currentVersion: string }
|
| { kind: 'not-available'; currentVersion: string; lastCheckedAt?: number }
|
||||||
| { kind: 'available'; version: string; releaseDate?: string }
|
| { kind: 'available'; version: string; releaseDate?: string }
|
||||||
| {
|
| {
|
||||||
kind: 'downloading'
|
kind: 'downloading'
|
||||||
@@ -220,4 +268,3 @@ export type UpdaterStatus =
|
|||||||
}
|
}
|
||||||
| { kind: 'downloaded'; version: string }
|
| { kind: 'downloaded'; version: string }
|
||||||
| { kind: 'error'; message: string }
|
| { kind: 'error'; message: string }
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"types": ["electron-vite/node"],
|
"types": ["electron-vite/node", "vite/client"],
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@shared/*": ["src/shared/*"]
|
"@shared/*": ["src/shared/*"]
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"useDefineForClassFields": true,
|
"useDefineForClassFields": true,
|
||||||
|
"types": ["vite/client"],
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@renderer/*": ["src/renderer/src/*"],
|
"@renderer/*": ["src/renderer/src/*"],
|
||||||
|
|||||||
Reference in New Issue
Block a user