Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
349 changes: 199 additions & 150 deletions app/dashboard/integrations/notifications/page.tsx

Large diffs are not rendered by default.

184 changes: 184 additions & 0 deletions components/notification/SecretInput.tsx
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}
/>
Comment on lines +150 to +159

Copy link
Copy Markdown

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 required is true, the component shows only an aria-hidden asterisk. Add the native required attribute 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 native required attribute.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<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}
/>
<Input
id={id}
type="password"
required={required}
value={state.value}
onChange={e => onChange({ ...state, value: e.target.value })}
placeholder={placeholder}
aria-describedby={describedBy}
autoComplete="new-password"
ref={inputRef}
/>

Source: Learnings

) : (
<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>
);
}
131 changes: 131 additions & 0 deletions components/notification/__tests__/SecretInput.test.tsx
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();
});
});
Loading
Loading