From e3776a42c72e5f0079dcdcc34d47f70cd3071a9f Mon Sep 17 00:00:00 2001 From: osadavc Date: Sat, 30 Jul 2022 23:51:01 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Final=20Fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chrome_extension/src/popup/components/App.tsx | 73 +++++++++++++++++-- .../src/popup/services/apiService.ts | 10 +++ client/prisma/schema.prisma | 13 ++-- client/src/components/Home/Footer.tsx | 42 ++++++----- client/src/components/Home/HowTwotion.tsx | 2 +- client/src/pages/api/notion/authorize.ts | 33 --------- client/src/pages/api/notion/info.ts | 6 +- client/src/pages/api/notion/isTweeted.ts | 72 ++++++++++++++++++ client/src/pages/dashboard.tsx | 4 +- 9 files changed, 186 insertions(+), 69 deletions(-) delete mode 100644 client/src/pages/api/notion/authorize.ts create mode 100644 client/src/pages/api/notion/isTweeted.ts diff --git a/chrome_extension/src/popup/components/App.tsx b/chrome_extension/src/popup/components/App.tsx index ea2fcd8..03e2aa3 100644 --- a/chrome_extension/src/popup/components/App.tsx +++ b/chrome_extension/src/popup/components/App.tsx @@ -2,17 +2,26 @@ import Spinner from "./Common/Spinner"; import logo from "../images/logo.png"; import { useEffect, useState } from "react"; import { useUser } from "../context/AuthContext"; -import { getNotionInfo, tweetNotionPage } from "../services/apiService"; +import { + getNotionInfo, + isPageTweeted, + tweetNotionPage, +} from "../services/apiService"; const App = () => { const [error, setError] = useState(""); - const [message, setMessage] = useState(false); + const [message, setMessage] = useState(false); + const [appError, setAppError] = useState(""); + const [appSuccess, setAppSuccess] = useState(false); const [tweeted, setTweeted] = useState(false); const [loading, setLoading] = useState(false); + const [isPageLoading, setIsPageLoading] = useState(false); const [path, setPath] = useState(""); + const [code, setCode] = useState(); const user = useUser(); useEffect(() => { + setIsPageLoading(true); chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.sendMessage( tabs[0].id!, @@ -43,17 +52,31 @@ const App = () => { setPath(path); try { - const data = await getNotionInfo(path); + const [notionInfo, isTweeted] = await Promise.all([ + getNotionInfo(path), + isPageTweeted(path), + ]); - setError(data.error); - setMessage(data.message); + setTweeted(isTweeted.isTweeted); + setCode(notionInfo.code); + setError(notionInfo.error); + setMessage(notionInfo.message); } catch (error) { setError("Error Ocurred"); + + setCode((error as any).response.status === 401 ? 401 : 500); + if ((error as any).response.status === 401 ? 401 : 500) { + setError("Login To Twotion Before Continuing"); + } + } finally { + setIsPageLoading(false); } } else { setError( "You're not inside notion. Please open notion before proceeding" ); + setCode(10); + setIsPageLoading(false); } } ); @@ -66,8 +89,12 @@ const App = () => { try { await tweetNotionPage(path); setTweeted(true); + setAppSuccess(true); + setAppError(null); } catch (error) { console.log(error); + setAppError((error as any).response.data.message); + setAppSuccess(false); } finally { setLoading(false); } @@ -80,12 +107,12 @@ const App = () => {
- {user.isLoading ? ( + {user.isLoading || isPageLoading ? (
) : ( -
+
{error && (

{error} @@ -98,6 +125,28 @@ const App = () => {

)} + {code === 10 && ( + + )} + + {code === 401 && ( + + )} + {!error && !message && (
)}
diff --git a/chrome_extension/src/popup/services/apiService.ts b/chrome_extension/src/popup/services/apiService.ts index 0df45fa..cc06024 100644 --- a/chrome_extension/src/popup/services/apiService.ts +++ b/chrome_extension/src/popup/services/apiService.ts @@ -28,6 +28,16 @@ export const getNotionInfo = async (pageId: string) => { return data; }; +export const isPageTweeted = async (pageId: string) => { + const { data } = await client.get("/notion/isTweeted", { + params: { + pageId, + }, + }); + + return data; +}; + export const tweetNotionPage = async (pageId: string) => { const { data } = await client.post("/notion/tweet", { pageId, diff --git a/client/prisma/schema.prisma b/client/prisma/schema.prisma index 5983567..b587e8f 100644 --- a/client/prisma/schema.prisma +++ b/client/prisma/schema.prisma @@ -31,15 +31,16 @@ model NotionOAuthOptions { } model TwitterThreads { - id String @id - User User @relation(fields: [id], references: [id]) + id String @id @default(cuid()) + User User @relation(fields: [id], references: [id]) tweets Tweet[] - notionPageId String? @unique - postedTime DateTime @default(now()) + notionPageId String? @unique + postedTime DateTime? @default(now()) } model Tweet { - id String @id @default(cuid()) + itemId String @id @default(cuid()) + id String text String images TweetImages[] @@ -50,6 +51,6 @@ model Tweet { model TweetImages { id String @id @default(cuid()) imageId String - tweet Tweet? @relation(fields: [tweetId], references: [id]) + tweet Tweet? @relation(fields: [tweetId], references: [itemId]) tweetId String? } diff --git a/client/src/components/Home/Footer.tsx b/client/src/components/Home/Footer.tsx index b1fa722..d4d9d90 100644 --- a/client/src/components/Home/Footer.tsx +++ b/client/src/components/Home/Footer.tsx @@ -1,25 +1,27 @@ const Footer = () => { return ( -
-

- Made For{" "} - - Hashnode X PlanetScale - {" "} - Hackathon By{" "} - - Osada Vidath - {" "} - With ❤️{" "} -

+
+
+

+ Made For{" "} + + Hashnode X PlanetScale + {" "} + Hackathon By{" "} + + Osada Vidath + {" "} + With ❤️{" "} +

+
); }; diff --git a/client/src/components/Home/HowTwotion.tsx b/client/src/components/Home/HowTwotion.tsx index 0c86821..ef1024d 100644 --- a/client/src/components/Home/HowTwotion.tsx +++ b/client/src/components/Home/HowTwotion.tsx @@ -85,7 +85,7 @@ const HowTwotion = () => { {why.map((item, index) => (

{index + 1}

diff --git a/client/src/pages/api/notion/authorize.ts b/client/src/pages/api/notion/authorize.ts deleted file mode 100644 index 7de3cfe..0000000 --- a/client/src/pages/api/notion/authorize.ts +++ /dev/null @@ -1,33 +0,0 @@ -import prisma from "lib/prisma"; -import { NextApiResponse } from "next"; -import { createRouter } from "next-connect"; - -import { - auth, - onError, - onNoMatch, - NextApiRequestWithUser, -} from "utils/apiUtils"; - -const router = createRouter(); - -router.use(auth); - -router.get(async (req, res) => { - const user = await prisma.user.findUnique({ - where: { id: req.user.id }, - include: { - notion: true, - }, - }); - - res.json({ - isAuthenticated: true, - isNotionAuthorized: !!user?.notion?.accessToken, - }); -}); - -export default router.handler({ - onError, - onNoMatch, -}); diff --git a/client/src/pages/api/notion/info.ts b/client/src/pages/api/notion/info.ts index e018fda..709ba50 100644 --- a/client/src/pages/api/notion/info.ts +++ b/client/src/pages/api/notion/info.ts @@ -43,6 +43,7 @@ router.get(async (req, res) => { message: "You're in the correct database. Create a new page inside it to start creating new twitter thread", error: false, + code: 1, }); } @@ -50,6 +51,7 @@ router.get(async (req, res) => { return res.status(200).json({ message: "You need to authorize Notion first", error: true, + code: 2, }); } @@ -76,6 +78,7 @@ router.get(async (req, res) => { message: "You're not in the correct database. Please open the connected database", error: true, + code: 3, }); } @@ -85,8 +88,9 @@ router.get(async (req, res) => { ) { return res.status(200).json({ message: - "Your page is not in the correct database. Please open a page in the correct database", + "Your page is not in the correct database. Please open a page in the correct database to tweet", error: true, + code: 4, }); } diff --git a/client/src/pages/api/notion/isTweeted.ts b/client/src/pages/api/notion/isTweeted.ts new file mode 100644 index 0000000..496bd68 --- /dev/null +++ b/client/src/pages/api/notion/isTweeted.ts @@ -0,0 +1,72 @@ +import { Client } from "@notionhq/client"; +import prisma from "lib/prisma"; +import { NextApiResponse } from "next"; +import { createRouter } from "next-connect"; + +import { + auth, + onError, + onNoMatch, + NextApiRequestWithUser, +} from "utils/apiUtils"; + +const router = createRouter(); + +router.use(auth); + +router.get(async (req, res) => { + const pageId = req.query.pageId; + + if (typeof pageId !== "string") { + return res.status(200).json({ + isTweeted: false, + }); + } + + const { notion: notionResponse } = (await prisma.user.findUnique({ + where: { + id: req.user.id, + }, + include: { + notion: true, + }, + }))!; + + if (!notionResponse) { + return res.status(400).json({ error: "Notion is not configured" }); + } + + const notion = new Client({ + auth: notionResponse?.accessToken, + }); + + const { id } = ( + await notion.search({ + page_size: 1, + query: pageId, + }) + ).results[0]; + + const notionPageId = ( + await prisma.twitterThreads.findUnique({ + where: { + notionPageId: id, + }, + }) + )?.notionPageId; + + if (notionPageId) { + return res.status(200).json({ + isTweeted: true, + }); + } + + return res.status(200).json({ + isTweeted: false, + }); +}); + +export default router.handler({ + onError, + onNoMatch, +}); diff --git a/client/src/pages/dashboard.tsx b/client/src/pages/dashboard.tsx index 31c1e19..d1d9c93 100644 --- a/client/src/pages/dashboard.tsx +++ b/client/src/pages/dashboard.tsx @@ -33,7 +33,9 @@ const Dashboard: NextPage = ({ )} - + {user.notion?.accessToken && ( + + )}

);