mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Moved native confirmation dialogs to custom component
This commit is contained in:
1 parent
c6aaded26d
commit
ce06731dc8
7 files changed
+101
-13
No files matched your search
@@ -681,6 +681,13 @@ frontend at `http://localhost:5173`.
|
|||||||
can show a mark twice). That file is a lookup of display ASSETS by platform
|
can show a mark twice). That file is a lookup of display ASSETS by platform
|
||||||
id, not platform branching; an unknown id falls back to a generic mark, so a
|
id, not platform branching; an unknown id falls back to a generic mark, so a
|
||||||
new adapter needs no change there to appear. Keep `api.ts` types aligned with `schemas.py`.
|
new adapter needs no change there to appear. Keep `api.ts` types aligned with `schemas.py`.
|
||||||
|
- **Never `window.confirm` / `alert` / `prompt`.** They cannot be themed, they
|
||||||
|
block the event loop (a playing deck stutters), and under Tauri they read as a
|
||||||
|
web page. Use `await askConfirm({ title, body, confirmLabel, tone? })` from
|
||||||
|
`lib/confirm.tsx` — same one-line shape as `confirm()`, rendered as the themed
|
||||||
|
`ConfirmDialog` by the `ConfirmHost` mounted once in `main.tsx`. `tone` is
|
||||||
|
`danger` (default, pink) or `primary`. Components that already hold dialog
|
||||||
|
state may still render `ConfirmDialog` directly.
|
||||||
|
|
||||||
## Theme — "dark liquid glass"
|
## Theme — "dark liquid glass"
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ export interface ConfirmRequest {
|
|||||||
body: ReactNode
|
body: ReactNode
|
||||||
/** The confirm button's label, e.g. "Remove 3 tracks". */
|
/** The confirm button's label, e.g. "Remove 3 tracks". */
|
||||||
confirmLabel: string
|
confirmLabel: string
|
||||||
|
/** `danger` (default) for destructive actions — a pink button; `primary`
|
||||||
|
* for a caution that is not destruction, e.g. switching library. */
|
||||||
|
tone?: 'danger' | 'primary'
|
||||||
onConfirm: () => Promise<void> | void
|
onConfirm: () => Promise<void> | void
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,7 +19,7 @@ interface Props extends ConfirmRequest {
|
|||||||
|
|
||||||
/** A yes/no confirmation for destructive actions. Enter confirms and Esc
|
/** A yes/no confirmation for destructive actions. Enter confirms and Esc
|
||||||
* cancels, so a confirmed Delete key stays a two-keystroke action. */
|
* cancels, so a confirmed Delete key stays a two-keystroke action. */
|
||||||
export function ConfirmDialog({ title, body, confirmLabel, onConfirm, onClose }: Props) {
|
export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', onConfirm, onClose }: Props) {
|
||||||
const [busy, setBusy] = useState(false)
|
const [busy, setBusy] = useState(false)
|
||||||
|
|
||||||
const confirm = async () => {
|
const confirm = async () => {
|
||||||
@@ -62,14 +65,18 @@ export function ConfirmDialog({ title, body, confirmLabel, onConfirm, onClose }:
|
|||||||
<button
|
<button
|
||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
disabled={busy}
|
disabled={busy}
|
||||||
className="rounded-md px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text disabled:opacity-40"
|
className="btn-glass rounded-full px-4 py-1.5 text-sm text-muted hover:text-text disabled:opacity-40"
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => void confirm()}
|
onClick={() => void confirm()}
|
||||||
disabled={busy}
|
disabled={busy}
|
||||||
className="rounded-full bg-pink/15 shadow-[inset_0_1px_0_rgb(255_255_255/0.12),inset_0_0_0_1px_rgb(255_122_154/0.55)] hover:bg-pink/25 px-4 py-1.5 text-sm font-semibold text-[#ffd0da] disabled:opacity-50"
|
className={`rounded-full px-4 py-1.5 text-sm font-semibold disabled:opacity-50 ${
|
||||||
|
tone === 'primary'
|
||||||
|
? 'btn-primary'
|
||||||
|
: 'bg-pink/15 text-[#ffd0da] shadow-[inset_0_1px_0_rgb(255_255_255/0.12),inset_0_0_0_1px_rgb(255_122_154/0.55)] hover:bg-pink/25'
|
||||||
|
}`}
|
||||||
>
|
>
|
||||||
{confirmLabel}
|
{confirmLabel}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { api, type ExportSet } from '../api'
|
|||||||
import { ExportDialog } from './ExportDialog'
|
import { ExportDialog } from './ExportDialog'
|
||||||
import { ExportRunDialog } from './ExportRunDialog'
|
import { ExportRunDialog } from './ExportRunDialog'
|
||||||
import type { Source } from './Sidebar'
|
import type { Source } from './Sidebar'
|
||||||
|
import { askConfirm } from '../lib/confirm'
|
||||||
import { Icon } from '../lib/icons'
|
import { Icon } from '../lib/icons'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,10 +105,16 @@ export function ExportsSection({ source, onSelect, onDone, onError }: Props) {
|
|||||||
onSelect={onSelect}
|
onSelect={onSelect}
|
||||||
onEdit={() => setDialog({ editing: set })}
|
onEdit={() => setDialog({ editing: set })}
|
||||||
onRun={() => setRunning(set)}
|
onRun={() => setRunning(set)}
|
||||||
onDelete={() => {
|
onDelete={async () => {
|
||||||
// Deleting a set never touches its destination folder — the files
|
// Deleting a set never touches its destination folder — the files
|
||||||
// already exported are the user's, not Konduktor's to clean up.
|
// already exported are the user's, not Konduktor's to clean up.
|
||||||
if (confirm(`Delete the export “${set.name}”? Nothing already exported is removed.`))
|
if (
|
||||||
|
await askConfirm({
|
||||||
|
title: `Delete the export “${set.name}”?`,
|
||||||
|
body: 'Nothing already exported is removed — the files in its folder stay where they are.',
|
||||||
|
confirmLabel: 'Delete export',
|
||||||
|
})
|
||||||
|
)
|
||||||
remove.mutate(set.id)
|
remove.mutate(set.id)
|
||||||
}}
|
}}
|
||||||
onError={onError}
|
onError={onError}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
|
import { askConfirm } from '../lib/confirm'
|
||||||
import { Icon } from '../lib/icons'
|
import { Icon } from '../lib/icons'
|
||||||
import { MIN_TAPS, RESET_MS, TapTempo } from '../lib/tapTempo'
|
import { MIN_TAPS, RESET_MS, TapTempo } from '../lib/tapTempo'
|
||||||
|
|
||||||
@@ -278,13 +279,15 @@ export function GridEditStrip({
|
|||||||
<span className="hidden @[34rem]:inline">Marker</span>
|
<span className="hidden @[34rem]:inline">Marker</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={async () => {
|
||||||
if (
|
if (
|
||||||
confirm(
|
await askConfirm({
|
||||||
flexible
|
title: "Delete this track's beatgrid?",
|
||||||
? `Delete this track's beatgrid? All ${markerCount} grid markers will be removed.`
|
body: flexible
|
||||||
: "Delete this track's beatgrid?",
|
? `All ${markerCount} grid markers will be removed.`
|
||||||
)
|
: 'The grid marker will be removed.',
|
||||||
|
confirmLabel: 'Delete grid',
|
||||||
|
})
|
||||||
) {
|
) {
|
||||||
onDeleteGrid()
|
onDeleteGrid()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { SettingsMenu } from './SettingsMenu'
|
|||||||
import { DevicesSection } from './DevicesSection'
|
import { DevicesSection } from './DevicesSection'
|
||||||
import { ExportsSection } from './ExportsSection'
|
import { ExportsSection } from './ExportsSection'
|
||||||
import { ConfirmDialog, type ConfirmRequest } from './ConfirmDialog'
|
import { ConfirmDialog, type ConfirmRequest } from './ConfirmDialog'
|
||||||
|
import { askConfirm } from '../lib/confirm'
|
||||||
import { ContextMenu, type MenuItem } from './ContextMenu'
|
import { ContextMenu, type MenuItem } from './ContextMenu'
|
||||||
import { Icon, type IconName } from '../lib/icons'
|
import { Icon, type IconName } from '../lib/icons'
|
||||||
import { PlatformIcon } from '../lib/platformIcons'
|
import { PlatformIcon } from '../lib/platformIcons'
|
||||||
@@ -325,10 +326,14 @@ export function Sidebar({
|
|||||||
// Switching library throws away every unsaved edit — the adapter holds them in
|
// Switching library throws away every unsaved edit — the adapter holds them in
|
||||||
// its native model, and opening another library replaces it. Worth a confirm:
|
// its native model, and opening another library replaces it. Worth a confirm:
|
||||||
// until now there was no way to switch at all, so this hazard is new.
|
// until now there was no way to switch at all, so this hazard is new.
|
||||||
const switchLibrary = () => {
|
const switchLibrary = async () => {
|
||||||
if (
|
if (
|
||||||
dirty &&
|
dirty &&
|
||||||
!confirm('You have unsaved changes. Opening a different library will discard them.')
|
!(await askConfirm({
|
||||||
|
title: 'Discard unsaved changes?',
|
||||||
|
body: 'You have unsaved changes. Opening a different library will discard them.',
|
||||||
|
confirmLabel: 'Discard and switch',
|
||||||
|
}))
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
onSwitchLibrary()
|
onSwitchLibrary()
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
// `askConfirm()` — the app's own replacement for `window.confirm()`.
|
||||||
|
//
|
||||||
|
// It is awaited exactly like the native one, so a call site stays a single
|
||||||
|
// line, but shows the themed `ConfirmDialog` instead of the browser's grey box.
|
||||||
|
// `ConfirmHost` (mounted once, in main.tsx) renders whichever request is open;
|
||||||
|
// a module-level handle is what lets plain event handlers — and components
|
||||||
|
// that have no dialog state of their own — ask without threading state around.
|
||||||
|
//
|
||||||
|
// Never call `window.confirm` / `alert` / `prompt` in this app: they cannot be
|
||||||
|
// themed, they block the event loop (a playing deck stutters), and under Tauri
|
||||||
|
// they read as a web page rather than the app.
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { ConfirmDialog, type ConfirmRequest } from '../components/ConfirmDialog'
|
||||||
|
|
||||||
|
type Ask = Omit<ConfirmRequest, 'onConfirm'>
|
||||||
|
type Pending = { req: Ask; resolve: (ok: boolean) => void }
|
||||||
|
|
||||||
|
let open: ((p: Pending) => void) | null = null
|
||||||
|
|
||||||
|
/** Show a confirmation and resolve true (confirmed) or false (cancelled). */
|
||||||
|
export function askConfirm(req: Ask): Promise<boolean> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
if (open) open({ req, resolve })
|
||||||
|
// No host mounted (should not happen outside tests): refuse rather than
|
||||||
|
// fall back to a native dialog, since a destructive action must not go
|
||||||
|
// ahead unconfirmed.
|
||||||
|
else resolve(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Renders the open confirmation, if any. Mount exactly once. */
|
||||||
|
export function ConfirmHost() {
|
||||||
|
const [pending, setPending] = useState<Pending | null>(null)
|
||||||
|
useEffect(() => {
|
||||||
|
open = (p) =>
|
||||||
|
setPending((prev) => {
|
||||||
|
prev?.resolve(false) // a second ask supersedes an unanswered one
|
||||||
|
return p
|
||||||
|
})
|
||||||
|
return () => {
|
||||||
|
open = null
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
if (!pending) return null
|
||||||
|
return (
|
||||||
|
<ConfirmDialog
|
||||||
|
{...pending.req}
|
||||||
|
// A promise settles once, so confirm-then-close resolves `true`.
|
||||||
|
onConfirm={() => pending.resolve(true)}
|
||||||
|
onClose={() => {
|
||||||
|
pending.resolve(false)
|
||||||
|
setPending(null)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import './index.css'
|
|||||||
import App from './App.tsx'
|
import App from './App.tsx'
|
||||||
import { UpdateCheck } from './components/UpdateDialog.tsx'
|
import { UpdateCheck } from './components/UpdateDialog.tsx'
|
||||||
import { AmbientBackdrop } from './components/AmbientBackdrop.tsx'
|
import { AmbientBackdrop } from './components/AmbientBackdrop.tsx'
|
||||||
|
import { ConfirmHost } from './lib/confirm.tsx'
|
||||||
|
|
||||||
const queryClient = new QueryClient({
|
const queryClient = new QueryClient({
|
||||||
defaultOptions: {
|
defaultOptions: {
|
||||||
@@ -25,6 +26,8 @@ createRoot(document.getElementById('root')!).render(
|
|||||||
<App />
|
<App />
|
||||||
{/* Mounted beside App so the update dialog shows even on the picker screen. */}
|
{/* Mounted beside App so the update dialog shows even on the picker screen. */}
|
||||||
<UpdateCheck />
|
<UpdateCheck />
|
||||||
|
{/* askConfirm()'s dialogs — the app's replacement for window.confirm. */}
|
||||||
|
<ConfirmHost />
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</StrictMode>,
|
</StrictMode>,
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user