mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add version-history modal for nsite snapshots
Adds a History icon button next to the Deploy button (visible only when an nsite provider is selected). Opens NsiteVersionsDialog, which queries the configured relays for kind 5128 manifest snapshots authored by the logged-in user for the current site, listed newest-first with date and file count. Each row exposes an "open" link to `https://v<base36id>.<gateway>` (NIP-5A snapshot URL) and a delete action that publishes a NIP-09 kind 5 event scoped to the snapshot kind. Deletion is gated behind an AlertDialog confirm and removes the row optimistically. A new buildNsiteSnapshotUrl helper is added in lib/utils/nsite.ts, and the existing pubkeyToBase36 is renamed to hex32ToBase36 (its existing name is kept as a deprecated alias). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This commit is contained in:
1 parent
a588509ec6
commit
21e4da5fbe
3 files changed
+362
-33
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Rocket, ExternalLink, AlertCircle, Settings, Cloud } from 'lucide-react';
|
||||
import { Rocket, ExternalLink, AlertCircle, Settings, Cloud, History } from 'lucide-react';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -24,6 +24,7 @@ import { ShakespeareDeployForm } from '@/components/deploy/ShakespeareDeployForm
|
||||
import { NetlifyDeployForm } from '@/components/deploy/NetlifyDeployForm';
|
||||
import { VercelDeployForm } from '@/components/deploy/VercelDeployForm';
|
||||
import { NsiteSettingsDialog } from '@/components/deploy/NsiteSettingsDialog';
|
||||
import { NsiteVersionsDialog } from '@/components/deploy/NsiteVersionsDialog';
|
||||
import { CloudflareDeployForm } from '@/components/deploy/CloudflareDeployForm';
|
||||
import { DenoDeployForm } from '@/components/deploy/DenoDeployForm';
|
||||
import { RailwayDeployForm } from '@/components/deploy/RailwayDeployForm';
|
||||
@@ -177,6 +178,7 @@ export function DeploySteps({ projectId, projectName, onClose }: DeployStepsProp
|
||||
const [nsiteVfsConfig, setNsiteVfsConfig] = useState<NsiteVfsConfig | null>(null);
|
||||
const [nsiteVfsConfigLoading, setNsiteVfsConfigLoading] = useState(true);
|
||||
const [nsiteSettingsOpen, setNsiteSettingsOpen] = useState(false);
|
||||
const [nsiteVersionsOpen, setNsiteVersionsOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!projectId) {
|
||||
@@ -777,6 +779,17 @@ export function DeploySteps({ projectId, projectName, onClose }: DeployStepsProp
|
||||
/>
|
||||
)}
|
||||
|
||||
{nsiteVfsConfig && user && selectedProvider?.type === 'nsite' && (
|
||||
<NsiteVersionsDialog
|
||||
open={nsiteVersionsOpen}
|
||||
onOpenChange={setNsiteVersionsOpen}
|
||||
pubkey={user.pubkey}
|
||||
siteIdentifier={(nsiteVfsConfig.id != null && nsiteVfsConfig.id !== '') ? nsiteVfsConfig.id : undefined}
|
||||
gateway={selectedProvider.gateway}
|
||||
relayUrls={nsiteVfsConfig.relays}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<h3 className="font-semibold mb-1">Deploy your project</h3>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
@@ -956,34 +969,49 @@ export function DeploySteps({ projectId, projectName, onClose }: DeployStepsProp
|
||||
)}
|
||||
|
||||
{selectedProvider && (
|
||||
<Button
|
||||
onClick={handleDeploy}
|
||||
disabled={
|
||||
isDeploying ||
|
||||
(selectedProvider.type === 'shakespeare' && !user) ||
|
||||
(selectedProvider.type === 'shakespeare' && !isShakespeareFormValid) ||
|
||||
(selectedProvider.type === 'nsite' && !user) ||
|
||||
(selectedProvider.type === 'nsite' && !nsiteVfsConfig) ||
|
||||
(selectedProvider.type === 'nsite' && (!nsiteVfsConfig?.relays?.length || !nsiteVfsConfig?.servers?.length)) ||
|
||||
(selectedProvider.type === 'netlify' && !netlifyForm.siteId && !netlifyForm.siteName) ||
|
||||
(selectedProvider.type === 'cloudflare' && !cloudflareForm.projectName) ||
|
||||
(selectedProvider.type === 'deno' && !denoDeployForm.projectName) ||
|
||||
(selectedProvider.type === 'railway' && !railwayForm.workspaceId)
|
||||
}
|
||||
className="w-full"
|
||||
>
|
||||
{isDeploying ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"></div>
|
||||
Deploying...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Rocket className="h-4 w-4 mr-2" />
|
||||
Deploy
|
||||
</>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={handleDeploy}
|
||||
disabled={
|
||||
isDeploying ||
|
||||
(selectedProvider.type === 'shakespeare' && !user) ||
|
||||
(selectedProvider.type === 'shakespeare' && !isShakespeareFormValid) ||
|
||||
(selectedProvider.type === 'nsite' && !user) ||
|
||||
(selectedProvider.type === 'nsite' && !nsiteVfsConfig) ||
|
||||
(selectedProvider.type === 'nsite' && (!nsiteVfsConfig?.relays?.length || !nsiteVfsConfig?.servers?.length)) ||
|
||||
(selectedProvider.type === 'netlify' && !netlifyForm.siteId && !netlifyForm.siteName) ||
|
||||
(selectedProvider.type === 'cloudflare' && !cloudflareForm.projectName) ||
|
||||
(selectedProvider.type === 'deno' && !denoDeployForm.projectName) ||
|
||||
(selectedProvider.type === 'railway' && !railwayForm.workspaceId)
|
||||
}
|
||||
className="flex-1"
|
||||
>
|
||||
{isDeploying ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"></div>
|
||||
Deploying...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Rocket className="h-4 w-4 mr-2" />
|
||||
Deploy
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{selectedProvider.type === 'nsite' && nsiteVfsConfig && user && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setNsiteVersionsOpen(true)}
|
||||
title="Version history"
|
||||
aria-label="Version history"
|
||||
>
|
||||
<History className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { ExternalLink, History, Loader2, Trash2 } from 'lucide-react';
|
||||
import type { NostrEvent } from '@nostrify/nostrify';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useNostrPublish } from '@/hooks/useNostrPublish';
|
||||
import { buildNsiteParentRef } from '@/lib/utils/nsiteAggregateHash';
|
||||
import { buildNsiteSnapshotUrl } from '@/lib/utils/nsite';
|
||||
|
||||
const NSITE_SNAPSHOT_KIND = 5128;
|
||||
const QUERY_LIMIT = 500;
|
||||
const QUERY_TIMEOUT_MS = 8_000;
|
||||
|
||||
interface NsiteVersionsDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
/** Author of the snapshots — typically the logged-in user's pubkey */
|
||||
pubkey: string;
|
||||
/** Named-site dTag (empty for root sites) */
|
||||
siteIdentifier?: string;
|
||||
/** Gateway hostname used to build snapshot URLs (e.g. "nsite.lol") */
|
||||
gateway: string;
|
||||
/** Relays to query and to publish deletion events through */
|
||||
relayUrls: string[];
|
||||
}
|
||||
|
||||
interface VersionRow {
|
||||
id: string;
|
||||
createdAt: number;
|
||||
fileCount: number;
|
||||
/** Lower-case aggregate hash hex if present on the event */
|
||||
aggregateHash?: string;
|
||||
}
|
||||
|
||||
function countPathTags(event: NostrEvent): number {
|
||||
let n = 0;
|
||||
for (const tag of event.tags) {
|
||||
if (tag[0] === 'path') n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function extractAggregateHash(event: NostrEvent): string | undefined {
|
||||
for (const tag of event.tags) {
|
||||
if (tag[0] === 'x' && tag[2] === 'aggregate') return tag[1];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const dateFormatter = new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
});
|
||||
|
||||
export function NsiteVersionsDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
pubkey,
|
||||
siteIdentifier,
|
||||
gateway,
|
||||
relayUrls,
|
||||
}: NsiteVersionsDialogProps) {
|
||||
const { nostr } = useNostr();
|
||||
const { mutateAsync: publishEvent } = useNostrPublish();
|
||||
|
||||
const [versions, setVersions] = useState<VersionRow[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Pending delete: which event id is awaiting confirmation
|
||||
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
|
||||
// Currently in-flight deletions (event ids)
|
||||
const [deletingIds, setDeletingIds] = useState<Set<string>>(new Set());
|
||||
|
||||
const parentRef = buildNsiteParentRef(pubkey, siteIdentifier);
|
||||
|
||||
const loadVersions = useCallback(async () => {
|
||||
if (!pubkey) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const relayClient = nostr.group(relayUrls);
|
||||
const events = await relayClient.query(
|
||||
[{
|
||||
kinds: [NSITE_SNAPSHOT_KIND],
|
||||
authors: [pubkey],
|
||||
'#a': [parentRef],
|
||||
limit: QUERY_LIMIT,
|
||||
}],
|
||||
{ signal: AbortSignal.timeout(QUERY_TIMEOUT_MS) },
|
||||
);
|
||||
|
||||
// Dedup by event id (relays may return duplicates) and sort newest-first.
|
||||
const byId = new Map<string, NostrEvent>();
|
||||
for (const ev of events) byId.set(ev.id, ev);
|
||||
|
||||
const rows: VersionRow[] = Array.from(byId.values())
|
||||
.sort((a, b) => b.created_at - a.created_at)
|
||||
.map(ev => ({
|
||||
id: ev.id,
|
||||
createdAt: ev.created_at,
|
||||
fileCount: countPathTags(ev),
|
||||
aggregateHash: extractAggregateHash(ev),
|
||||
}));
|
||||
|
||||
setVersions(rows);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to load versions');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [nostr, relayUrls, pubkey, parentRef]);
|
||||
|
||||
// Refetch each time the dialog opens so the list is fresh after a new deploy.
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
loadVersions();
|
||||
}, [open, loadVersions]);
|
||||
|
||||
const handleDelete = async (eventId: string) => {
|
||||
setConfirmDeleteId(null);
|
||||
setDeletingIds(prev => new Set(prev).add(eventId));
|
||||
try {
|
||||
// NIP-09 deletion: kind 5 event with `e` tag for the event id and `k` tag
|
||||
// for its kind so clients can scope the deletion intent.
|
||||
await publishEvent({
|
||||
kind: 5,
|
||||
content: '',
|
||||
tags: [
|
||||
['e', eventId],
|
||||
['k', String(NSITE_SNAPSHOT_KIND)],
|
||||
],
|
||||
});
|
||||
// Optimistically drop from the list — relay propagation is best-effort.
|
||||
setVersions(prev => prev.filter(v => v.id !== eventId));
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to delete version');
|
||||
} finally {
|
||||
setDeletingIds(prev => {
|
||||
const next = new Set(prev);
|
||||
next.delete(eventId);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const pendingDelete = confirmDeleteId
|
||||
? versions.find(v => v.id === confirmDeleteId)
|
||||
: null;
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-lg max-h-[85vh] overflow-hidden flex flex-col">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<History className="h-4 w-4" />
|
||||
Version history
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
Snapshots published for this nsite. Each version is a permanent,
|
||||
shareable copy of the site at the time it was deployed.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex-1 overflow-y-auto -mx-6 px-6">
|
||||
{loading && versions.length === 0 ? (
|
||||
<div className="flex items-center justify-center py-12 text-muted-foreground">
|
||||
<Loader2 className="h-5 w-5 animate-spin mr-2" />
|
||||
<span className="text-sm">Loading versions…</span>
|
||||
</div>
|
||||
) : error ? (
|
||||
<p className="text-sm text-destructive py-4">{error}</p>
|
||||
) : versions.length === 0 ? (
|
||||
<div className="py-12 text-center text-sm text-muted-foreground">
|
||||
<p className="mb-2">No published versions yet.</p>
|
||||
<p className="text-xs">
|
||||
Enable "Publish version history" in site settings to start
|
||||
tracking deploys.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<ul className="divide-y">
|
||||
{versions.map((v) => {
|
||||
const url = buildNsiteSnapshotUrl({ eventIdHex: v.id, gateway });
|
||||
const deleting = deletingIds.has(v.id);
|
||||
return (
|
||||
<li key={v.id} className="py-3 flex items-center gap-3">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-sm font-medium">
|
||||
{dateFormatter.format(new Date(v.createdAt * 1000))}
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{v.fileCount} {v.fileCount === 1 ? 'file' : 'files'}
|
||||
{v.aggregateHash && (
|
||||
<>
|
||||
<span className="mx-1.5">·</span>
|
||||
<code className="font-mono">{v.aggregateHash.slice(0, 8)}</code>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
asChild
|
||||
title="Open this version in the gateway"
|
||||
>
|
||||
<a href={url} target="_blank" rel="noopener noreferrer">
|
||||
<ExternalLink className="h-3.5 w-3.5" />
|
||||
</a>
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => setConfirmDeleteId(v.id)}
|
||||
disabled={deleting}
|
||||
title="Delete this version (publishes a NIP-09 delete event)"
|
||||
>
|
||||
{deleting ? (
|
||||
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
||||
) : (
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
)}
|
||||
</Button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
|
||||
{/* Delete confirmation */}
|
||||
<AlertDialog
|
||||
open={confirmDeleteId !== null}
|
||||
onOpenChange={(o) => { if (!o) setConfirmDeleteId(null); }}
|
||||
>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete this version?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{pendingDelete && (
|
||||
<>
|
||||
Publishes a Nostr delete event for the snapshot from{' '}
|
||||
<strong>{dateFormatter.format(new Date(pendingDelete.createdAt * 1000))}</strong>.
|
||||
Relays that honor NIP-09 will drop the event; gateways may continue
|
||||
serving cached copies until they refresh.
|
||||
</>
|
||||
)}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => confirmDeleteId && handleDelete(confirmDeleteId)}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
+22
-5
@@ -63,16 +63,19 @@ export function isValidDTag(value: string): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a 32-byte pubkey (as hex string) into a 50-character lowercase base36 string.
|
||||
* Used to build named-site subdomain URLs: `{base36pubkey}{dTag}.{gateway}`
|
||||
* Encode a 32-byte hex value into a 50-character lowercase base36 string.
|
||||
*
|
||||
* Used to build canonical nsite subdomain URLs:
|
||||
* - Named site: `{base36pubkey}{dTag}.{gateway}`
|
||||
* - Snapshot: `v{base36snapshotId}.{gateway}`
|
||||
*
|
||||
* Ported from nsite.run/apps/spa/src/lib/base36.js
|
||||
*/
|
||||
export function pubkeyToBase36(pubkeyHex: string): string {
|
||||
export function hex32ToBase36(hex: string): string {
|
||||
// hex → Uint8Array
|
||||
const bytes = new Uint8Array(32);
|
||||
for (let i = 0; i < 32; i++) {
|
||||
bytes[i] = parseInt(pubkeyHex.slice(i * 2, i * 2 + 2), 16);
|
||||
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
||||
}
|
||||
|
||||
const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
@@ -92,6 +95,9 @@ export function pubkeyToBase36(pubkeyHex: string): string {
|
||||
return digits.reverse().join('').padStart(50, '0');
|
||||
}
|
||||
|
||||
/** @deprecated use {@link hex32ToBase36} — kept as an alias for callers that still use this name. */
|
||||
export const pubkeyToBase36 = hex32ToBase36;
|
||||
|
||||
/**
|
||||
* Build the deployed URL for an nsite.
|
||||
*
|
||||
@@ -106,8 +112,19 @@ export function buildNsiteUrl(opts: {
|
||||
}): string {
|
||||
const { pubkeyHex, npub, gateway, siteIdentifier } = opts;
|
||||
if (siteIdentifier) {
|
||||
const base36 = pubkeyToBase36(pubkeyHex);
|
||||
const base36 = hex32ToBase36(pubkeyHex);
|
||||
return `https://${base36}${siteIdentifier}.${gateway}`;
|
||||
}
|
||||
return `https://${npub}.${gateway}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the canonical URL for an nsite manifest snapshot (NIP-5A kind 5128).
|
||||
* `https://v{base36snapshotId}.{gateway}`
|
||||
*/
|
||||
export function buildNsiteSnapshotUrl(opts: {
|
||||
eventIdHex: string;
|
||||
gateway: string;
|
||||
}): string {
|
||||
return `https://v${hex32ToBase36(opts.eventIdHex)}.${opts.gateway}`;
|
||||
}
|
||||
Reference in new issue
Block a user