diff --git a/forge-web/app/new/page.tsx b/forge-web/app/new/page.tsx index e0946bd8b..bfa9a8196 100644 --- a/forge-web/app/new/page.tsx +++ b/forge-web/app/new/page.tsx @@ -1,81 +1,131 @@ 'use client' /** - * `/new` — create a repository. Instantiates the per-repo token contract (~1.18 DASH, - * CRITICAL key) + initial config + registry listing. The 1.18 DASH cost is previewed and - * confirmed before signing; on success we jump to the new repo home. + * `/new` — create a forge-v2 repository: three documents in forge-core (the `repo`, the + * owner's `maintainer`, the first `config`), about 0.0013 DASH. The cost is previewed before + * signing; the steps run one by one and are journaled in IndexedDB, so a closed tab offers + * "Finish creating " on the next visit instead of leaving a half-made repo. On success + * the repo's empty state shows the push commands (`ux-dx-spec.md` §5.5). + * + * On a network without forge-v2 (testnet today) repos are not created from the browser: a v1 + * repo was its own ~1.18 DASH contract, and that path is gone. */ -import { useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { useRouter } from 'next/navigation' -import { GitBranch, Hammer, Lock, Terminal } from 'lucide-react' +import { Check, GitBranch, Hammer, Loader2, Lock, Terminal } from 'lucide-react' import { AppShell } from '@/components/app-shell' import { Button } from '@/components/ui/button' import { Field, Input, Textarea } from '@/components/ui/input' import { CostPreview } from '@/components/ui/cost-preview' import { ConfirmDialog } from '@/components/confirm-dialog' import { EmptyState } from '@/components/ui/states' -import { NotDeployedState, isRegistryDeployed, isV2Deployed } from '@/components/ui/network-badge' -import { ACTIVE_NETWORK } from '@/lib/constants' +import { NotDeployedState, isV2Deployed } from '@/components/ui/network-badge' +import { ACTIVE_NETWORK, DEFAULT_NETWORK } from '@/lib/constants' import { useAuth } from '@/contexts/auth-context' import { useUiStore } from '@/hooks/use-ui-store' import { useSdk } from '@/hooks/use-sdk' -import { createRepo, normalizeRepoName } from '@/lib/repo' -import { previewCredits, COST_ESTIMATE_CREDITS } from '@/lib/sdk' - -// The mode is recorded in the repo config. It does not yet route pushes: `git push` stores -// packs on Platform whatever this says (roadmap gap 3), and only `dg repack` / `dg reseed` -// move them — so the hints describe the preference, not where bytes will land. -const BACKENDS = [ - { mode: 0, label: '⛓ platform', hint: 'Packs stored on Dash Platform' }, - { mode: 1, label: '🌐 ipfs', hint: 'Prefer IPFS for packs; hashes stay on Platform' }, - { mode: 2, label: '🌐 s3', hint: 'Prefer S3-compatible storage for packs' }, - { mode: 3, label: '🌐 https', hint: 'Prefer an HTTPS origin for packs' }, +import { useWriteGuard } from '@/hooks/use-write-guard' +import { + checkRepoInput, + createRepoV2, + discardRepoCreation, + normalizeRepoName, + pendingRepoCreations, + type CreateRepoInput, + type CreateRepoStep, + type RepoCreationJournal, +} from '@/lib/repo' +import { previewCreate, sumPreviews } from '@/lib/sdk' +import { errorMessage } from '@/lib/utils' + +const STEPS: readonly { step: CreateRepoStep; label: string }[] = [ + { step: 'repo', label: 'Repository document' }, + { step: 'maintainer', label: 'You, as its first maintainer' }, + { step: 'config', label: 'Initial config (default branch)' }, ] +type StepState = 'todo' | 'running' | 'done' +const INITIAL_PROGRESS: Record = { repo: 'todo', maintainer: 'todo', config: 'todo' } + export default function NewRepoPage(): JSX.Element { const router = useRouter() const { sdk } = useSdk() const { identity, signer } = useAuth() const openLogin = useUiStore((s) => s.openLogin) + const guard = useWriteGuard() + const forge = ACTIVE_NETWORK.v2 const [name, setName] = useState('') const [description, setDescription] = useState('') const [defaultBranch, setDefaultBranch] = useState('main') - const [backendMode, setBackendMode] = useState(0) - const [confirm, setConfirm] = useState(false) + const [confirm, setConfirm] = useState(null) + const [progress, setProgress] = useState | null>(null) + const [pending, setPending] = useState([]) + + const reloadPending = useCallback(() => { + if (!identity) return + pendingRepoCreations(DEFAULT_NETWORK, identity).then(setPending, () => setPending([])) + }, [identity]) + useEffect(reloadPending, [reloadPending]) const nameError = useMemo(() => { if (name.trim() === '') return null try { - normalizeRepoName(name.trim()) + checkRepoInput({ name: normalizeRepoName(name), description }) return null } catch (e) { - return e instanceof Error ? e.message : 'invalid name' + return errorMessage(e, 'invalid name') + } + }, [name, description]) + // An unfinished creation of this name resumes with the values it started with. + const resuming = useMemo(() => { + if (nameError !== null || name.trim() === '') return null + const n = normalizeRepoName(name) + return pending.find((j) => j.input.name === n) ?? null + }, [name, nameError, pending]) + const differs = + resuming !== null && + ((resuming.input.description ?? '') !== description.trim() || (resuming.input.defaultBranch ?? 'main') !== (defaultBranch.trim() || 'main')) + + const input = (): CreateRepoInput => ({ + name: normalizeRepoName(name), + ...(description.trim() ? { description: description.trim() } : {}), + ...(defaultBranch.trim() && defaultBranch.trim() !== 'main' ? { defaultBranch: defaultBranch.trim() } : {}), + }) + const costOf = (i: CreateRepoInput) => + sumPreviews([ + previewCreate('repo', { ...i, visibility: 'public' }), + previewCreate('maintainer'), + previewCreate('config', { defaultBranch: i.defaultBranch ?? 'main' }), + ]) + const cost = costOf(name.trim() && nameError === null ? input() : { name: 'x' }) + + const create = async (i: CreateRepoInput): Promise => { + if (!sdk || !signer || !forge) throw new Error('sign in first') + setProgress(INITIAL_PROGRESS) + let result + try { + result = await createRepoV2(sdk, signer, forge, i, (step, state) => + setProgress((p) => ({ ...(p ?? INITIAL_PROGRESS), [step]: state === 'start' ? 'running' : 'done' })), + ) + } catch (e) { + setProgress(null) + throw e + } finally { + reloadPending() } - }, [name]) - - const cost = previewCredits(COST_ESTIMATE_CREDITS.repoCreate) - - const create = async (): Promise => { - if (!sdk || !signer) throw new Error('sign in first') - const result = await createRepo(sdk, signer, name.trim(), { - defaultBranch, - backendMode, - description: description.trim(), - }) - void result - router.push(`/repo?owner=${encodeURIComponent(identity ?? '')}&name=${encodeURIComponent(normalizeRepoName(name.trim()))}`) + router.push(`/repo?owner=${encodeURIComponent(identity ?? '')}&name=${encodeURIComponent(result.name)}&created=1`) } - if (!isRegistryDeployed()) { + if (!isV2Deployed()) { return ( - {isV2Deployed() ? ( + {ACTIVE_NETWORK.registryContractId !== null ? ( .`} + title={`New repos are forge-v2, which ${ACTIVE_NETWORK.key} does not run yet`} + body="Existing v1 repos here stay readable and writable (issues, comments, stars). A v1 repo was its own ~1.18 DASH contract; creating one from the browser is no longer offered." /> ) : ( @@ -90,7 +140,7 @@ export default function NewRepoPage(): JSX.Element { Sign in} /> @@ -106,26 +156,39 @@ export default function NewRepoPage(): JSX.Element {

Forge a new repo

-

A fresh contract, owned by your identity.

+

Three documents on {ACTIVE_NETWORK.key}, owned by your identity.

+ {pending.map((j) => ( +
+ + Creating {j.input.name} did not finish ({j.done.length} of 3 steps). + + + +
+ ))} +
- - setName(e.target.value)} - placeholder="forge-core" - className="font-mono" - spellCheck={false} - autoFocus - /> + + setName(e.target.value)} placeholder="forge-core" className="font-mono" spellCheck={false} autoFocus /> {nameError ?

{nameError}

: null} - -