319 lines
10 KiB
React
319 lines
10 KiB
React
/*
|
|
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
|
|
*/
|
|
import React, { useMemo, useRef, useEffect, useState } from 'react';
|
|
import { calculateModelPrice, getLobeHubIcon } from '../../../../helpers';
|
|
import BentoCard from './BentoCard';
|
|
import { Empty } from '@douyinfe/semi-ui';
|
|
import {
|
|
IllustrationNoResult,
|
|
IllustrationNoResultDark,
|
|
} from '../../../common/illustrations';
|
|
import { Layers } from 'lucide-react';
|
|
|
|
// Vendor accent colors for section headers
|
|
const vendorAccents = {
|
|
OpenAI: 'from-emerald-500 to-teal-500',
|
|
Anthropic: 'from-amber-500 to-orange-500',
|
|
Google: 'from-blue-500 to-brand-to',
|
|
Meta: 'from-blue-600 to-blue-700',
|
|
Mistral: 'from-orange-500 to-red-500',
|
|
Midjourney: 'from-brand-from to-brand-to',
|
|
default: 'from-brand-from to-brand-to',
|
|
};
|
|
|
|
const getVendorAccent = (vendor) =>
|
|
vendorAccents[vendor] || vendorAccents.default;
|
|
|
|
// Skeleton card for loading state
|
|
const SkeletonCard = () => (
|
|
<div className='app-card overflow-hidden min-h-[140px] animate-pulse'>
|
|
<div className='p-5 flex flex-col gap-3'>
|
|
<div className='flex items-start justify-between'>
|
|
<div className='w-12 h-12 bg-gray-200 dark:bg-neutral-700 rounded-xl' />
|
|
<div className='w-6 h-6 bg-gray-200 dark:bg-neutral-700 rounded-lg' />
|
|
</div>
|
|
<div className='w-3/4 h-5 bg-gray-200 dark:bg-neutral-700 rounded-lg' />
|
|
<div className='mt-2 px-3 py-3 bg-gray-100 dark:bg-neutral-800 rounded-xl'>
|
|
<div className='w-full h-3 bg-gray-200 dark:bg-neutral-700 rounded mb-2' />
|
|
<div className='w-2/3 h-3 bg-gray-200 dark:bg-neutral-700 rounded' />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
// Hook: observe element visibility via IntersectionObserver
|
|
const useInView = (options = {}) => {
|
|
const ref = useRef(null);
|
|
const [inView, setInView] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const el = ref.current;
|
|
if (!el) return;
|
|
const observer = new IntersectionObserver(
|
|
([entry]) => {
|
|
if (entry.isIntersecting) {
|
|
setInView(true);
|
|
observer.disconnect(); // only trigger once
|
|
}
|
|
},
|
|
{ rootMargin: '200px 0px', ...options },
|
|
);
|
|
observer.observe(el);
|
|
return () => observer.disconnect();
|
|
}, []);
|
|
|
|
return [ref, inView];
|
|
};
|
|
|
|
const BentoGrid = ({
|
|
filteredModels,
|
|
loading,
|
|
selectedGroup,
|
|
groupRatio,
|
|
groupModelDiscount,
|
|
tokenUnit,
|
|
displayPrice,
|
|
currency,
|
|
hidePrice,
|
|
t,
|
|
copyText,
|
|
openModelDetail,
|
|
}) => {
|
|
// Group models by Vendor for sections
|
|
const sections = useMemo(() => {
|
|
if (!filteredModels) return [];
|
|
|
|
const groups = {};
|
|
|
|
filteredModels.forEach((model) => {
|
|
const vendor = model.vendor_name || 'Others';
|
|
if (!groups[vendor]) {
|
|
groups[vendor] = [];
|
|
}
|
|
groups[vendor].push(model);
|
|
});
|
|
|
|
// Sort keys with priority vendors first
|
|
const sortedKeys = Object.keys(groups).sort((a, b) => {
|
|
const priority = ['OpenAI', 'Anthropic', 'Google', 'Midjourney'];
|
|
const idxA = priority.indexOf(a);
|
|
const idxB = priority.indexOf(b);
|
|
if (idxA !== -1 && idxB !== -1) return idxA - idxB;
|
|
if (idxA !== -1) return -1;
|
|
if (idxB !== -1) return 1;
|
|
return a.localeCompare(b);
|
|
});
|
|
|
|
return sortedKeys.map((key) => ({
|
|
title: key,
|
|
models: groups[key],
|
|
}));
|
|
}, [filteredModels]);
|
|
|
|
// Loading skeleton state
|
|
if (loading) {
|
|
return (
|
|
<div className='container mx-auto px-6 py-6 pb-20'>
|
|
{[1, 2].map((sectionIdx) => (
|
|
<div key={sectionIdx} className='mb-10'>
|
|
{/* Skeleton section header */}
|
|
<div className='flex items-center gap-4 mb-5'>
|
|
<div className='w-10 h-10 bg-gray-200 dark:bg-neutral-700 rounded-xl animate-pulse' />
|
|
<div className='w-32 h-6 bg-gray-200 dark:bg-neutral-700 rounded-lg animate-pulse' />
|
|
<div className='flex-1 h-px bg-gray-200 dark:bg-neutral-700' />
|
|
<div className='w-16 h-7 bg-gray-200 dark:bg-neutral-700 rounded-full animate-pulse' />
|
|
</div>
|
|
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-5'>
|
|
{Array.from({ length: 8 }).map((_, i) => (
|
|
<SkeletonCard key={i} />
|
|
))}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (filteredModels.length === 0) {
|
|
return (
|
|
<div className='flex flex-col items-center justify-center py-32'>
|
|
<div className='relative'>
|
|
<div className='absolute inset-0 bg-gradient-to-r from-brand-from/10 to-brand-to/10 blur-3xl rounded-full' />
|
|
<Empty
|
|
image={<IllustrationNoResult style={{ width: 180, height: 180 }} />}
|
|
darkModeImage={
|
|
<IllustrationNoResultDark style={{ width: 180, height: 180 }} />
|
|
}
|
|
description={
|
|
<span className='text-gray-500 dark:text-neutral-400 font-medium mt-4 block'>
|
|
{t('No models found')}
|
|
</span>
|
|
}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className='container mx-auto px-6 py-6 pb-20'>
|
|
{sections.map((section, sectionIndex) => (
|
|
<VendorSection
|
|
key={section.title}
|
|
section={section}
|
|
sectionIndex={sectionIndex}
|
|
selectedGroup={selectedGroup}
|
|
groupRatio={groupRatio}
|
|
groupModelDiscount={groupModelDiscount}
|
|
tokenUnit={tokenUnit}
|
|
displayPrice={displayPrice}
|
|
currency={currency}
|
|
hidePrice={hidePrice}
|
|
t={t}
|
|
copyText={copyText}
|
|
openModelDetail={openModelDetail}
|
|
/>
|
|
))}
|
|
|
|
{/* CSS for animations */}
|
|
<style>{`
|
|
@keyframes fadeInUp {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(12px);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
}
|
|
`}</style>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
// Lazy-rendered vendor section — only renders cards when scrolled into view
|
|
const VendorSection = React.memo(
|
|
({
|
|
section,
|
|
sectionIndex,
|
|
selectedGroup,
|
|
groupRatio,
|
|
groupModelDiscount,
|
|
tokenUnit,
|
|
displayPrice,
|
|
currency,
|
|
hidePrice,
|
|
t,
|
|
copyText,
|
|
openModelDetail,
|
|
}) => {
|
|
const [sectionRef, isInView] = useInView();
|
|
const accent = getVendorAccent(section.title);
|
|
// 分组标题优先展示真实供应商 logo(取该组任一模型的 vendor_icon),
|
|
// 没有时回退到原来的渐变通用图标
|
|
const vendorIcon = section.models.find((m) => m.vendor_icon)?.vendor_icon;
|
|
|
|
return (
|
|
<div ref={sectionRef} className='mb-10'>
|
|
{/* Section Header */}
|
|
<div className='flex items-center gap-4 mb-5'>
|
|
{vendorIcon ? (
|
|
<div className='p-2.5 rounded-xl bg-white/80 dark:bg-neutral-800/80 backdrop-blur-sm border border-gray-200/60 dark:border-neutral-700/60 shadow-sm'>
|
|
<div className='w-5 h-5 flex items-center justify-center'>
|
|
{getLobeHubIcon(vendorIcon, 20)}
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div
|
|
className={`p-2.5 rounded-xl bg-gradient-to-br ${accent} shadow-lg shadow-brand/20`}
|
|
>
|
|
<Layers size={20} className='text-white' />
|
|
</div>
|
|
)}
|
|
|
|
<h2 className='text-2xl font-bold text-gray-900 dark:text-white tracking-tight'>
|
|
{section.title}
|
|
</h2>
|
|
|
|
<div className='flex-1 h-px bg-gradient-to-r from-gray-200 via-gray-300/50 to-transparent dark:from-neutral-700 dark:via-neutral-800/50 dark:to-transparent' />
|
|
|
|
<div className='flex items-center gap-2'>
|
|
<span
|
|
className={`text-sm font-bold font-mono px-3 py-1.5 rounded-full bg-gradient-to-r ${accent} text-white shadow-md`}
|
|
>
|
|
{section.models.length}
|
|
</span>
|
|
<span className='text-sm text-gray-500 dark:text-neutral-400 font-medium'>
|
|
{t('MODELS')}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Cards Grid — render skeletons when not in view */}
|
|
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-5'>
|
|
{isInView
|
|
? section.models.map((model, index) => {
|
|
const priceData = calculateModelPrice({
|
|
record: model,
|
|
selectedGroup,
|
|
groupRatio,
|
|
groupModelDiscount,
|
|
tokenUnit,
|
|
displayPrice,
|
|
currency,
|
|
});
|
|
|
|
// Cap animation delay at 600ms so cards never stay invisible too long
|
|
const delay = Math.min(index * 40, 600);
|
|
|
|
return (
|
|
<div
|
|
key={model.key || index}
|
|
style={{
|
|
animationDelay: `${delay}ms`,
|
|
animation: 'fadeInUp 0.35s ease-out forwards',
|
|
opacity: 0,
|
|
}}
|
|
>
|
|
<BentoCard
|
|
model={model}
|
|
priceData={priceData}
|
|
hidePrice={hidePrice}
|
|
t={t}
|
|
onClick={() => openModelDetail(model)}
|
|
onCopy={copyText}
|
|
/>
|
|
</div>
|
|
);
|
|
})
|
|
: // Placeholder skeleton cards when section is not yet visible
|
|
Array.from({ length: Math.min(section.models.length, 8) }).map(
|
|
(_, i) => <SkeletonCard key={i} />,
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
},
|
|
);
|
|
|
|
VendorSection.displayName = 'VendorSection';
|
|
|
|
export default BentoGrid;
|