Files
ForgeBase/Max Avatar/src/main/coach/exerciseWindow.ts
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +01:00

107 lines
3.7 KiB
TypeScript

import { randomUUID } from 'crypto'
import { BrowserWindow, ipcMain, type IpcMainEvent } from 'electron'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'
import type { ExerciseFormAnswer, ExerciseFormItem } from '../../shared/coach'
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from '../utils/windowSecurity'
let win: BrowserWindow | null = null
// Delai avant fermeture automatique de la fenetre une fois le formulaire soumis (demande
// utilisateur explicite : la fenetre ne doit pas rester ouverte indefiniment apres soumission).
const AUTO_CLOSE_DELAY_MS = 2000
// Fenetre normale et independante de l'avatar et de la courbe d'apprentissage, meme pattern
// singleton que progressWindow.ts.
function ensureWindow(): BrowserWindow {
if (win && !win.isDestroyed()) return win
win = new BrowserWindow({
width: 520,
height: 480,
title: 'Max — exercice',
alwaysOnTop: true,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
...SECURE_WEB_PREFERENCES
}
})
blockExternalNavigation(win)
// Niveau en dessous de l'avatar ('screen-saver') mais au-dessus de tout le reste, y compris une
// appli en plein ecran : sans ca, la fenetre pouvait s'ouvrir derriere le premier plan actuel et
// l'utilisateur devait la retrouver via la barre des taches (constate en test reel).
win.setAlwaysOnTop(true, 'floating')
win.maximize()
win.on('closed', () => {
win = null
})
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
void win.loadURL(`${process.env['ELECTRON_RENDERER_URL']}#coach-exercise`)
} else {
void win.loadFile(join(__dirname, '../renderer/index.html'), { hash: 'coach-exercise' })
}
return win
}
export interface QuestionInput {
question: string
type: ExerciseFormItem['type']
options: string[] | null
}
/**
* Ouvre la fenetre d'exercice, pose TOUTES les questions en une seule fois (formulaire groupe, pas
* une par une : demande utilisateur explicite) et attend la soumission de l'utilisateur. Renvoie un
* tableau de reponses (meme ordre que `questions`), `null` par entree si la fenetre est fermee sans
* soumission ou si le delai est depasse. La fenetre se ferme automatiquement 2 secondes apres
* soumission.
*/
export function requestWrittenAnswers(
questions: QuestionInput[],
timeoutMs = 30 * 60 * 1000
): Promise<(string | null)[]> {
return new Promise((resolve) => {
const window = ensureWindow()
window.show()
window.moveTop()
window.focus()
const requestId = randomUUID()
const items: ExerciseFormItem[] = questions.map((q, index) => ({
id: String(index),
question: q.question,
type: q.type,
options: q.options
}))
let settled = false
const finish = (answers: (string | null)[]): void => {
if (settled) return
settled = true
clearTimeout(timeout)
ipcMain.removeListener('written-form-result', handler)
resolve(answers)
}
const timeout = setTimeout(() => finish(items.map(() => null)), timeoutMs)
function handler(_event: IpcMainEvent, id: string, answers: ExerciseFormAnswer[]): void {
if (id !== requestId) return
const byId = new Map(answers.map((a) => [a.id, a.answer]))
finish(items.map((item) => byId.get(item.id) ?? null))
setTimeout(() => {
if (win && !win.isDestroyed()) win.close()
}, AUTO_CLOSE_DELAY_MS)
}
ipcMain.on('written-form-result', handler)
window.once('closed', () => finish(items.map(() => null)))
const send = (): void => window.webContents.send('ask-written-form', { requestId, items })
if (window.webContents.isLoading()) {
window.webContents.once('did-finish-load', send)
} else {
send()
}
})
}