diff --git a/apps/discord-bot/src/handlers/onCommentCreateWeb.ts b/apps/discord-bot/src/handlers/onCommentCreateWeb.ts index 6ff58c9c..a6f1a508 100644 --- a/apps/discord-bot/src/handlers/onCommentCreateWeb.ts +++ b/apps/discord-bot/src/handlers/onCommentCreateWeb.ts @@ -20,7 +20,8 @@ export const onCommentCreateWeb = async ( client: Client, threadId: string, comment: string, - discordUserName: string + discordUserName: string, + didSession: string ): Promise => { const existhreaingThread = await prisma.thread .findUnique({ @@ -41,7 +42,7 @@ export const onCommentCreateWeb = async ( }, }); - if (user == null || !user.didSession) { + if (user == null || !user.didSession || user.didSession!==didSession) { return { result: false, value: "user not signed in from discord or did session has expired", @@ -104,7 +105,7 @@ export const onCommentCreateWeb = async ( }, create: { discordId: message.id, - streamId: composeResponse.data.createComment.document.id, + streamId: composeResponse.data.createComment?.document.id, createdAt: message.createdAt, discordAuthor: discordUserName, text: comment, diff --git a/apps/discord-bot/src/handlers/onThreadCreateWeb.ts b/apps/discord-bot/src/handlers/onThreadCreateWeb.ts index e54501a2..1418a469 100644 --- a/apps/discord-bot/src/handlers/onThreadCreateWeb.ts +++ b/apps/discord-bot/src/handlers/onThreadCreateWeb.ts @@ -26,7 +26,8 @@ export const onThreadCreateWeb = async ( client: Client, threadTitle: string, community: string, - discordUserName: string + discordUserName: string, + didSession: string ): Promise => { let guild = await client.guilds.cache.get(community); @@ -47,7 +48,7 @@ export const onThreadCreateWeb = async ( }, }); - if (user == null || !user.didSession) { + if (user == null || !user.didSession || user.didSession!==didSession) { return { result: false, value: "user not signed in from discord or did session has expired", diff --git a/apps/discord-bot/src/index.ts b/apps/discord-bot/src/index.ts index f7c043ca..e1b44c1c 100644 --- a/apps/discord-bot/src/index.ts +++ b/apps/discord-bot/src/index.ts @@ -114,13 +114,14 @@ const updateCommunities = async () => { // apis app.post("/webcomment", async (req, res) => { - const { threadId, comment, discordUserName } = req.body; + const { threadId, comment, discordUserName, didSession } = req.body; console.log({ threadId: threadId, comment: comment }); const response = await onCommentCreateWeb( client, threadId, comment, - discordUserName + discordUserName, + didSession ); console.log(response); if (response.result) { @@ -131,7 +132,7 @@ app.post("/webcomment", async (req, res) => { }); app.post("/webthread", async (req, res) => { - const { threadTitle, community, discordUserName } = req.body; + const { threadTitle, community, discordUserName, didSession } = req.body; console.log({ threadTitle: threadTitle, community: community, @@ -141,7 +142,8 @@ app.post("/webthread", async (req, res) => { client, threadTitle, community, - discordUserName + discordUserName, + didSession ); console.log(response); if (response.result) { diff --git a/apps/web/package.json b/apps/web/package.json index a82a65d4..6ce07e5b 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -39,6 +39,7 @@ "prettier-plugin-tailwindcss": "^0.2.1", "react": "18.2.0", "react-dom": "18.2.0", + "react-toastify": "^9.1.1", "siwe": "^1.1.6", "superjson": "1.12.1", "wagmi": "^0.10.2", diff --git a/apps/web/src/components/Layout/Layout.tsx b/apps/web/src/components/Layout/Layout.tsx index 34250958..961a8ea2 100644 --- a/apps/web/src/components/Layout/Layout.tsx +++ b/apps/web/src/components/Layout/Layout.tsx @@ -1,11 +1,15 @@ import { NavBar } from "../NavBar"; -const Layout = ({ children }) => { +const Layout = (props) => { + return (
- +
- {children} + {props.children}
); diff --git a/apps/web/src/components/NavBar/NavBar.tsx b/apps/web/src/components/NavBar/NavBar.tsx index 7339922d..081505bc 100644 --- a/apps/web/src/components/NavBar/NavBar.tsx +++ b/apps/web/src/components/NavBar/NavBar.tsx @@ -19,7 +19,7 @@ function classNames(...classes: string[]) { const navigation = [{ name: "Ask a question", href: "#", current: true }]; -const NavBar = () => { +const NavBar = (props) => { const { disconnectAsync } = useDisconnect(); const { connectors, connectAsync } = useConnect(); const { address, isConnected } = useAccount(); @@ -34,6 +34,7 @@ const NavBar = () => { }); const discordUserName = authorDiscord.data?.discordUsername; + discordUserName && props.handleDiscordUser(true); const connectWallet = async () => { await Promise.all( connectors.map(async (connector) => { @@ -64,7 +65,7 @@ const NavBar = () => { ); setDidSession(session.serialize()); - + props.handleDidSession(true); setDid(session.did.id); }; diff --git a/apps/web/src/components/Thread/CommentInput.tsx b/apps/web/src/components/Thread/CommentInput.tsx index 7e4a13e1..900a5a29 100644 --- a/apps/web/src/components/Thread/CommentInput.tsx +++ b/apps/web/src/components/Thread/CommentInput.tsx @@ -46,6 +46,7 @@ const CommentInput = (props: { threadId: string; refresh: () => void }) => { threadId: props.threadId, comment: String(comment), discordUserName: String(discordUserName), + didSession:String(didSession) }), method: "POST", headers: { @@ -58,33 +59,6 @@ const CommentInput = (props: { threadId: string; refresh: () => void }) => { }); }; - if (!isConnected) - return ( -
-
- Please connect to publish comments. -
-
- ); - - if (!didSession) - return ( -
-
- Please create a DID session -
-
- ); - - if (!isDiscordUser) - return ( -
-
- Please connect to Discord -
-
- ); - return (
void }) => { +const NewThread = (props) => { const { isConnected } = useAccount(); const [did, setDid] = useState(""); const [didSession] = useLocalStorage("didSession", ""); const [community, setCommunity] = useState(""); const allCommunities = trpc.public.getAllCommunities.useQuery(); - + const [thread, setThread] = useState(""); - useEffect(() => { const getData = async () => { if (!didSession || !isConnected) return; @@ -27,7 +27,6 @@ const NewThread = (props: { refresh: () => void }) => { didSession: didSession, }); - const isDiscordUser = authorDiscord.data?.discordUsername; const discordUserName = authorDiscord.data?.discordUsername ?? "Anonymous"; const onThreadSumbit = async () => { @@ -36,44 +35,21 @@ const NewThread = (props: { refresh: () => void }) => { threadTitle: thread, community: community, discordUserName: discordUserName, + didSession: String(didSession) }), method: "POST", headers: { "Content-Type": "application/json", }, - }).then(() => { + }).then(async (response) => { + if(!response.ok){ + toast.error("Community missing or Api failed"); + } setThread(""); props.refresh(); - }); + }) }; - if (!isConnected) - return ( -
-
- Please connect to publish comments. -
-
- ); - - if (!didSession) - return ( -
-
- Please create a DID session -
-
- ); - - if (!isDiscordUser) - return ( -
-
- Please connect to Discord -
-
- ); - return (
{ const router = useRouter(); @@ -15,6 +18,11 @@ const QuestionPage = () => { const allThreads = trpc.public.getAllThreads.useQuery(); const allComments = trpc.public.getAllComments.useQuery(); + const [didSession] = useLocalStorage("didSession",""); + const { isConnected } = useAccount(); + const [isDidSession, setDidSession] = useState(didSession?true:false); + const [isDiscordUser, setDiscordUser] = useState(false); + if (!allComments.data || !allThreads.data) return
Loading
; const thisThread = allThreads.data.filter((thread) => thread.node.id == id)[0] @@ -23,8 +31,47 @@ const QuestionPage = () => { .filter((comment) => comment.node.threadID == id) .map((comment) => comment.node); + const handleDidSession = (value) =>{ + setDidSession(value) + } + const handleDiscordUser = (value) => { + setDiscordUser(value) + } + + const checkConnected = () =>{ + if (!isConnected) + return ( +
+
+ Please connect to publish comments. +
+
+ ); + + if (!isDidSession) + return ( +
+
+ Please create a DID session +
+
+ ); + + if (!isDiscordUser) + return ( +
+
+ Please connect to Discord +
+
+ ); + } + return ( - +
@@ -57,13 +104,13 @@ const QuestionPage = () => {
- { allThreads.refetch(); allComments.refetch(); }} - /> + /> : checkConnected()}
diff --git a/apps/web/src/pages/_app.tsx b/apps/web/src/pages/_app.tsx index 880c5851..a3c55472 100644 --- a/apps/web/src/pages/_app.tsx +++ b/apps/web/src/pages/_app.tsx @@ -12,6 +12,10 @@ import { Session } from "next-auth"; import { configureChains, createClient, WagmiConfig } from "wagmi"; import { mainnet, optimism } from "wagmi/chains"; +import { ToastContainer } from 'react-toastify'; + +import 'react-toastify/dist/ReactToastify.css'; + const { chains, provider, webSocketProvider } = configureChains( [mainnet], [publicProvider()] @@ -35,6 +39,18 @@ const client = createClient({ const MyApp: AppType = ({ Component, pageProps }) => { return ( + ); diff --git a/apps/web/src/pages/index.tsx b/apps/web/src/pages/index.tsx index 6805e5dd..2f2600c2 100644 --- a/apps/web/src/pages/index.tsx +++ b/apps/web/src/pages/index.tsx @@ -1,17 +1,62 @@ import { type NextPage } from "next"; +import { useState } from "react"; import { Layout } from "../components/Layout"; import NewThread from "../components/Thread/NewThread"; import ThreadCard from "../components/ThreadCard"; +import { useAccount } from "wagmi"; +import useLocalStorage from "../hooks/useLocalStorage"; import { trpc } from "../utils/trpc"; const Home: NextPage = () => { const threads = trpc.public.getAllThreads.useQuery(); + const [didSession] = useLocalStorage("didSession",""); + const { isConnected } = useAccount(); + const [isDidSession, setDidSession] = useState(didSession?true:false); + const [isDiscordUser, setDiscordUser] = useState(false); if (!threads.data) return
Loading...
; + const handleDidSession = (value) =>{ + setDidSession(value) + } + const handleDiscordUser = (value) => { + setDiscordUser(value) + } + + const checkConnected = () =>{ + if (!isConnected) + return ( +
+
+ Please connect to publish comments. +
+
+ ); + + if (!isDidSession) + return ( +
+
+ Please create a DID session +
+
+ ); + + if (!isDiscordUser) + return ( +
+
+ Please connect to Discord +
+
+ ); + } return ( - +
@@ -26,11 +71,13 @@ const Home: NextPage = () => {
- { threads.refetch(); }} - /> + /> : checkConnected()}