/*
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 .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useState, useEffect } from 'react';
import { Avatar, Button } from '@/components/ui';
import {
MessageCircle,
Sparkles,
ScanLine,
Zap,
ArrowRight,
} from 'lucide-react';
import { API } from '../../helpers';
import { useTranslation } from 'react-i18next';
/**
* 自定义 Hook:获取客服展示数据
* 供父组件使用,避免多个 CustomerServiceQRCard 实例重复请求
*/
export const useCustomerServiceDisplay = () => {
const [loading, setLoading] = useState(true);
const [agent, setAgent] = useState(null);
const [hasConfig, setHasConfig] = useState(false);
useEffect(() => {
const fetchCustomerService = async () => {
try {
// 先检查是否有启用的客服配置
const configRes = await API.get('/api/customer-service/config');
if (configRes.data.success && configRes.data.data?.enabled) {
setHasConfig(true);
// 使用专门的展示接口获取客服信息(只读,不增加会话计数)
const agentRes = await API.get('/api/customer-service/display');
if (agentRes.data.success && agentRes.data.data) {
setAgent(agentRes.data.data);
}
}
} catch (error) {
// 静默处理错误,不显示给用户
console.error('Failed to fetch customer service:', error);
} finally {
setLoading(false);
}
};
fetchCustomerService();
}, []);
// 检查客服是否至少有一种联系方式
const hasContactMethod = agent && (agent.qrcode_url || agent.wecom_url);
// 是否应该显示客服卡片
const showCard = !loading && hasConfig && !!agent && !!hasContactMethod;
return { loading, agent, hasConfig, showCard };
};
/**
* 客服二维码卡片组件(紧凑版)
* 用于在密钥管理页面侧边栏展示客服二维码,引导用户添加客服咨询
*
* @param {Object} props
* @param {Object} props.agent - 客服信息(由父组件通过 useCustomerServiceDisplay 获取后传入)
*/
const CustomerServiceQRCard = ({ agent }) => {
const { t } = useTranslation();
const [qrcodeError, setQrcodeError] = useState(false);
// 如果没有客服数据,不显示组件
if (!agent) {
return null;
}
// 处理二维码加载失败
const handleQrcodeError = () => {
console.warn('QR code image failed to load:', agent.qrcode_url);
setQrcodeError(true);
};
return (