From 244c67b36990a22e70ee4aac77c24c1ae62bd830 Mon Sep 17 00:00:00 2001 From: dakshesh14 Date: Thu, 7 Sep 2023 18:26:45 +0530 Subject: [PATCH] fix: authorize editor --- web/layouts/web-view-layout/index.tsx | 61 +++++++++++++++ web/pages/m/[workspaceSlug]/editor.tsx | 104 +++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 web/layouts/web-view-layout/index.tsx create mode 100644 web/pages/m/[workspaceSlug]/editor.tsx diff --git a/web/layouts/web-view-layout/index.tsx b/web/layouts/web-view-layout/index.tsx new file mode 100644 index 00000000000..33502aad857 --- /dev/null +++ b/web/layouts/web-view-layout/index.tsx @@ -0,0 +1,61 @@ +// swr +import useSWR from "swr"; + +// services +import userService from "services/user.service"; + +// fetch keys +import { CURRENT_USER } from "constants/fetch-keys"; + +// icons +import { AlertCircle } from "lucide-react"; + +// ui +import { Spinner } from "components/ui"; + +type Props = { + children: React.ReactNode; + fullScreen?: boolean; +}; + +const getIfInWebview = (userAgent: NavigatorID["userAgent"]) => { + if (/iphone|ipod|ipad/.test(userAgent) || userAgent.includes("wv")) return true; + else return false; +}; + +const useMobileDetect = () => { + const userAgent = typeof navigator === "undefined" ? "SSR" : navigator.userAgent; + return getIfInWebview(userAgent); +}; + +const WebViewLayout: React.FC = ({ children, fullScreen = true }) => { + const { data: currentUser, error } = useSWR(CURRENT_USER, () => userService.currentUser()); + + const isWebview = useMobileDetect(); + + if (!currentUser && !error) { + return ( +
+
+

Loading your profile...

+ +
+
+ ); + } + + return ( +
+ {error || !isWebview ? ( +
+ +

You are not authorized to view this page.

+
+ ) : ( + children + )} +
+ ); +}; + +export default WebViewLayout; diff --git a/web/pages/m/[workspaceSlug]/editor.tsx b/web/pages/m/[workspaceSlug]/editor.tsx new file mode 100644 index 00000000000..296b05ac91b --- /dev/null +++ b/web/pages/m/[workspaceSlug]/editor.tsx @@ -0,0 +1,104 @@ +import { useEffect, useState } from "react"; + +// next +import type { NextPage } from "next"; +import { useRouter } from "next/router"; + +// cookies +import Cookies from "js-cookie"; + +// react-hook-form +import { Controller, useForm } from "react-hook-form"; + +// layouts +import WebViewLayout from "layouts/web-view-layout"; + +// components +import { TipTapEditor } from "components/tiptap"; +import { PrimaryButton, Spinner } from "components/ui"; + +const Editor: NextPage = () => { + const [isLoading, setIsLoading] = useState(false); + + const router = useRouter(); + const { workspaceSlug, editable } = router.query; + + const isEditable = editable === "true"; + + const { + watch, + setValue, + control, + formState: { errors }, + } = useForm({ + defaultValues: { + data: "", + data_html: "", + }, + }); + + useEffect(() => { + setIsLoading(true); + if (!isEditable) return; + setIsLoading(false); + const data_html = Cookies.get("data_html"); + setValue("data_html", data_html ?? ""); + }, [isEditable, setValue]); + + return ( + + {isLoading ? ( +
+ +
+ ) : ( + <> + ( + { + onChange(description_html); + setValue("data_html", description_html); + setValue("data", JSON.stringify(description)); + }} + /> + )} + /> + {isEditable && ( + { + console.log( + "submitted", + JSON.stringify({ + data_html: watch("data_html"), + }) + ); + }} + > + Submit + + )} + + )} +
+ ); +}; + +export default Editor;