-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move Matomo, Askem and HDS-style handling of the BaseApp to…
… new files HCRC-106
- Loading branch information
1 parent
47469db
commit d55cf3d
Showing
4 changed files
with
99 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
|
||
/** | ||
* Unset hidden visibility that was applied to hide the first server render | ||
* that does not include styles from HDS. HDS applies styling by injecting | ||
* style tags into the head. This requires the existence of a document object. | ||
* The document object does not exist during server side renders. | ||
*/ | ||
// TODO: Remove this hackfix to ensure that pre-rendered pages' | ||
// SEO performance is not impacted. | ||
export default function useHdsStyleFix() { | ||
React.useEffect(() => { | ||
setTimeout(() => { | ||
const body = document?.body; | ||
|
||
if (body) { | ||
body.style.visibility = 'unset'; | ||
} | ||
}, 10); | ||
}, []); | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/components/src/components/askem/useAskemContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useCookies } from 'hds-react'; | ||
import isEqual from 'lodash/isEqual'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import createAskemInstance from './instance'; | ||
import type { AskemConfigs, AskemInstance } from './types'; | ||
|
||
export default function useAskemContext({ | ||
cookieDomain, | ||
asPath, | ||
askemConfigurationInput, | ||
}: { | ||
cookieDomain: string; | ||
asPath: string; | ||
askemConfigurationInput: AskemConfigs; | ||
}) { | ||
const { getAllConsents } = useCookies({ cookieDomain }); | ||
const [askemConsentGiven, setAskemConsentGiven] = useState<boolean>(false); | ||
const [askemInstance, setAskemInstance] = useState<AskemInstance | null>( | ||
null | ||
); | ||
const [askemConfiguration, setAskemConfiguration] = | ||
useState<AskemConfigs | null>(null); | ||
|
||
const handleConsentGiven = useCallback(() => { | ||
const consents = getAllConsents(); | ||
setAskemConsentGiven( | ||
consents['askemBid'] && | ||
consents['askemBidTs'] && | ||
consents['askemReaction'] | ||
); | ||
}, [getAllConsents]); | ||
|
||
useEffect(() => { | ||
if (asPath) { | ||
handleConsentGiven(); | ||
} | ||
}, [handleConsentGiven, asPath]); | ||
|
||
const newAskemConfiguration: AskemConfigs = { | ||
...askemConfigurationInput, | ||
consentGiven: askemConsentGiven, | ||
}; | ||
|
||
if (!askemInstance || !isEqual(askemConfiguration, newAskemConfiguration)) { | ||
setAskemConfiguration(newAskemConfiguration); | ||
setAskemInstance(createAskemInstance(newAskemConfiguration)); | ||
} | ||
|
||
return { askemInstance, handleConsentGiven }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createInstance as createMatomoInstance } from '@jonkoops/matomo-tracker-react'; | ||
import { useMemo } from 'react'; | ||
|
||
export default function useMatomoInstance( | ||
matomoConfiguration: Parameters<typeof createMatomoInstance>[0] | ||
) { | ||
return useMemo( | ||
() => createMatomoInstance(matomoConfiguration), | ||
[matomoConfiguration] | ||
); | ||
} |