import { ScrollView } from "@getpaseo/plugin/client/react-native";
import { Fragment, useId, useRef, useState, type ReactNode } from "react";
import { Platform, Pressable, Text, View } from "react-native";
import { Disclosure, Dot, Section, surface, text, tint, toneColor, type Theme, type Tone } from "./kit";
/** The header, tabs and "needs you" list every screen of the plugin shares. */
type ThemeProps = { theme: Theme };
/** A screen's status at a glance: a dot, breathing while something runs, and a few words. */
export function StatusChip({ label, tone, moving = false, theme }: ThemeProps & { label: string; tone: Tone; moving?: boolean }) {
const color = toneColor(theme, tone);
return (
{label}
);
}
/**
* Header, status and actions above one tab's content. `alerts` qualify what the tab shows and stay
* visible; `notes` explain how to read it and are folded under "How this works".
*/
export function Frame({
title,
description,
status,
actions,
alerts = [],
notes = [],
compact,
theme,
children,
}: ThemeProps & {
title: string;
description?: string | null;
status?: ReactNode;
actions?: ReactNode;
alerts?: readonly string[];
notes?: readonly string[];
compact: boolean;
children: ReactNode;
}) {
return (
{title}
{description ? {description} : null}
{status || actions ? (
{status}
{actions}
) : null}
{alerts.length > 0 ? (
{alerts.map((alert) => (
{alert}
))}
) : null}
{children}
);
}
export type Tab = { id: Id; label: string; badge?: { label: string; tone?: Tone } | null };
type TabKeyEvent = { key?: string; nativeEvent?: { key?: string }; preventDefault(): void };
type Focusable = { focus(): void };
function tabForKey(tabs: readonly Tab[], active: Id, key: string): Id | null {
const index = tabs.findIndex((tab) => tab.id === active);
if (key === "Home") return tabs[0].id;
if (key === "End") return tabs[tabs.length - 1].id;
if (key === "ArrowRight") return tabs[(index + 1) % tabs.length].id;
if (key === "ArrowLeft") return tabs[(index + tabs.length - 1) % tabs.length].id;
return null;
}
/**
* A tab bar over panels that are built the first time they open and then kept, so a draft survives
* switching tabs. `renderTab` draws one panel.
*/
export function Tabbed({
tabs,
active,
onSelect,
renderTab,
label,
compact,
theme,
}: ThemeProps & {
tabs: readonly Tab[];
active: Id;
onSelect(id: Id): void;
renderTab(id: Id): ReactNode;
label: string;
compact: boolean;
}) {
const idPrefix = `sbx-${useId().replace(/:/g, "")}`;
const [opened, setOpened] = useState([active]);
const [focused, setFocused] = useState(null);
const refs = useRef>>({});
const built = new Set([...opened, active]);
const select = (id: Id) => {
setOpened((previous) => (previous.includes(id) ? previous : [...previous, id]));
onSelect(id);
};
const muted = theme.colors.foregroundMuted;
const accent = toneColor(theme, "accent");
return (
{tabs.map((tab) => {
const selected = active === tab.id;
const badgeColor = tab.badge?.tone && tab.badge.tone !== "neutral" ? toneColor(theme, tab.badge.tone) : muted;
const webProps =
Platform.OS === "web"
? {
"aria-controls": `${idPrefix}-panel-${tab.id}`,
"aria-selected": selected,
onKeyDown: (event: TabKeyEvent) => {
const next = tabForKey(tabs, tab.id, event.key ?? event.nativeEvent?.key ?? "");
if (!next) return;
event.preventDefault();
select(next);
refs.current[next]?.focus();
},
}
: {};
return (
{
refs.current[tab.id] = node;
}}
nativeID={`${idPrefix}-tab-${tab.id}`}
role="tab"
accessibilityLabel={tab.label}
accessibilityHint={tab.badge?.label}
accessibilityState={{ selected }}
tabIndex={selected ? 0 : -1}
onPress={() => select(tab.id)}
onFocus={() => setFocused(tab.id)}
onBlur={() => setFocused(null)}
{...webProps}
// The selected tab is underlined; the focused one is ringed, so keyboard focus shows on the selected tab too.
style={(state) => ({
minHeight: 44,
flexDirection: "row",
alignItems: "center",
gap: 7,
paddingHorizontal: compact ? 10 : 12,
marginBottom: -1,
borderWidth: 2,
borderRadius: 8,
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
borderColor: focused === tab.id ? accent : "transparent",
borderBottomColor: selected || focused === tab.id ? accent : "transparent",
backgroundColor:
(state as { hovered?: boolean }).hovered && !selected ? tint(theme, muted, 0.08) : "transparent",
})}
>
{tab.label}
{tab.badge ? (
{tab.badge.label}
) : null}
);
})}
{tabs.map(({ id }) => (
{built.has(id) ? renderTab(id) : null}
))}
);
}
/** One thing waiting on you: what it is, why, and where to fix it. */
export type Need = { tone: Tone; title: string; detail?: string | null; action?: { label: string; onPress(): void } | null };
/** What waits on you, each item with the way to fix it; a calm line when nothing does. */
export function Needs({ needs, calm, theme }: ThemeProps & { needs: readonly Need[]; calm: { title: string; detail: string } }) {
const success = toneColor(theme, "success");
if (!needs.length) {
return (
✓
{calm.title}
{calm.detail}
);
}
return (
need.tone === "danger") ? "danger" : "warning"}
overline={`Needs you · ${needs.length}`}
gap={4}
padding={16}
>
{needs.map((need, index) => (
{need.title}
{need.detail ? {need.detail} : null}
{need.action ? (
{need.action.label} →
) : null}
))}
);
}
/** A grid of figures that wraps on narrow layouts. */
export function Figures({ children }: { children: ReactNode }) {
return {children};
}
/** Two columns side by side on wide layouts, one above the other on narrow ones. */
export function Columns({ compact, children }: { compact: boolean; children: ReactNode }) {
return (
{children}
);
}
export function Column({ compact, children }: { compact: boolean; children: ReactNode }) {
return (
{children}
);
}
/** Cards in two columns, filled in turn so neither runs much longer; one column on compact layouts. */
export function Grid- ({
items,
keyOf,
render,
compact,
}: {
items: readonly Item[];
keyOf(item: Item): string;
render(item: Item): ReactNode;
compact: boolean;
}) {
const card = (item: Item) => {render(item)};
if (compact) return {items.map(card)};
return (
{[0, 1].map((side) => (
{items.filter((_, index) => index % 2 === side).map(card)}
))}
);
}
/** An empty state: what is missing and how it fills. */
export function Empty({ title, detail, theme, children }: ThemeProps & { title: string; detail: string; children?: ReactNode }) {
return (
{title}
{detail}
{children}
);
}