1- import React from "react" ;
1+ "use client" ;
2+
3+ import React , { useContext } from "react" ;
24import { connect } from "react-redux" ;
35import { AppState } from "@courselit/state-management" ;
6+ import { usePathname } from "next/navigation" ;
7+ import { SiteInfoContext } from "@components/contexts" ;
48
59type InjectionSection = "head" | "body" ;
610
711interface 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 / ^ \/ d a s h b o a r d / . 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
0 commit comments