Skip to content

Commit

Permalink
Separate posthog provider component
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Jan 20, 2025
1 parent 662e67e commit e0b8e25
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/keychain/src/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AlertIcon, ExternalIcon, Button } from "@cartridge/ui-next";
import { useConnection } from "@/hooks/connection";
import { CARTRIDGE_DISCORD_LINK } from "@/const";
import { Link } from "react-router-dom";
import { usePostHog } from "@/context/posthog";
import { usePostHog } from "@/hooks/posthog";
import { useEffect } from "react";

export class ErrorBoundary extends React.Component<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useCreateController } from "./useCreateController";
import { ErrorAlert } from "@/components/ErrorAlert";
import { VerifiableControllerTheme } from "@/context/theme";
import InAppSpy from "inapp-spy";
import { usePostHog } from "@/context/posthog";
import { usePostHog } from "@/hooks/posthog";

interface CreateControllerViewProps {
theme: VerifiableControllerTheme;
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Settings } from "./settings";
import { SignMessage } from "./SignMessage";
import { PageLoading } from "./Loading";
import { execute } from "@/utils/connection/execute";
import { usePostHog } from "@/context/posthog";
import { usePostHog } from "@/hooks/posthog";

export function Home() {
const { context, setContext, controller, error, policies, upgrade } =
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/components/provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { useConnectionValue } from "@/hooks/connection";
import { CartridgeAPIProvider } from "@cartridge/utils/api/cartridge";
import { ENDPOINT } from "@/utils/graphql";
import { ConnectionContext } from "./connection";
import { PostHogProvider } from "./posthog";
import { ControllerThemeProvider } from "./theme";
import { jsonRpcProvider, StarknetConfig, voyager } from "@starknet-react/core";
import { sepolia, mainnet } from "@starknet-react/chains";
import { constants, num } from "starknet";
import { BrowserRouter } from "react-router-dom";
import { PostHogProvider } from "@/context/posthog";

export function Provider({ children }: PropsWithChildren) {
const connection = useConnectionValue();
Expand Down
16 changes: 16 additions & 0 deletions packages/keychain/src/components/provider/posthog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PropsWithChildren } from "react";
import { PostHogContext, PostHogWrapper } from "@/context/posthog";

export function PostHogProvider({ children }: PropsWithChildren) {
const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, {
host: import.meta.env.VITE_POSTHOG_HOST,
persistence: "memory",
autocapture: false,
});

return (
<PostHogContext.Provider value={{ posthog }}>
{children}
</PostHogContext.Provider>
);
}
30 changes: 5 additions & 25 deletions packages/keychain/src/context/posthog.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createContext, useContext, PropsWithChildren } from "react";
import { createContext } from "react";
import PostHog from "posthog-js-lite";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Properties = Record<string, any>;

class PostHogWrapper extends PostHog {
export class PostHogWrapper extends PostHog {
isLocal =
typeof window !== "undefined" &&
window.location.hostname.includes("localhost");
Expand Down Expand Up @@ -87,26 +87,6 @@ interface PostHogContextType {
posthog: PostHogWrapper;
}

const PostHogContext = createContext<PostHogContextType | undefined>(undefined);

export function PostHogProvider({ children }: PropsWithChildren) {
const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, {
host: import.meta.env.VITE_POSTHOG_HOST,
persistence: "memory",
autocapture: false,
});

return (
<PostHogContext.Provider value={{ posthog }}>
{children}
</PostHogContext.Provider>
);
}

export function usePostHog() {
const context = useContext(PostHogContext);
if (!context) {
return undefined;
}
return context.posthog;
}
export const PostHogContext = createContext<PostHogContextType | undefined>(
undefined,
);
2 changes: 1 addition & 1 deletion packages/keychain/src/hooks/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ConnectionContextValue,
} from "@/components/provider/connection";
import { UpgradeInterface, useUpgrade } from "./upgrade";
import { usePostHog } from "@/context/posthog";
import { usePostHog } from "@/hooks/posthog";
import { Policies } from "@cartridge/presets";
import {
defaultTheme,
Expand Down
10 changes: 10 additions & 0 deletions packages/keychain/src/hooks/posthog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useContext } from "react";
import { PostHogContext } from "@/context/posthog";

export function usePostHog() {
const context = useContext(PostHogContext);
if (!context) {
return undefined;
}
return context.posthog;
}

0 comments on commit e0b8e25

Please sign in to comment.