feat: Page loading indicator (#10195)

This commit is contained in:
sean-brydon authored and GitHub committed 2023-07-17 13:00:44 -07:00
1 parent 64147d3618
commit 2ac709630f
4 files changed
+99 -87

No files matched your search

@@ -0,0 +1,80 @@
import { useNProgress } from "@tanem/react-nprogress";
import type { NextRouter } from "next/router";
import type { PropsWithChildren } from "react";
import { useEffect, useState, forwardRef } from "react";
import { classNames } from "@calcom/lib";
interface NProgressOptions {
isAnimating: boolean;
}
export type NProgressProps = PropsWithChildren<NProgressOptions>;
const NProgress = forwardRef<HTMLDivElement, NProgressProps>(({ isAnimating, ...rest }, ref) => {
const { animationDuration, isFinished, progress } = useNProgress({
isAnimating,
});
return (
<div
{...rest}
ref={ref}
className={classNames("fixed left-0 top-0 w-full", isFinished ? "opacity-0" : "opacity-100")}
style={{
transition: `opacity ${animationDuration}ms linear`,
}}>
<div
className="bg-brand-emphasis h-0.5 w-full"
style={{
marginLeft: `${(-1 + progress) * 100}%`,
transition: `margin-left ${animationDuration}ms linear`,
}}
/>
</div>
);
});
NProgress.displayName = "NProgress";
export const NProgressNextRouter = ({ router }: { router: NextRouter }) => {
const [state, setState] = useState({
isRouteChanging: false,
loadingKey: 0,
});
useEffect(() => {
const handleRouteChangeStart = () => {
setState((prevState) => ({
...prevState,
isRouteChanging: true,
loadingKey: prevState.loadingKey ^ 1,
}));
};
const handleRouteChangeEnd = () => {
setState((prevState) => ({
...prevState,
isRouteChanging: false,
}));
};
router.events.on("routeChangeStart", handleRouteChangeStart);
router.events.on("routeChangeComplete", handleRouteChangeEnd);
router.events.on("routeChangeError", handleRouteChangeEnd);
return () => {
router.events.off("routeChangeStart", handleRouteChangeStart);
router.events.off("routeChangeComplete", handleRouteChangeEnd);
router.events.off("routeChangeError", handleRouteChangeEnd);
};
}, [router.events]);
const isAnimating = state.isRouteChanging;
return (
<>
<NProgress isAnimating={isAnimating} key={state.loadingKey} />
</>
);
};
+3
View File
@@ -80,6 +80,7 @@ import {
} from "@calcom/ui/components/icon";
import FreshChatProvider from "../ee/support/lib/freshchat/FreshChatProvider";
import { NProgressNextRouter } from "./NProgressPageIndicator";
import { TeamInviteBadge } from "./TeamInviteBadge";
// need to import without ssr to prevent hydration errors
@@ -162,6 +163,7 @@ function useRedirectToOnboardingIfNeeded() {
}
const Layout = (props: LayoutProps) => {
const router = useRouter();
const pageTitle = typeof props.heading === "string" && !props.title ? props.heading : props.title;
const bannerRef = useRef<HTMLDivElement | null>(null);
const [bannersHeight, setBannersHeight] = useState<number>(0);
@@ -201,6 +203,7 @@ const Layout = (props: LayoutProps) => {
<TimezoneChangeDialog />
<div className="flex min-h-screen flex-col">
<div ref={bannerRef} className="sticky top-0 z-10 w-full divide-y divide-black">
<NProgressNextRouter router={router} />
<TeamsUpgradeBanner />
<OrgUpgradeBanner />
<ImpersonatingBanner />