Skip to Content
dimah-s3v2.8.1

Shared View

The page a share link opens, with its password form, the shared file, or the shared folder.

Render the public page of a share link in one component. SharedView asks for the password when the link has one, then offers the shared file, or lists the shared folder in a read-only ObjectList with search, sort, and a download per file. A link that does not open says why. To build the page yourself, follow The shared page.

The password is demo.

Opening the link

Installation

npm i @dimah-s3/share
import { useShareVisit } from "@dimah-s3/share/react";
import { SharedView } from "@dimah-s3/ui/share";

Usage

Render it on the page share({ url }) points at. It takes useShareVisit for the token in the URL, as ObjectList takes useObjectList:

app/s/[token]/page.tsx
import { SharedPage } from "./shared-page";

export default async function Page({
  params,
}: {
  params: Promise<{ token: string }>;
}) {
  const { token } = await params;
  return <SharedPage token={token} />;
}
app/s/[token]/shared-page.tsx
"use client";

import type { ShareVisitorLink } from "@dimah-s3/share/client";
import { useShareVisit } from "@dimah-s3/share/react";
import { SharedView } from "@dimah-s3/ui/share";
import { s3Client } from "@/lib/s3-client";

export function SharedPage({
  token,
  link,
}: {
  token: string;
  link?: ShareVisitorLink;
}) {
  const visit = useShareVisit(s3Client.share, token, { link });
  return <SharedView visit={visit} />;
}

Visitors have no session: let the public share actions through your guard with isSharePublicAction.

Pass link from s3.share.api.openLink to useShareVisit to render the file or folder without a round trip. A locked link still shows the password form.

app/s/[token]/page.tsx
import { getS3 } from "@/lib/s3";

const { link } = await getS3().share.api.openLink({ token });
return <SharedPage token={token} link={link} />;

The default header shows the name, the expiry, and the downloads left, and counts down after each download. Pass header to render your own, such as who sent the link:

<SharedView
  visit={visit}
  header={(link) => (
    <h1>{link.type === "folder" ? link.folder.name : link.object.name}</h1>
  )}
/>

Errors

Every refusal reads through useFormatError, so SHARE_LINK_EXPIRED reads "This link has expired", in your translations. A link that stops opening while the page is open, such as one revoked or out of downloads, turns into that message on the next download or listing. One whose password proof expired asks for the password again. Any other refused download, such as TOO_MANY_REQUESTS, shows above the file, and a network error offers Try again.

Accessibility

  • The password field is labelled "This file needs a password" or "This folder needs a password". A wrong password is announced with role="alert", and focus returns to the field with the text selected.
  • Once the password opens the link, focus moves to what it opened.
  • Each file's download button in a folder is named after the file, and is busy while its download starts.
  • "Download started" is announced in a polite live region once the browser has the file's URL, and the button shows a check for two seconds.
  • The downloads left are read out when a download changes them.

API Reference

SharedView

PropTypeDefaultDescription
visitUseShareVisitReturnrequireduseShareVisit for the token in the page URL
header(link: ShareVisitorOpenLink) => ReactNode—Replaces the default header once the link opens
rootLabelstringthe folder's nameThe top of a shared folder in its breadcrumb
heightstring"32rem"Height of a shared folder's list
classNamestring—Classes on the outer element

On this page