Three defects found by driving the real admin panel, none of which a
typecheck or a unit test would have caught:
- The API key could not be entered at all. Field-level read access denies
the value, and Payload does not render a field you cannot read, so the
input was absent while the collection said it existed. The input is now
a `ui` component that writes the hidden value through useField, which
keeps the key server-only and gives it a working control.
- The probe panel and key input used frontend/ zinc styling, which reads
as an accidental dark block on the admin panel's light theme. They now
use the admin bundle's own --theme-elevation variables.
- An unreachable host reported "fetch failed" with no cause. Node hides
the connect error inside `cause`, and that string ("connect
ECONNREFUSED 10.0.0.5:8888") is the only part an admin can act on, so
it is now included.
Also labels the acronym fields ("Base URL", "Model ID", "Has API key")
and covers the unreachable-host path in the generate-modes spec.
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useField } from "@payloadcms/ui";
|
|
|
|
/**
|
|
* Write-only API key input.
|
|
*
|
|
* The `apiKey` field itself has read access denied, which is what keeps the secret
|
|
* out of the admin form value, REST, GraphQL and populated relationships. The side
|
|
* effect is that Payload does not render a field you cannot read, so the input lives
|
|
* here as a `ui` field and writes the hidden value through `useField`.
|
|
*
|
|
* Only a *new* value is ever typed: the stored key is never shown, so the field
|
|
* starts empty on every load and an empty submission leaves the stored key alone
|
|
* (enforced by the collection's beforeChange).
|
|
*/
|
|
export function ApiKeyField() {
|
|
const { value, setValue } = useField<string>({ path: "apiKey" });
|
|
const { value: saved } = useField<boolean>({ path: "hasApiKey" });
|
|
const [reveal, setReveal] = useState(false);
|
|
|
|
const forged = typeof value === "string" ? value : "";
|
|
|
|
return (
|
|
<div style={{ marginBottom: "1.5rem" }}>
|
|
<label
|
|
htmlFor="ai-connection-api-key"
|
|
style={{ display: "block", marginBottom: "0.5rem", fontSize: "0.85rem" }}
|
|
>
|
|
API Key
|
|
</label>
|
|
|
|
<div style={{ display: "flex", alignItems: "center", gap: "0.5rem" }}>
|
|
<input
|
|
id="ai-connection-api-key"
|
|
type={reveal ? "text" : "password"}
|
|
autoComplete="off"
|
|
spellCheck={false}
|
|
value={forged}
|
|
placeholder={saved ? "A key is stored. Type a new one to replace it." : "No key stored."}
|
|
onChange={(event) => setValue(event.target.value)}
|
|
style={{
|
|
flex: 1,
|
|
padding: "0.5rem 0.75rem",
|
|
border: "1px solid var(--theme-elevation-150)",
|
|
borderRadius: "4px",
|
|
background: "var(--theme-input-bg)",
|
|
color: "var(--theme-text)",
|
|
fontFamily: "monospace",
|
|
}}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setReveal((current) => !current)}
|
|
style={{
|
|
padding: "0.5rem 0.75rem",
|
|
border: "1px solid var(--theme-elevation-150)",
|
|
borderRadius: "4px",
|
|
background: "var(--theme-elevation-50)",
|
|
color: "var(--theme-text)",
|
|
fontSize: "0.75rem",
|
|
cursor: "pointer",
|
|
}}
|
|
>
|
|
{reveal ? "Hide" : "Show"}
|
|
</button>
|
|
</div>
|
|
|
|
<p
|
|
style={{
|
|
marginTop: "0.5rem",
|
|
fontSize: "0.75rem",
|
|
color: "var(--theme-elevation-500)",
|
|
}}
|
|
>
|
|
{saved
|
|
? "A key is stored and never displayed. Type a new value to replace it."
|
|
: "Leave blank to use the UNSLOTH_API_KEY environment variable."}{" "}
|
|
Blank means unchanged; use Clear API key to delete it.
|
|
</p>
|
|
</div>
|
|
);
|
|
}
|