Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjdominguez committed May 22, 2024
1 parent 77af215 commit ef4f19e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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 <BrowserOnly fallback={<div>Loading...</div>}>{() => <UserFetcher />}</BrowserOnly>;
}
25 changes: 22 additions & 3 deletions src/theme/DocRoot/Layout/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
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';
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 (
<div className={styles.docsWrapper}>
<BackToTopButton />
<UserFetcherWrapper />
<div className={styles.docRoot}>
{sidebar && (
<DocRootLayoutSidebar
Expand Down

0 comments on commit ef4f19e

Please sign in to comment.