Skip to content

Commit

Permalink
feat: add context injector for services
Browse files Browse the repository at this point in the history
  • Loading branch information
ogous committed Sep 25, 2024
1 parent 35d20c8 commit b6daf8e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = withSentryConfig(
output: "export",
images: { unoptimized: true },
env: {
GIT_HASH: gitHash,
NEXT_PUBLIC_GIT_HASH: gitHash,
},
webpack(config) {
if (process.env.NODE_V8_COVERAGE) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default async function RootLayout({
}: {
children: React.ReactNode;
}) {
const gitHash = process.env.GIT_HASH;
const gitHash = process.env.NEXT_PUBLIC_GIT_HASH;

const featuresDataRequest = await fetch(
"https://features.long.so/features.json",
Expand Down
5 changes: 5 additions & 0 deletions web/src/config/clientEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const clientEnvSchema = z.object({
* Walletconnect project id.
*/
NEXT_PUBLIC_LONGTAIL_WALLETCONNECT_PROJECT_ID: z.string(),
/**
* Web app version as git commit hash.
*/
NEXT_PUBLIC_GIT_HASH: z.string(),
});

type ClientEnvSchemaType = z.infer<typeof clientEnvSchema>;
Expand All @@ -18,6 +22,7 @@ declare global {
const clientEnv = clientEnvSchema.safeParse({
NEXT_PUBLIC_LONGTAIL_WALLETCONNECT_PROJECT_ID:
process.env.NEXT_PUBLIC_LONGTAIL_WALLETCONNECT_PROJECT_ID,
NEXT_PUBLIC_GIT_HASH: process.env.NEXT_PUBLIC_GIT_HASH,
});

if (!clientEnv.success) {
Expand Down
26 changes: 26 additions & 0 deletions web/src/context/contextInjector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client";

import { useAccount, useChainId } from "wagmi";
import { useEffect } from "react";
import { setContext, setUser } from "@sentry/nextjs";
import appConfig from "@/config";
export default function ContextInjector() {
const account = useAccount();

useEffect(() => {
if (account?.address) {
// window.localStorage.setItem("walletAddress", account.address); // this one is going to be needed for GTM
setUser({ id: account.address });
} else {
// window.localStorage.removeItem("walletAddress");
setUser(null);
}
}, [account?.address]);

useEffect(() => {
setContext("version", { commitHash: appConfig.NEXT_PUBLIC_GIT_HASH });
setContext("chain", { id: account?.chainId });
}, [account?.chainId]);

return null;
}
6 changes: 5 additions & 1 deletion web/src/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createWeb3Modal } from "@web3modal/wagmi/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

import { State, WagmiProvider } from "wagmi";
import ContextInjector from "./contextInjector";

// Setup queryClient
export const queryClient = new QueryClient();
Expand All @@ -29,7 +30,10 @@ export default function Web3ModalProvider({
}) {
return (
<WagmiProvider config={appConfig.wagmiConfig} initialState={initialState}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
<QueryClientProvider client={queryClient}>
<ContextInjector />
{children}
</QueryClientProvider>
</WagmiProvider>
);
}

0 comments on commit b6daf8e

Please sign in to comment.