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
@@ -7,6 +7,9 @@ export interface ConfirmRequest {
|
||||
body: ReactNode
|
||||
/** The confirm button's label, e.g. "Remove 3 tracks". */
|
||||
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
|
||||
}
|
||||
|
||||
@@ -16,7 +19,7 @@ interface Props extends ConfirmRequest {
|
||||
|
||||
/** A yes/no confirmation for destructive actions. Enter confirms and Esc
|
||||
* 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 confirm = async () => {
|
||||
@@ -62,14 +65,18 @@ export function ConfirmDialog({ title, body, confirmLabel, onConfirm, onClose }:
|
||||
<button
|
||||
onClick={onClose}
|
||||
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
|
||||
</button>
|
||||
<button
|
||||
onClick={() => void confirm()}
|
||||
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}
|
||||
</button>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { api, type ExportSet } from '../api'
|
||||
import { ExportDialog } from './ExportDialog'
|
||||
import { ExportRunDialog } from './ExportRunDialog'
|
||||
import type { Source } from './Sidebar'
|
||||
import { askConfirm } from '../lib/confirm'
|
||||
import { Icon } from '../lib/icons'
|
||||
|
||||
/**
|
||||
@@ -104,10 +105,16 @@ export function ExportsSection({ source, onSelect, onDone, onError }: Props) {
|
||||
onSelect={onSelect}
|
||||
onEdit={() => setDialog({ editing: set })}
|
||||
onRun={() => setRunning(set)}
|
||||
onDelete={() => {
|
||||
onDelete={async () => {
|
||||
// Deleting a set never touches its destination folder — the files
|
||||
// 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)
|
||||
}}
|
||||
onError={onError}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { askConfirm } from '../lib/confirm'
|
||||
import { Icon } from '../lib/icons'
|
||||
import { MIN_TAPS, RESET_MS, TapTempo } from '../lib/tapTempo'
|
||||
|
||||
@@ -278,13 +279,15 @@ export function GridEditStrip({
|
||||
<span className="hidden @[34rem]:inline">Marker</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
onClick={async () => {
|
||||
if (
|
||||
confirm(
|
||||
flexible
|
||||
? `Delete this track's beatgrid? All ${markerCount} grid markers will be removed.`
|
||||
: "Delete this track's beatgrid?",
|
||||
)
|
||||
await askConfirm({
|
||||
title: "Delete this track's beatgrid?",
|
||||
body: flexible
|
||||
? `All ${markerCount} grid markers will be removed.`
|
||||
: 'The grid marker will be removed.',
|
||||
confirmLabel: 'Delete grid',
|
||||
})
|
||||
) {
|
||||
onDeleteGrid()
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { SettingsMenu } from './SettingsMenu'
|
||||
import { DevicesSection } from './DevicesSection'
|
||||
import { ExportsSection } from './ExportsSection'
|
||||
import { ConfirmDialog, type ConfirmRequest } from './ConfirmDialog'
|
||||
import { askConfirm } from '../lib/confirm'
|
||||
import { ContextMenu, type MenuItem } from './ContextMenu'
|
||||
import { Icon, type IconName } from '../lib/icons'
|
||||
import { PlatformIcon } from '../lib/platformIcons'
|
||||
@@ -325,10 +326,14 @@ export function Sidebar({
|
||||
// Switching library throws away every unsaved edit — the adapter holds them in
|
||||
// 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.
|
||||
const switchLibrary = () => {
|
||||
const switchLibrary = async () => {
|
||||
if (
|
||||
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
|
||||
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 { UpdateCheck } from './components/UpdateDialog.tsx'
|
||||
import { AmbientBackdrop } from './components/AmbientBackdrop.tsx'
|
||||
import { ConfirmHost } from './lib/confirm.tsx'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
@@ -25,6 +26,8 @@ createRoot(document.getElementById('root')!).render(
|
||||
<App />
|
||||
{/* Mounted beside App so the update dialog shows even on the picker screen. */}
|
||||
<UpdateCheck />
|
||||
{/* askConfirm()'s dialogs — the app's replacement for window.confirm. */}
|
||||
<ConfirmHost />
|
||||
</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
)
|
||||
Reference in new issue
Block a user