SPB Git

Repository Public

PublicShare.tsx 3.4 KB 55 lines tsx

1// Public share-link viewer (#/s/<id>/<secret>): works signed out. The secret stays in memory; only its auth half is sent.2import { useState } from "react";3import { type Item, isUuid, linkKeys, normalizeItem, openLink, TEMPLATES, unb64u } from "../../../../packages/protocol/src/index.ts";4import { ApiError } from "../core/engine.ts";5import { errText, transport } from "../state.ts";6import { Btn, Callout, ErrorText, Monogram } from "./common.tsx";7import { ItemBody } from "./ItemBody.tsx";8import { TYPE_ICON } from "./itemmeta.ts";9import { AuthLayout } from "./Onboarding.tsx";1011export function PublicShare({ id, secret, onLeave }: { id: string; secret: string; onLeave: () => void }) {12  const [item, setItem] = useState<Item | null>(null);13  const [error, setError] = useState("");14  const [busy, setBusy] = useState(false);15  const wellFormed = isUuid(id) && /^[A-Za-z0-9_-]{43}$/.test(secret);1617  const open = async () => {18    setBusy(true); setError("");19    try {20      const key = unb64u(secret), k = await linkKeys(key, id);21      // FetchTransport sends the x-spv: 1 header; the request carries only the auth half derived from the secret.22      const r = await transport.request("POST", `/api/v1/public/shares/${id}`, { auth: k.auth });23      setItem(normalizeItem(await openLink<Item>(key, id, r.box)));24    } catch (e) {25      setError(e instanceof ApiError && (e.status === 404 || e.status === 410) ? "This link is no longer available: it expired, was revoked, or was already opened the allowed number of times." : e instanceof ApiError ? errText(e) : "The shared item could not be decrypted. The link may be incomplete.");26    } finally { setBusy(false); }27  };2829  return (30    <AuthLayout wide={!!item}>31      <div className="stack" data-testid="public-share">32        {!wellFormed ? (33          <><h1 className="serif auth-title">Incomplete link</h1><p className="muted">This share link is malformed. Ask the sender to copy the whole link, including everything after the # sign.</p></>34        ) : !item ? (35          <>36            <span className="eyebrow">Shared with SP Vault</span>37            <h1 className="serif auth-title">Someone sent you an item</h1>38            <p className="muted">It is decrypted in this browser with the key contained in the link. The server stores only ciphertext and never receives that key.</p>39            <Callout kind="warn" title="This may be a one-time link"><p>Opening it counts as a view and may make the link unusable afterwards. Open it only when you are ready to read and save its content.</p></Callout>40            <ErrorText text={error} />41            {!error && <Btn variant="primary" className="block" onClick={open} busy={busy} data-testid="share-open" autoFocus>Open the shared item</Btn>}42          </>43        ) : (44          <>45            <div className="share-head"><Monogram title={item.title || "?"} icon={item.type === "login" ? undefined : TYPE_ICON[item.type]} size={44} /><div><h1 className="serif" data-testid="share-title">{item.title || "Untitled"}</h1><p className="muted">{TEMPLATES[item.type].label} · read-only copy</p></div></div>46            <Callout kind="warn"><p>Copy what you need now: this page keeps the item in memory only, and the link may not open a second time. Close the tab when you are done.</p></Callout>47            <ItemBody item={item} />48          </>49        )}50        <Btn variant="ghost" onClick={onLeave} data-testid="share-leave">{item ? "Close" : "Go to SP Vault"}</Btn>51      </div>52    </AuthLayout>53  );54}55
Files main
Menu

Theme