From bc6f959c7ade8fd554942272e07f99c81cae5991 Mon Sep 17 00:00:00 2001 From: Nafees Nazik <84864519+G3root@users.noreply.github.com> Date: Thu, 20 Oct 2022 23:29:24 +0530 Subject: [PATCH] fix: make all apps category tab scrollable (#5113) * feat: make left and right button workable * refactor: separate category tab into a seperate component to reduce unnecessary re-rendering while scrolling through the arrow buttons. Co-authored-by: Peer Richelsen Co-authored-by: alannnc --- packages/ui/v2/core/apps/AllApps.tsx | 146 ++++++++++++++++----------- 1 file changed, 85 insertions(+), 61 deletions(-) diff --git a/packages/ui/v2/core/apps/AllApps.tsx b/packages/ui/v2/core/apps/AllApps.tsx index 44bfbaecb1..8da15525b2 100644 --- a/packages/ui/v2/core/apps/AllApps.tsx +++ b/packages/ui/v2/core/apps/AllApps.tsx @@ -38,13 +38,95 @@ export function useShouldShowArrows() { type AllAppsPropsType = { apps: (App & { credentials: Credential[] | undefined })[] }; -export default function AllApps({ apps }: AllAppsPropsType) { +interface CategoryTabProps { + selectedCategory: string | null; + categories: string[]; +} + +function CategoryTab({ selectedCategory, categories }: CategoryTabProps) { const { t } = useLocale(); const router = useRouter(); + const { ref, calculateScroll, leftVisible, rightVisible } = useShouldShowArrows(); + const handleLeft = () => { + if (ref.current) { + ref.current.scrollLeft -= 100; + } + }; + + const handleRight = () => { + if (ref.current) { + ref.current.scrollLeft += 100; + } + }; + return ( +
+

+ {t("explore_apps", { + category: + (selectedCategory && selectedCategory[0].toUpperCase() + selectedCategory.slice(1)) || + t("all_apps"), + })} +

+ {leftVisible && ( + + )} +
    calculateScroll(e)} + ref={ref}> +
  • { + router.replace(router.asPath.split("?")[0], undefined, { shallow: true }); + }} + className={classNames( + selectedCategory === null ? "bg-gray-900 text-gray-50" : "bg-gray-50 text-gray-900", + "rounded-md px-4 py-2.5 text-sm font-medium hover:cursor-pointer hover:bg-gray-900 hover:text-gray-50" + )}> + {t("all_apps")} +
  • + {categories.map((cat, pos) => ( +
  • { + if (selectedCategory === cat) { + router.replace(router.asPath.split("?")[0], undefined, { shallow: true }); + } else { + router.replace(router.asPath.split("?")[0] + `?category=${cat}`, undefined, { + shallow: true, + }); + } + }} + className={classNames( + selectedCategory === cat ? "bg-gray-900 text-gray-50" : "bg-gray-50 text-gray-900", + "rounded-md px-4 py-2.5 text-sm font-medium hover:cursor-pointer hover:bg-gray-900 hover:text-gray-50" + )}> + {cat[0].toUpperCase() + cat.slice(1)} +
  • + ))} +
+ {rightVisible && ( + + )} +
+ ); +} + +export default function AllApps({ apps }: AllAppsPropsType) { + const router = useRouter(); const [selectedCategory, setSelectedCategory] = useState(null); const [appsContainerRef] = useAutoAnimate(); - const { ref, calculateScroll, leftVisible, rightVisible } = useShouldShowArrows(); + const categories = apps .map((app) => app.category) .filter((cat, pos, self) => { @@ -61,65 +143,7 @@ export default function AllApps({ apps }: AllAppsPropsType) { return (
-
-

- {t("explore_apps", { - category: - (selectedCategory && selectedCategory[0].toUpperCase() + selectedCategory.slice(1)) || - t("all_apps"), - })} -

- {leftVisible && ( -
-
- -
-
-
- )} -
    calculateScroll(e)} - ref={ref}> -
  • { - router.replace(router.asPath.split("?")[0], undefined, { shallow: true }); - }} - className={classNames( - selectedCategory === null ? "bg-gray-900 text-gray-50" : "bg-gray-50 text-gray-900", - "rounded-md px-4 py-2.5 text-sm font-medium hover:cursor-pointer hover:bg-gray-900 hover:text-gray-50" - )}> - {t("all_apps")} -
  • - {categories.map((cat, pos) => ( -
  • { - if (selectedCategory === cat) { - router.replace(router.asPath.split("?")[0], undefined, { shallow: true }); - } else { - router.replace(router.asPath.split("?")[0] + `?category=${cat}`, undefined, { - shallow: true, - }); - } - }} - className={classNames( - selectedCategory === cat ? "bg-gray-900 text-gray-50" : "bg-gray-50 text-gray-900", - "rounded-md px-4 py-2.5 text-sm font-medium hover:cursor-pointer hover:bg-gray-900 hover:text-gray-50" - )}> - {cat[0].toUpperCase() + cat.slice(1)} -
  • - ))} -
- {rightVisible && ( -
-
-
- -
-
- )} -
+