-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Register Update dialog to UpdatesDashboard #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33e7ab7
feat: generic RegisterUpdateDialog for SOVD POST /updates
bburda ba35e12
feat: add Register Update button to UpdatesDashboard
bburda e495d64
fix: reset RegisterUpdateDialog state on close and block metadata sha…
bburda 8a89040
fix: cancel registerUpdate on unmount and block dialog close while su…
bburda e82bf27
fix: review round 3 - a11y, consistency, UX on register flow
bburda e7b4145
fix: dialog polish and make progress bars reach 100% on completion
bburda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { RegisterUpdateDialog } from './RegisterUpdateDialog'; | ||
|
|
||
| describe('RegisterUpdateDialog', () => { | ||
| it('renders the four fields when open', () => { | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={async () => {}} />); | ||
| expect(screen.getByLabelText(/^id$/i)).toBeInTheDocument(); | ||
| expect(screen.getByLabelText(/^name$/i)).toBeInTheDocument(); | ||
| expect(screen.getByLabelText(/^automated$/i)).toBeInTheDocument(); | ||
| expect(screen.getByLabelText(/additional metadata/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('rejects empty id', async () => { | ||
| const onSubmit = vi.fn(); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| expect(await screen.findByText(/id is required/i)).toBeInTheDocument(); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects malformed JSON metadata', async () => { | ||
| const onSubmit = vi.fn(); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'x' } }); | ||
| fireEvent.change(screen.getByLabelText(/additional metadata/i), { target: { value: '{not json' } }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| expect(await screen.findByText(/invalid json/i)).toBeInTheDocument(); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('strips reserved keys from metadata so UI-validated fields win', async () => { | ||
| const onSubmit = vi.fn().mockResolvedValue(undefined); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'ui-id' } }); | ||
| fireEvent.change(screen.getByLabelText(/additional metadata/i), { | ||
| target: { value: '{"id":"evil","update_name":"evil","automated":true,"extra":1}' }, | ||
| }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| await waitFor(() => | ||
| expect(onSubmit).toHaveBeenCalledWith({ | ||
| id: 'ui-id', | ||
| update_name: 'ui-id', | ||
| automated: false, | ||
| extra: 1, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['string', '"just a string"'], | ||
| ['array', '[1, 2, 3]'], | ||
| ['null', 'null'], | ||
| ['number', '42'], | ||
| ])('rejects non-object JSON metadata (%s)', async (_label, raw) => { | ||
| const onSubmit = vi.fn(); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'x' } }); | ||
| fireEvent.change(screen.getByLabelText(/additional metadata/i), { target: { value: raw } }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| expect(await screen.findByText(/invalid json/i)).toBeInTheDocument(); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('surfaces onSubmit rejection as inline error and keeps dialog open', async () => { | ||
| const onSubmit = vi.fn().mockRejectedValue(new Error('backend exploded')); | ||
| const onClose = vi.fn(); | ||
| render(<RegisterUpdateDialog open onClose={onClose} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'x' } }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| expect(await screen.findByRole('alert')).toHaveTextContent(/backend exploded/i); | ||
| expect(onClose).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('submits merged body on valid input', async () => { | ||
| const onSubmit = vi.fn().mockResolvedValue(undefined); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'pkg-1' } }); | ||
| fireEvent.change(screen.getByLabelText(/^name$/i), { target: { value: 'Pkg One' } }); | ||
| fireEvent.click(screen.getByLabelText(/^automated$/i)); | ||
| fireEvent.change(screen.getByLabelText(/additional metadata/i), { | ||
| target: { value: '{"origins":["a"]}' }, | ||
| }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| await waitFor(() => | ||
| expect(onSubmit).toHaveBeenCalledWith({ | ||
| id: 'pkg-1', | ||
| update_name: 'Pkg One', | ||
| automated: true, | ||
| origins: ['a'], | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('always includes automated=false when unchecked', async () => { | ||
| const onSubmit = vi.fn().mockResolvedValue(undefined); | ||
| render(<RegisterUpdateDialog open onClose={() => {}} onSubmit={onSubmit} />); | ||
| fireEvent.change(screen.getByLabelText(/^id$/i), { target: { value: 'plain' } }); | ||
| fireEvent.click(screen.getByRole('button', { name: /^register$/i })); | ||
| await waitFor(() => | ||
| expect(onSubmit).toHaveBeenCalledWith({ | ||
| id: 'plain', | ||
| update_name: 'plain', | ||
| automated: false, | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // Copyright 2026 bburda | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import { useEffect, useState } from 'react'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Input } from '@/components/ui/input'; | ||
| import { Label } from '@/components/ui/label'; | ||
| import { Textarea } from '@/components/ui/textarea'; | ||
| import { Checkbox } from '@/components/ui/checkbox'; | ||
| import { Loader2 } from 'lucide-react'; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from '@/components/ui/dialog'; | ||
|
|
||
| export interface RegisterUpdateBody { | ||
| id: string; | ||
| update_name?: string; | ||
| automated?: boolean; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| interface Props { | ||
| open: boolean; | ||
| onClose: () => void; | ||
| onSubmit: (body: RegisterUpdateBody) => Promise<void>; | ||
| } | ||
|
|
||
| export function RegisterUpdateDialog({ open, onClose, onSubmit }: Props) { | ||
| const [id, setId] = useState(''); | ||
| const [name, setName] = useState(''); | ||
| const [automated, setAutomated] = useState(false); | ||
| const [metadata, setMetadata] = useState('{}'); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [submitting, setSubmitting] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!open) { | ||
| setId(''); | ||
| setName(''); | ||
| setAutomated(false); | ||
| setMetadata('{}'); | ||
| setError(null); | ||
| setSubmitting(false); | ||
| } | ||
| }, [open]); | ||
|
|
||
| const handleSubmit = async () => { | ||
| setError(null); | ||
| if (!id.trim()) { | ||
| setError('id is required'); | ||
| return; | ||
| } | ||
| let extras: Record<string, unknown> = {}; | ||
| if (metadata.trim()) { | ||
| try { | ||
| const parsed = JSON.parse(metadata); | ||
| if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) { | ||
| throw new Error('not an object'); | ||
| } | ||
| const { id: _i, update_name: _n, automated: _a, ...safe } = parsed as Record<string, unknown>; | ||
| void _i; | ||
| void _n; | ||
| void _a; | ||
| extras = safe; | ||
| } catch { | ||
| setError('invalid JSON in additional metadata'); | ||
| return; | ||
| } | ||
| } | ||
| const body: RegisterUpdateBody = { | ||
| ...extras, | ||
| id: id.trim(), | ||
| update_name: name.trim() || id.trim(), | ||
| automated, | ||
| }; | ||
| setSubmitting(true); | ||
| try { | ||
| await onSubmit(body); | ||
| onClose(); | ||
| } catch (e) { | ||
| setError(e instanceof Error ? e.message : String(e)); | ||
| } finally { | ||
| setSubmitting(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog | ||
| open={open} | ||
| onOpenChange={(o) => { | ||
| if (o || submitting) return; | ||
| onClose(); | ||
| }} | ||
| > | ||
| <DialogContent | ||
| onEscapeKeyDown={(e) => submitting && e.preventDefault()} | ||
| onPointerDownOutside={(e) => submitting && e.preventDefault()} | ||
| onInteractOutside={(e) => submitting && e.preventDefault()} | ||
| > | ||
| <DialogHeader> | ||
| <DialogTitle>Register Update</DialogTitle> | ||
| <DialogDescription> | ||
| Register a new update package with the gateway. Vendor-specific fields (origins, signatures, | ||
| etc.) go into the metadata JSON. | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <div className="space-y-3"> | ||
| <div> | ||
| <Label htmlFor="reg-id">id</Label> | ||
| <Input | ||
| id="reg-id" | ||
| value={id} | ||
| onChange={(e) => setId(e.target.value)} | ||
| aria-invalid={!!error} | ||
| aria-describedby={error ? 'reg-error' : undefined} | ||
| /> | ||
| </div> | ||
| <div> | ||
| <Label htmlFor="reg-name">name</Label> | ||
| <Input id="reg-name" value={name} onChange={(e) => setName(e.target.value)} /> | ||
| </div> | ||
| <div className="flex items-center gap-2"> | ||
| <Checkbox id="reg-auto" checked={automated} onCheckedChange={(v) => setAutomated(v === true)} /> | ||
| <Label htmlFor="reg-auto">automated</Label> | ||
| </div> | ||
| <div> | ||
| <Label htmlFor="reg-meta">additional metadata (JSON)</Label> | ||
| <Textarea | ||
| id="reg-meta" | ||
| rows={6} | ||
| value={metadata} | ||
| onChange={(e) => setMetadata(e.target.value)} | ||
| aria-invalid={!!error} | ||
| aria-describedby={error ? 'reg-error' : undefined} | ||
| /> | ||
| </div> | ||
| {error && ( | ||
| <p id="reg-error" role="alert" className="text-sm text-destructive"> | ||
| {error} | ||
| </p> | ||
| )} | ||
| </div> | ||
| <DialogFooter> | ||
| <Button variant="outline" onClick={onClose} disabled={submitting}> | ||
| Cancel | ||
| </Button> | ||
| <Button onClick={handleSubmit} disabled={submitting}> | ||
| {submitting ? ( | ||
| <> | ||
| <Loader2 className="w-4 h-4 mr-2 animate-spin" /> | ||
| Registering... | ||
| </> | ||
| ) : ( | ||
| 'Register' | ||
| )} | ||
| </Button> | ||
|
bburda marked this conversation as resolved.
|
||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.