|
1 | 1 | import clsx from 'clsx' |
2 | 2 | import React, { useEffect, useState } from 'react' |
3 | | -import { BsBoxArrowInUpRight } from 'react-icons/bs' |
4 | | -import { ref } from 'src/config' |
5 | 3 | import { AdvBanner } from 'src/features/adv' |
6 | | -import { useRemoteConfigStore } from 'src/features/remoteConfig' |
7 | | -import { useUserPreferences } from 'src/stores/preferences' |
| 4 | +import { DesktopBreakpoint } from 'src/providers/DesktopBreakpoint' |
| 5 | +import { MobileBreakpoint } from 'src/providers/MobileBreakpoint' |
8 | 6 | import { CardPropsType } from 'src/types' |
9 | 7 |
|
10 | 8 | type RootCardProps = CardPropsType & { |
11 | 9 | children: React.ReactNode |
12 | 10 | titleComponent?: React.ReactNode |
| 11 | + settingsComponent?: React.ReactNode |
13 | 12 | fullBlock?: boolean |
14 | 13 | } |
| 14 | +export const Card = React.forwardRef<HTMLDivElement, RootCardProps>( |
| 15 | + ( |
| 16 | + { |
| 17 | + meta, |
| 18 | + titleComponent, |
| 19 | + settingsComponent, |
| 20 | + className, |
| 21 | + withAds = false, |
| 22 | + children, |
| 23 | + fullBlock = false, |
| 24 | + knob, |
| 25 | + }, |
| 26 | + ref |
| 27 | + ) => { |
| 28 | + const { icon, label, badge } = meta |
| 29 | + const [canAdsLoad, setCanAdsLoad] = useState(true) |
15 | 30 |
|
16 | | -export const Card = ({ |
17 | | - meta, |
18 | | - titleComponent, |
19 | | - className, |
20 | | - withAds = false, |
21 | | - children, |
22 | | - fullBlock = false, |
23 | | - knob, |
24 | | -}: RootCardProps) => { |
25 | | - const { openLinksNewTab } = useUserPreferences() |
26 | | - const { link, icon, label, badge } = meta |
27 | | - const [canAdsLoad, setCanAdsLoad] = useState(true) |
28 | | - const { adsConfig } = useRemoteConfigStore() |
29 | | - |
30 | | - useEffect(() => { |
31 | | - if (!adsConfig.enabled || !withAds) { |
32 | | - return |
33 | | - } |
| 31 | + useEffect(() => { |
| 32 | + if (!withAds) { |
| 33 | + return |
| 34 | + } |
34 | 35 |
|
35 | | - const handleClassChange = () => { |
36 | | - if (document.documentElement.classList.contains('dndState')) { |
37 | | - setCanAdsLoad(false) |
38 | | - } else { |
39 | | - setCanAdsLoad(true) |
| 36 | + const handleClassChange = () => { |
| 37 | + if (document.documentElement.classList.contains('dndState')) { |
| 38 | + setCanAdsLoad(false) |
| 39 | + } else { |
| 40 | + setCanAdsLoad(true) |
| 41 | + } |
40 | 42 | } |
41 | | - } |
42 | 43 |
|
43 | | - const observer = new MutationObserver(handleClassChange) |
44 | | - observer.observe(document.documentElement, { attributes: true }) |
| 44 | + const observer = new MutationObserver(handleClassChange) |
| 45 | + observer.observe(document.documentElement, { attributes: true }) |
45 | 46 |
|
46 | | - return () => { |
47 | | - observer.disconnect() |
48 | | - } |
49 | | - }, [withAds, adsConfig.enabled]) |
| 47 | + return () => { |
| 48 | + observer.disconnect() |
| 49 | + } |
| 50 | + }, [withAds]) |
50 | 51 |
|
51 | | - const handleHeaderLinkClick = (e: React.MouseEvent<HTMLAnchorElement>) => { |
52 | | - e.preventDefault() |
53 | | - let url = `${link}?${ref}` |
54 | | - window.open(url, openLinksNewTab ? '_blank' : '_self') |
55 | | - } |
| 52 | + return ( |
| 53 | + <div ref={ref} className={clsx('block', fullBlock && 'fullBlock', className)}> |
| 54 | + <MobileBreakpoint> |
| 55 | + {settingsComponent && <button className="floatingFilter">{settingsComponent}</button>} |
| 56 | + </MobileBreakpoint> |
| 57 | + <div className="blockHeader"> |
| 58 | + {knob} |
| 59 | + <span className="blockHeaderIcon">{icon}</span> {titleComponent || label}{' '} |
| 60 | + <DesktopBreakpoint> |
| 61 | + {settingsComponent && ( |
| 62 | + <span className="blockHeaderSettingsButton">{settingsComponent}</span> |
| 63 | + )} |
| 64 | + </DesktopBreakpoint> |
| 65 | + {badge && <span className="blockHeaderBadge">{badge}</span>} |
| 66 | + </div> |
56 | 67 |
|
57 | | - return ( |
58 | | - <div className={clsx('block', fullBlock && 'fullBlock', className)}> |
59 | | - <div className="blockHeader"> |
60 | | - {knob} |
61 | | - <span className="blockHeaderIcon">{icon}</span> {titleComponent || label}{' '} |
62 | | - {link && ( |
63 | | - <a className="blockHeaderLink" href={link} onClick={handleHeaderLinkClick}> |
64 | | - <BsBoxArrowInUpRight /> |
65 | | - </a> |
| 68 | + {canAdsLoad && withAds && ( |
| 69 | + <div className="ad-wrapper blockRow"> |
| 70 | + <AdvBanner /> |
| 71 | + </div> |
66 | 72 | )} |
67 | | - {badge && <span className="blockHeaderBadge">{badge}</span>} |
68 | | - </div> |
69 | 73 |
|
70 | | - {canAdsLoad && adsConfig.enabled && withAds && ( |
71 | | - <div className="ad-wrapper blockRow"> |
72 | | - <AdvBanner /> |
73 | | - </div> |
74 | | - )} |
75 | | - |
76 | | - <div className="blockContent scrollable">{children}</div> |
77 | | - </div> |
78 | | - ) |
79 | | -} |
| 74 | + <div className="blockContent scrollable">{children}</div> |
| 75 | + </div> |
| 76 | + ) |
| 77 | + } |
| 78 | +) |
0 commit comments