@anthusai/papyrus 1.19.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +1,24 @@
1
1
  "use client";
2
2
 
3
- import {
4
- BookOpenIcon,
5
- ClipboardListIcon,
6
- FileTextIcon,
7
- LayoutGridIcon,
8
- MenuIcon,
9
- SearchIcon,
10
- TagsIcon,
11
- } from "lucide-react";
12
- import Link from "next/link";
13
- import type { ReactNode } from "react";
14
- import { useMemo, useState } from "react";
15
- import { Badge } from "./ui/badge";
16
- import { Button, buttonVariants } from "./ui/button";
17
- import { ScrollArea } from "./ui/scroll-area";
3
+ import { SearchIcon } from "lucide-react";
4
+ import { Button } from "./ui/button";
18
5
  import { Separator } from "./ui/separator";
19
- import {
20
- Sheet,
21
- SheetContent,
22
- SheetHeader,
23
- SheetTitle,
24
- SheetTrigger,
25
- } from "./ui/sheet";
26
- import {
27
- getNewsroomNavHref,
28
- NEWSROOM_MOBILE_PRIMARY_NAV,
29
- NEWSROOM_OPS_NAV,
30
- type NewsroomNavTabId,
31
- } from "../lib/newsroom-nav";
32
- import {
33
- NEWSROOM_SIDEBAR_NAV_ACTIVE,
34
- NEWSROOM_SIDEBAR_NAV_INACTIVE,
35
- } from "../lib/newsroom-list-selection";
36
6
  import { cn } from "../lib/utils";
7
+
8
+ import { useEffect, useId, useState, type ReactNode } from "react";
9
+ import Link from "next/link";
10
+ import { usePathname, useRouter, useSearchParams } from "next/navigation";
11
+ import { BookOpen, ClipboardList, Menu, Search, MessagesSquare, PanelLeftClose, PanelLeftOpen, Settings, UserRound, LogIn, LogOut } from "lucide-react";
12
+ import { ORGANIZER_NAV_GROUPS, ORGANIZER_PRIMARY_NAV, ORGANIZER_SETTINGS_DESTINATION } from "../lib/newsroom-organizer-nav";
13
+ import { browserPathToInternalNewsroomPath } from "../lib/newsroom-base-path";
14
+ import { getNewsroomNavHref } from "../lib/newsroom-nav";
37
15
  import { SITE_BRAND } from "../lib/site-brand";
38
- import { NewsroomOrganizerShell } from "./newsroom-organizer-shell";
16
+ import { useOptionalNewsDeskClient } from "./news-desk-client-provider";
17
+ import { ReaderAuthControl } from "./reader-auth-control";
18
+ import { ProspectusAccountChooser, useProspectusAccountSummary } from "./prospectus-account-chooser";
19
+ import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription, SheetTrigger } from "./ui/sheet";
20
+ import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuItem, DropdownMenuSeparator } from "./ui/dropdown-menu";
21
+ import "./newsroom-organizer.css";
39
22
 
