mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import { useState, useEffect, useRef } from 'react';
|
|
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure';
|
|
import { nip19 } from 'nostr-tools';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Button } from '@/components/ui/button';
|
|
import { PasswordInput } from '@/components/ui/password-input';
|
|
import { RefreshCw } from 'lucide-react';
|
|
|
|
interface NsiteDeployFormProps {
|
|
gateway: string;
|
|
savedNsec?: string;
|
|
onNsecChange: (nsec: string) => void;
|
|
}
|
|
|
|
export function NsiteDeployForm({
|
|
gateway,
|
|
savedNsec,
|
|
onNsecChange,
|
|
}: NsiteDeployFormProps) {
|
|
const [nsec, setNsec] = useState(savedNsec || '');
|
|
const [npub, setNpub] = useState('');
|
|
const initialized = useRef(false);
|
|
|
|
// Initialize nsec on mount
|
|
useEffect(() => {
|
|
if (!initialized.current) {
|
|
initialized.current = true;
|
|
if (savedNsec) {
|
|
// If we have a saved nsec, notify parent immediately
|
|
onNsecChange(savedNsec);
|
|
} else if (!nsec) {
|
|
// Generate a new nsec if none exists
|
|
const sk = generateSecretKey();
|
|
const newNsec = nip19.nsecEncode(sk);
|
|
setNsec(newNsec);
|
|
onNsecChange(newNsec);
|
|
}
|
|
}
|
|
}, [savedNsec, nsec, onNsecChange]);
|
|
|
|
// Update npub when nsec changes
|
|
useEffect(() => {
|
|
if (nsec) {
|
|
try {
|
|
const decoded = nip19.decode(nsec);
|
|
if (decoded.type === 'nsec') {
|
|
const pk = getPublicKey(decoded.data);
|
|
const npubEncoded = nip19.npubEncode(pk);
|
|
setNpub(npubEncoded);
|
|
}
|
|
} catch {
|
|
setNpub('');
|
|
}
|
|
}
|
|
}, [nsec]);
|
|
|
|
const handleNsecChange = (value: string) => {
|
|
setNsec(value);
|
|
onNsecChange(value);
|
|
};
|
|
|
|
const handleGenerateNew = () => {
|
|
const sk = generateSecretKey();
|
|
const newNsec = nip19.nsecEncode(sk);
|
|
setNsec(newNsec);
|
|
onNsecChange(newNsec);
|
|
};
|
|
|
|
const fullDomain = npub ? `${npub}.${gateway}` : `npub1...${gateway}`;
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label>Site URL</Label>
|
|
<p className="text-sm font-mono bg-muted p-2 rounded-md break-all">
|
|
{fullDomain}
|
|
</p>
|
|
<p className="text-xs text-muted-foreground">
|
|
Each site has a unique Nostr keypair
|
|
</p>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="nsite-nsec">Site Private Key (nsec)</Label>
|
|
<div className="flex gap-2">
|
|
<PasswordInput
|
|
id="nsite-nsec"
|
|
value={nsec}
|
|
onChange={(e) => handleNsecChange(e.target.value)}
|
|
placeholder="nsec1..."
|
|
className="flex-1"
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="icon"
|
|
onClick={handleGenerateNew}
|
|
title="Generate new key"
|
|
>
|
|
<RefreshCw className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
Keep this private key secure. It's used to sign site metadata.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|