/*
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, { 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 = () => (
);
// 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 (
{[1, 2].map((sectionIdx) => (
{/* Skeleton section header */}
{Array.from({ length: 8 }).map((_, i) => (
))}
))}
);
}
if (filteredModels.length === 0) {
return (
}
darkModeImage={
}
description={
{t('No models found')}
}
/>
);
}
return (
{sections.map((section, sectionIndex) => (
))}
{/* CSS for animations */}
);
};
// 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 (
{/* Section Header */}
{vendorIcon ? (
{getLobeHubIcon(vendorIcon, 20)}
) : (
)}
{section.title}
{section.models.length}
{t('MODELS')}
{/* Cards Grid — render skeletons when not in view */}
{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 (
openModelDetail(model)}
onCopy={copyText}
/>
);
})
: // Placeholder skeleton cards when section is not yet visible
Array.from({ length: Math.min(section.models.length, 8) }).map(
(_, i) =>
,
)}
);
},
);
VendorSection.displayName = 'VendorSection';
export default BentoGrid;