Keep Codex layout polish, but force emoji/orbit/constellation animations when immersive is on even under OS reduced-motion. Co-authored-by: Cursor <cursoragent@cursor.com>
231 lines
5.5 KiB
TypeScript
231 lines
5.5 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useRef, useState, type ReactNode } from "react";
|
||
import { ArrowUpRight, Check, Copy, Search, X } from "lucide-react";
|
||
import { useLocalState } from "@social/lib/useLocalState";
|
||
import { useSocialConfig } from "./SocialExperience";
|
||
import { usePathname } from "next/navigation";
|
||
|
||
export function PageIntro({
|
||
eyebrow,
|
||
title,
|
||
accent,
|
||
description,
|
||
children,
|
||
}: {
|
||
eyebrow: string;
|
||
title: string;
|
||
accent: string;
|
||
description: string;
|
||
children?: ReactNode;
|
||
}) {
|
||
const config = useSocialConfig();
|
||
const key = usePathname().split("/")[2] || "home";
|
||
const copy = config.copy.socialPages?.[key];
|
||
eyebrow =
|
||
typeof copy?.eyebrow === "string" && copy.eyebrow.trim()
|
||
? copy.eyebrow
|
||
: eyebrow;
|
||
title =
|
||
typeof copy?.title === "string" && copy.title.trim() ? copy.title : title;
|
||
accent =
|
||
typeof copy?.accent === "string" && copy.accent.trim()
|
||
? copy.accent
|
||
: accent;
|
||
description =
|
||
typeof copy?.description === "string" && copy.description.trim()
|
||
? copy.description
|
||
: description;
|
||
return (
|
||
<section className="social-page-intro">
|
||
<div className="social-page-orbit" aria-hidden="true">
|
||
<i />
|
||
<i />
|
||
<i />
|
||
<span>✦</span>
|
||
</div>
|
||
<div className="social-page-kicker">
|
||
<span />
|
||
{eyebrow}
|
||
</div>
|
||
<h1>
|
||
{title}
|
||
<br />
|
||
<span>{accent}</span>
|
||
</h1>
|
||
<p>{description}</p>
|
||
{children}
|
||
</section>
|
||
);
|
||
}
|
||
|
||
export function SearchField({
|
||
value,
|
||
onChange,
|
||
placeholder,
|
||
label = "搜索",
|
||
}: {
|
||
value: string;
|
||
onChange: (v: string) => void;
|
||
placeholder: string;
|
||
label?: string;
|
||
}) {
|
||
return (
|
||
<label className="social-search">
|
||
<Search size={17} />
|
||
<input
|
||
type="search"
|
||
aria-label={label}
|
||
placeholder={placeholder}
|
||
value={value}
|
||
onChange={(event) => onChange(event.target.value)}
|
||
/>
|
||
{value && (
|
||
<button
|
||
type="button"
|
||
aria-label="清空搜索"
|
||
onClick={() => onChange("")}
|
||
>
|
||
<X size={14} />
|
||
</button>
|
||
)}
|
||
</label>
|
||
);
|
||
}
|
||
|
||
export function BookmarkButton({
|
||
id,
|
||
collection = "saved-items",
|
||
label = "收藏",
|
||
}: {
|
||
id: string;
|
||
collection?: string;
|
||
label?: string;
|
||
}) {
|
||
const [items, setItems] = useLocalState<string[]>(collection, []);
|
||
const selected = Array.isArray(items) && items.includes(id);
|
||
return (
|
||
<button
|
||
type="button"
|
||
className={`social-save${selected ? " selected" : ""}`}
|
||
aria-pressed={selected}
|
||
onClick={() =>
|
||
setItems((current) =>
|
||
selected ? current.filter((item) => item !== id) : [...current, id],
|
||
)
|
||
}
|
||
title="收藏保存在当前设备"
|
||
>
|
||
{selected ? <Check size={15} /> : <span aria-hidden>+</span>}
|
||
{selected ? "已收藏" : label}
|
||
</button>
|
||
);
|
||
}
|
||
|
||
export function ShareButton({ path, title }: { path: string; title: string }) {
|
||
const [state, setState] = useState("");
|
||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||
useEffect(
|
||
() => () => {
|
||
if (timer.current) clearTimeout(timer.current);
|
||
},
|
||
[],
|
||
);
|
||
return (
|
||
<button
|
||
type="button"
|
||
className="social-save"
|
||
onClick={async () => {
|
||
const url = new URL(path, location.origin).href;
|
||
try {
|
||
if (navigator.share) {
|
||
await navigator.share({ title, url });
|
||
setState("已分享");
|
||
} else {
|
||
await navigator.clipboard.writeText(url);
|
||
setState("已复制");
|
||
}
|
||
} catch (error) {
|
||
if ((error as DOMException).name !== "AbortError")
|
||
setState("请复制地址栏链接");
|
||
}
|
||
if (timer.current) clearTimeout(timer.current);
|
||
timer.current = setTimeout(() => setState(""), 3000);
|
||
}}
|
||
>
|
||
<Copy size={14} />
|
||
<span role="status">{state || "分享"}</span>
|
||
</button>
|
||
);
|
||
}
|
||
|
||
export function EmptyState({
|
||
title = "暂时没有找到",
|
||
description = "换个关键词,或试试其他筛选。",
|
||
children,
|
||
}: {
|
||
title?: string;
|
||
description?: string;
|
||
children?: ReactNode;
|
||
}) {
|
||
return (
|
||
<div className="social-empty">
|
||
<span aria-hidden>✦</span>
|
||
<h3>{title}</h3>
|
||
<p>{description}</p>
|
||
{children}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function SocialDialog({
|
||
title,
|
||
onClose,
|
||
children,
|
||
}: {
|
||
title: string;
|
||
onClose: () => void;
|
||
children: ReactNode;
|
||
}) {
|
||
const ref = useRef<HTMLDialogElement>(null);
|
||
useEffect(() => {
|
||
const dialog = ref.current;
|
||
const opener = document.activeElement as HTMLElement;
|
||
const overflow = document.body.style.overflow;
|
||
dialog?.showModal();
|
||
document.body.style.overflow = "hidden";
|
||
return () => {
|
||
dialog?.close();
|
||
document.body.style.overflow = overflow;
|
||
opener?.focus();
|
||
};
|
||
}, []);
|
||
return (
|
||
<dialog
|
||
className="social-detail-dialog"
|
||
ref={ref}
|
||
aria-label={title}
|
||
onCancel={(event) => {
|
||
event.preventDefault();
|
||
onClose();
|
||
}}
|
||
onClick={(event) => {
|
||
if (event.target === event.currentTarget) onClose();
|
||
}}
|
||
>
|
||
<div>
|
||
<header>
|
||
<span>
|
||
MAKE A CONNECTION <ArrowUpRight size={14} />
|
||
</span>
|
||
<button type="button" aria-label="关闭详情" onClick={onClose}>
|
||
<X size={20} />
|
||
</button>
|
||
</header>
|
||
<h2>{title}</h2>
|
||
{children}
|
||
</div>
|
||
</dialog>
|
||
);
|
||
}
|