From 5ebd64d72901d997afeb5980b3c78c562ff40b99 Mon Sep 17 00:00:00 2001 From: Prateek Shourya Date: Thu, 15 Feb 2024 18:24:01 +0530 Subject: [PATCH] chore: user auth wrapper. --- god-mode/app/layout.tsx | 56 ++++++++----------- god-mode/app/page.tsx | 60 +++++++++++--------- god-mode/components/views/general-view.tsx | 38 ------------- god-mode/components/views/index.ts | 1 - god-mode/constants/swr-config.ts | 8 +++ god-mode/helpers/common.helper.ts | 3 + god-mode/lib/wrappers/app-wrapper.tsx | 53 ++++++++++++++++++ god-mode/lib/wrappers/user-auth-wrapper.tsx | 61 +++++++++++++++++++++ god-mode/services/auth.service.ts | 4 +- god-mode/services/instance.service.ts | 4 +- god-mode/services/user.service.ts | 6 +- 11 files changed, 188 insertions(+), 106 deletions(-) delete mode 100644 god-mode/components/views/general-view.tsx delete mode 100644 god-mode/components/views/index.ts create mode 100644 god-mode/constants/swr-config.ts create mode 100644 god-mode/helpers/common.helper.ts create mode 100644 god-mode/lib/wrappers/app-wrapper.tsx create mode 100644 god-mode/lib/wrappers/user-auth-wrapper.tsx diff --git a/god-mode/app/layout.tsx b/god-mode/app/layout.tsx index d67a188a7..7aaff2ae0 100644 --- a/god-mode/app/layout.tsx +++ b/god-mode/app/layout.tsx @@ -1,6 +1,6 @@ // lib -import { ThemeProvider } from "lib/theme-provider"; -import { ToastContextProvider } from "lib/toast-provider"; +import AppWrapper from "lib/wrappers/app-wrapper"; +import { UserAuthWrapper } from "lib/wrappers/user-auth-wrapper"; // components import { InstanceSidebar } from "./sidebar"; import { InstanceHeader } from "./header"; @@ -16,38 +16,26 @@ interface RootLayoutProps { children: React.ReactNode; } -export const RootLayout = async ({ children }: RootLayoutProps) => { - const isUserInstanceAdmin = true; - return ( - - - {/* */} - {/* {isUserInstanceAdmin || true ? ( */} - - -
- -
- -
-
- {children} -
+export const RootLayout = async ({ children }: RootLayoutProps) => ( + + + + +
+ +
+ +
+
+ {children}
-
-
- - - {/* ) : ( -
Login
- )} */} - - - ); -}; +
+
+
+ + + + +); export default RootLayout; diff --git a/god-mode/app/page.tsx b/god-mode/app/page.tsx index b3e6098a9..8dad17b6c 100644 --- a/god-mode/app/page.tsx +++ b/god-mode/app/page.tsx @@ -1,41 +1,51 @@ "use client"; import useSWR from "swr"; -import useSWRImmutable from "swr/immutable"; +import { observer } from "mobx-react-lite"; // hooks -import useUser from "hooks/use-user"; import useInstance from "hooks/use-instance"; +// ui +import { Loader } from "@plane/ui"; // components -import { GeneralView } from "components/views"; +import { InstanceGeneralForm } from "components/forms"; -export default function Home() { - const { - // isUserInstanceAdmin, - fetchCurrentUser, - fetchCurrentUserInstanceAdminStatus, - } = useUser(); - const { fetchInstanceInfo, fetchInstanceAdmins } = useInstance(); +const GeneralSettingsPage = observer(() => { + // store + const { instance, instanceAdmins, fetchInstanceInfo, fetchInstanceAdmins } = + useInstance(); - // fetching user information - useSWR("CURRENT_USER_DETAILS", () => fetchCurrentUser(), { - shouldRetryOnError: false, - }); - // fetching current user instance admin status - useSWRImmutable( - "CURRENT_USER_INSTANCE_ADMIN_STATUS", - () => fetchCurrentUserInstanceAdminStatus(), - { - shouldRetryOnError: false, - } - ); // fetching instance information useSWR("INSTANCE_INFO", () => fetchInstanceInfo()); // fetching instance admins useSWR("INSTANCE_ADMINS", () => fetchInstanceAdmins()); return ( -
- +
+
+
+ ID your instance easily +
+
+ Change the name of your instance and instance admin e-mail addresses. + If you have a paid subscription, you will find your license key here. +
+
+ {instance && instanceAdmins ? ( + + ) : ( + +
+ + +
+ +
+ )}
); -} +}); + +export default GeneralSettingsPage; diff --git a/god-mode/components/views/general-view.tsx b/god-mode/components/views/general-view.tsx deleted file mode 100644 index e0e1ae0d5..000000000 --- a/god-mode/components/views/general-view.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { observer } from "mobx-react-lite"; -// hooks -import useInstance from "hooks/use-instance"; -// ui -import { Loader } from "@plane/ui"; -// components -import { InstanceGeneralForm } from "components/forms"; - -export const GeneralView = observer(() => { - const { instance, instanceAdmins } = useInstance(); - return ( -
-
-
- ID your instance easily -
-
- Change the name of your instance and instance admin e-mail addresses. - If you have a paid subscription, you will find your license key here. -
-
- {instance && instanceAdmins ? ( - - ) : ( - -
- - -
- -
- )} -
- ); -}); diff --git a/god-mode/components/views/index.ts b/god-mode/components/views/index.ts deleted file mode 100644 index 0f6c53263..000000000 --- a/god-mode/components/views/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./general-view"; diff --git a/god-mode/constants/swr-config.ts b/god-mode/constants/swr-config.ts new file mode 100644 index 000000000..c6099882e --- /dev/null +++ b/god-mode/constants/swr-config.ts @@ -0,0 +1,8 @@ +export const SWR_CONFIG = { + refreshWhenHidden: false, + revalidateIfStale: false, + revalidateOnFocus: false, + revalidateOnMount: true, + refreshInterval: 600000, + errorRetryCount: 3, +}; \ No newline at end of file diff --git a/god-mode/helpers/common.helper.ts b/god-mode/helpers/common.helper.ts new file mode 100644 index 000000000..e40502d1a --- /dev/null +++ b/god-mode/helpers/common.helper.ts @@ -0,0 +1,3 @@ +export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL + ? process.env.NEXT_PUBLIC_API_BASE_URL + : ""; diff --git a/god-mode/lib/wrappers/app-wrapper.tsx b/god-mode/lib/wrappers/app-wrapper.tsx new file mode 100644 index 000000000..e3e17edc4 --- /dev/null +++ b/god-mode/lib/wrappers/app-wrapper.tsx @@ -0,0 +1,53 @@ +"use client"; + +import { FC, ReactNode, useEffect } from "react"; +import { observer } from "mobx-react-lite"; +import { SWRConfig } from "swr"; +// lib +import { ThemeProvider } from "lib/theme-provider"; +import { ToastContextProvider } from "lib/toast-provider"; +// hooks +import useAppTheme from "hooks/use-theme"; +import useUser from "hooks/use-user"; +// constants +import { SWR_CONFIG } from "constants/swr-config"; + +interface IAppWrapper { + children: ReactNode; +} + +const AppWrapper: FC = observer(({ children }) => { + // store hooks + const { sidebarCollapsed, toggleSidebar } = useAppTheme(); + const { currentUser } = useUser(); + + /** + * Sidebar collapsed fetching from local storage + */ + useEffect(() => { + const localValue = + localStorage && localStorage.getItem("god_mode_sidebar_collapsed"); + const localBoolValue = localValue + ? localValue === "true" + ? true + : false + : false; + + if (localValue && sidebarCollapsed === undefined) + toggleSidebar(localBoolValue); + }, [sidebarCollapsed, currentUser, toggleSidebar]); + + return ( + + + {children} + + + ); +}); + +export default AppWrapper; diff --git a/god-mode/lib/wrappers/user-auth-wrapper.tsx b/god-mode/lib/wrappers/user-auth-wrapper.tsx new file mode 100644 index 000000000..023f6ec4a --- /dev/null +++ b/god-mode/lib/wrappers/user-auth-wrapper.tsx @@ -0,0 +1,61 @@ +"use client"; + +import { FC, ReactNode } from "react"; +// import { useRouter, usePathname } from "next/navigation"; +import { observer } from "mobx-react-lite"; +import useSWR from "swr"; +import useSWRImmutable from "swr/immutable"; +// hooks +import useUser from "hooks/use-user"; +// ui +import { Spinner } from "@plane/ui"; + +export interface IUserAuthWrapper { + children: ReactNode; +} + +export const UserAuthWrapper: FC = observer((props) => { + const { children } = props; + // store hooks + const { + currentUser, + currentUserLoader, + currentUserError, + fetchCurrentUser, + fetchCurrentUserInstanceAdminStatus, + } = useUser(); + // router + // const router = useRouter(); + // const pathname = usePathname(); + // fetching user information + useSWR("CURRENT_USER_DETAILS", () => fetchCurrentUser(), { + shouldRetryOnError: false, + }); + // fetching current user instance admin status + useSWRImmutable( + "CURRENT_USER_INSTANCE_ADMIN_STATUS", + () => fetchCurrentUserInstanceAdminStatus(), + { + shouldRetryOnError: false, + } + ); + + if (currentUserLoader && !currentUser && !currentUserError) { + return ( +
+
+ +
+
+ ); + } + + // TODO: Login page + if (currentUserError) { + // router.push(`/?next_path=${pathname}`); + // return null; + return
Login Page
; + } + + return <>{children}; +}); diff --git a/god-mode/services/auth.service.ts b/god-mode/services/auth.service.ts index 81b694c78..3b88cf6f7 100644 --- a/god-mode/services/auth.service.ts +++ b/god-mode/services/auth.service.ts @@ -8,8 +8,8 @@ import { IMagicSignInData, IPasswordSignInData, } from "@plane/types"; - -const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL ? process.env.NEXT_PUBLIC_API_BASE_URL : ""; +// helpers +import { API_BASE_URL } from "helpers/common.helper"; export class AuthService extends APIService { constructor() { diff --git a/god-mode/services/instance.service.ts b/god-mode/services/instance.service.ts index 2fd478d4a..a5f716cdc 100644 --- a/god-mode/services/instance.service.ts +++ b/god-mode/services/instance.service.ts @@ -1,8 +1,8 @@ import { APIService } from "services/api.service"; // types import type { IFormattedInstanceConfiguration, IInstance, IInstanceAdmin, IInstanceConfiguration } from "@plane/types"; - -const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL ? process.env.NEXT_PUBLIC_API_BASE_URL : ""; +// helpers +import { API_BASE_URL } from "helpers/common.helper"; export class InstanceService extends APIService { constructor() { diff --git a/god-mode/services/user.service.ts b/god-mode/services/user.service.ts index 520881cfe..7684906f2 100644 --- a/god-mode/services/user.service.ts +++ b/god-mode/services/user.service.ts @@ -2,10 +2,8 @@ import { APIService } from "services/api.service"; // types import type { IUser, IInstanceAdminStatus } from "@plane/types"; - -const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL - ? process.env.NEXT_PUBLIC_API_BASE_URL - : ""; +// helpers +import { API_BASE_URL } from "helpers/common.helper"; export class UserService extends APIService { constructor() {