# Building a UI – Document Manager

Folder pages, slug paths, breadcrumbs and download links in Next.js.

This page builds folder pages from the tree: resolving an address, breadcrumbs and a page that shows a folder or a download link. It uses the types and `toDocItem` from [TypeScript](https://contentful.geeconglobal.com/apps/document-manager/docs/typescript).

## Building a UI

The sample's folder page is one route, `/[library]/[[...path]]`. The first segment is the library's slug. The rest are folder slugs, and the last can be a file.

### Resolve a slug path

Walk down from the library, matching each slug among the current item's children. It returns the item and the trail of items above it, or `null` for a bad address:

```ts
export function resolvePath(library: DocLibrary, slugs: string[]) {
  const trail: DocItem[] = [library];
  let item: DocItem = library;
  for (const slug of slugs) {
    const next: DocItem | undefined = item.kind === "file" ? undefined : item.items.find((i) => i.slug === slug);
    if (!next) return null;
    trail.push(next);
    item = next;
  }
  return { item, trail };
}
```

### Breadcrumbs

Each crumb links to the path up to that point:

```ts
export function breadcrumbs(trail: DocItem[]) {
  return trail.map((item, i) => ({
    title: item.title,
    href: `/${trail
      .slice(0, i + 1)
      .map((t) => t.slug)
      .join("/")}`,
  }));
}
```

### A folder page

In Next.js 16, `params` is a Promise. The page fetches the library, resolves the path and renders a folder's items or a file's download link:

```tsx
export default async function Page({ params }: { params: Promise<{ library: string; path?: string[] }> }) {
  const { library: slug, path = [] } = await params;
  const library = await getLibrary(slug);
  const found = library && resolvePath(library, path);
  if (!found) notFound();
  const { item, trail } = found;

  return (
    <>
      <Breadcrumbs trail={trail} />
      <h1>{item.title}</h1>
      {item.kind === "file" ? (
        item.file ? (
          <p>
            <a href={item.file.url} download>
              Download {item.file.fileName}
            </a>{" "}
            <small>{fileLabel(item.file)}</small>
          </p>
        ) : (
          <p>No file attached.</p>
        )
      ) : (
        <ItemList items={item.items} base={`/${trail.map((t) => t.slug).join("/")}`} />
      )}
    </>
  );
}
```

`getLibrary` is the fetch from [Fetching a library or folder](https://contentful.geeconglobal.com/apps/document-manager/docs/fetching#fetching-a-library-or-folder), then `fetchUnresolved`, then `toDocItem`. `fileLabel` and `ItemList` are in the sample. The sample renders at request time, so it needs no credentials to build. It caches each library for 60 seconds with Next.js `unstable_cache`. A library with no slug is found by its entry ID instead.

## Files are public

Asset URLs are public. Don't use the tree as access control. See [When to use it](https://contentful.geeconglobal.com/apps/document-manager/docs/when-to-use).
