Skip to content

Commit

Permalink
fix: improve useInViewObserver hook to trigger when observer is alrea…
Browse files Browse the repository at this point in the history
…dy in view on mount (calcom#18455)
  • Loading branch information
cnhhoang850 authored and MuhammadAimanSulaiman committed Feb 25, 2025
1 parent cc7732b commit 8510d50
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions packages/ui/components/apps/AllApps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import { useAutoAnimate } from "@formkit/auto-animate/react";
import type { AppCategories } from "@prisma/client";
import { usePathname, useRouter } from "next/navigation";
import type { UIEvent } from "react";
import { useEffect, useRef, useState } from "react";

import { classNames } from "@calcom/lib";
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import type { UserAdminTeams } from "@calcom/lib/server/repository/user";
import type { AppFrontendPayload as App } from "@calcom/types/App";
Expand Down Expand Up @@ -59,14 +57,13 @@ interface CategoryTabProps {
selectedCategory: string | null;
categories: string[];
searchText?: string;
onCategoryChange: (category: string | null) => void;
}

function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabProps) {
const pathname = usePathname();
const searchParams = useCompatSearchParams();
function CategoryTab({ selectedCategory, categories, searchText, onCategoryChange }: CategoryTabProps) {
const { t } = useLocale();
const router = useRouter();
const { ref, calculateScroll, leftVisible, rightVisible } = useShouldShowArrows();

const handleLeft = () => {
if (ref.current) {
ref.current.scrollLeft -= 100;
Expand All @@ -78,6 +75,7 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
ref.current.scrollLeft += 100;
}
};

return (
<div className="relative mb-4 flex flex-col justify-between lg:flex-row lg:items-center">
<h2 className="text-emphasis hidden text-base font-semibold leading-none sm:block">
Expand All @@ -90,7 +88,7 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
})}
</h2>
{leftVisible && (
<button onClick={handleLeft} className="absolute bottom-0 flex md:-top-1 md:left-1/2">
<button onClick={handleLeft} className="absolute bottom-0 flex lg:left-1/2">
<div className="bg-default flex h-12 w-5 items-center justify-end">
<Icon name="chevron-left" className="text-subtle h-4 w-4" />
</div>
Expand All @@ -103,9 +101,7 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
ref={ref}>
<li
onClick={() => {
if (pathname !== null) {
router.replace(pathname);
}
onCategoryChange(null);
}}
className={classNames(
selectedCategory === null ? "bg-emphasis text-default" : "bg-muted text-emphasis",
Expand All @@ -118,13 +114,9 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
key={pos}
onClick={() => {
if (selectedCategory === cat) {
if (pathname !== null) {
router.replace(pathname);
}
onCategoryChange(null);
} else {
const _searchParams = new URLSearchParams(searchParams ?? undefined);
_searchParams.set("category", cat);
router.replace(`${pathname}?${_searchParams.toString()}`);
onCategoryChange(cat);
}
}}
className={classNames(
Expand All @@ -136,7 +128,7 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
))}
</ul>
{rightVisible && (
<button onClick={handleRight} className="absolute bottom-0 right-0 flex md:-top-1">
<button onClick={handleRight} className="absolute bottom-0 right-0 flex ">
<div className="to-default flex h-12 w-5 bg-gradient-to-r from-transparent" />
<div className="bg-default flex h-12 w-5 items-center justify-end">
<Icon name="chevron-right" className="text-subtle h-4 w-4" />
Expand All @@ -148,22 +140,15 @@ function CategoryTab({ selectedCategory, categories, searchText }: CategoryTabPr
}

export function AllApps({ apps, searchText, categories, userAdminTeams }: AllAppsPropsType) {
const searchParams = useCompatSearchParams();
const { t } = useLocale();
const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
const [appsContainerRef, enableAnimation] = useAutoAnimate<HTMLDivElement>();
const categoryQuery = searchParams?.get("category");

if (searchText) {
enableAnimation && enableAnimation(false);
}

useEffect(() => {
const queryCategory =
typeof categoryQuery === "string" && categories.includes(categoryQuery) ? categoryQuery : null;
setSelectedCategory(queryCategory);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [categoryQuery]);
const handleCategoryChange = (category: string | null) => {
const validCategory =
category && typeof category === "string" && categories.includes(category) ? category : null;
setSelectedCategory(validCategory);
};

const filteredApps = apps
.filter((app) =>
Expand All @@ -182,7 +167,12 @@ export function AllApps({ apps, searchText, categories, userAdminTeams }: AllApp

return (
<div>
<CategoryTab selectedCategory={selectedCategory} searchText={searchText} categories={categories} />
<CategoryTab
selectedCategory={selectedCategory}
searchText={searchText}
categories={categories}
onCategoryChange={handleCategoryChange}
/>
{filteredApps.length ? (
<div
className="grid gap-3 lg:grid-cols-4 [@media(max-width:1270px)]:grid-cols-3 [@media(max-width:500px)]:grid-cols-1 [@media(max-width:730px)]:grid-cols-1"
Expand Down

0 comments on commit 8510d50

Please sign in to comment.