Skip to content

Commit

Permalink
Implement basic hooks
Browse files Browse the repository at this point in the history
Based on Threshold Dashboard implementation (WIP)
  • Loading branch information
kpyszkowski committed Oct 1, 2024
1 parent 46671e9 commit e8c55f5
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dapp/src/hooks/posthog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./usePostHogIdentify"
export * from "./usePostHog"
export * from "./usePostHogCapture"
export * from "./usePostHogCapturePageView"
7 changes: 7 additions & 0 deletions dapp/src/hooks/posthog/usePostHog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { usePostHogCapturePageView } from "./usePostHogCapturePageView"
import { usePostHogIdentify } from "./usePostHogIdentify"

export const usePostHog = () => {
usePostHogCapturePageView()
usePostHogIdentify()
}
15 changes: 15 additions & 0 deletions dapp/src/hooks/posthog/usePostHogCapture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { featureFlags } from "#/constants"
import { PostHog } from "posthog-js"
import { usePostHog as usePostHogClient } from "posthog-js/react"
import { useEffect } from "react"

export const usePostHogCapture = (
...captureArgs: Parameters<PostHog["capture"]>
) => {
const postHog = usePostHogClient()

useEffect(() => {
if (!featureFlags.POSTHOG_ENABLED) return
postHog.capture(...captureArgs)
}, [postHog, captureArgs])
}
7 changes: 7 additions & 0 deletions dapp/src/hooks/posthog/usePostHogCapturePageView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { usePostHogCapture } from "./usePostHogCapture"

export const usePostHogCapturePageView = () => {
const { pathname } = window.location

usePostHogCapture("$pageview", { pathname })
}
70 changes: 70 additions & 0 deletions dapp/src/hooks/posthog/usePostHogIdentify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { featureFlags } from "#/constants"
import { usePostHog } from "posthog-js/react"
import { useCallback, useEffect } from "react"
import { hashString, isSameETHAddress, logPromiseFailure } from "#/utils"
import { EventCallback, useSubscribeToConnectorEvent } from "@orangekit/react"
import { getAddress } from "ethers"
import { useConfig } from "wagmi"
import { useWallet } from "../useWallet"
import { useConnector } from "../orangeKit"

export const usePostHogIdentify = () => {
const postHog = usePostHog()
const connector = useConnector()
const { address } = useWallet()
const wagmiConfig = useConfig()

useEffect(() => {
if (!featureFlags.POSTHOG_ENABLED || !address) return

const handleIdentify = async () => {
const hashedAccountAddress = await hashString({

Check failure on line 21 in dapp/src/hooks/posthog/usePostHogIdentify.ts

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe assignment of an `any` value

Check failure on line 21 in dapp/src/hooks/posthog/usePostHogIdentify.ts

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe call of an `any` typed value
value: address.toUpperCase(),
})

postHog.identify(hashedAccountAddress)

Check failure on line 25 in dapp/src/hooks/posthog/usePostHogIdentify.ts

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe argument of type `any` assigned to a parameter of type `string | undefined`
}

logPromiseFailure(handleIdentify())
}, [postHog, address])

const handleConnectorChange = useCallback<EventCallback<"change">>(
(updated) => {
const [updatedAddress] = updated.accounts || []

if (!updatedAddress) {
postHog.reset()
return
}

const isChanged =
updatedAddress && address && !isSameETHAddress(updatedAddress, address)

Check failure on line 41 in dapp/src/hooks/posthog/usePostHogIdentify.ts

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe call of an `any` typed value

if (isChanged) {
postHog.reset()
postHog.identify(getAddress(updatedAddress))
}
},
[postHog, address],
)

const handleConnectorDisconnect = useCallback<
EventCallback<"disconnect">
>(() => {
postHog.reset()
}, [postHog])

useSubscribeToConnectorEvent(
wagmiConfig,
connector!.name,
"change",
handleConnectorChange,
)

useSubscribeToConnectorEvent(
wagmiConfig,
connector!.name,
"disconnect",
handleConnectorDisconnect,
)
}
26 changes: 26 additions & 0 deletions dapp/src/posthog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { PropsWithChildren } from "react"
import { PostHogProvider as Provider } from "posthog-js/react"
import { PostHogConfig } from "posthog-js"
import { featureFlags, env } from "./constants"

const options: Partial<PostHogConfig> = {
api_host: env.POSTHOG_API_HOST,

Check failure on line 7 in dapp/src/posthog.tsx

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe assignment of an `any` value
capture_pageview: false,
persistence: "memory",
}

function PostHogProvider(props: PropsWithChildren) {
const { children } = props

if (!featureFlags.POSTHOG_ENABLED) {
return children
}

return (
<Provider apiKey={env.POSTHOG_API_KEY} options={options}>

Check failure on line 20 in dapp/src/posthog.tsx

View workflow job for this annotation

GitHub Actions / dapp-format

Unsafe assignment of an `any` value
{children}
</Provider>
)
}

export default PostHogProvider
17 changes: 17 additions & 0 deletions dapp/src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type HashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512"

export const hashString = async ({
algorithm = "SHA-256",
value,
}: {
algorithm?: HashAlgorithm
value: string
}) =>
Array.prototype.map
.call(
new Uint8Array(
await crypto.subtle.digest(algorithm, new TextEncoder().encode(value)),
),
(x: number) => `0${x.toString(16)}`.slice(-2),
)
.join("")

0 comments on commit e8c55f5

Please sign in to comment.