Skip to content

Commit

Permalink
refactor(docs): move components out of app
Browse files Browse the repository at this point in the history
  • Loading branch information
charislam committed May 23, 2024
1 parent 8643c25 commit 68a3019
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 246 deletions.
60 changes: 60 additions & 0 deletions apps/docs/features/auth/auth.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use client'

import { SessionContextProvider } from '@supabase/auth-helpers-react'
import { createClient } from '@supabase/supabase-js'
import { useQueryClient } from '@tanstack/react-query'
import { AuthProvider, useConstant } from 'common'
import { type PropsWithChildren, useCallback } from 'react'
import { IS_PLATFORM } from '~/lib/constants'
import { LOCAL_STORAGE_KEYS, remove } from '~/lib/storage'
import { useOnLogout } from '~/lib/userAuth'

const AuthContainerInternal = ({ children }: PropsWithChildren) => {
const supabase = useConstant(() =>
IS_PLATFORM
? createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
)
: undefined
)

return IS_PLATFORM ? (
<SessionContextProvider supabaseClient={supabase!}>
<AuthProvider>{children}</AuthProvider>
</SessionContextProvider>
) : (
<AuthProvider>{children}</AuthProvider>
)
}

/**
*
* !!! IMPORTANT !!!
* Ensure data is cleared on sign out.
*
*/
const SignOutHandler = ({ children }: PropsWithChildren) => {
const queryClient = useQueryClient()

const cleanUp = useCallback(() => {
queryClient.cancelQueries()
queryClient.clear()

Object.keys(LOCAL_STORAGE_KEYS).forEach((key) => {
remove('local', LOCAL_STORAGE_KEYS[key])
})
}, [queryClient])

useOnLogout(cleanUp)

return <>{children}</>
}

const AuthContainer = ({ children }: PropsWithChildren) => (
<AuthContainerInternal>
<SignOutHandler>{children}</SignOutHandler>
</AuthContainerInternal>
)

export { AuthContainer }
15 changes: 15 additions & 0 deletions apps/docs/features/data/queryClient.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client'

import { QueryClientProvider as QueryClientProviderPrimitive } from '@tanstack/react-query'
import { type PropsWithChildren } from 'react'
import { useRootQueryClient } from '~/lib/fetch/queryClient'

const QueryClientProvider = ({ children }: PropsWithChildren) => {
const queryClient = useRootQueryClient()

return (
<QueryClientProviderPrimitive client={queryClient}>{children}</QueryClientProviderPrimitive>
)
}

export { QueryClientProvider }
31 changes: 31 additions & 0 deletions apps/docs/features/envs/staging.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client'

import { type PropsWithChildren, useEffect, useState } from 'react'
import { BUILD_PREVIEW_HTML, IS_PREVIEW } from '~/lib/constants'

/**
* Preview builds don't need to be statically generated to optimize performance.
* This (somewhat hacky) way of shortcutting preview builds cuts their build
* time and speeds up the feedback loop for previewing docs changes in Vercel.
*
* This technically breaks the Rules of Hooks to avoid an unnecessary full-app
* rerender in prod, but this is fine because IS_PREVIEW will never change on
* you within a single build.
*/
const ShortcutPreviewBuild = ({ children }: PropsWithChildren) => {
if (!BUILD_PREVIEW_HTML && IS_PREVIEW) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [isMounted, setIsMounted] = useState(false)

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
setIsMounted(true)
}, [])

return isMounted ? children : null
}

return children
}

export { ShortcutPreviewBuild }
52 changes: 52 additions & 0 deletions apps/docs/features/telemetry/telemetry.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use client'

import { useTelemetryProps } from 'common'
import { usePathname } from 'next/navigation'
import { useCallback, useEffect } from 'react'
import { useConsent } from 'ui-patterns/ConsentToast'
import { BASE_PATH, IS_PLATFORM } from '~/lib/constants'
import { unauthedAllowedPost } from '~/lib/fetch/fetchWrappers'

