Repository navigation
Closed #119 #123
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
Closed
Closed
Closed #119 #123
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,184 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useRef, useState } from "react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { CheckCircle2, Trash2, Undo2 } from "lucide-react"; | ||
| import type { SecretFieldState } from "@/lib/notifications/channel-form"; | ||
|
|
||
| interface SecretInputProps { | ||
| id: string; | ||
| label: string; | ||
| placeholder?: string; | ||
| helpText?: string; | ||
| required?: boolean; | ||
| /** Show a control that clears the stored secret on save. */ | ||
| allowRemove?: boolean; | ||
| state: SecretFieldState; | ||
| onChange: (next: SecretFieldState) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Input for a value the API never returns. When a secret is already stored the | ||
| * field renders empty with a "Configured" indicator; typing a value rotates it | ||
| * and the remove control clears it. | ||
| */ | ||
| export function SecretInput({ | ||
| id, | ||
| label, | ||
| placeholder, | ||
| helpText, | ||
| required, | ||
| allowRemove, | ||
| state, | ||
| onChange, | ||
| }: SecretInputProps) { | ||
| const [isReplacing, setIsReplacing] = useState(false); | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| // Move focus into the field the user just revealed so keyboard users land | ||
| // where they can type the new value. | ||
| useEffect(() => { | ||
| if (isReplacing) { | ||
| inputRef.current?.focus(); | ||
| } | ||
| }, [isReplacing]); | ||
|
|
||
| const statusId = `${id}-status`; | ||
| const helpId = helpText ? `${id}-help` : undefined; | ||
| const describedBy = [statusId, helpId].filter(Boolean).join(" "); | ||
|
|
||
| const showInput = !state.configured || isReplacing; | ||
|
|
||
| const startReplacing = () => { | ||
| setIsReplacing(true); | ||
| onChange({ ...state, markedForRemoval: false }); | ||
| }; | ||
|
|
||
| const cancelReplacing = () => { | ||
| setIsReplacing(false); | ||
| onChange({ ...state, value: "" }); | ||
| }; | ||
|
|
||
| const markForRemoval = () => { | ||
| setIsReplacing(false); | ||
| onChange({ ...state, value: "", markedForRemoval: true }); | ||
| }; | ||
|
|
||
| const undoRemoval = () => { | ||
| onChange({ ...state, markedForRemoval: false }); | ||
| }; | ||
|
|
||
| let status: string; | ||
| if (state.markedForRemoval) { | ||
| status = `${label} will be removed when you save`; | ||
| } else if (showInput && state.configured) { | ||
| status = `Enter a new ${label} to replace the stored one`; | ||
| } else if (state.configured) { | ||
| status = `${label} is configured. The stored value is never displayed.`; | ||
| } else { | ||
| status = `${label} is not configured`; | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className="flex items-center justify-between gap-2"> | ||
| <Label htmlFor={showInput ? id : undefined}> | ||
| {label} | ||
| {required && ( | ||
| <span aria-hidden="true" className="text-destructive"> | ||
| * | ||
| </span> | ||
| )} | ||
| </Label> | ||
|
|
||
| <div className="flex items-center gap-1"> | ||
| {state.configured && !state.markedForRemoval && !isReplacing && ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| className="h-7 px-2 text-xs" | ||
| onClick={startReplacing} | ||
| > | ||
| Replace | ||
| </Button> | ||
| )} | ||
| {state.configured && isReplacing && ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| className="h-7 px-2 text-xs" | ||
| onClick={cancelReplacing} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| )} | ||
| {allowRemove && | ||
| state.configured && | ||
| !state.markedForRemoval && | ||
| !isReplacing && ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| className="h-7 px-2 text-xs text-destructive hover:text-destructive" | ||
| onClick={markForRemoval} | ||
| > | ||
| <Trash2 className="mr-1 h-3 w-3" aria-hidden="true" /> | ||
| Remove | ||
| </Button> | ||
| )} | ||
| {state.markedForRemoval && ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| className="h-7 px-2 text-xs" | ||
| onClick={undoRemoval} | ||
| > | ||
| <Undo2 className="mr-1 h-3 w-3" aria-hidden="true" /> | ||
| Undo | ||
| </Button> | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| {showInput && !state.markedForRemoval ? ( | ||
| <Input | ||
| id={id} | ||
| type="password" | ||
| value={state.value} | ||
| onChange={e => onChange({ ...state, value: e.target.value })} | ||
| placeholder={placeholder} | ||
| aria-describedby={describedBy} | ||
| autoComplete="new-password" | ||
| ref={inputRef} | ||
| /> | ||
| ) : ( | ||
| <div className="flex h-9 items-center gap-2 rounded-md border border-dashed px-3 text-sm text-muted-foreground"> | ||
| {state.markedForRemoval ? ( | ||
| <Trash2 className="h-3.5 w-3.5 shrink-0" aria-hidden="true" /> | ||
| ) : ( | ||
| <CheckCircle2 className="h-3.5 w-3.5 shrink-0" aria-hidden="true" /> | ||
| )} | ||
| <span> | ||
| {state.markedForRemoval ? "Marked for removal" : "Configured"} | ||
| </span> | ||
| </div> | ||
| )} | ||
|
|
||
| <p id={statusId} role="status" className="sr-only"> | ||
| {status} | ||
| </p> | ||
|
|
||
| {helpText && ( | ||
| <p id={helpId} className="text-xs text-muted-foreground mt-1"> | ||
| {helpText} | ||
| </p> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
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,131 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { useState } from "react"; | ||
| import { SecretInput } from "../SecretInput"; | ||
| import type { SecretFieldState } from "@/lib/notifications/channel-form"; | ||
|
|
||
| function Harness({ | ||
| initial, | ||
| allowRemove, | ||
| onState, | ||
| }: { | ||
| initial: SecretFieldState; | ||
| allowRemove?: boolean; | ||
| onState?: (state: SecretFieldState) => void; | ||
| }) { | ||
| const [state, setState] = useState(initial); | ||
| return ( | ||
| <SecretInput | ||
| id="webhook-url" | ||
| label="Webhook URL" | ||
| placeholder="https://hooks.slack.com/..." | ||
| required | ||
| allowRemove={allowRemove} | ||
| state={state} | ||
| onChange={next => { | ||
| setState(next); | ||
| onState?.(next); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const configured: SecretFieldState = { | ||
| value: "", | ||
| configured: true, | ||
| markedForRemoval: false, | ||
| }; | ||
|
|
||
| const empty: SecretFieldState = { | ||
| value: "", | ||
| configured: false, | ||
| markedForRemoval: false, | ||
| }; | ||
|
|
||
| describe("SecretInput", () => { | ||
| it("renders an editable input when nothing is stored", () => { | ||
| render(<Harness initial={empty} />); | ||
|
|
||
| expect(screen.getByLabelText(/Webhook URL/)).toHaveValue(""); | ||
| expect( | ||
| screen.getByText("Webhook URL is not configured") | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides the input and announces the configured state", () => { | ||
| render(<Harness initial={configured} />); | ||
|
|
||
| expect(screen.queryByLabelText(/Webhook URL/)).not.toBeInTheDocument(); | ||
| expect(screen.getByText("Configured")).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText( | ||
| "Webhook URL is configured. The stored value is never displayed." | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("opens an empty input when replacing a stored secret", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<Harness initial={configured} />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: "Replace" })); | ||
|
|
||
| const input = screen.getByLabelText(/Webhook URL/); | ||
| expect(input).toHaveValue(""); | ||
| expect(input).toHaveAttribute("type", "password"); | ||
| }); | ||
|
|
||
| it("reports the typed replacement to the parent", async () => { | ||
| const user = userEvent.setup(); | ||
| const onState = vi.fn(); | ||
| render(<Harness initial={configured} onState={onState} />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: "Replace" })); | ||
| await user.type(screen.getByLabelText(/Webhook URL/), "new"); | ||
|
|
||
| expect(onState).toHaveBeenLastCalledWith({ | ||
| value: "new", | ||
| configured: true, | ||
| markedForRemoval: false, | ||
| }); | ||
| }); | ||
|
|
||
| it("clears the typed value when the replacement is cancelled", async () => { | ||
| const user = userEvent.setup(); | ||
| const onState = vi.fn(); | ||
| render(<Harness initial={configured} onState={onState} />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: "Replace" })); | ||
| await user.type(screen.getByLabelText(/Webhook URL/), "oops"); | ||
| await user.click(screen.getByRole("button", { name: "Cancel" })); | ||
|
|
||
| expect(onState).toHaveBeenLastCalledWith({ | ||
| value: "", | ||
| configured: true, | ||
| markedForRemoval: false, | ||
| }); | ||
| expect(screen.getByText("Configured")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("marks the secret for removal and allows undo", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<Harness initial={configured} allowRemove />); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: /Remove/ })); | ||
| expect( | ||
| screen.getByText("Webhook URL will be removed when you save") | ||
| ).toBeInTheDocument(); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: /Undo/ })); | ||
| expect(screen.getByText("Configured")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides the remove control when removal is not allowed", () => { | ||
| render(<Harness initial={configured} />); | ||
|
|
||
| expect( | ||
| screen.queryByRole("button", { name: /Remove/ }) | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Expose the required state to assistive technology.
When
requiredis true, the component shows only anaria-hiddenasterisk. Add the nativerequiredattribute so screen readers report the constraint.Proposed fix
<Input id={id} type="password" + required={required} value={state.value}Based on learnings, required fields should set
aria-required="true"or use the nativerequiredattribute.📝 Committable suggestion
Source: Learnings