- Setup, per people block: each one's role or title under their name (`titles`) - An events list slide: presenters under the titles, columns that flow, shrunk to fit rather than scrolled (Fit) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1097 lines
35 KiB
TypeScript
1097 lines
35 KiB
TypeScript
import { ArrowRight, CaretDown, X } from "@phosphor-icons/react";
|
|
import {
|
|
useCallback,
|
|
useEffect,
|
|
useId,
|
|
useLayoutEffect,
|
|
useMemo,
|
|
useRef,
|
|
useState,
|
|
type CSSProperties,
|
|
type HTMLAttributes,
|
|
} from "react";
|
|
|
|
import { Link } from "react-router-dom";
|
|
|
|
import { get } from "../lib/api.ts";
|
|
import { isBadId, personHref } from "../lib/hrefs.ts";
|
|
import { mediaUrl, initials } from "../lib/media.ts";
|
|
import { useSmallImage } from "../lib/useSmallImage.ts";
|
|
import { paletteFill } from "../lib/color.ts";
|
|
import { useReadingMode } from "../lib/readingMode.ts";
|
|
import PlatformIcon from "./PlatformIcon.tsx";
|
|
import "./PeopleTiles.css";
|
|
|
|
/**
|
|
* PeopleTiles, a horizontal, polaroid-style people list.
|
|
*
|
|
* Supply data any of four ways:
|
|
*
|
|
* <PeopleTiles size="lg" teams="ngu-board" />
|
|
* <PeopleTiles size="md" teams={["ngu-board", "nw-leadership"]} />
|
|
* <PeopleTiles size="sm" people={[
|
|
* { peopleslug: "john-doe", title: "Hospitality" },
|
|
* { peopleslug: "jane-doe" },
|
|
* ]} />
|
|
* <PeopleTiles size="sm" people={volunteers} overflow="scroll" />
|
|
*
|
|
* With `teams`, each team is fetched in the order given and split
|
|
* into a lead group and a members group by the affiliation's
|
|
* is_owner flag.
|
|
*
|
|
* With `peopleslug`, the person is fetched by id and any other key
|
|
* on the entry overrides what came back, so a title can be given
|
|
* per placement, and is blank when it isn't. A slug entry can sit
|
|
* beside a fully-written person in the same array.
|
|
*
|
|
* Sizes
|
|
* sm photo + name
|
|
* md photo + name + title
|
|
* lg photo + name + title, and a chevron on anyone who has
|
|
* something to expand: pronouns, home organization, location,
|
|
* public email or a bio. Not bio alone, the details panel is
|
|
* worth opening before anyone has written prose.
|
|
*
|
|
* At every size the photo is square and the caption a fixed two
|
|
* lines of name (and two of title), so a row's cards are one
|
|
* height. The list as a whole has the frame, not each tile.
|
|
*
|
|
* Rows fill (balancedColumns): as many tiles across as fit, never
|
|
* fewer than 2 or more than 10, one fewer when that would leave a
|
|
* lone tile on the last row. When 2 don't fit at full size they
|
|
* shrink. And when everyone (every group, leads and members alike)
|
|
* fits on one line with tiles at ONE_ROW_SHRINK of full width or
|
|
* wider, they all share that one line instead (fitsOneRow): a
|
|
* board of 7 reads as one row, not 5 and 2.
|
|
*
|
|
* Details open in a panel right under the tile, over what follows
|
|
* (flipped to end at the tile's right edge when it would run off
|
|
* the screen), and close on Escape, the tile again, or a click
|
|
* anywhere else. A scrolling row would clip that panel, so with
|
|
* overflow="scroll" it opens under the whole list instead. What doesn't fit ends in an ellipsis, and that tile gets
|
|
* the chevron too, opening the details with it in full. A person
|
|
* with no photo gets their initials on a palette color
|
|
* (paletteFill in color.ts).
|
|
*
|
|
* Reading mode (readingMode.ts) gets everyone, no "Show all", each
|
|
* a heading (linked to their profile) and their title, facts and bio
|
|
* as lines (ReadingPeople).
|
|
*
|
|
* Field names follow the API (is_owner, location_label), so a row
|
|
* from /api/teams/:id/people drops in unchanged.
|
|
*
|
|
* Profiles
|
|
* A person with a string id is taken to be a people row and links
|
|
* to /people/:id. A tile with nothing to expand is that link; an
|
|
* expandable one stays the button that opens its panel, a link
|
|
* can't sit inside a button, and the panel carries the link
|
|
* instead. A hand-written entry with no id links nowhere.
|
|
*/
|
|
|
|
export interface Person {
|
|
id?: string | number;
|
|
name: string;
|
|
title?: string | null;
|
|
tagline?: string | null;
|
|
photo?: string | null;
|
|
pronouns?: string | null;
|
|
location_label?: string | null;
|
|
public_email?: string | null;
|
|
/** Their website, music and social links (a directory's people). */
|
|
links?: PersonLink[] | null;
|
|
org?: string | { id?: string; name: string; href?: string } | null;
|
|
bio?: string | string[] | null;
|
|
accent?: string;
|
|
role?: string | null;
|
|
is_owner?: boolean;
|
|
/** Accepted as an alias so hand-written entries can use either. */
|
|
isOwner?: boolean;
|
|
}
|
|
|
|
export interface PersonLink {
|
|
kind?: string;
|
|
platform?: string | null;
|
|
label: string;
|
|
url: string;
|
|
}
|
|
|
|
/** A person named by slug. Any other field overrides the record. */
|
|
export interface PersonRef extends Partial<Omit<Person, "name">> {
|
|
peopleslug: string;
|
|
name?: string;
|
|
}
|
|
|
|
export type PersonInput = Person | PersonRef;
|
|
|
|
export interface PeopleGroup {
|
|
id?: string;
|
|
label?: string;
|
|
note?: string;
|
|
accent?: string;
|
|
people: Person[];
|
|
}
|
|
|
|
export interface PeopleGroupInput extends Omit<PeopleGroup, "people"> {
|
|
people: PersonInput[];
|
|
}
|
|
|
|
export interface TeamSpec {
|
|
id: string;
|
|
/** Heading for the members group beside a lead group, or among
|
|
* several teams. Defaults to the team's name. */
|
|
label?: string;
|
|
/** Heading for the lead group. Defaults to the lead's own title. */
|
|
leadLabel?: string;
|
|
/** Set false to keep owners inline with everyone else. */
|
|
splitOwners?: boolean;
|
|
accent?: string;
|
|
}
|
|
|
|
export type TeamSource = string | TeamSpec;
|
|
|
|
export type PeopleTilesSize = "sm" | "md" | "lg";
|
|
|
|
export interface PeopleTilesProps
|
|
extends Omit<HTMLAttributes<HTMLDivElement>, "onSelect"> {
|
|
people?: PersonInput[];
|
|
groups?: PeopleGroupInput[];
|
|
teams?: TeamSource | TeamSource[];
|
|
size?: PeopleTilesSize;
|
|
scale?: number;
|
|
overflow?: "wrap" | "scroll";
|
|
align?: "start" | "center";
|
|
accent?: string;
|
|
tilt?: boolean;
|
|
/** The group labels' heading level, by what they sit under (3 under
|
|
* a section's h2). In reading mode the names are one level under. */
|
|
headingLevel?: 3 | 4 | 5;
|
|
/** Just round photos and names in a row, as presented (095). */
|
|
row?: boolean;
|
|
/** In the row, each one's role or title under their name. */
|
|
titles?: boolean;
|
|
/** How long a fetched team or person is reused, in ms. */
|
|
ttl?: number;
|
|
emptyMessage?: string;
|
|
loadingMessage?: string;
|
|
errorMessage?: string;
|
|
onExpand?: (person: Person | null, group: PeopleGroup | null) => void;
|
|
}
|
|
|
|
/* A list longer than this shows this many tiles, then "Show all". */
|
|
const SHOW_FIRST = 30;
|
|
|
|
const SIZE_FEATURES: Record<PeopleTilesSize, { title: boolean; details: boolean }> = {
|
|
sm: { title: false, details: false },
|
|
md: { title: true, details: false },
|
|
lg: { title: true, details: true },
|
|
};
|
|
|
|
/* ── Data ──────────────────────────────────────────────────────
|
|
Fetching lives here rather than in every section, but the view
|
|
below stays pure, it only ever sees resolved groups, whatever
|
|
produced them.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
export default function PeopleTiles({
|
|
teams,
|
|
groups,
|
|
people,
|
|
ttl = 5 * 60_000,
|
|
loadingMessage = "Loading…",
|
|
errorMessage = "Couldn't load this list right now.",
|
|
row = false,
|
|
titles = false,
|
|
...view
|
|
}: PeopleTilesProps) {
|
|
const View = useMemo(
|
|
() => (row ? (props) => <PeopleRow {...props} titles={titles} /> : PeopleTilesView),
|
|
[row, titles],
|
|
);
|
|
const specs = useMemo(() => normalizeTeams(teams), [teams]);
|
|
const teamKey = specs.map((spec) => spec.id).join(",");
|
|
|
|
// Sorted so two sections naming the same people in a different
|
|
// order still hit the same cached request.
|
|
const slugs = useMemo(
|
|
() => (specs.length ? [] : collectSlugs(groups, people)),
|
|
[specs.length, groups, people],
|
|
);
|
|
const slugKey = slugs.join(",");
|
|
|
|
const [teamGroups, setTeamGroups] = useState<PeopleGroup[] | null>(null);
|
|
const [directory, setDirectory] = useState<Record<string, Person> | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!specs.length) {
|
|
setTeamGroups(null);
|
|
return undefined;
|
|
}
|
|
|
|
let live = true;
|
|
setFailed(false);
|
|
|
|
Promise.all(
|
|
specs.map((spec) =>
|
|
get(`/teams/${spec.id}/people`, { ttl }).then((data: TeamResponse) => ({
|
|
spec,
|
|
data,
|
|
})),
|
|
),
|
|
)
|
|
.then((results) => {
|
|
if (!live) return;
|
|
// Order follows the order the teams were supplied in, not
|
|
// whichever request came back first.
|
|
setTeamGroups(
|
|
results.flatMap(({ spec, data }) => buildTeamGroups(spec, data, specs.length)),
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
if (!live) return;
|
|
console.error("PeopleTiles: couldn't load teams", teamKey, err);
|
|
setFailed(true);
|
|
});
|
|
|
|
return () => {
|
|
live = false;
|
|
};
|
|
}, [teamKey, ttl, specs]);
|
|
|
|
useEffect(() => {
|
|
if (!slugKey) {
|
|
setDirectory(null);
|
|
return undefined;
|
|
}
|
|
|
|
let live = true;
|
|
setFailed(false);
|
|
|
|
get(`/people?ids=${encodeURIComponent(slugKey)}`, { ttl })
|
|
.then((data: { people: Person[] }) => {
|
|
if (!live) return;
|
|
const byId: Record<string, Person> = {};
|
|
for (const person of data.people) byId[String(person.id)] = person;
|
|
setDirectory(byId);
|
|
})
|
|
.catch((err) => {
|
|
if (!live) return;
|
|
console.error("PeopleTiles: couldn't load people", slugKey, err);
|
|
setFailed(true);
|
|
});
|
|
|
|
return () => {
|
|
live = false;
|
|
};
|
|
}, [slugKey, ttl]);
|
|
|
|
if (specs.length) {
|
|
if (failed) return <p className="pl__empty">{errorMessage}</p>;
|
|
if (!teamGroups) return <p className="pl__empty">{loadingMessage}</p>;
|
|
return <View {...view} groups={teamGroups} />;
|
|
}
|
|
|
|
if (slugKey) {
|
|
if (failed) return <p className="pl__empty">{errorMessage}</p>;
|
|
if (!directory) return <p className="pl__empty">{loadingMessage}</p>;
|
|
return (
|
|
<View
|
|
{...view}
|
|
groups={groups?.map((group) => ({
|
|
...group,
|
|
people: resolveAll(group.people, directory),
|
|
}))}
|
|
people={people ? resolveAll(people, directory) : undefined}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<View
|
|
{...view}
|
|
groups={groups as PeopleGroup[] | undefined}
|
|
people={people as Person[] | undefined}
|
|
/>
|
|
);
|
|
}
|
|
|
|
interface TeamResponse {
|
|
team: { id: string; name: string; color?: string | null };
|
|
people: Person[];
|
|
}
|
|
|
|
function normalizeTeams(teams: PeopleTilesProps["teams"]): TeamSpec[] {
|
|
if (!teams) return [];
|
|
const list = Array.isArray(teams) ? teams : [teams];
|
|
return list
|
|
.map((entry) => (typeof entry === "string" ? { id: entry } : entry))
|
|
.filter((spec): spec is TeamSpec => Boolean(spec?.id));
|
|
}
|
|
|
|
function isRef(entry: PersonInput): entry is PersonRef {
|
|
return typeof (entry as PersonRef).peopleslug === "string";
|
|
}
|
|
|
|
function collectSlugs(
|
|
groups?: PeopleGroupInput[],
|
|
people?: PersonInput[],
|
|
): string[] {
|
|
const found = new Set<string>();
|
|
const scan = (list?: PersonInput[]) => {
|
|
for (const entry of list ?? []) {
|
|
if (entry && isRef(entry)) found.add(entry.peopleslug);
|
|
}
|
|
};
|
|
scan(people);
|
|
for (const group of groups ?? []) scan(group.people);
|
|
return [...found].sort();
|
|
}
|
|
|
|
/* The fetched record is the base; anything else on the entry wins,
|
|
including an explicit null, that's how a title is deliberately
|
|
left blank rather than inherited. */
|
|
function resolveAll(
|
|
list: PersonInput[],
|
|
directory: Record<string, Person>,
|
|
): Person[] {
|
|
const resolved: Person[] = [];
|
|
|
|
for (const entry of list) {
|
|
if (!entry) continue;
|
|
|
|
if (!isRef(entry)) {
|
|
resolved.push(entry);
|
|
continue;
|
|
}
|
|
|
|
const base = directory[entry.peopleslug];
|
|
if (!base) {
|
|
// Unpublished, deleted, or a typo in the slug. Leaving the
|
|
// tile out beats rendering a nameless placeholder.
|
|
console.warn(`PeopleTiles: no published person "${entry.peopleslug}"`);
|
|
continue;
|
|
}
|
|
|
|
const { peopleslug, ...overrides } = entry;
|
|
const merged: Person = { ...base };
|
|
for (const [key, value] of Object.entries(overrides)) {
|
|
if (value !== undefined) (merged as any)[key] = value;
|
|
}
|
|
resolved.push(merged);
|
|
}
|
|
|
|
return resolved;
|
|
}
|
|
|
|
function buildTeamGroups(
|
|
spec: TeamSpec,
|
|
data: TeamResponse,
|
|
teamCount: number,
|
|
): PeopleGroup[] {
|
|
const accent = spec.accent ?? data.team.color ?? undefined;
|
|
const name = spec.label ?? data.team.name;
|
|
const split = spec.splitOwners !== false;
|
|
|
|
const owners = split ? data.people.filter(owns) : [];
|
|
const rest = split ? data.people.filter((person) => !owns(person)) : data.people;
|
|
|
|
// A lead only reads as a lead when there's a body of people to
|
|
// stand apart from. All owners, or none, is just a list.
|
|
if (!owners.length || !rest.length) {
|
|
return [
|
|
{
|
|
id: data.team.id,
|
|
// One team alone needs no heading (its page or section names
|
|
// it); several always do. `label` names the members beside a
|
|
// lead group.
|
|
label: teamCount > 1 ? name : undefined,
|
|
accent,
|
|
people: data.people,
|
|
},
|
|
];
|
|
}
|
|
|
|
return [
|
|
{
|
|
id: `${data.team.id}-lead`,
|
|
label: spec.leadLabel ?? leadLabel(owners, name),
|
|
accent,
|
|
people: owners,
|
|
},
|
|
{ id: data.team.id, label: name, accent, people: rest },
|
|
];
|
|
}
|
|
|
|
function leadLabel(owners: Person[], teamName: string): string {
|
|
if (owners.length === 1 && titleOf(owners[0])) return titleOf(owners[0]) as string;
|
|
return `${teamName} lead${owners.length > 1 ? "s" : ""}`;
|
|
}
|
|
|
|
/* ── View ────────────────────────────────────────────────────── */
|
|
|
|
export function PeopleTilesView({
|
|
people,
|
|
groups,
|
|
size = "md",
|
|
scale = 1,
|
|
overflow = "wrap",
|
|
align = "start",
|
|
accent,
|
|
tilt = false,
|
|
emptyMessage = "No one listed yet.",
|
|
headingLevel = 3,
|
|
onExpand,
|
|
className = "",
|
|
style,
|
|
...rest
|
|
}: Omit<
|
|
PeopleTilesProps,
|
|
"teams" | "ttl" | "loadingMessage" | "errorMessage" | "people" | "groups"
|
|
> & {
|
|
people?: Person[];
|
|
groups?: PeopleGroup[];
|
|
}) {
|
|
const baseId = useId().replace(/:/g, "");
|
|
const [openKey, setOpenKey] = useState<string | null>(null);
|
|
// Tiles whose name or title didn't fit and was cut off: they get
|
|
// the down arrow, and the details panel shows it whole.
|
|
const [clipped, setClipped] = useState<ReadonlySet<string>>(() => new Set());
|
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
const probeRef = useRef<HTMLSpanElement>(null);
|
|
const reading = useReadingMode();
|
|
|
|
const resolvedSize: PeopleTilesSize = SIZE_FEATURES[size] ? size : "md";
|
|
const features = SIZE_FEATURES[resolvedSize];
|
|
|
|
const resolvedGroups = useMemo<PeopleGroup[]>(() => {
|
|
const source = groups?.length
|
|
? groups
|
|
: people?.length
|
|
? [{ id: "all", people }]
|
|
: [];
|
|
|
|
return source
|
|
.map((group, groupIndex) => ({
|
|
...group,
|
|
id: group.id ?? `group-${groupIndex}`,
|
|
people: (group.people || []).filter(Boolean),
|
|
}))
|
|
.filter((group) => group.people.length > 0);
|
|
}, [groups, people]);
|
|
|
|
const hasPeople = resolvedGroups.length > 0;
|
|
|
|
// A long list shows its first SHOW_FIRST tiles until "Show all".
|
|
const [showAll, setShowAll] = useState(false);
|
|
const total = resolvedGroups.reduce((sum, group) => sum + group.people.length, 0);
|
|
const shownGroups = useMemo<PeopleGroup[]>(() => {
|
|
if (showAll || total <= SHOW_FIRST) return resolvedGroups;
|
|
let left = SHOW_FIRST;
|
|
const out: PeopleGroup[] = [];
|
|
for (const group of resolvedGroups) {
|
|
if (left <= 0) break;
|
|
out.push({ ...group, people: group.people.slice(0, left) });
|
|
left -= group.people.length;
|
|
}
|
|
return out;
|
|
}, [resolvedGroups, showAll, total]);
|
|
|
|
// How much room a row has, and one tile's width and gap there, in
|
|
// px: the probe carries --pl-w and --pl-gap, so the CSS stays the
|
|
// one place that sizes tiles.
|
|
const [room, setRoom] = useState<{ width: number; tile: number; gap: number } | null>(null);
|
|
|
|
useLayoutEffect(() => {
|
|
const root = rootRef.current;
|
|
const probe = probeRef.current;
|
|
if (!root || !probe || overflow === "scroll") return undefined;
|
|
const measure = () => {
|
|
const padding = parseFloat(getComputedStyle(root).paddingLeft) * 2;
|
|
setRoom({ width: root.clientWidth - padding, tile: probe.offsetWidth, gap: probe.offsetHeight });
|
|
};
|
|
measure();
|
|
const observer = new ResizeObserver(measure);
|
|
observer.observe(root);
|
|
return () => observer.disconnect();
|
|
}, [overflow, hasPeople]);
|
|
|
|
// Which captions are cut off, every tile in one pass: after each
|
|
// render that can change it (the room, the people, the title line)
|
|
// and once the web font has arrived, since it changes text widths
|
|
// without resizing anything.
|
|
const checkClips = useCallback(() => {
|
|
const root = rootRef.current;
|
|
if (!root) return;
|
|
const next = new Set<string>();
|
|
for (const caption of root.querySelectorAll<HTMLElement>("[data-clip-key]")) {
|
|
if (Array.from(caption.children).some((line) => line.scrollHeight > line.clientHeight + 1)) {
|
|
next.add(caption.dataset.clipKey!);
|
|
}
|
|
}
|
|
setClipped((current) =>
|
|
current.size === next.size && [...next].every((key) => current.has(key)) ? current : next,
|
|
);
|
|
}, []);
|
|
|
|
useLayoutEffect(checkClips, [checkClips, room, shownGroups, features.title]);
|
|
|
|
useEffect(() => {
|
|
let live = true;
|
|
document.fonts?.ready.then(() => live && checkClips());
|
|
return () => {
|
|
live = false;
|
|
};
|
|
}, [checkClips]);
|
|
|
|
// A click anywhere outside the list closes the panel.
|
|
useEffect(() => {
|
|
if (!openKey) return undefined;
|
|
const onPointerDown = (event: PointerEvent) => {
|
|
if (!rootRef.current?.contains(event.target as Node)) setOpenKey(null);
|
|
};
|
|
document.addEventListener("pointerdown", onPointerDown);
|
|
return () => document.removeEventListener("pointerdown", onPointerDown);
|
|
}, [openKey]);
|
|
|
|
// Close the panel if the person it belongs to disappears.
|
|
useEffect(() => {
|
|
if (!openKey) return;
|
|
const stillThere = resolvedGroups.some((group) =>
|
|
group.people.some((person, index) => keyFor(group, person, index) === openKey),
|
|
);
|
|
if (!stillThere) setOpenKey(null);
|
|
}, [openKey, resolvedGroups]);
|
|
|
|
if (!resolvedGroups.length) {
|
|
return emptyMessage ? <p className="pl__empty">{emptyMessage}</p> : null;
|
|
}
|
|
if (reading) return <ReadingPeople groups={resolvedGroups} level={headingLevel} />;
|
|
const Label = `h${headingLevel}` as "h3";
|
|
|
|
const open = openKey ? findByKey(resolvedGroups, openKey) : null;
|
|
const oneRow =
|
|
room !== null && overflow !== "scroll" &&
|
|
fitsOneRow(shownGroups.map((group) => group.people.length), room.width, room.tile, room.gap);
|
|
const anchored = overflow !== "scroll";
|
|
// Back to the tile that opened it, as Escape does (below).
|
|
const closePanel = () => {
|
|
rootRef.current?.querySelector<HTMLButtonElement>('.pl__tile[aria-expanded="true"]')?.focus();
|
|
setOpenKey(null);
|
|
onExpand?.(null, null);
|
|
};
|
|
|
|
function toggle(group: PeopleGroup, person: Person, index: number) {
|
|
const key = keyFor(group, person, index);
|
|
const next = openKey === key ? null : key;
|
|
setOpenKey(next);
|
|
onExpand?.(next ? person : null, next ? group : null);
|
|
}
|
|
|
|
function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
|
|
if (event.key === "Escape" && openKey) {
|
|
event.stopPropagation();
|
|
setOpenKey(null);
|
|
const button = rootRef.current?.querySelector<HTMLButtonElement>(
|
|
'.pl__tile[aria-expanded="true"]',
|
|
);
|
|
button?.focus();
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div
|
|
ref={rootRef}
|
|
className={`pl ${className}`.trim()}
|
|
data-size={resolvedSize}
|
|
data-overflow={overflow}
|
|
data-align={align}
|
|
data-tilt={tilt ? "on" : "off"}
|
|
style={
|
|
{
|
|
...(scale !== 1 ? { "--pl-scale": scale } : null),
|
|
...(accent ? { "--pl-accent": accent } : null),
|
|
...style,
|
|
} as CSSProperties
|
|
}
|
|
onKeyDown={handleKeyDown}
|
|
{...rest}
|
|
>
|
|
<span ref={probeRef} className="pl__probe" aria-hidden="true" />
|
|
<div className="pl__groups" data-row={oneRow ? "one" : undefined}>
|
|
{shownGroups.map((group) => (
|
|
<section
|
|
key={group.id}
|
|
className="pl__group"
|
|
style={
|
|
{
|
|
...(group.accent ? { "--pl-accent": group.accent } : null),
|
|
// Sharing one line: each group's width follows its head count.
|
|
...(oneRow ? { "--pl-share": group.people.length } : null),
|
|
} as CSSProperties
|
|
}
|
|
aria-label={group.label || undefined}
|
|
>
|
|
{(group.label || group.note) && (
|
|
<header className="pl__group-head">
|
|
{group.label && <Label className="pl__group-label">{group.label}</Label>}
|
|
{group.note && <p className="pl__group-note">{group.note}</p>}
|
|
</header>
|
|
)}
|
|
|
|
<ul
|
|
className="pl__row"
|
|
style={
|
|
room
|
|
? ({
|
|
"--pl-cols": oneRow
|
|
? group.people.length
|
|
: balancedColumns(group.people.length, room.width, room.tile, room.gap),
|
|
} as CSSProperties)
|
|
: undefined
|
|
}
|
|
>
|
|
{group.people.map((person, index) => {
|
|
const key = keyFor(group, person, index);
|
|
const expandable = (features.details && hasDetails(person)) || clipped.has(key);
|
|
const isOpen = expandable && openKey === key;
|
|
|
|
return (
|
|
<li
|
|
key={key}
|
|
className="pl__item"
|
|
style={
|
|
person.accent
|
|
? ({ "--pl-accent": person.accent } as CSSProperties)
|
|
: undefined
|
|
}
|
|
>
|
|
<Tile
|
|
person={person}
|
|
showTitle={features.title}
|
|
expandable={expandable}
|
|
isOpen={isOpen}
|
|
panelId={`${baseId}-bio`}
|
|
onToggle={() => toggle(group, person, index)}
|
|
clipKey={key}
|
|
/>
|
|
{isOpen && anchored && open && (
|
|
<DetailPanel
|
|
id={`${baseId}-bio`}
|
|
person={open.person}
|
|
group={open.group}
|
|
anchored
|
|
onClose={closePanel}
|
|
/>
|
|
)}
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</section>
|
|
))}
|
|
</div>
|
|
|
|
{!showAll && total > SHOW_FIRST && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowAll(true)}
|
|
className="btn mt-4 border border-brand/40 px-5 py-2 text-sm text-link hover:border-brand"
|
|
>
|
|
Show all {total}
|
|
</button>
|
|
)}
|
|
|
|
{open && !anchored && (
|
|
<DetailPanel
|
|
id={`${baseId}-bio`}
|
|
person={open.person}
|
|
group={open.group}
|
|
onClose={closePanel}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Tile({
|
|
person,
|
|
showTitle,
|
|
expandable,
|
|
isOpen,
|
|
panelId,
|
|
onToggle,
|
|
clipKey,
|
|
}: {
|
|
person: Person;
|
|
showTitle: boolean;
|
|
expandable: boolean;
|
|
isOpen: boolean;
|
|
panelId: string;
|
|
onToggle: () => void;
|
|
clipKey: string;
|
|
}) {
|
|
const title = titleOf(person);
|
|
|
|
// Every caption is the same height (two lines of name, two of
|
|
// title), so the row stays even; a name or title that needs more
|
|
// is cut with an ellipsis, and the list (checkClips) gives it the
|
|
// down arrow.
|
|
const content = (
|
|
<span className="pl__frame">
|
|
<span className="pl__photo">
|
|
<Photo src={mediaUrl(person.photo)} name={person.name} />
|
|
</span>
|
|
<span className="pl__caption" data-clip-key={clipKey} data-title={showTitle ? "on" : "off"}>
|
|
<span className="pl__name">{person.name}</span>
|
|
{showTitle && title && <span className="pl__title">{title}</span>}
|
|
</span>
|
|
{expandable && (
|
|
<span className="pl__badge" aria-hidden="true">
|
|
<Chevron />
|
|
</span>
|
|
)}
|
|
</span>
|
|
);
|
|
|
|
if (!expandable) {
|
|
const href = profileHref(person);
|
|
return href ? (
|
|
<Link to={href} className="pl__tile pl__tile--link">
|
|
{content}
|
|
</Link>
|
|
) : (
|
|
<div className="pl__tile">{content}</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
className="pl__tile pl__tile--button"
|
|
aria-expanded={isOpen}
|
|
aria-controls={panelId}
|
|
onClick={onToggle}
|
|
>
|
|
{content}
|
|
<span className="pl__sr">{isOpen ? "Hide details" : "Read more"}</span>
|
|
</button>
|
|
);
|
|
}
|
|
|
|
/* Everyone in the groups, then the people, as round photos over
|
|
their names, left to right: a people block presented (095). */
|
|
function PeopleRow({ groups, people, titles = false }: { groups?: PeopleGroup[]; people?: Person[]; titles?: boolean }) {
|
|
const everyone = [...(groups ?? []).flatMap((group) => group.people), ...(people ?? [])];
|
|
return (
|
|
<ul className="flex flex-wrap justify-center gap-6">
|
|
{everyone.map((person, i) => (
|
|
<li key={person.id ?? i} className="flex w-28 flex-col items-center text-center">
|
|
<span className="block h-24 w-24 overflow-hidden rounded-full">
|
|
<Photo src={mediaUrl(person.photo)} name={person.name} />
|
|
</span>
|
|
<span className="mt-2 text-sm font-semibold leading-tight text-ink">{person.name}</span>
|
|
{titles && person.title && <span className="mt-0.5 text-xs leading-tight text-muted">{person.title}</span>}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
);
|
|
}
|
|
|
|
function Photo({ src, name }: { src?: string | null; name: string }) {
|
|
const img = useSmallImage(src);
|
|
|
|
// No photo: the initials on a palette color picked by the name.
|
|
if (img.failed) {
|
|
return (
|
|
<span className="pl__initials" style={paletteFill(name)} aria-hidden="true">
|
|
{initials(name)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<img
|
|
className="pl__img"
|
|
src={img.src}
|
|
alt=""
|
|
loading="lazy"
|
|
decoding="async"
|
|
onError={img.onError}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function DetailPanel({
|
|
id,
|
|
person,
|
|
group,
|
|
anchored = false,
|
|
onClose,
|
|
}: {
|
|
id: string;
|
|
person: Person;
|
|
group: PeopleGroup;
|
|
anchored?: boolean;
|
|
onClose: () => void;
|
|
}) {
|
|
const panelRef = useRef<HTMLDivElement>(null);
|
|
// How far the panel shifts from the tile's left edge, and where the
|
|
// notch sits on it, in px.
|
|
const [place, setPlace] = useState<{ shift: number; notch: number } | null>(null);
|
|
|
|
// Under the tile, starting at its left edge, slid left just enough
|
|
// to stay 8px inside the screen on either side (a narrow phone
|
|
// included), with the notch kept under the tile's middle.
|
|
useLayoutEffect(() => {
|
|
if (!anchored || !panelRef.current) return;
|
|
const tile = panelRef.current.parentElement?.getBoundingClientRect();
|
|
if (!tile) return;
|
|
const width = panelRef.current.offsetWidth;
|
|
const screen = document.documentElement.clientWidth;
|
|
let shift = Math.min(0, screen - 8 - (tile.left + width));
|
|
shift = Math.max(shift, 8 - tile.left);
|
|
const notch = Math.min(Math.max(tile.width / 2 - shift - 8, 8), width - 24);
|
|
setPlace({ shift, notch });
|
|
}, [anchored]);
|
|
|
|
const org = resolveOrg(person.org);
|
|
const title = titleOf(person);
|
|
const paragraphs = Array.isArray(person.bio) ? person.bio : [person.bio];
|
|
const tint = person.accent || group?.accent;
|
|
const profile = profileHref(person);
|
|
|
|
return (
|
|
<div
|
|
ref={panelRef}
|
|
id={id}
|
|
className={anchored ? "pl__bio pl__bio--anchored" : "pl__bio"}
|
|
style={
|
|
{
|
|
...(tint ? { "--pl-accent": tint } : null),
|
|
...(place ? { left: `${place.shift}px`, "--pl-notch": `${place.notch}px` } : null),
|
|
} as CSSProperties
|
|
}
|
|
role="region"
|
|
aria-label={`About ${person.name}`}
|
|
>
|
|
<div className="pl__bio-head">
|
|
<div>
|
|
<p className="pl__bio-name">{person.name}</p>
|
|
{title && <p className="pl__bio-title">{title}</p>}
|
|
</div>
|
|
<button type="button" className="pl__close" onClick={onClose}>
|
|
<span className="pl__sr">Close details</span>
|
|
<X size={18} weight="bold" aria-hidden="true" />
|
|
</button>
|
|
</div>
|
|
|
|
<dl className="pl__facts">
|
|
{person.pronouns && (
|
|
<div className="pl__fact">
|
|
<dt>Pronouns</dt>
|
|
<dd>{person.pronouns}</dd>
|
|
</div>
|
|
)}
|
|
{org && (
|
|
<div className="pl__fact">
|
|
<dt>Home organization</dt>
|
|
<dd>
|
|
{org.href ? (
|
|
<a href={org.href} target="_blank" rel="noreferrer">
|
|
{org.name}
|
|
</a>
|
|
) : (
|
|
org.name
|
|
)}
|
|
</dd>
|
|
</div>
|
|
)}
|
|
{person.location_label && (
|
|
<div className="pl__fact">
|
|
<dt>Based in</dt>
|
|
<dd>{person.location_label}</dd>
|
|
</div>
|
|
)}
|
|
{person.public_email && (
|
|
<div className="pl__fact">
|
|
<dt>Email</dt>
|
|
<dd>
|
|
<a href={`mailto:${person.public_email}`}>{person.public_email}</a>
|
|
</dd>
|
|
</div>
|
|
)}
|
|
{person.links?.length ? (
|
|
<div className="pl__fact">
|
|
<dt>Links</dt>
|
|
<dd className="pl__links">
|
|
{person.links.map((link) => (
|
|
<a key={link.url} href={link.url} target="_blank" rel="noreferrer">
|
|
<PlatformIcon platform={link.platform} />
|
|
{link.label}
|
|
</a>
|
|
))}
|
|
</dd>
|
|
</div>
|
|
) : null}
|
|
</dl>
|
|
|
|
{paragraphs.filter(Boolean).map((paragraph, index) => (
|
|
<p key={index} className="pl__bio-text">
|
|
{paragraph}
|
|
</p>
|
|
))}
|
|
|
|
{profile && (
|
|
<Link to={profile} className="pl__profile">
|
|
View full profile <ArrowRight size="1em" weight="bold" className="inline align-[-0.125em]" aria-hidden="true" />
|
|
</Link>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/* Reading mode: every person and what their details panel says. */
|
|
function ReadingPeople({ groups, level }: { groups: PeopleGroup[]; level: 3 | 4 | 5 }) {
|
|
const Label = `h${level}` as "h3";
|
|
return (
|
|
<div>
|
|
{groups.map((group) => {
|
|
// Names sit under the group's label, or take its place.
|
|
const Name = `h${group.label ? level + 1 : level}` as "h4";
|
|
return (
|
|
<section key={group.id}>
|
|
{group.label && <Label>{group.label}</Label>}
|
|
{group.note && <p>{group.note}</p>}
|
|
{group.people.map((person, index) => {
|
|
const href = profileHref(person);
|
|
const title = titleOf(person);
|
|
const org = resolveOrg(person.org);
|
|
const bio = Array.isArray(person.bio) ? person.bio : [person.bio];
|
|
return (
|
|
<div key={keyFor(group, person, index)}>
|
|
<Name>{href ? <Link to={href}>{person.name}</Link> : person.name}</Name>
|
|
{title && <p>{title}</p>}
|
|
{person.pronouns && <p>Pronouns: {person.pronouns}</p>}
|
|
{org && (
|
|
<p>
|
|
Home organization: {org.href ? <a href={org.href}>{org.name}</a> : org.name}
|
|
</p>
|
|
)}
|
|
{person.location_label && <p>Based in: {person.location_label}</p>}
|
|
{person.public_email && (
|
|
<p>
|
|
Email: <a href={`mailto:${person.public_email}`}>{person.public_email}</a>
|
|
</p>
|
|
)}
|
|
{person.links?.map((link) => (
|
|
<p key={link.url}>
|
|
<a href={link.url}>{link.label}</a>
|
|
</p>
|
|
))}
|
|
{bio.filter(Boolean).map((paragraph, i) => (
|
|
<p key={i}>{paragraph}</p>
|
|
))}
|
|
</div>
|
|
);
|
|
})}
|
|
</section>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Chevron() {
|
|
return (
|
|
<CaretDown size={12} weight="bold" aria-hidden="true" />
|
|
);
|
|
}
|
|
|
|
/* ── Helpers ─────────────────────────────────────────────────── */
|
|
|
|
/* How small a tile may get so a whole list shares one line. */
|
|
export const ONE_ROW_SHRINK = 0.75;
|
|
|
|
/* Whether every group fits side by side on one line, 10 tiles at most,
|
|
with tiles no smaller than ONE_ROW_SHRINK of full width. Groups are
|
|
divided by a gap, padding and a 1px rule (PeopleTiles.css). */
|
|
export function fitsOneRow(counts: number[], width: number, tile: number, gap: number): boolean {
|
|
const total = counts.reduce((sum, n) => sum + n, 0);
|
|
if (total < 2 || total > 10) return false;
|
|
const smallest = tile * ONE_ROW_SHRINK;
|
|
const tiles = counts.reduce((sum, n) => sum + n * smallest + (n - 1) * gap, 0);
|
|
const dividers = (counts.length - 1) * (gap * 3 + 1);
|
|
return tiles + dividers <= width;
|
|
}
|
|
|
|
/* Tiles across a row: as many as fit, at least 2 and at most 10
|
|
(never more than there are). If that would leave one tile alone on
|
|
the last row, one fewer across, so the last row has two: 7 that fit
|
|
6 across go 5 and 2. With only 2 across, a third, narrower column
|
|
takes the odd one instead, if tiles stay at least 60% of full
|
|
width; on a narrow phone a lone last tile beats three unreadable
|
|
ones. */
|
|
export function balancedColumns(count: number, width: number, tile: number, gap: number): number {
|
|
if (count <= 1) return 1;
|
|
const fit = Math.floor((width + gap) / (tile + gap));
|
|
const cols = Math.min(count, 10, Math.max(2, fit));
|
|
if (count % cols !== 1) return cols;
|
|
if (cols > 2) return cols - 1;
|
|
const third = (width - 2 * gap) / 3;
|
|
return count >= 3 && third >= tile * 0.6 ? 3 : 2;
|
|
}
|
|
|
|
/* A string id is a people slug; a numeric or missing one is a
|
|
hand-written entry with no page behind it. */
|
|
function profileHref(person: Person): string | null {
|
|
return typeof person.id === "string" && !isBadId(person.id) ? personHref(person.id) : null;
|
|
}
|
|
|
|
function keyFor(group: PeopleGroup, person: Person, index: number): string {
|
|
return `${group.id}:${person.id ?? person.name ?? index}`;
|
|
}
|
|
|
|
function findByKey(groups: PeopleGroup[], key: string | null) {
|
|
if (!key) return null;
|
|
for (const group of groups) {
|
|
for (let index = 0; index < group.people.length; index += 1) {
|
|
const person = group.people[index];
|
|
if (keyFor(group, person, index) === key) return { group, person };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/* The seat they hold here, or what they're called when there is no
|
|
seat, the same fallback content.js applies to leadership rows. */
|
|
function titleOf(person: Person): string | null {
|
|
return person.title || person.tagline || null;
|
|
}
|
|
|
|
function owns(person: Person): boolean {
|
|
return Boolean(person.is_owner ?? person.isOwner);
|
|
}
|
|
|
|
/* Anything the panel would have to show. Gating on bio alone left
|
|
every tile flat until someone wrote prose. */
|
|
function hasDetails(person: Person): boolean {
|
|
if (Array.isArray(person.bio) ? person.bio.some(Boolean) : Boolean(person.bio)) {
|
|
return true;
|
|
}
|
|
return Boolean(
|
|
person.pronouns ||
|
|
resolveOrg(person.org) ||
|
|
person.location_label ||
|
|
person.public_email ||
|
|
person.links?.length,
|
|
);
|
|
}
|
|
|
|
|
|
|
|
function resolveOrg(org: Person["org"]) {
|
|
if (!org) return null;
|
|
if (typeof org === "string") return { name: org, href: undefined };
|
|
if (!org.name) return null;
|
|
return org;
|
|
}
|