forked from PostHog/posthog.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.tsx
65 lines (60 loc) · 2.23 KB
/
gatsby-browser.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react'
import { initKea, wrapElement } from './kea'
import './src/styles/global.css'
import HandbookLayout from './src/templates/Handbook'
import Job from './src/templates/Job'
import Posts from './src/components/Edition/Posts'
import { Provider as ToastProvider } from './src/context/toast'
import { RouteUpdateArgs } from 'gatsby'
import { UserProvider } from './src/hooks/useUser'
initKea(false)
export const wrapRootElement = ({ element }) => (
<UserProvider>
<ToastProvider>{wrapElement({ element })}</ToastProvider>
</UserProvider>
)
export const onRouteUpdate = ({ location, prevLocation }: RouteUpdateArgs) => {
// This is checked and set on initial load in the body script set in gatsby-ssr.js
// Checking for prevLocation prevents this from happening twice
if (typeof window !== 'undefined' && prevLocation) {
var theme = (window as any).__theme
document.body.className = theme
}
if (window?.posthog) {
if (prevLocation) {
window.posthog.capture('$pageleave', {
$host: prevLocation.host,
$pathname: prevLocation.pathname,
$current_url: prevLocation.href,
})
}
window?.posthog?.capture('$pageview')
}
}
export const wrapPageElement = ({ element, props }) => {
const slug = props.location.pathname.substring(1)
return !/^posts\/new|^posts\/(.*)\/edit/.test(slug) &&
(props.pageContext.post || /^posts|^changelog\/(.*?)\//.test(slug)) ? (
<Posts {...props}>{element}</Posts>
) : props.custom404 || !props.data || props.pageContext.ignoreWrapper ? (
element
) : /^handbook|^docs\/(?!api)|^manual/.test(slug) &&
![
'docs/api/post-only-endpoints',
'docs/api/user',
'docs/integrations',
'docs/product-analytics',
'docs/session-replay',
'docs/feature-flags',
'docs/experiments',
'docs/data',
].includes(slug) ? (
<HandbookLayout {...props} />
) : /^session-replay|^product-analytics|^feature-flags|^ab-testing|^product-os/.test(slug) ? (
<Product {...props} />
) : /^careers\//.test(slug) ? (
<Job {...props} />
) : (
element
)
}