'use client' import { useCallback, useEffect, useState } from 'react' import type { FC } from 'react' import useSWRInfinite from 'swr/infinite' import { useTranslation } from 'react-i18next' import { flatten } from 'lodash-es' import { useRouter, useSelectedLayoutSegment } from 'next/navigation' import classNames from 'classnames' import { CircleStackIcon, PuzzlePieceIcon } from '@heroicons/react/24/outline' import { CommandLineIcon, Squares2X2Icon } from '@heroicons/react/24/solid' import Link from 'next/link' import AccountDropdown from './account-dropdown' import Nav from './nav' import s from './index.module.css' import type { GithubRepo, LangGeniusVersionResponse, UserProfileResponse } from '@/models/common' import type { AppListResponse } from '@/models/app' import NewAppDialog from '@/app/(commonLayout)/apps/NewAppDialog' import { WorkspaceProvider } from '@/context/workspace-context' import { useDatasetsContext } from '@/context/datasets-context' import { fetchAppList } from '@/service/apps' const BuildAppsIcon = ({ isSelected }: { isSelected: boolean }) => ( ) export type IHeaderProps = { curAppId?: string userProfile: UserProfileResponse onLogout: () => void langeniusVersionInfo: LangGeniusVersionResponse isBordered: boolean } const navClassName = ` flex items-center relative mr-3 px-3 h-8 rounded-xl font-medium text-[14px] cursor-pointer ` const headerEnvClassName: { [k: string]: string } = { DEVELOPMENT: 'bg-[#FEC84B] border-[#FDB022] text-[#93370D]', TESTING: 'bg-[#A5F0FC] border-[#67E3F9] text-[#164C63]', } const getKey = (pageIndex: number, previousPageData: AppListResponse) => { if (!pageIndex || previousPageData.has_more) return { url: 'apps', params: { page: pageIndex + 1, limit: 30 } } return null } const Header: FC = ({ curAppId, userProfile, onLogout, langeniusVersionInfo, isBordered, }) => { const { t } = useTranslation() const [showNewAppDialog, setShowNewAppDialog] = useState(false) const { data: appsData, isLoading, setSize } = useSWRInfinite(curAppId ? getKey : () => null, fetchAppList, { revalidateFirstPage: false }) const { datasets, currentDataset } = useDatasetsContext() const router = useRouter() const showEnvTag = langeniusVersionInfo.current_env === 'TESTING' || langeniusVersionInfo.current_env === 'DEVELOPMENT' const selectedSegment = useSelectedLayoutSegment() const isPluginsComingSoon = selectedSegment === 'plugins-coming-soon' const isExplore = selectedSegment === 'explore' const [starCount, setStarCount] = useState(0) useEffect(() => { globalThis.fetch('https://api.github.com/repos/langgenius/dify').then(res => res.json()).then((data: GithubRepo) => { setStarCount(data.stargazers_count) }) }, []) const appItems = flatten(appsData?.map(appData => appData.data)) const handleLoadmore = useCallback(() => { if (isLoading) return setSize(size => size + 1) }, [setSize, isLoading]) return (
{ starCount > 0 && (
Star
{`${starCount}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
) }
{t('common.menus.explore')}
{ showEnvTag && (
{ langeniusVersionInfo.current_env === 'TESTING' && ( <>
{t('common.environment.testing')} ) } { langeniusVersionInfo.current_env === 'DEVELOPMENT' && ( <> {t('common.environment.development')} ) }
) }
setShowNewAppDialog(false)} />
) } export default Header