Copie de la cle publique : retour visuel succes / echec
- bouton CopyButton commun (dialogue serveur et dialogue cle SSH) :
vert + coche "Copiee" en cas de succes, rouge + alerte en cas d'echec,
retour a l'etat initial apres quelques secondes ;
- en cas d'echec, la cle est selectionnee dans le bloc pour une copie manuelle ;
- repli execCommand("copy") quand l'API Clipboard est absente (contexte non
securise, HTTP) ou refusee (permission).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm4yWr3pL8mEeo8475a
This commit is contained in:
1 parent
ae36cf55d8
commit
99e5a7af80
2 files changed
+90
-18
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState, type RefObject } from "react";
|
||||
import { api, type PingResult, type Server, type ServerInput } from "./api";
|
||||
import { ConfirmDialog } from "./Dialogs";
|
||||
import { CheckIcon, CloseIcon, CopyIcon, PlusIcon, TrashIcon, WarningIcon } from "./icons";
|
||||
@@ -13,13 +13,76 @@ function useEscape(onClose: () => void) {
|
||||
}, [onClose]);
|
||||
}
|
||||
|
||||
/** Copie dans le presse-papiers : API Clipboard (contexte securise), sinon repli execCommand. */
|
||||
async function copy(text: string): Promise<boolean> {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
if (window.isSecureContext && navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
// permission refusee, document sans focus... : on tente le repli
|
||||
}
|
||||
}
|
||||
const prev = document.activeElement as HTMLElement | null;
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
ta.setAttribute("readonly", "");
|
||||
ta.style.position = "fixed";
|
||||
ta.style.top = "0";
|
||||
ta.style.left = "0";
|
||||
ta.style.opacity = "0";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
let ok = false;
|
||||
try {
|
||||
ok = document.execCommand("copy");
|
||||
} catch {
|
||||
ok = false;
|
||||
}
|
||||
document.body.removeChild(ta);
|
||||
prev?.focus();
|
||||
return ok;
|
||||
}
|
||||
|
||||
/** Selectionne le contenu d'un element, pour une copie manuelle (Ctrl/Cmd+C). */
|
||||
function selectContents(el: HTMLElement | null) {
|
||||
const sel = window.getSelection();
|
||||
if (!el || !sel) return;
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(el);
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
}
|
||||
|
||||
type CopyState = "idle" | "ok" | "ko";
|
||||
|
||||
/** Bouton "copier" avec retour visuel : succes (vert) ou echec (rouge), puis retour a l'etat initial. */
|
||||
function CopyButton({ text, label, source }: { text: string; label: string; source?: RefObject<HTMLElement> }) {
|
||||
const [state, setState] = useState<CopyState>("idle");
|
||||
useEffect(() => {
|
||||
if (state === "idle") return;
|
||||
const t = window.setTimeout(() => setState("idle"), state === "ok" ? 2500 : 6000);
|
||||
return () => window.clearTimeout(t);
|
||||
}, [state]);
|
||||
|
||||
const onClick = async () => {
|
||||
setState("idle");
|
||||
const ok = await copy(text);
|
||||
if (!ok) selectContents(source?.current ?? null);
|
||||
setState(ok ? "ok" : "ko");
|
||||
};
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className={state === "idle" ? undefined : `copy-${state}`}
|
||||
onClick={onClick}
|
||||
title={state === "ko" ? "Le navigateur a refusé la copie : la clé est sélectionnée, copiez-la avec Ctrl/Cmd+C." : undefined}
|
||||
>
|
||||
{state === "ok" ? <CheckIcon /> : state === "ko" ? <WarningIcon /> : <CopyIcon />}
|
||||
<span aria-live="polite">{state === "ok" ? "Copiée" : state === "ko" ? "Échec : copiez à la main" : label}</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
interface TargetForm {
|
||||
@@ -68,7 +131,7 @@ export function ServerDialog({
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [ping, setPing] = useState<PingResult[] | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const keyRef = useRef<HTMLPreElement>(null);
|
||||
useEscape(onClose);
|
||||
|
||||
const set = (k: keyof typeof f) => (e: React.ChangeEvent<HTMLInputElement>) => setF({ ...f, [k]: k === "enabled" ? e.target.checked : e.target.value });
|
||||
@@ -203,11 +266,10 @@ export function ServerDialog({
|
||||
<p className="muted small-text">
|
||||
À ajouter dans <code>~/.ssh/authorized_keys</code> de chaque utilisateur ci-dessus :
|
||||
</p>
|
||||
<pre className="example">{publicKey}</pre>
|
||||
<button type="button" onClick={async () => setCopied(await copy(publicKey.trim()))}>
|
||||
<CopyIcon />
|
||||
{copied ? "Copiée" : "Copier"}
|
||||
</button>
|
||||
<pre className="example" ref={keyRef}>
|
||||
{publicKey}
|
||||
</pre>
|
||||
<CopyButton text={publicKey.trim()} label="Copier" source={keyRef} />
|
||||
</details>
|
||||
)}
|
||||
</div>
|
||||
@@ -243,7 +305,7 @@ export function ServerDialog({
|
||||
|
||||
/** Clé publique SSH de stackwatch, à autoriser sur chaque serveur. */
|
||||
export function KeyDialog({ publicKey, onClose }: { publicKey: string; onClose: () => void }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const keyRef = useRef<HTMLPreElement>(null);
|
||||
useEscape(onClose);
|
||||
const cmd = `echo '${publicKey.trim()}' >> ~/.ssh/authorized_keys`;
|
||||
return (
|
||||
@@ -258,7 +320,9 @@ export function KeyDialog({ publicKey, onClose }: { publicKey: string; onClose:
|
||||
{publicKey ? (
|
||||
<>
|
||||
<p>Stackwatch se connecte en SSH avec cette clé (créée au premier démarrage, stockée dans son volume de données). Ajoutez-la à chaque utilisateur déclaré sur vos serveurs :</p>
|
||||
<pre className="example">{publicKey}</pre>
|
||||
<pre className="example" ref={keyRef}>
|
||||
{publicKey}
|
||||
</pre>
|
||||
<p className="muted small-text">Sur le serveur, connecté en tant que cet utilisateur :</p>
|
||||
<pre className="example">{cmd}</pre>
|
||||
<p className="muted small-text">
|
||||
@@ -266,10 +330,7 @@ export function KeyDialog({ publicKey, onClose }: { publicKey: string; onClose:
|
||||
</p>
|
||||
<div className="modal-actions">
|
||||
<span className="spacer" />
|
||||
<button onClick={async () => setCopied(await copy(publicKey.trim()))}>
|
||||
<CopyIcon />
|
||||
{copied ? "Copiée" : "Copier la clé"}
|
||||
</button>
|
||||
<CopyButton text={publicKey.trim()} label="Copier la clé" source={keyRef} />
|
||||
<button className="primary" onClick={onClose}>
|
||||
Fermer
|
||||
</button>
|
||||
|
||||
@@ -1241,3 +1241,14 @@ textarea:focus-visible {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Bouton copier : retour visuel succes / echec */
|
||||
button.copy-ok {
|
||||
color: var(--ok);
|
||||
border-color: var(--ok);
|
||||
}
|
||||
button.copy-ko {
|
||||
color: var(--danger);
|
||||
border-color: var(--danger);
|
||||
background: var(--danger-soft);
|
||||
}
|
||||
Reference in new issue
Block a user