40
23
  export type NewsroomNavCount = {
41
24
  count: number | null;
@@ -44,7 +27,7 @@ export type NewsroomNavCount = {
44
27
  };
45
28
 
46
29
  export type NewsroomOpsShellProps = {
47
- activeTab: NewsroomNavTabId | string;
30
+ activeTab: string;
48
31
  appTitle: string;
49
32
  backHref?: string;
50
33
  backLabel: string;
@@ -56,215 +39,140 @@ export type NewsroomOpsShellProps = {
56
39
  tabCounts?: Partial<Record<string, NewsroomNavCount>>;
57
40
  };
58
41
 
59
- const MOBILE_TAB_ICONS: Record<string, typeof LayoutGridIcon> = {
60
- overview: LayoutGridIcon,
61
- articles: FileTextIcon,
62
- assignments: ClipboardListIcon,
63
- references: BookOpenIcon,
64
- topics: TagsIcon,
65
- };
66
-
67
- function formatCountLabel(count: number | null, missing?: boolean): string {
68
- if (missing) return "?";
69
- if (count == null) return "…";
70
- if (count >= 1000) return `${Math.round(count / 100) / 10}k`;
71
- return String(count);
42
+ /** The standard place to search the knowledge base; the Search page is the results page. */
43
+ function OrganizerSearch({ demo }: { demo: boolean }) {
44
+ const router = useRouter();
45
+ const searchParams = useSearchParams();
46
+ const [text, setText] = useState(searchParams.get("q") ?? "");
47
+ return <form className="organizer-search" role="search" onSubmit={event => {
48
+ event.preventDefault();
49
+ const query = text.trim();
50
+ if (query) router.push(getNewsroomNavHref(`/newsroom/search?q=${encodeURIComponent(query)}`, demo));
51
+ }}>
52
+ <Search size={14} aria-hidden="true" />
53
+ <input aria-label="Search the knowledge base" data-newsroom-search type="search" onChange={event => setText(event.target.value)} placeholder="Search" value={text} />
54
+ </form>;
72
55
  }
73
56
 
74
- function NavCountBadge({
75
- countState,
76
- }: {
77
- countState?: NewsroomNavCount;
78
- }) {
79
- if (!countState || countState.visible === false) return null;
80
- return (
81
- <Badge
82
- className="newsroom-nav-count min-w-7 px-1.5"
83
- data-newsroom-nav-count
84
- variant={countState.missing ? "outline" : "secondary"}
85
- >
86
- {formatCountLabel(countState.count, countState.missing)}
87
- </Badge>
88
- );
57
+ const ICONS = { articles: ClipboardList, references: BookOpen, messages: MessagesSquare };
58
+
59
+ /** Navigation is a presentation layer; existing route and server authorization still own access. */
60
+ function Navigation({ activePath, demo, admin, surface, onNavigate }: { activePath: string; demo: boolean; admin: boolean; surface: string; onNavigate?: () => void }) {
61
+ const id = useId();
62
+ const all = ORGANIZER_NAV_GROUPS.flatMap(group => group.items);
63
+ const active = all.filter(item => [item.path, ...(item.alsoActiveOnPaths ?? [])].some(candidate => activePath === candidate || (candidate !== "/newsroom" && activePath.startsWith(candidate + "/")))).sort((a, b) => b.path.length - a.path.length)[0]?.key;
64
+ return <nav className="organizer-destinations" aria-label={surface === "sidebar" ? "Newsroom" : "All workspaces"}>
65
+ {ORGANIZER_NAV_GROUPS.map((group, index) => <section key={group.title} aria-labelledby={`${id}-${index}`}>
66
+ <h3 id={`${id}-${index}`}>{group.title}</h3>
67
+ {group.items.filter(item => item.access !== "admin" || admin).map(item => <Link key={item.key}
68
+ href={getNewsroomNavHref(item.path, demo)} onClick={onNavigate} aria-label={item.label}
69
+ aria-current={active === item.key ? "page" : undefined} aria-describedby={`${id}-${item.key}`}
70
+ data-news-desk-tab={item.key}>
71
+ <strong>{item.label}</strong><small id={`${id}-${item.key}`}>{item.description}</small>
72
+ </Link>)}
73
+ </section>)}
74
+ </nav>;
89
75
  }
90
76
 
91
- function SidebarNavLink({
92
- active,
93
- countState,
94
- demo,
95
- item,
96
- onNavigate,
97
- }: {
98
- active: boolean;
99
- countState?: NewsroomNavCount;
100
- demo?: boolean;
101
- item: (typeof NEWSROOM_OPS_NAV)[number];
102
- onNavigate?: () => void;
103
- }) {
104
- return (
105
- <Link
106
- aria-current={active ? "page" : undefined}
107
- className={cn(
108
- buttonVariants({ variant: "ghost", size: "default" }),
109
- "h-auto min-h-11 w-full justify-start gap-3 px-3 py-2.5 text-left font-normal",
110
- active ? NEWSROOM_SIDEBAR_NAV_ACTIVE : NEWSROOM_SIDEBAR_NAV_INACTIVE,
111
- )}
112
- data-news-desk-tab={item.id}
113
- href={getNewsroomNavHref(item.href, demo)}
114
- onClick={onNavigate}
115
- >
116
- <NavCountBadge countState={countState} />
117
- <span className="min-w-0 flex-1">
118
- <span className="block text-sm font-medium">{item.label}</span>
119
- <span className={cn("block text-xs", active ? "text-foreground/70" : "text-muted-foreground")}>{item.detail}</span>
120
- </span>
121
- </Link>
122
- );
77
+ function Account({ placement }: { placement: "sidebar" | "header" }) {
78
+ const session = useOptionalNewsDeskClient();
79
+ const auth = session?.shell.auth ?? { status: "loading" as const, label: "Checking sign-in" };
80
+ const [open, setOpen] = useState(false);
81
+ const accountSummary = useProspectusAccountSummary();
82
+ useEffect(() => {
83
+ const media = matchMedia("(max-width: 900px)");
84
+ const close = () => setOpen(false);
85
+ media.addEventListener("change", close);
86
+ return () => media.removeEventListener("change", close);
87
+ }, []);
88
+ return <DropdownMenu open={open} onOpenChange={setOpen}>
89
+ <DropdownMenuTrigger id={`organizer-account-trigger-${placement}`} className={`organizer-account organizer-account-${placement}`} aria-label="Account menu" title="Account menu">
90
+ <UserRound size={18} aria-hidden="true" /><span><strong>{auth.label}</strong><small>{accountSummary ?? (auth.status === "signedIn" ? "Account & preferences" : auth.status === "signedOut" ? "Not signed in" : "Checking session")}</small></span>
91
+ </DropdownMenuTrigger>
92
+ <DropdownMenuContent className="organizer-account-popup" side={placement === "sidebar" ? "top" : "bottom"} align="end">
93
+ <DropdownMenuGroup>
94
+ <DropdownMenuLabel>{auth.label}</DropdownMenuLabel><DropdownMenuSeparator />
95
+ <ProspectusAccountChooser />
96
+ <DropdownMenuItem render={<Link href={getNewsroomNavHref(ORGANIZER_SETTINGS_DESTINATION.path)} />}><Settings aria-hidden="true" />{ORGANIZER_SETTINGS_DESTINATION.label}</DropdownMenuItem>
97
+ <ReaderAuthControl authState={auth} renderAction={({ disabled, onClick, signedIn }) => <DropdownMenuItem disabled={disabled} closeOnClick={false} onClick={onClick}>
98
+ {signedIn ? <LogOut aria-hidden="true" /> : <LogIn aria-hidden="true" />}{signedIn ? "Sign out" : "Sign in"}
99
+ </DropdownMenuItem>} />
100
+ </DropdownMenuGroup>
101
+ </DropdownMenuContent>
102
+ </DropdownMenu>;
123
103
  }
124
104
 
125
- export function NewsroomOpsShell({
126
- activeTab,
127
- appTitle,
128
- backHref = "/",
129
- backLabel,
130
- children,
131
- demo = false,
132
- headerActions,
133
- pageTitle,
134
- showNavigation = true,
135
- tabCounts = {},
136
- }: NewsroomOpsShellProps) {
105
+ export function NewsroomOpsShell({ activeTab, appTitle, backHref = "/", backLabel, children, demo = false, headerActions, pageTitle, showNavigation = true }: NewsroomOpsShellProps) {
106
+ const pathname = usePathname();
107
+ const session = useOptionalNewsDeskClient();
108
+ const admin = demo || session?.shell.dashboard?.canManageUsers === true;
137
109
  const [menuOpen, setMenuOpen] = useState(false);
138
- const resolvedActiveTab = activeTab === "desks" ? "topics" : activeTab;
139
- const mobilePrimary = useMemo(() => NEWSROOM_MOBILE_PRIMARY_NAV, []);
140
-
141
- if (SITE_BRAND.newsroomLayout === "organizer") return <NewsroomOrganizerShell activeTab={activeTab} appTitle={appTitle}
142
- backHref={backHref} backLabel={backLabel} demo={demo} headerActions={headerActions} pageTitle={pageTitle}
143
- showNavigation={showNavigation} tabCounts={tabCounts}>{children}</NewsroomOrganizerShell>;
144
-
145
- return (
146
- <div
147
- className="flex h-dvh max-h-dvh flex-col overflow-hidden bg-background text-foreground"
148
- data-news-desk
149
- data-newsroom-chrome="app"
150
- data-newsroom-ops-shell="true"
151
- >
152
- <header
153
- className="flex h-14 shrink-0 items-center gap-3 border-b border-border bg-card/50 px-3 sm:px-4"
154
- data-newsroom-ops-topbar
155
- >
156
- <Link
157
- className={cn(buttonVariants({ variant: "ghost", size: "sm" }), "shrink-0 px-2")}
158
- href={backHref}
159
- >
160
- {backLabel}
161
- </Link>
162
- <div className="min-w-0 flex-1">
163
- <p className="truncate text-[0.65rem] font-semibold uppercase tracking-[0.12em] text-muted-foreground">
164
- {appTitle}
165
- </p>
166
- <h1 className="truncate text-base font-semibold tracking-tight text-foreground" data-newsroom-ops-title>
167
- {pageTitle}
168
- </h1>
169
- </div>
170
- <div className="flex shrink-0 items-center gap-1.5">{headerActions}</div>
171
- </header>
172
-
173
- <div className="flex min-h-0 flex-1">
174
- {showNavigation ? (
175
- <aside
176
- aria-label="Newsroom sections"
177
- className="hidden w-64 shrink-0 flex-col border-r border-border bg-muted/20 md:flex"
178
- data-newsroom-ops-sidebar
179
- >
180
- <ScrollArea className="min-h-0 flex-1 p-3">
181
- <nav className="flex flex-col gap-1">
182
- {NEWSROOM_OPS_NAV.map((item) => (
183
- <SidebarNavLink
184
- active={resolvedActiveTab === item.id}
185
- countState={tabCounts[item.id]}
186
- demo={demo}
187
- item={item}
188
- key={item.id}
189
- />
190
- ))}
191
- </nav>
192
- </ScrollArea>
193
- </aside>
194
- ) : null}
195
-
196
- <main className="flex min-h-0 min-w-0 flex-1 flex-col">
197
- <ScrollArea className="min-h-0 flex-1">
198
- <div className="mx-auto w-full max-w-6xl p-4 sm:p-6">{children}</div>
199
- </ScrollArea>
200
- </main>
110
+ const [collapsed, setCollapsed] = useState(false);
111
+ const [loadedPreference, setLoadedPreference] = useState(false);
112
+ const preferenceKey = `papyrus.cms.sidebar.v1.${SITE_BRAND.id}`;
113
+ useEffect(() => {
114
+ try { setCollapsed(localStorage.getItem(preferenceKey) === "true"); } catch { /* Optional browser preference. */ }
115
+ setLoadedPreference(true);
116
+ }, [preferenceKey]);
117
+ useEffect(() => { if (loadedPreference) try { localStorage.setItem(preferenceKey, String(collapsed)); } catch { /* Controls still work without storage. */ } }, [collapsed, loadedPreference, preferenceKey]);
118
+ useEffect(() => { setMenuOpen(false); }, [pathname]);
119
+ useEffect(() => {
120
+ const media = matchMedia("(max-width: 900px)");
121
+ let previousFocus: Element | null = null;
122
+ let frame = 0;
123
+ const remember = () => { if (document.activeElement !== document.body) previousFocus = document.activeElement; };
124
+ const change = () => {
125
+ const active = document.activeElement === document.body ? previousFocus : document.activeElement;
126
+ if (!(active instanceof HTMLElement)) return;
127
+ if (!media.matches) setMenuOpen(false);
128
+ const selector = active.closest(".organizer-account, .organizer-account-popup") ? ".organizer-account"
129
+ : media.matches && active.closest(".organizer-sidebar, .organizer-toggle") ? ".organizer-more"
130
+ : !media.matches && active.closest(".organizer-bottom-nav, .organizer-menu") ? ".organizer-toggle" : null;
131
+ if (!selector) return;
132
+ cancelAnimationFrame(frame);
133
+ frame = requestAnimationFrame(() => {
134
+ if (document.activeElement === active || document.activeElement === document.body || !(document.activeElement as HTMLElement)?.getClientRects().length)
135
+ Array.from(document.querySelectorAll<HTMLElement>(selector)).find(element => element.getClientRects().length)?.focus();
136
+ });
137
+ };
138
+ document.addEventListener("focusin", remember);
139
+ media.addEventListener("change", change);
140
+ return () => { document.removeEventListener("focusin", remember); media.removeEventListener("change", change); cancelAnimationFrame(frame); };
141
+ }, []);
142
+ const activePath = browserPathToInternalNewsroomPath(pathname ?? "/newsroom");
143
+ const sidebarToggleLabel = collapsed ? "Expand sidebar" : "Collapse sidebar";
144
+ const sidebarToggle = <button type="button" className="organizer-toggle" title={sidebarToggleLabel}
145
+ aria-label={sidebarToggleLabel} aria-expanded={!collapsed} aria-controls="organizer-sidebar" onClick={() => setCollapsed(value => !value)}>
146
+ {collapsed ? <PanelLeftOpen size={16} aria-hidden="true" /> : <PanelLeftClose size={16} aria-hidden="true" />}
147
+ </button>;
148
+ return <div className="newsroom-organizer" data-news-desk data-newsroom-ops-shell="true" data-newsroom-chrome="organizer" data-sidebar-collapsed={collapsed || !showNavigation ? "true" : "false"}>
149
+ {showNavigation && <aside className="organizer-sidebar" hidden={collapsed} id="organizer-sidebar">
150
+ <div className="organizer-sidebar-head">
151
+ <Link className="organizer-brand" href={getNewsroomNavHref("/newsroom", demo)}>{SITE_BRAND.components?.NewsroomLogo ? <SITE_BRAND.components.NewsroomLogo className="organizer-brand-logo" /> : appTitle}<small>The newsroom</small></Link>
152
+ {sidebarToggle}
201
153
  </div>
202
-
203
- {showNavigation ? (
204
- <nav
205
- aria-label="Primary newsroom navigation"
206
- className="shrink-0 border-t border-border bg-card/95 pb-[max(env(safe-area-inset-bottom),0.35rem)] pt-1 md:hidden"
207
- data-newsroom-ops-bottom-nav
208
- >
209
- <ul className="grid grid-cols-6 gap-1 px-1">
210
- {mobilePrimary.map((item) => {
211
- const Icon = MOBILE_TAB_ICONS[item.id] ?? LayoutGridIcon;
212
- const active = resolvedActiveTab === item.id;
213
- return (
214
- <li key={item.id}>
215
- <Link
216
- aria-current={active ? "page" : undefined}
217
- className={cn(
218
- "flex min-h-14 flex-col items-center justify-center gap-1 rounded-xl px-1 py-2 text-[0.68rem] font-medium",
219
- active
220
- ? "bg-primary text-primary-foreground shadow-sm"
221
- : "text-muted-foreground",
222
- )}
223
- data-news-desk-tab={item.id}
224
- href={getNewsroomNavHref(item.href, demo)}
225
- >
226
- <Icon className="size-4" aria-hidden="true" />
227
- <span>{item.label}</span>
228
- </Link>
229
- </li>
230
- );
231
- })}
232
- <li>
233
- <Sheet onOpenChange={setMenuOpen} open={menuOpen}>
234
- <SheetTrigger
235
- className={cn(
236
- "flex min-h-14 w-full flex-col items-center justify-center gap-1 rounded-xl px-1 py-2 text-[0.68rem] font-medium text-muted-foreground",
237
- )}
238
- >
239
- <MenuIcon className="size-4" aria-hidden="true" />
240
- <span>More</span>
241
- </SheetTrigger>
242
- <SheetContent className="gap-0 p-0" side="bottom">
243
- <SheetHeader className="border-b border-border px-4 py-4">
244
- <SheetTitle>All sections</SheetTitle>
245
- </SheetHeader>
246
- <ScrollArea className="max-h-[60dvh] p-3">
247
- <div className="flex flex-col gap-1">
248
- {NEWSROOM_OPS_NAV.map((item) => (
249
- <SidebarNavLink
250
- active={resolvedActiveTab === item.id}
251
- countState={tabCounts[item.id]}
252
- demo={demo}
253
- item={item}
254
- key={item.id}
255
- onNavigate={() => setMenuOpen(false)}
256
- />
257
- ))}
258
- </div>
259
- </ScrollArea>
260
- </SheetContent>
261
- </Sheet>
262
- </li>
263
- </ul>
264
- </nav>
265
- ) : null}
154
+ <Navigation activePath={activePath} demo={demo} admin={admin} surface="sidebar" />
155
+ <Account placement="sidebar" />
156
+ </aside>}
157
+ <div className="organizer-workspace">
158
+ <header className="organizer-topbar" data-newsroom-ops-topbar>
159
+ {showNavigation && collapsed && sidebarToggle}
160
+ <Link className="organizer-masthead" href={backHref} title={backLabel}>{appTitle}</Link>
161
+ <h1 data-newsroom-ops-title>{pageTitle}</h1><OrganizerSearch demo={demo} /><div className="organizer-header-actions">{headerActions}</div><Account placement="header" />
162
+ </header>
163
+ <main className="organizer-main">{demo && <p className="organizer-demo-notice">Demo workspace · synthetic examples</p>}{children}</main>
266
164
  </div>
267
- );
165
+ {showNavigation && <nav className="organizer-bottom-nav" aria-label="Primary newsroom navigation"><ul>
166
+ {ORGANIZER_PRIMARY_NAV.map(item => { const Icon = ICONS[item.key as keyof typeof ICONS]; return <li key={item.key}><Link
167
+ href={getNewsroomNavHref(item.path, demo)} title={item.description} aria-label={item.label} aria-current={activeTab === item.key ? "page" : undefined}>
168
+ <Icon size={18} aria-hidden="true" /><span>{item.label}</span></Link></li>; })}
169
+ <li><Sheet open={menuOpen} onOpenChange={setMenuOpen}><SheetTrigger className="organizer-more" aria-label="More"><Menu size={18} aria-hidden="true" /><span>More</span></SheetTrigger>
170
+ <SheetContent side="bottom" className="organizer-menu"><SheetHeader><SheetTitle>Newsroom</SheetTitle><SheetDescription>Workspaces and administration</SheetDescription></SheetHeader>
171
+ <Navigation activePath={activePath} demo={demo} admin={admin} surface="menu" onNavigate={() => setMenuOpen(false)} />
172
+ </SheetContent>
173
+ </Sheet></li>
174
+ </ul></nav>}
175
+ </div>;
268
176
  }
269
177
 
270
178
  export function NewsroomOpsSearchButton({