Skip to content

Commit 1fc3aa2

Browse files
rajat1saxenaRajat Saxena
andauthored
Fix (#605)
Co-authored-by: Rajat Saxena <hi@rajatsaxena.dev>
1 parent 9129bbc commit 1fc3aa2

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

apps/web/app/(with-contexts)/(with-layout)/home-page-layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { BaseLayout } from "@components/public/base-layout";
1212
import { Profile } from "@courselit/common-models";
1313
import { getFullSiteSetup } from "@ui-lib/utils";
1414
import { useContext } from "react";
15+
import { CodeInjectorWrapper } from "@components/public/code-injector";
1516

1617
export default function HomepageLayout({
1718
children,
@@ -60,6 +61,7 @@ export default function HomepageLayout({
6061
}}
6162
>
6263
{children}
64+
<CodeInjectorWrapper />
6365
</BaseLayout>
6466
);
6567
}

apps/web/components/public/code-injector.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
import React from "react";
1+
"use client";
2+
3+
import React, { useContext } from "react";
24
import { connect } from "react-redux";
35
import { AppState } from "@courselit/state-management";
6+
import { usePathname } from "next/navigation";
7+
import { SiteInfoContext } from "@components/contexts";
48

59
type InjectionSection = "head" | "body";
610

711
interface CodeInjectorProps {
8-
router: Record<string, unknown>;
12+
isAdminPage?: boolean;
913
head?: string;
1014
body?: string;
1115
}
1216

13-
class CodeInjector extends React.Component<CodeInjectorProps> {
14-
isAnAdminPage() {
15-
return /^\/dashboard/.test(this.props.router.asPath as string);
16-
}
17+
export const CodeInjectorWrapper = () => {
18+
const siteInfo = useContext(SiteInfoContext);
19+
const pathname = usePathname();
20+
21+
return (
22+
<CodeInjector
23+
isAdminPage={pathname?.startsWith("/dashboard")}
24+
head={siteInfo?.codeInjectionHead}
25+
body={siteInfo?.codeInjectionBody}
26+
/>
27+
);
28+
};
1729

30+
export class CodeInjector extends React.Component<CodeInjectorProps> {
1831
componentDidMount() {
19-
if (!this.isAnAdminPage()) {
32+
if (!this.props.isAdminPage) {
2033
const targetTagsForInjection: InjectionSection[] = ["head", "body"];
2134
for (const target of targetTagsForInjection) {
2235
this.injectCodeIn(target);
@@ -36,7 +49,7 @@ class CodeInjector extends React.Component<CodeInjectorProps> {
3649
this.copyAttributes(elem, script);
3750
elem = script;
3851
}
39-
(document as Record<string, any>)[targetHTMLTag].appendChild(elem);
52+
(document as Record<string, any>)[targetHTMLTag]?.appendChild(elem);
4053
}
4154
}
4255

@@ -52,7 +65,7 @@ class CodeInjector extends React.Component<CodeInjectorProps> {
5265
}
5366

5467
render() {
55-
return <></>;
68+
return null;
5669
}
5770
}
5871

apps/web/pages/_app.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function MyApp({
4949
>
5050
<Component {...pageProps} />
5151
</div>
52-
<CodeInjector router={router} />
52+
<CodeInjector
53+
isAdminPage={router.pathname.startsWith("/dashboard")}
54+
/>
5355
</ThemeProvider>
5456
</Provider>
5557
</SessionProvider>

0 commit comments

Comments
 (0)