Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
+356
View File
@@ -0,0 +1,356 @@
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
/*
* PageGuide —— 企业控制台页面级「使用指引」三层体系的可复用实现。
*
* 三层结构(详见 docs 设计提案「页面功能指引」):
* 1. 常驻入口 <PageGuideTrigger>:放在页面 hero 动作区,随时唤起抽屉;
* 2. 进度胶囊 <PageGuidePill>:有上手流程的页面放在 hero 下方,
* 展示数据驱动的完成度与「下一步」,全部完成后自动隐藏;
* 3. 侧滑抽屉 <PageGuideDrawer>:承载定位一句话、分步操作、FAQ 与直达 CTA,
* 首次访问自动展开一次(localStorage 按 guideKey 记忆)。
*
* 页面只需 usePageGuide({...配置}) 后把三个组件放到对应位置;
* steps[].done 传布尔值即启用进度跟踪,不传则渲染为纯阅读步骤。
*/
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Button, SideSheet } from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
import {
BookOpenText,
CheckCircle2,
ChevronDown,
Compass,
Loader2,
} from 'lucide-react';
import './page-guide.css';
const seenKey = (guideKey) => `page-guide-seen-${guideKey}`;
// localStorage 在隐私模式/配额耗尽时会抛异常(知识页 verified-key 处已有同款
// 防御)。指引记忆是增强功能,绝不能反过来弄坏「使用指引」入口:写失败静默
// 放弃;读失败按「已看过」处理,避免每次进页都重复自动弹出。
// sessionSeen 兜"读正常、写抛异常"(配额耗尽)的场景:持久化失败时至少
// 本会话内不重复自动弹出——否则每次路由进页都会弹一次,比不弹更糟。
const sessionSeen = new Set();
const readSeen = (guideKey) => {
if (sessionSeen.has(guideKey)) return true;
try {
return window.localStorage.getItem(seenKey(guideKey)) === '1';
} catch {
return true;
}
};
const markSeen = (guideKey) => {
sessionSeen.add(guideKey);
try {
window.localStorage.setItem(seenKey(guideKey), '1');
} catch {
// 忽略:仅损失跨会话记忆(本会话由 sessionSeen 兜底)
}
};
const prefersReducedMotion = () =>
window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches ?? false;
// 指引 CTA 通用落点:滚动到目标区块并短暂高亮。延迟一拍等抽屉先关闭——
// SideSheet 关闭动画约 300ms,早于它执行 scrollIntoView 会被遮罩滚动锁吞掉。
export const flashGuideTarget = (selector, focusSelector) => {
window.setTimeout(() => {
const node = document.querySelector(selector);
if (!node) return;
node.scrollIntoView({
behavior: prefersReducedMotion() ? 'auto' : 'smooth',
block: 'center',
});
node.classList.add('page-guide-flash');
window.setTimeout(() => node.classList.remove('page-guide-flash'), 1700);
if (focusSelector) {
window.setTimeout(
() =>
document.querySelector(focusSelector)?.focus({ preventScroll: true }),
450,
);
}
}, 350);
};
export const usePageGuide = ({
guideKey,
title,
intro,
steps = [],
faq = [],
footer,
autoOpen = true,
// ready=false 时进度胶囊不渲染:页面数据未就绪前 done 全为 false,
// 会对老用户闪现一个错误的「0/N」胶囊。
ready = true,
}) => {
const [visible, setVisible] = useState(false);
// focusStep:从胶囊进入时定位展开的步骤;null 表示按「下一步」默认展开。
const [focusStep, setFocusStep] = useState(null);
useEffect(() => {
if (!autoOpen || !guideKey) return undefined;
if (readSeen(guideKey)) return undefined;
// 延迟一拍再展开,等页面首屏渲染完成,避免抽屉与骨架屏同时抢焦点。
const timer = window.setTimeout(() => {
// 二次核对:800ms 窗口内用户手动打开过(open() 已 markSeen)就不再
// 重弹——否则"快速开又关"后抽屉会自行复活一次。
if (readSeen(guideKey)) return;
markSeen(guideKey);
setVisible(true);
}, 800);
return () => window.clearTimeout(timer);
}, [autoOpen, guideKey]);
const progress = useMemo(() => {
const trackable = steps.some((step) => typeof step.done === 'boolean');
const doneCount = steps.filter((step) => step.done).length;
const nextIndex = trackable ? steps.findIndex((step) => !step.done) : -1;
return {
trackable,
doneCount,
total: steps.length,
nextIndex,
allDone: trackable && nextIndex === -1,
};
}, [steps]);
const open = useCallback(
(stepIndex = null) => {
if (guideKey) markSeen(guideKey);
setFocusStep(stepIndex);
setVisible(true);
},
[guideKey],
);
const close = useCallback(() => setVisible(false), []);
return {
visible,
open,
close,
focusStep,
progress,
ready,
config: { guideKey, title, intro, steps, faq, footer },
};
};
export const PageGuideTrigger = ({ guide, className = '' }) => {
const { t } = useTranslation();
return (
<Button
theme='borderless'
className={`page-guide-trigger ${className}`}
icon={<BookOpenText size={16} />}
onClick={() => guide.open()}
>
{t('使用指引')}
</Button>
);
};
export const PageGuidePill = ({ guide, className = '' }) => {
const { t } = useTranslation();
const { progress, config } = guide;
if (!guide.ready || !progress.trackable || progress.allDone) return null;
const nextStep = config.steps[progress.nextIndex];
return (
<button
type='button'
className={`page-guide-pill ${className}`}
onClick={() => guide.open(progress.nextIndex)}
>
<Compass size={14} className='page-guide-pill-icon' />
<span className='page-guide-pill-dots' aria-hidden='true'>
{config.steps.map((step, index) => (
<i
key={step.title}
className={
step.done
? 'is-done'
: index === progress.nextIndex
? 'is-next'
: ''
}
/>
))}
</span>
<span className='page-guide-pill-text'>
{t('快速上手')} {progress.doneCount}/{progress.total}
{' · '}
<strong>
{t('下一步')}
{t(':')}
{nextStep?.working ? t('进行中') : nextStep?.title}
</strong>
</span>
<span className='page-guide-pill-go'>{t('继续')} →</span>
</button>
);
};
const StepIcon = ({ step, index, isNext }) => {
if (step.done) return <CheckCircle2 size={14} />;
if (!step.done && step.working)
return <Loader2 size={13} className='page-guide-spin' />;
return <span className={isNext ? 'is-next' : ''}>{index + 1}</span>;
};
export const PageGuideDrawer = ({ guide }) => {
const { t } = useTranslation();
const { config, progress, visible, focusStep } = guide;
// 展开哪一步:胶囊点进来的目标步 > 数据驱动的下一步 > 第一步。
const defaultExpanded =
focusStep ?? (progress.nextIndex >= 0 ? progress.nextIndex : 0);
const [expanded, setExpanded] = useState(defaultExpanded);
useEffect(() => {
if (visible) setExpanded(defaultExpanded);
// 抽屉每次打开时重新对齐展开项;关闭期间不响应数据变化。
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible, focusStep]);
const runAction = (step) => {
guide.close();
if (typeof step.onAction === 'function') step.onAction();
};
return (
<SideSheet
className='page-guide-drawer'
closeOnEsc
title={
<div className='page-guide-head'>
<strong>{config.title}</strong>
{config.intro && <span>{config.intro}</span>}
</div>
}
visible={visible}
onCancel={guide.close}
width={420}
footer={
<div className='page-guide-foot'>
{config.footer || t('可随时从页面右上角「使用指引」再次打开')}
</div>
}
>
<div className='page-guide-body'>
{progress.trackable && (
<p className='page-guide-progress'>
{t('上手进度')}{' '}
<b>
{progress.doneCount}/{progress.total}
</b>
{' · '}
{progress.allDone
? t('已完成全部入门步骤')
: t('完成态由页面真实数据驱动,无需手动勾选')}
</p>
)}
<ol className='page-guide-steps'>
{config.steps.map((step, index) => {
const isNext = index === progress.nextIndex;
const isOpen = index === expanded;
const detailId = `page-guide-step-${config.guideKey}-${index}`;
const stateClass = step.done
? 'is-done'
: step.working
? 'is-working'
: isNext
? 'is-next'
: '';
return (
<li
key={step.title}
className={`page-guide-step ${stateClass} ${isOpen ? 'is-open' : ''}`}
>
<button
type='button'
className='page-guide-step-head'
aria-expanded={isOpen}
aria-controls={isOpen ? detailId : undefined}
onClick={() => setExpanded(isOpen ? -1 : index)}
>
<span className='page-guide-step-ic'>
<StepIcon step={step} index={index} isNext={isNext} />
</span>
<span className='page-guide-step-copy'>
<strong>
{step.title}
{step.done && (
<em className='page-guide-badge is-done'>
{t('已完成')}
</em>
)}
{!step.done && step.working && (
<em className='page-guide-badge is-working'>
{t('进行中')}
</em>
)}
{!step.done && !step.working && isNext && (
<em className='page-guide-badge'>{t('下一步')}</em>
)}
</strong>
{step.detail && <small>{step.detail}</small>}
</span>
<ChevronDown size={15} className='page-guide-step-arrow' />
</button>
{isOpen && (
<div className='page-guide-step-detail' id={detailId}>
{Array.isArray(step.points) && step.points.length > 0 && (
<ul>
{step.points.map((point) => (
<li key={point}>{point}</li>
))}
</ul>
)}
{step.cta && step.onAction && (
<button
type='button'
className='page-guide-step-cta'
onClick={() => runAction(step)}
>
{step.cta} →
</button>
)}
</div>
)}
</li>
);
})}
</ol>
{guide.config.faq.length > 0 && (
<>
<p className='page-guide-section-title'>{t('常见问题')}</p>
{config.faq.map((item) => (
<div className='page-guide-faq' key={item.q}>
<b>{item.q}</b>
<span>{item.a}</span>
</div>
))}
</>
)}
</div>
</SideSheet>
);
};
@@ -0,0 +1,396 @@
/* PageGuide —— 页面级使用指引(触发按钮 / 进度胶囊 / 侧滑抽屉)
* 配色全部从主题 token 派生,随亮暗色与品牌预设自动适配。 */
.page-guide-pill,
.page-guide-drawer {
--pg-ink: var(--text-primary);
--pg-muted: var(--text-secondary);
--pg-line: var(--border-subtle);
--pg-accent: var(--semi-color-primary);
--pg-accent-deep: var(--semi-color-primary-active, var(--semi-color-primary));
--pg-tint: color-mix(in srgb, var(--semi-color-primary) 8%, transparent);
--pg-tint-strong: color-mix(
in srgb,
var(--semi-color-primary) 14%,
transparent
);
--pg-tint-line: color-mix(
in srgb,
var(--semi-color-primary) 28%,
transparent
);
--pg-ok: var(--semi-color-success);
--pg-ok-tint: color-mix(in srgb, var(--semi-color-success) 14%, transparent);
}
/* ===== 进度胶囊 ===== */
.page-guide-pill {
display: inline-flex;
align-items: center;
gap: 10px;
max-width: 100%;
padding: 6px 8px 6px 14px;
border: 1px solid var(--pg-tint-line);
border-radius: 999px;
background: color-mix(
in srgb,
var(--semi-color-primary) 4%,
var(--surface-card)
);
color: var(--pg-ink);
font-size: 13px;
line-height: 1.5;
cursor: pointer;
transition:
border-color 0.15s ease,
box-shadow 0.15s ease;
}
.page-guide-pill:hover {
border-color: var(--pg-accent);
box-shadow: 0 2px 10px var(--pg-tint);
}
.page-guide-pill:focus-visible,
.page-guide-trigger:focus-visible,
.page-guide-step-head:focus-visible,
.page-guide-step-cta:focus-visible {
outline: 2px solid
color-mix(in srgb, var(--semi-color-primary) 55%, transparent);
outline-offset: 2px;
}
.page-guide-pill-icon {
flex: none;
color: var(--pg-accent);
}
.page-guide-pill-dots {
display: inline-flex;
gap: 4px;
flex: none;
}
.page-guide-pill-dots i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--pg-line);
}
.page-guide-pill-dots i.is-done {
background: var(--pg-ok);
}
.page-guide-pill-dots i.is-next {
background: var(--pg-accent);
}
.page-guide-pill-text {
color: var(--pg-muted);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.page-guide-pill-text strong {
color: var(--pg-accent);
font-weight: 600;
}
.page-guide-pill-go {
flex: none;
padding: 2px 10px;
border-radius: 999px;
background: var(--pg-tint);
color: var(--pg-accent);
font-size: 12px;
font-weight: 600;
}
/* ===== 抽屉 ===== */
.page-guide-drawer .semi-sidesheet-inner {
max-width: 100%;
}
.page-guide-drawer .semi-sidesheet-body {
padding: 14px 22px 20px;
overflow-y: auto;
}
/* 抽屉滚动条:复用品牌滚动条 token(同 app-scrollbar 的取色来源) */
.page-guide-drawer .semi-sidesheet-body::-webkit-scrollbar {
width: 6px;
}
.page-guide-drawer .semi-sidesheet-body::-webkit-scrollbar-thumb {
border-radius: 999px;
background: var(--brand-scroll);
}
.page-guide-drawer .semi-sidesheet-body::-webkit-scrollbar-thumb:hover {
background: var(--brand-scroll-hover);
}
.page-guide-head {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
}
.page-guide-head strong {
font-size: 16px;
color: var(--pg-ink);
letter-spacing: -0.01em;
}
.page-guide-head span {
font-size: 12.5px;
font-weight: 400;
color: var(--pg-muted);
line-height: 1.6;
}
.page-guide-progress {
margin: 0 0 12px;
font-size: 12px;
color: var(--pg-muted);
}
.page-guide-progress b {
color: var(--pg-ok);
font-variant-numeric: tabular-nums;
}
/* ===== 步骤 ===== */
.page-guide-steps {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
gap: 10px;
}
.page-guide-step {
border: 1px solid var(--pg-line);
border-radius: 10px;
overflow: hidden;
background: var(--surface-card);
}
.page-guide-step.is-next {
border-color: var(--pg-tint-line);
background: color-mix(
in srgb,
var(--semi-color-primary) 5%,
var(--surface-card)
);
}
.page-guide-step-head {
display: flex;
align-items: flex-start;
gap: 10px;
width: 100%;
padding: 12px 14px;
border: 0;
background: none;
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
}
.page-guide-step-ic {
flex: none;
display: flex;
align-items: center;
justify-content: center;
width: 22px;
height: 22px;
margin-top: 1px;
border: 1px solid var(--pg-line);
border-radius: 50%;
font-size: 12px;
color: var(--pg-muted);
}
.page-guide-step.is-done .page-guide-step-ic {
border-color: transparent;
background: var(--pg-ok-tint);
color: var(--pg-ok);
}
.page-guide-step.is-next .page-guide-step-ic {
border-color: transparent;
background: var(--pg-accent);
color: var(--semi-color-white, var(--surface-card));
font-weight: 600;
}
.page-guide-step.is-working .page-guide-step-ic {
border-color: var(--pg-tint-line);
color: var(--pg-accent);
}
.page-guide-step-copy {
flex: 1;
min-width: 0;
}
.page-guide-step-copy strong {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
font-size: 13.5px;
color: var(--pg-ink);
}
.page-guide-step-copy small {
display: block;
margin-top: 2px;
font-size: 12.5px;
line-height: 1.6;
color: var(--pg-muted);
}
.page-guide-badge {
flex: none;
padding: 1px 8px;
border: 1px solid var(--pg-tint-line);
border-radius: 999px;
background: var(--surface-card);
color: var(--pg-accent);
font-size: 11px;
font-style: normal;
font-weight: 600;
}
.page-guide-badge.is-done {
border-color: transparent;
background: var(--pg-ok-tint);
color: var(--pg-ok);
}
.page-guide-badge.is-working {
border-color: transparent;
background: var(--pg-tint);
}
.page-guide-step-arrow {
flex: none;
margin-top: 3px;
color: var(--pg-muted);
transition: transform 0.15s ease;
}
.page-guide-step.is-open .page-guide-step-arrow {
transform: rotate(180deg);
}
.page-guide-step-detail {
padding: 0 14px 12px 46px;
}
.page-guide-step-detail ul {
margin: 0;
padding-left: 16px;
font-size: 12.5px;
line-height: 1.7;
color: var(--pg-muted);
}
.page-guide-step-detail li + li {
margin-top: 4px;
}
.page-guide-step-cta {
margin-top: 10px;
padding: 5px 12px;
border: 1px solid var(--pg-tint-line);
border-radius: 8px;
background: var(--surface-card);
color: var(--pg-accent);
font-size: 12.5px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s ease;
}
.page-guide-step-cta:hover {
background: var(--pg-tint);
}
/* ===== FAQ ===== */
.page-guide-section-title {
margin: 20px 0 10px;
font-size: 12px;
font-weight: 600;
letter-spacing: 0.08em;
color: var(--pg-muted);
}
.page-guide-faq {
margin-bottom: 10px;
padding-left: 12px;
border-left: 2px solid var(--pg-tint-line);
font-size: 12.5px;
line-height: 1.7;
}
.page-guide-faq b {
display: block;
color: var(--pg-ink);
font-weight: 600;
}
.page-guide-faq span {
color: var(--pg-muted);
}
.page-guide-foot {
font-size: 12px;
color: var(--pg-muted);
}
/* 指引 CTA 落点的短暂高亮(flashGuideTarget) */
.page-guide-flash {
animation: page-guide-flash-pulse 1.6s ease;
}
@keyframes page-guide-flash-pulse {
0%,
100% {
box-shadow: none;
}
30% {
box-shadow: 0 0 0 4px
color-mix(in srgb, var(--semi-color-primary) 45%, transparent);
}
}
.page-guide-spin {
animation: page-guide-spin 1s linear infinite;
}
@keyframes page-guide-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.page-guide-pill,
.page-guide-step-arrow,
.page-guide-step-cta {
transition: none;
}
.page-guide-spin,
.page-guide-flash {
animation: none;
}
}