const useSendPageTelemetryWithConsent = () => {
const { hasAcceptedConsent } = useConsent()
const telemetryProps = useTelemetryProps()

const sendPageTelemetry = useCallback(
(route: string) => {
if (!(IS_PLATFORM && hasAcceptedConsent)) return

unauthedAllowedPost('/platform/telemetry/page', {
body: {
referrer: document.referrer,
title: document.title,
route: `${BASE_PATH}${route}`,
ga: {
screen_resolution: telemetryProps?.screenResolution,
language: telemetryProps?.language,
session_id: '',
},
},
}).catch((e) => {
console.error('Problem sending telemetry:', e)
})
},
[telemetryProps, hasAcceptedConsent]
)

return sendPageTelemetry
}

const PageTelemetry = () => {
const pathname = usePathname()
const sendPageTelemetry = useSendPageTelemetryWithConsent()

useEffect(() => {
if (pathname) {
sendPageTelemetry(pathname)
}
}, [pathname, sendPageTelemetry])

return null
}

export { PageTelemetry }
3 changes: 3 additions & 0 deletions apps/docs/features/ui/helpers.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const DOCS_CONTENT_CONTAINER_ID = 'docs-content-container'

export { DOCS_CONTENT_CONTAINER_ID }
34 changes: 34 additions & 0 deletions apps/docs/features/ui/helpers.scroll.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client'

import { usePathname } from 'next/navigation'
import { useEffect } from 'react'
import { DOCS_CONTENT_CONTAINER_ID } from './helpers.constants'

const useScrollTopOnPageChange = () => {
const pathname = usePathname()

useEffect(() => {
if (document && pathname) {
// Don't scroll on reference pages
if (pathname.startsWith('/reference/')) return

const container = document.getElementById(DOCS_CONTENT_CONTAINER_ID)
if (container) container.scrollTop = 0
/**
* a11y works by default, so no need to specially handle it
*/
}
}, [pathname])
}

/**
* Scroll the docs content container to top on page change. Can't use Next.js's
* native scroll restoration, because we scroll the content container, not the
* document.
*/
const ScrollRestoration = () => {
useScrollTopOnPageChange()
return null
}

export { ScrollRestoration }
11 changes: 11 additions & 0 deletions apps/docs/features/ui/theme.client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client'

import { useThemeSandbox } from 'common'

const ThemeSandbox = () => {
useThemeSandbox()

return null
}

export { ThemeSandbox }
7 changes: 3 additions & 4 deletions apps/docs/layouts/MainSkeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { useTheme } from 'next-themes'
import Image from 'next/legacy/image'
import Link from 'next/link'
import { type CSSProperties, type PropsWithChildren, memo, useEffect } from 'react'

import { cn } from 'ui'

import Footer from '~/components/Navigation/Footer'
import HomeMenuIconPicker from '~/components/Navigation/NavigationMenu/HomeMenuIconPicker'
import NavigationMenu, { type MenuId } from '~/components/Navigation/NavigationMenu/NavigationMenu'
import TopNavBar from '~/components/Navigation/NavigationMenu/TopNavBar'
import { DOCS_CONTENT_CONTAINER_ID } from '~/features/ui/helpers.constants'
import { menuState, useMenuMobileOpen } from '~/hooks/useMenuState'

const levelsData = {
Expand Down Expand Up @@ -275,8 +274,8 @@ const Container = memo(function Container({

return (
<div
// #docs-content-container is used by layout to scroll to top
id="docs-content-container"
// used by layout to scroll to top
id={DOCS_CONTENT_CONTAINER_ID}
className={cn(
// 'overflow-x-auto',
'w-full transition-all ease-out',
Expand Down
9 changes: 5 additions & 4 deletions apps/docs/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export const BUILD_PREVIEW_HTML = process.env.NEXT_PUBLIC_BUILD_PREVIEW_HTML === 'true'
export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
export const IS_PREVIEW = process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
export const LOCAL_SUPABASE = process.env.NEXT_PUBLIC_LOCAL_SUPABASE === 'true'
export const API_URL = (
process.env.NODE_ENV === 'development'
? process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080'
: process.env.NEXT_PUBLIC_API_URL
).replace(/\/platform$/, '')
export const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || '/docs'
export const BUILD_PREVIEW_HTML = process.env.NEXT_PUBLIC_BUILD_PREVIEW_HTML === 'true'
export const IS_PLATFORM = process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
export const IS_PREVIEW = process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview'
export const LOCAL_SUPABASE = process.env.NEXT_PUBLIC_LOCAL_SUPABASE === 'true'

0 comments on commit 68a3019

Please sign in to comment.