新增: 实时热卖榜(11分类/想要人数排序/热门搜索词热度)、价格监控(目标价通知/卖家信用筛选/销量下限/AI精准监控)、 一键转卖(收藏自动加载/改价直发/详情直达)、AI回复免费token(ai.88531.cn)、API密钥掩码、 推理模型兼容修复、商品本地编辑、授权系统接入(在线人数/版本/公告)、品牌更名躺赚鱼。 README 重写为项目介绍(改动清单/优势/真实截图/两种安装方式/体验地址 tzy.88531.cn/使用引导), 原版 README 完整保留为 README.upstream.md。
430 lines
14 KiB
TypeScript
430 lines
14 KiB
TypeScript
import React, { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
||
import { createPortal } from 'react-dom';
|
||
import {
|
||
AlertTriangle,
|
||
CheckCircle2,
|
||
ChevronDown,
|
||
Inbox,
|
||
Loader2,
|
||
LucideIcon,
|
||
X,
|
||
} from 'lucide-react';
|
||
|
||
interface PageHeaderProps {
|
||
title: string;
|
||
description?: string;
|
||
icon?: LucideIcon;
|
||
badge?: React.ReactNode;
|
||
actions?: React.ReactNode;
|
||
}
|
||
|
||
export const PageHeader: React.FC<PageHeaderProps> = ({
|
||
title,
|
||
description,
|
||
icon: Icon,
|
||
badge,
|
||
actions,
|
||
}) => (
|
||
<header className="page-header">
|
||
<div className="page-header__identity">
|
||
{Icon && (
|
||
<span className="page-header__icon" aria-hidden="true">
|
||
<Icon className="h-5 w-5" />
|
||
</span>
|
||
)}
|
||
<div className="min-w-0">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<h1 className="page-title">{title}</h1>
|
||
{badge}
|
||
</div>
|
||
{description && <p className="page-description">{description}</p>}
|
||
</div>
|
||
</div>
|
||
{actions && <div className="page-actions">{actions}</div>}
|
||
</header>
|
||
);
|
||
|
||
interface PageTabsProps<T extends string> {
|
||
value: T;
|
||
onChange: (value: T) => void;
|
||
items: Array<{
|
||
id: T;
|
||
label: string;
|
||
icon?: LucideIcon;
|
||
count?: number;
|
||
}>;
|
||
ariaLabel?: string;
|
||
}
|
||
|
||
export const PageTabs = <T extends string>({
|
||
value,
|
||
onChange,
|
||
items,
|
||
ariaLabel = '页面分区',
|
||
}: PageTabsProps<T>) => (
|
||
<div className="page-tabs" role="tablist" aria-label={ariaLabel}>
|
||
{items.map((item) => {
|
||
const Icon = item.icon;
|
||
const active = value === item.id;
|
||
return (
|
||
<button
|
||
key={item.id}
|
||
type="button"
|
||
role="tab"
|
||
aria-selected={active}
|
||
onClick={() => onChange(item.id)}
|
||
className={`page-tab ${active ? 'page-tab--active' : ''}`}
|
||
>
|
||
{Icon && <Icon className="h-4 w-4 shrink-0" />}
|
||
<span>{item.label}</span>
|
||
{item.count !== undefined && <span className="page-tab__count">{item.count}</span>}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
);
|
||
|
||
interface SectionHeaderProps {
|
||
title: string;
|
||
description?: string;
|
||
icon?: LucideIcon;
|
||
actions?: React.ReactNode;
|
||
}
|
||
|
||
export const SectionHeader: React.FC<SectionHeaderProps> = ({
|
||
title,
|
||
description,
|
||
icon: Icon,
|
||
actions,
|
||
}) => (
|
||
<div className="section-panel__header">
|
||
<div className="flex min-w-0 items-start gap-3">
|
||
{Icon && (
|
||
<span className="section-header__icon" aria-hidden="true">
|
||
<Icon className="h-4 w-4" />
|
||
</span>
|
||
)}
|
||
<div className="min-w-0">
|
||
<h2 className="section-title">{title}</h2>
|
||
{description && <p className="section-description">{description}</p>}
|
||
</div>
|
||
</div>
|
||
{actions && <div className="section-header__actions">{actions}</div>}
|
||
</div>
|
||
);
|
||
|
||
interface CollapsibleSectionProps {
|
||
title: string;
|
||
description?: string;
|
||
icon?: LucideIcon;
|
||
actions?: React.ReactNode;
|
||
defaultOpen?: boolean;
|
||
busy?: boolean;
|
||
children?: React.ReactNode;
|
||
}
|
||
|
||
/** 可折叠分区面板:点击标题或右侧箭头展开/收起,操作按钮始终可见。 */
|
||
export const CollapsibleSection: React.FC<CollapsibleSectionProps> = ({
|
||
title,
|
||
description,
|
||
icon: Icon,
|
||
actions,
|
||
defaultOpen = false,
|
||
busy,
|
||
children,
|
||
}) => {
|
||
const [open, setOpen] = useState(defaultOpen);
|
||
const titleId = useId();
|
||
const bodyId = useId();
|
||
|
||
return (
|
||
<section className="section-panel" aria-labelledby={titleId} aria-busy={busy || undefined}>
|
||
<div className={`section-panel__header ${open ? '' : 'section-panel__header--collapsed'}`}>
|
||
<h2 className="min-w-0 flex-1">
|
||
<button
|
||
type="button"
|
||
className="flex w-full cursor-pointer items-start gap-3 text-left focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--focus-ring)]"
|
||
aria-expanded={open}
|
||
aria-controls={bodyId}
|
||
onClick={() => setOpen((prev) => !prev)}
|
||
>
|
||
{Icon && (
|
||
<span className="section-header__icon" aria-hidden="true">
|
||
<Icon className="h-4 w-4" />
|
||
</span>
|
||
)}
|
||
<span className="min-w-0 flex-1">
|
||
<span id={titleId} className="section-title block">{title}</span>
|
||
{description && <span className="section-description block">{description}</span>}
|
||
</span>
|
||
<ChevronDown
|
||
className={`mt-0.5 h-4 w-4 shrink-0 text-[var(--text-soft)] transition-transform duration-150 motion-reduce:transition-none ${open ? 'rotate-180' : ''}`}
|
||
aria-hidden="true"
|
||
/>
|
||
</button>
|
||
</h2>
|
||
{actions && <div className="section-header__actions">{actions}</div>}
|
||
</div>
|
||
<div id={bodyId} hidden={!open}>{children}</div>
|
||
</section>
|
||
);
|
||
};
|
||
|
||
interface NoticeBannerProps {
|
||
type: 'success' | 'error' | 'warning' | 'info';
|
||
message?: string;
|
||
onClose?: () => void;
|
||
children?: React.ReactNode;
|
||
}
|
||
|
||
export const NoticeBanner: React.FC<NoticeBannerProps> = ({
|
||
type,
|
||
message,
|
||
onClose,
|
||
children,
|
||
}) => {
|
||
const Icon = type === 'success' ? CheckCircle2 : AlertTriangle;
|
||
return (
|
||
<div className={`notice-banner notice-banner--${type}`} role="status">
|
||
<Icon className="h-4 w-4 shrink-0" />
|
||
{/* 同时接受 message 与 children:只认 message 时,写成子元素的文案会被
|
||
静默丢掉,页面上只剩一个感叹号图标,而 TS 不会报错(FC 隐式允许 children) */}
|
||
<span className="min-w-0 flex-1">{message ?? children}</span>
|
||
{onClose && (
|
||
<button type="button" onClick={onClose} aria-label="关闭提示">
|
||
<X className="h-4 w-4" />
|
||
</button>
|
||
)}
|
||
</div>
|
||
);
|
||
};
|
||
|
||
interface EmptyStateProps {
|
||
title: string;
|
||
description?: string;
|
||
icon?: LucideIcon;
|
||
action?: React.ReactNode;
|
||
compact?: boolean;
|
||
}
|
||
|
||
export const EmptyState: React.FC<EmptyStateProps> = ({
|
||
title,
|
||
description,
|
||
icon: Icon = Inbox,
|
||
action,
|
||
compact = false,
|
||
}) => (
|
||
<div className={`empty-state ${compact ? 'empty-state--compact' : ''}`}>
|
||
<span className="empty-state__icon" aria-hidden="true">
|
||
<Icon className="h-6 w-6" />
|
||
</span>
|
||
<h3>{title}</h3>
|
||
{description && <p>{description}</p>}
|
||
{action && <div className="mt-4">{action}</div>}
|
||
</div>
|
||
);
|
||
|
||
export const PageLoading: React.FC<{ label?: string }> = ({ label = '正在加载' }) => (
|
||
<div className="page-loading" role="status">
|
||
<Loader2 className="h-5 w-5 animate-spin" />
|
||
<span>{label}</span>
|
||
</div>
|
||
);
|
||
|
||
interface TooltipProps {
|
||
/** 气泡内容,通常为一句操作或字段说明。 */
|
||
content: React.ReactNode;
|
||
/** 气泡方位;同侧空间不足时自动翻转到另一侧。 */
|
||
side?: 'top' | 'bottom';
|
||
/** 与触发器的水平对齐,贴边触发器用 right/left 防止溢出视口。 */
|
||
align?: 'left' | 'center' | 'right';
|
||
/** 触发器,通常为说明图标按钮:悬浮、聚焦或点击都会显示气泡。 */
|
||
children: React.ReactElement;
|
||
}
|
||
|
||
/** 通用说明气泡:挂载到 body 定位,不受父级 overflow 裁剪影响。 */
|
||
export const Tooltip: React.FC<TooltipProps> = ({ content, side = 'top', align = 'center', children }) => {
|
||
const [open, setOpen] = useState(false);
|
||
const [coords, setCoords] = useState<{ top: number; left: number; resolvedSide: 'top' | 'bottom' } | null>(null);
|
||
const rootRef = useRef<HTMLSpanElement>(null);
|
||
const bubbleRef = useRef<HTMLSpanElement>(null);
|
||
const id = useId();
|
||
|
||
useLayoutEffect(() => {
|
||
if (!open) return undefined;
|
||
const update = () => {
|
||
const rect = rootRef.current?.getBoundingClientRect();
|
||
if (!rect) return;
|
||
const margin = 8;
|
||
const gap = 6;
|
||
const bubbleWidth = bubbleRef.current?.offsetWidth ?? 0;
|
||
const bubbleHeight = bubbleRef.current?.offsetHeight ?? 0;
|
||
const spaceAbove = rect.top - margin;
|
||
const spaceBelow = window.innerHeight - rect.bottom - margin;
|
||
const resolvedSide: 'top' | 'bottom' = side === 'top'
|
||
? (spaceAbove >= bubbleHeight || spaceAbove >= spaceBelow ? 'top' : 'bottom')
|
||
: (spaceBelow >= bubbleHeight || spaceBelow >= spaceAbove ? 'bottom' : 'top');
|
||
const left = align === 'left'
|
||
? rect.left
|
||
: align === 'right'
|
||
? rect.right - bubbleWidth
|
||
: rect.left + rect.width / 2 - bubbleWidth / 2;
|
||
setCoords({
|
||
top: resolvedSide === 'top' ? rect.top - bubbleHeight - gap : rect.bottom + gap,
|
||
left: Math.min(Math.max(margin, left), Math.max(margin, window.innerWidth - bubbleWidth - margin)),
|
||
resolvedSide,
|
||
});
|
||
};
|
||
update();
|
||
let frame = 0;
|
||
const requestUpdate = () => {
|
||
cancelAnimationFrame(frame);
|
||
frame = requestAnimationFrame(update);
|
||
};
|
||
window.addEventListener('resize', requestUpdate);
|
||
window.addEventListener('scroll', requestUpdate, true);
|
||
return () => {
|
||
cancelAnimationFrame(frame);
|
||
window.removeEventListener('resize', requestUpdate);
|
||
window.removeEventListener('scroll', requestUpdate, true);
|
||
};
|
||
}, [open, side, align, content]);
|
||
|
||
const trigger = React.isValidElement<{ 'aria-describedby'?: string }>(children)
|
||
? React.cloneElement(children, { 'aria-describedby': open ? id : undefined })
|
||
: children;
|
||
|
||
return (
|
||
<>
|
||
<span
|
||
ref={rootRef}
|
||
className="ui-tooltip"
|
||
onMouseEnter={() => setOpen(true)}
|
||
onMouseLeave={() => setOpen(false)}
|
||
onFocusCapture={() => setOpen(true)}
|
||
onBlurCapture={() => setOpen(false)}
|
||
onClick={() => setOpen(true)}
|
||
>
|
||
{trigger}
|
||
</span>
|
||
{createPortal(
|
||
<span
|
||
ref={bubbleRef}
|
||
role="tooltip"
|
||
id={id}
|
||
className={`ui-tooltip__bubble ui-tooltip__bubble--${coords?.resolvedSide ?? side} ui-tooltip__bubble--${align} ${
|
||
open ? 'ui-tooltip__bubble--open' : ''
|
||
}`}
|
||
style={open && coords ? { top: coords.top, left: coords.left } : undefined}
|
||
>
|
||
{content}
|
||
</span>,
|
||
document.body,
|
||
)}
|
||
</>
|
||
);
|
||
};
|
||
|
||
interface PopoverProps {
|
||
open: boolean;
|
||
onClose: () => void;
|
||
trigger: React.ReactNode;
|
||
placement?: 'top' | 'bottom';
|
||
align?: 'left' | 'right';
|
||
panelClassName?: string;
|
||
children: React.ReactNode;
|
||
/** 面板挂载到 body 并按触发器定位:父级存在 overflow 裁剪或滚动容器时开启,避免下拉被截断。 */
|
||
portal?: boolean;
|
||
}
|
||
|
||
export const Popover: React.FC<PopoverProps> = ({
|
||
open,
|
||
onClose,
|
||
trigger,
|
||
placement = 'bottom',
|
||
align = 'left',
|
||
panelClassName = 'max-h-72 w-80',
|
||
children,
|
||
portal = false,
|
||
}) => {
|
||
const rootRef = useRef<HTMLDivElement>(null);
|
||
const panelRef = useRef<HTMLDivElement>(null);
|
||
const [coords, setCoords] = useState<{ top: number; left: number; minWidth: number } | null>(null);
|
||
|
||
useEffect(() => {
|
||
if (!open) return undefined;
|
||
const handlePointerDown = (event: MouseEvent) => {
|
||
const target = event.target as Node;
|
||
if (rootRef.current?.contains(target)) return;
|
||
if (panelRef.current?.contains(target)) return;
|
||
onClose();
|
||
};
|
||
const handleKeyDown = (event: KeyboardEvent) => {
|
||
if (event.key === 'Escape') onClose();
|
||
};
|
||
document.addEventListener('mousedown', handlePointerDown);
|
||
document.addEventListener('keydown', handleKeyDown);
|
||
return () => {
|
||
document.removeEventListener('mousedown', handlePointerDown);
|
||
document.removeEventListener('keydown', handleKeyDown);
|
||
};
|
||
}, [open, onClose]);
|
||
|
||
// portal 模式下面板挂到 body:按触发器实时定位,空间不足时自动翻转到另一侧,
|
||
// 并在页面滚动、缩放时跟随,保证下拉始终完整可见。
|
||
useLayoutEffect(() => {
|
||
if (!open || !portal) return undefined;
|
||
const update = () => {
|
||
const rect = rootRef.current?.getBoundingClientRect();
|
||
if (!rect) return;
|
||
const margin = 8;
|
||
const panelHeight = panelRef.current?.offsetHeight ?? 0;
|
||
const panelWidth = panelRef.current?.offsetWidth ?? 0;
|
||
const spaceBelow = window.innerHeight - rect.bottom - margin;
|
||
const spaceAbove = rect.top - margin;
|
||
const resolved: 'top' | 'bottom' = placement === 'top'
|
||
? (spaceAbove >= panelHeight || spaceAbove >= spaceBelow ? 'top' : 'bottom')
|
||
: (spaceBelow >= panelHeight || spaceBelow >= spaceAbove ? 'bottom' : 'top');
|
||
const top = resolved === 'top' ? rect.top - panelHeight - margin : rect.bottom + margin;
|
||
const left = align === 'left'
|
||
? Math.min(rect.left, Math.max(margin, window.innerWidth - panelWidth - margin))
|
||
: Math.max(margin, Math.min(rect.right - panelWidth, window.innerWidth - panelWidth - margin));
|
||
setCoords({ top, left, minWidth: rect.width });
|
||
};
|
||
update();
|
||
let frame = 0;
|
||
const requestUpdate = () => {
|
||
cancelAnimationFrame(frame);
|
||
frame = requestAnimationFrame(update);
|
||
};
|
||
window.addEventListener('resize', requestUpdate);
|
||
window.addEventListener('scroll', requestUpdate, true);
|
||
return () => {
|
||
cancelAnimationFrame(frame);
|
||
window.removeEventListener('resize', requestUpdate);
|
||
window.removeEventListener('scroll', requestUpdate, true);
|
||
};
|
||
}, [open, portal, placement, align, children]);
|
||
|
||
const panelNode = (
|
||
<div
|
||
ref={panelRef}
|
||
className={`overflow-y-auto rounded-lg border border-[var(--border)] bg-[var(--surface)] p-2 shadow-lg ${
|
||
portal
|
||
? 'fixed z-50'
|
||
: `absolute z-20 ${placement === 'top' ? 'bottom-8' : 'top-8'} ${align === 'left' ? 'left-0' : 'right-0'}`
|
||
} ${panelClassName}`}
|
||
style={portal && coords ? { top: coords.top, left: coords.left, minWidth: coords.minWidth } : undefined}
|
||
>
|
||
{children}
|
||
</div>
|
||
);
|
||
|
||
return (
|
||
<div ref={rootRef} className="relative">
|
||
{trigger}
|
||
{open && (portal ? createPortal(panelNode, document.body) : panelNode)}
|
||
</div>
|
||
);
|
||
};
|