Skip to content

A full-featured download manager.

fix(plugins): preserve consent during dialog exit (#2335)

Dr_rOotb602aa655c1d
diff --git a/src/renderer/routes/plugins/components/registry-install-action.test.tsx b/src/renderer/routes/plugins/components/registry-install-action.test.tsx
index 0a8a66f1..45125fee 100644
--- a/src/renderer/routes/plugins/components/registry-install-action.test.tsx
+++ b/src/renderer/routes/plugins/components/registry-install-action.test.tsx
@@ -195,6 +195,43 @@ describe('RegistryInstallAction', () => {
})
})
+ it('retains consent through dismissal and resets grants on reopening', async () => {
+ const exit = deferred<Animation>()
+ const animation = { finished: exit.promise } as Animation
+ const animations = vi
+ .spyOn(Element.prototype, 'getAnimations')
+ .mockImplementation(function (this: Element) {
+ return this.getAttribute('data-slot') === 'dialog-content' &&
+ this.hasAttribute('data-closed')
+ ? [animation]
+ : []
+ })
+ try {
+ render(<RegistryInstallAction pluginId="acme.smart-link" />)
+ start()
+ const popup = await screen.findByRole('dialog')
+ const toggle = screen.getByRole('switch', {
+ name: 'Send notifications',
+ })
+ fireEvent.click(toggle)
+ fireEvent.click(screen.getByRole('button', { name: 'Close' }))
+
+ await waitFor(() => expect(popup).toHaveAttribute('data-closed'))
+ expect(popup).toBeInTheDocument()
+ expect(popup).toHaveTextContent('Install Smart Link 1.0.0?')
+ expect(toggle).toHaveAttribute('aria-checked', 'true')
+
+ await act(async () => exit.resolve(animation))
+ await waitFor(() => expect(popup).not.toBeInTheDocument())
+ start()
+ expect(
+ await screen.findByRole('switch', { name: 'Send notifications' })
+ ).toHaveAttribute('aria-checked', 'false')
+ } finally {
+ animations.mockRestore()
+ }
+ })
+
it('locks confirmation and grants until the commit completes', async () => {
const commit = deferred<undefined>()
render(<RegistryInstallAction pluginId="acme.smart-link" />)
diff --git a/src/renderer/routes/plugins/hooks/use-plugin-install.ts b/src/renderer/routes/plugins/hooks/use-plugin-install.ts
index 1507d01b..fd521076 100644
--- a/src/renderer/routes/plugins/hooks/use-plugin-install.ts
+++ b/src/renderer/routes/plugins/hooks/use-plugin-install.ts
@@ -155,7 +155,6 @@ export function usePluginInstall() {
stagingRef.current = null
if (mountedRef.current) {
setStagingId(null)
- setConsent(null)
}
return true
} catch (e) {
@@ -196,8 +195,6 @@ export function usePluginInstall() {
stagingRef.current = null
if (mountedRef.current) {
setStagingId(null)
- setConsent(null)
- setError(null)
}
return true
} catch (e) {
@@ -211,5 +208,21 @@ export function usePluginInstall() {
}
}, [showError])
- return { stagingId, consent, pending, error, startInstall, confirm, cancel }
+ // Keep the reviewed content intact while its dialog animates out. Starting
+ // another install also resets it, independently of animation completion.
+ const resetPresentation = useCallback(() => {
+ setConsent(null)
+ setError(null)
+ }, [])
+
+ return {
+ stagingId,
+ consent,
+ pending,
+ error,
+ startInstall,
+ confirm,
+ cancel,
+ resetPresentation,
+ }
}
diff --git a/src/renderer/routes/plugins/plugin-install-dialog.test.tsx b/src/renderer/routes/plugins/plugin-install-dialog.test.tsx
index dccb120d..ede107f6 100644
--- a/src/renderer/routes/plugins/plugin-install-dialog.test.tsx
+++ b/src/renderer/routes/plugins/plugin-install-dialog.test.tsx
@@ -16,6 +16,7 @@ interface MockInstallState {
startInstall: ReturnType<typeof vi.fn>
confirm: ReturnType<typeof vi.fn>
cancel: ReturnType<typeof vi.fn>
+ resetPresentation: ReturnType<typeof vi.fn>
}
const state: MockInstallState = {
@@ -26,6 +27,7 @@ const state: MockInstallState = {
startInstall: vi.fn(),
confirm: vi.fn(),
cancel: vi.fn(),
+ resetPresentation: vi.fn(),
}
vi.mock('./hooks/use-plugin-install', () => ({
@@ -65,6 +67,7 @@ beforeEach(() => {
state.startInstall = vi.fn()
state.confirm = vi.fn().mockResolvedValue(true)
state.cancel = vi.fn()
+ state.resetPresentation = vi.fn()
})
describe('PluginInstallDialog', () => {
diff --git a/src/renderer/routes/plugins/plugin-install-dialog.tsx b/src/renderer/routes/plugins/plugin-install-dialog.tsx
index 6a3e0f0e..43416777 100644
--- a/src/renderer/routes/plugins/plugin-install-dialog.tsx
+++ b/src/renderer/routes/plugins/plugin-install-dialog.tsx
@@ -119,7 +119,7 @@ function PluginInstallSession({
const callbacksRef = useRef({ open, onOpenChange, onInstalled })
callbacksRef.current = { open, onOpenChange, onInstalled }
const pluginId = fixedSource?.pluginId
- const { startInstall, cancel } = install
+ const { startInstall, cancel, resetPresentation } = install
useEffect(
() => () => {
startedRef.current = false
@@ -134,6 +134,10 @@ function PluginInstallSession({
wasOpenRef.current = false
return
}
+ if (!wasOpenRef.current) {
+ resetPresentation()
+ setGrants({})
+ }
wasOpenRef.current = true
if (!pluginId || startedRef.current) return
startedRef.current = true
@@ -146,10 +150,9 @@ function PluginInstallSession({
callbacksRef.current.onOpenChange(false)
}
)
- }, [open, pluginId, startInstall, cancel, clearUpdate])
+ }, [open, pluginId, startInstall, cancel, clearUpdate, resetPresentation])
function close() {
- setGrants({})
onOpenChange(false)
}
@@ -222,7 +225,16 @@ function PluginInstallSession({
cancel: () => void onCancel(),
retry: () => void retry(),
})}
- <Dialog open={dialogOpen} onOpenChange={(v) => !v && void onCancel()}>
+ <Dialog
+ open={dialogOpen}
+ onOpenChange={(v) => !v && void onCancel()}
+ onOpenChangeComplete={(next) => {
+ if (!next && !callbacksRef.current.open) {
+ resetPresentation()
+ setGrants({})
+ }
+ }}
+ >
<DialogContent
showCloseButton={!committing}
initialFocus={install.consent ? cancelButtonRef : undefined}