|
| 1 | +/** |
| 2 | + * URL-encodes a password (or any string) for use in a `postgres://` connection |
| 3 | + * URI. Replaces this component with a plain note in non-JS environments. |
| 4 | + * |
| 5 | + * @see _livesync-connection-string-note.mdx |
| 6 | + */ |
| 7 | +import { useState, useCallback } from "react"; |
| 8 | +import CopyToClipboard from "./CopyToClipboard"; |
| 9 | + |
| 10 | +export interface PasswordEncoderProps { |
| 11 | + /** Placeholder for the input. Default: "Paste your password". */ |
| 12 | + placeholder?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export default function PasswordEncoder({ |
| 16 | + placeholder = "Paste your password", |
| 17 | +}: PasswordEncoderProps) { |
| 18 | + const [value, setValue] = useState(""); |
| 19 | + |
| 20 | + const encoded = useCallback((s: string) => { |
| 21 | + return s === "" ? "" : encodeURIComponent(s); |
| 22 | + }, []); |
| 23 | + |
| 24 | + const result = encoded(value); |
| 25 | + |
| 26 | + return ( |
| 27 | + <div style={{ display: "flex", flexDirection: "column", gap: "0.5rem" }}> |
| 28 | + <label |
| 29 | + htmlFor="password-encoder-input" |
| 30 | + style={{ fontWeight: 600, fontSize: "0.875rem" }} |
| 31 | + > |
| 32 | + Password |
| 33 | + </label> |
| 34 | + <input |
| 35 | + id="password-encoder-input" |
| 36 | + type="text" |
| 37 | + value={value} |
| 38 | + onChange={(e) => setValue(e.target.value)} |
| 39 | + placeholder={placeholder} |
| 40 | + autoComplete="off" |
| 41 | + spellCheck={false} |
| 42 | + aria-describedby="password-encoder-hint" |
| 43 | + style={{ |
| 44 | + padding: "0.5rem 0.75rem", |
| 45 | + borderRadius: "0.375rem", |
| 46 | + border: "1px solid var(--sl-color-gray-5, #ccc)", |
| 47 | + background: "var(--sl-color-bg, #fff)", |
| 48 | + color: "var(--sl-color-text, #000)", |
| 49 | + fontSize: "0.875rem", |
| 50 | + fontFamily: "var(--sl-font-mono, monospace)", |
| 51 | + }} |
| 52 | + /> |
| 53 | + <p |
| 54 | + id="password-encoder-hint" |
| 55 | + style={{ margin: 0, fontSize: "0.8125rem", color: "var(--sl-color-text-soft, #666)" }} |
| 56 | + > |
| 57 | + Enter your password to get the URL-encoded version. Characters that are |
| 58 | + safe in a URI (like <code>*</code>, <code>~</code>, <code>.</code>, <code>-</code>, <code>_</code>) |
| 59 | + are left as-is. |
| 60 | + </p> |
| 61 | + {result !== "" && ( |
| 62 | + <div |
| 63 | + style={{ |
| 64 | + display: "flex", |
| 65 | + alignItems: "center", |
| 66 | + gap: "0.5rem", |
| 67 | + padding: "0.5rem 0.75rem", |
| 68 | + borderRadius: "0.375rem", |
| 69 | + background: "var(--sl-color-gray-7, #f5f5f5)", |
| 70 | + border: "1px solid var(--sl-color-gray-5, #e0e0e0)", |
| 71 | + }} |
| 72 | + > |
| 73 | + <code |
| 74 | + style={{ |
| 75 | + flex: 1, |
| 76 | + fontSize: "0.8125rem", |
| 77 | + wordBreak: "break-all", |
| 78 | + fontFamily: "var(--sl-font-mono, monospace)", |
| 79 | + }} |
| 80 | + > |
| 81 | + {result} |
| 82 | + </code> |
| 83 | + <CopyToClipboard text={result} label="Copy" copiedLabel="Copied!" variant="subtle" /> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
0 commit comments