From ef4f19e78188796353e56b02718697b5cb76ef10 Mon Sep 17 00:00:00 2001 From: Rob Dominguez Date: Wed, 22 May 2024 17:00:45 -0500 Subject: [PATCH] Simplify --- .../Layout/{Posthog => FetchUser}/index.js | 33 +------------------ src/theme/DocRoot/Layout/index.js | 25 ++++++++++++-- 2 files changed, 23 insertions(+), 35 deletions(-) rename src/theme/DocRoot/Layout/{Posthog => FetchUser}/index.js (50%) diff --git a/src/theme/DocRoot/Layout/Posthog/index.js b/src/theme/DocRoot/Layout/FetchUser/index.js similarity index 50% rename from src/theme/DocRoot/Layout/Posthog/index.js rename to src/theme/DocRoot/Layout/FetchUser/index.js index fc8b9738d..f85baf35e 100644 --- a/src/theme/DocRoot/Layout/Posthog/index.js +++ b/src/theme/DocRoot/Layout/FetchUser/index.js @@ -1,13 +1,4 @@ -import { useState, useEffect } from 'react'; -import BrowserOnly from '@docusaurus/BrowserOnly'; -import posthog from 'posthog-js'; - -// 🦔 config -posthog.init('phc_MZpdcQLGf57lyfOUT0XA93R3jaCxGsqftVt4iI4MyUY', { - api_host: 'https://analytics-posthog.hasura-app.io', -}); - -async function fetchUser() { +export default async function fetchUser() { const url = 'https://data.pro.hasura.io/v1/graphql'; const headers = { @@ -47,25 +38,3 @@ async function fetchUser() { return null; } } - -function UserFetcher() { - const [docsUser, setDocsUser] = useState(null); - - useEffect(() => { - async function getUser() { - const user = await fetchUser(); - if (user) { - setDocsUser(user); - posthog.identify(user.data.users[0]?.id, { email: user.data.users[0]?.email }); - } - } - - getUser(); - }, []); - - return null; -} - -export default function UserFetcherWrapper() { - return Loading...}>{() => }; -} diff --git a/src/theme/DocRoot/Layout/index.js b/src/theme/DocRoot/Layout/index.js index 91e233b41..0e19888c2 100644 --- a/src/theme/DocRoot/Layout/index.js +++ b/src/theme/DocRoot/Layout/index.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { useDocsSidebar } from '@docusaurus/theme-common/internal'; import BackToTopButton from '@theme/BackToTopButton'; import DocRootLayoutSidebar from '@theme/DocRoot/Layout/Sidebar'; @@ -6,16 +6,35 @@ import DocRootLayoutMain from '@theme/DocRoot/Layout/Main'; import styles from './styles.module.css'; import BrowserOnly from '@docusaurus/BrowserOnly'; import { AiChatBot } from '@site/src/components/AiChatBot/AiChatBot'; -import UserFetcherWrapper from '@theme/DocRoot/Layout/Posthog'; +import fetchUser from '@theme/DocRoot/Layout/FetchUser'; +import posthog from 'posthog-js'; + +// 🦔 config +posthog.init('phc_MZpdcQLGf57lyfOUT0XA93R3jaCxGsqftVt4iI4MyUY', { + api_host: 'https://analytics-posthog.hasura-app.io', +}); export default function DocRootLayout({ children }) { const sidebar = useDocsSidebar(); const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false); + useEffect(() => { + const getUser = async () => { + try { + const user = await fetchUser(); + console.log('User fetched:', user); + posthog.identify(user.data.users[0]?.id, { email: user.data.users[0]?.email }); + } catch (error) { + console.error('Error fetching user:', error); + } + }; + + getUser(); + }, []); + return (
-
{sidebar && (