mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-28 12:43:12 +08:00
website: make sidebar ad unit refresh periodically
This commit is contained in:
@ -1,10 +1,12 @@
|
|||||||
import React from 'react';
|
import React, {useEffect, useState} from 'react';
|
||||||
import BrowserOnly from '@docusaurus/BrowserOnly';
|
import BrowserOnly from '@docusaurus/BrowserOnly';
|
||||||
|
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
|
const AD_REFRESH_RATE = 20 * 1000;
|
||||||
|
|
||||||
const BACKGROUNDS = [
|
const BACKGROUNDS = [
|
||||||
styles.backgroundPurplin,
|
styles.backgroundPurplin,
|
||||||
styles.backgroundFirewatch,
|
styles.backgroundFirewatch,
|
||||||
@ -125,13 +127,22 @@ function EducativeSystemDesign({className, position}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default React.memo(function SidebarAd({position}) {
|
export default React.memo(function SidebarAd({position}) {
|
||||||
|
const [counter, setCounter] = useState(0);
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setCounter((counter) => counter + 1);
|
||||||
|
}, AD_REFRESH_RATE);
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [counter]);
|
||||||
|
|
||||||
const backgroundClass =
|
const backgroundClass =
|
||||||
BACKGROUNDS[Math.floor(Math.random() * BACKGROUNDS.length)];
|
BACKGROUNDS[Math.floor(Math.random() * BACKGROUNDS.length)];
|
||||||
|
|
||||||
// Because the SSR and client output can differ and hydration doesn't patch attribute differences,
|
// Because the SSR and client output can differ and hydration doesn't patch attribute differences,
|
||||||
// we'll render this on the browser only.
|
// we'll render this on the browser only.
|
||||||
return (
|
return (
|
||||||
<BrowserOnly>
|
<BrowserOnly key={counter}>
|
||||||
{() => {
|
{() => {
|
||||||
const path = window.location.pathname;
|
const path = window.location.pathname;
|
||||||
// Ugly hack to show conditional sidebar content.
|
// Ugly hack to show conditional sidebar content.
|
||||||
|
Reference in New Issue
Block a user