import type { PluginHostProps } from "@getpaseo/plugin/client";
import { TextInput } from "@getpaseo/plugin/client/ui";
import { useEffect, useRef, useState, type ReactNode } from "react";
import { AccessibilityInfo, Animated, Easing, Platform, Pressable, Switch, Text, View } from "react-native";
/**
* Surfaces, figures, controls and motion the plugin's screens share, after the GSD observer's kit.
* Every color comes from a theme token: tints are a token at a low opacity, the way Paseo tints its
* own status colors, and motion stops when the system asks for reduced motion.
*/
export type Theme = PluginHostProps["theme"];
export type Tone = "neutral" | "accent" | "success" | "warning" | "danger";
type ThemeProps = { theme: Theme };
/** Paseo theme colors are hex strings; "#abc" and "#aabbccdd" are read as "#aabbcc". */
function hex6(color: string): string | null {
if (/^#[0-9a-f]{6}(?:[0-9a-f]{2})?$/i.test(color)) return color.slice(0, 7);
if (/^#[0-9a-f]{3}$/i.test(color)) return `#${[...color.slice(1)].map((digit) => digit + digit).join("")}`;
return null;
}
const channels = (color: string) => [1, 3, 5].map((index) => parseInt(color.slice(index, index + 2), 16));
function luminance(color: string): number {
const value = hex6(color);
if (!value) return 1;
const [r, g, b] = channels(value);
return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
}
export const isDark = (theme: Theme) => luminance(theme.colors.surface0) < 0.4;
/** A token at a low opacity: 12% on light themes, a third more on dark ones, as Paseo's own status tints. */
export function tint(theme: Theme, color: string, strength = 0.12): string {
const value = hex6(color);
if (!value) return color;
const alpha = Math.max(0, Math.min(1, isDark(theme) ? strength * 1.35 : strength));
return `${value}${Math.round(alpha * 255)
.toString(16)
.padStart(2, "0")}`;
}
/** A dark accent (Paseo's green) is made for fills; lifted toward white it also reads as text and lines on dark surfaces. */
function lifted(color: string): string {
const value = hex6(color);
if (!value) return color;
return `#${channels(value)
.map((channel) =>
Math.round(channel + (255 - channel) * 0.42)
.toString(16)
.padStart(2, "0"),
)
.join("")}`;
}
export function toneColor(theme: Theme, tone: Tone): string {
const { colors } = theme;
if (tone === "neutral") return colors.foregroundMuted;
if (tone === "accent") return isDark(theme) && luminance(colors.accent) < 0.45 ? lifted(colors.accent) : colors.accent;
return tone === "success" ? colors.statusSuccess : tone === "warning" ? colors.statusWarning : colors.statusDanger;
}
export const surface = (theme: Theme, level: 1 | 2 = 1) => (level === 2 ? theme.colors.surface2 : theme.colors.surface1);
type Weight = "400" | "500" | "600" | "700";
export const text = (theme: Theme, size = 14, weight: Weight = "400", muted = false) => ({
color: muted ? theme.colors.foregroundMuted : theme.colors.foreground,
fontSize: size,
lineHeight: Math.round(size * 1.45),
fontWeight: weight,
flexShrink: 1,
});
export const kicker = (theme: Theme) => ({
...text(theme, 11, "600", true),
letterSpacing: 0.8,
textTransform: "uppercase" as const,
});
export const mono = (theme: Theme, size = 12, muted = false) => ({
...text(theme, size, "400", muted),
fontFamily: Platform.select({ ios: "Menlo", default: "monospace" }),
});
export const figures = { fontVariant: ["tabular-nums" as const] };
/** Opacity and transform animate on the native thread where there is one; the web has none. */
const nativeDriver = () => Platform.OS !== "web";
export function useReducedMotion(): boolean {
const [reduced, setReduced] = useState(false);
useEffect(() => {
let live = true;
AccessibilityInfo.isReduceMotionEnabled()
.then((value) => {
if (live) setReduced(value);
})
.catch(() => undefined);
const subscription = AccessibilityInfo.addEventListener("reduceMotionChanged", setReduced);
return () => {
live = false;
subscription.remove();
};
}, []);
return reduced;
}
/** The current time, every `intervalMs`, for relative times that keep up without a refresh. */
export function useNow(intervalMs = 15_000): number {
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const timer = setInterval(() => setNow(Date.now()), intervalMs);
return () => clearInterval(timer);
}, [intervalMs]);
return now;
}
/** "just now", "12m ago", "3h 5m ago", "2d ago" for an epoch-milliseconds time. */
export function ago(time: number | null, now = Date.now()): string {
if (time === null) return "never";
const seconds = Math.max(0, Math.round((now - time) / 1000));
if (seconds < 45) return "just now";
if (seconds < 3_600) return `${Math.max(1, Math.round(seconds / 60))}m ago`;
if (seconds < 86_400) return `${Math.floor(seconds / 3_600)}h ${Math.floor((seconds % 3_600) / 60)}m ago`;
return `${Math.floor(seconds / 86_400)}d ago`;
}
/** A dot with a breathing ring: something is running right now. */
export function Pulse({ color, size = 8 }: { color: string; size?: number }) {
const reduced = useReducedMotion();
const value = useRef(new Animated.Value(0)).current;
useEffect(() => {
if (reduced) return;
const loop = Animated.loop(
Animated.timing(value, { toValue: 1, duration: 1_800, easing: Easing.out(Easing.quad), useNativeDriver: nativeDriver() }),
);
loop.start();
return () => loop.stop();
}, [reduced, value]);
const ring = size * 2.6;
return (
{reduced ? null : (
)}
);
}
/** A status dot: breathing while `moving`, still otherwise. */
export function Dot({ color, moving = false, size = 8 }: { color: string; moving?: boolean; size?: number }) {
if (moving) return ;
return ;
}
/** Fades and lifts its children in when they first appear. */
export function FadeIn({ children, delay = 0 }: { children: ReactNode; delay?: number }) {
const reduced = useReducedMotion();
const value = useRef(new Animated.Value(0)).current;
useEffect(() => {
if (reduced) {
value.setValue(1);
return;
}
Animated.timing(value, {
toValue: 1,
duration: 380,
delay,
easing: Easing.out(Easing.cubic),
useNativeDriver: nativeDriver(),
}).start();
}, [delay, reduced, value]);
return (
{children}
);
}
export function Pill({
label,
tone = "neutral",
theme,
dot = false,
moving = false,
}: ThemeProps & { label: string; tone?: Tone; dot?: boolean; moving?: boolean }) {
const color = toneColor(theme, tone);
return (
{dot ? : null}
{label}
);
}
/** A borderless raised surface; a tone gives it a soft wash and a rule down its left edge. */
export function Section({
title,
overline,
aside,
children,
theme,
tone,
padding = 20,
gap = 14,
}: ThemeProps & {
title?: string;
overline?: string;
aside?: ReactNode;
children?: ReactNode;
tone?: Tone;
padding?: number;
gap?: number;
}) {
const color = tone ? toneColor(theme, tone) : null;
return (
{color ? : null}
{title || overline || aside ? (
{overline ? {overline} : null}
{title ? (
{title}
) : null}
{aside}
) : null}
{children}
);
}
/** One headline figure with its label above and an optional detail below. */
export function Stat({
label,
value,
detail,
theme,
tone,
size = 24,
children,
}: ThemeProps & { label: string; value: string; detail?: string | null; tone?: Tone; size?: number; children?: ReactNode }) {
const toned = tone && tone !== "neutral";
return (
{label}
{value}
{detail ? {detail} : null}
{children}
);
}
export type Segment = { value: number; color: string; label: string };
/** One bar split by value; a tiny segment keeps a sliver so it never disappears. */
export function SegmentBar({
segments,
height = 10,
theme,
label,
}: ThemeProps & { segments: readonly Segment[]; height?: number; label: string }) {
const shown = segments.filter((segment) => segment.value > 0);
return (
1 ? 2 : 0,
borderRadius: height / 2,
overflow: "hidden",
backgroundColor: surface(theme, 2),
}}
>
{shown.map((segment) => (
))}
);
}
export function Legend({ items, theme }: ThemeProps & { items: readonly { color: string; label: string }[] }) {
return (
{items.map((item) => (
{item.label}
))}
);
}
/** A thin bar filled to `value` of `max`. */
export function Meter({
value,
max,
color,
theme,
height = 6,
}: ThemeProps & { value: number; max: number; color: string; height?: number }) {
const percent = max > 0 ? Math.min(100, Math.max(value > 0 ? 1.5 : 0, (100 * value) / max)) : 0;
return (
);
}
/** The notes behind a screen's figures, folded until asked for. */
export function Disclosure({ title, notes, theme }: ThemeProps & { title: string; notes: readonly string[] }) {
const [open, setOpen] = useState(false);
if (!notes.length) return null;
return (
setOpen(!open)}
style={{ alignSelf: "flex-start", minHeight: 32, justifyContent: "center" }}
>
{open ? "▾" : "▸"} {title}
{open ? (
{notes.map((note) => (
{note}
))}
) : null}
);
}
/** A segmented choice between a few options. */
export function Toggle({
label,
options,
value,
onChange,
theme,
}: ThemeProps & { label: string; options: readonly { value: V; label: string }[]; value: V; onChange(value: V): void }) {
return (
{options.map((option) => {
const selected = option.value === value;
return (
onChange(option.value)}
style={{
minHeight: 30,
justifyContent: "center",
paddingHorizontal: 12,
borderRadius: 8,
backgroundColor: selected ? theme.colors.surface0 : "transparent",
}}
>
{option.label}
);
})}
);
}
export type ButtonKind = "primary" | "secondary" | "danger" | "quiet";
/** A rounded action; `primary` is the one thing a surface is for, `danger` a destructive one. */
export function Button({
label,
onPress,
theme,
kind = "secondary",
disabled = false,
busy = false,
small = false,
}: ThemeProps & { label: string; onPress(): void; kind?: ButtonKind; disabled?: boolean; busy?: boolean; small?: boolean }) {
const danger = toneColor(theme, "danger");
const background =
kind === "primary"
? theme.colors.accent
: kind === "danger"
? tint(theme, danger, 0.16)
: kind === "quiet"
? "transparent"
: // Translucent, so it reads on tinted sections as well as on plain surfaces.
tint(theme, theme.colors.foregroundMuted, 0.16);
const color = kind === "primary" ? theme.colors.accentForeground : kind === "danger" ? danger : theme.colors.foreground;
const inactive = disabled || busy;
return (
({
minHeight: small ? 30 : 36,
justifyContent: "center",
alignItems: "center",
paddingHorizontal: small ? 11 : 14,
borderRadius: 999,
backgroundColor: background,
opacity: disabled ? 0.45 : (state as { hovered?: boolean }).hovered ? 0.85 : 1,
})}
>
{busy ? `${label}…` : label}
);
}
/** A destructive action that asks for a second press instead of a modal, and disarms after a while. */
export function ConfirmButton({
label,
confirmLabel,
onConfirm,
theme,
disabled,
busy,
small,
}: ThemeProps & { label: string; confirmLabel: string; onConfirm(): void; disabled?: boolean; busy?: boolean; small?: boolean }) {
const [armed, setArmed] = useState(false);
useEffect(() => {
if (!armed) return;
const timer = setTimeout(() => setArmed(false), 5_000);
return () => clearTimeout(timer);
}, [armed]);
return (