import { GetServerSidePropsContext } from "next"; import { ChangeEventHandler, useState } from "react"; import { getAppRegistry, getAppRegistryWithCredentials } from "@calcom/app-store/_appRegistry"; import { classNames } from "@calcom/lib"; import { getSession } from "@calcom/lib/auth"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { AppCategories } from "@calcom/prisma/client"; import { inferSSRProps } from "@calcom/types/inferSSRProps"; import { AllApps, AppStoreCategories, HorizontalTabItemProps, HorizontalTabs, TextField, PopularAppsSlider, } from "@calcom/ui"; import { FiSearch } from "@calcom/ui/components/icon"; import AppsLayout from "@components/apps/layouts/AppsLayout"; import { ssgInit } from "@server/lib/ssg"; const tabs: HorizontalTabItemProps[] = [ { name: "app_store", href: "/apps", }, { name: "installed_apps", href: "/apps/installed", }, ]; function AppsSearch({ onChange, className, }: { onChange: ChangeEventHandler; className?: string; }) { return ( } addOnClassname="!border-gray-100" containerClassName={classNames("focus:!ring-offset-0", className)} type="search" autoComplete="false" onChange={onChange} /> ); } export default function Apps({ categories, appStore }: inferSSRProps) { const { t } = useLocale(); const [searchText, setSearchText] = useState(undefined); return ( (
setSearchText(e.target.value)} />
)} headerClassName="sm:hidden lg:block hidden" emptyStore={!appStore.length}>
{!searchText && ( <> )} category.name)} />
); } export const getServerSideProps = async (context: GetServerSidePropsContext) => { const ssg = await ssgInit(context); const session = await getSession(context); let appStore; if (session?.user?.id) { appStore = await getAppRegistryWithCredentials(session.user.id); } else { appStore = await getAppRegistry(); } const categoryQuery = appStore.map(({ categories }) => ({ categories: categories || [], })); const categories = categoryQuery.reduce((c, app) => { for (const category of app.categories) { c[category] = c[category] ? c[category] + 1 : 1; } return c; }, {} as Record); return { props: { categories: Object.entries(categories) .map(([name, count]): { name: AppCategories; count: number } => ({ name: name as AppCategories, count, })) .sort(function (a, b) { return b.count - a.count; }), appStore, trpcState: ssg.dehydrate(), }, }; };