|
| 1 | +import * as React from "react"; |
| 2 | +import "./styles.scss"; |
| 3 | +import Utils from "../../../utils/utils"; |
| 4 | +import classNames from "classnames"; |
| 5 | +import { BsPinAngle, BsPinAngleFill, BsUnlock, BsLockFill } from "react-icons/bs"; |
| 6 | +import { MdAutorenew } from "react-icons/md"; |
| 7 | +import CFM from "../../../utils/config"; |
| 8 | + |
| 9 | +interface OverviewCardProps { |
| 10 | + onExit: () => void; |
| 11 | + onToggle: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +const OverviewCard = ({ onExit, onToggle }: OverviewCardProps) => { |
| 15 | + const [visibility, setVisibility] = React.useState(true); |
| 16 | + const [pinned, setPinned] = React.useState(CFM.get("overviewCardPinned")); |
| 17 | + |
| 18 | + const overviewCardTimer = React.useRef<NodeJS.Timeout | null>(null); |
| 19 | + |
| 20 | + const hideCard = (timeout = 3000) => { |
| 21 | + if (overviewCardTimer.current) clearTimeout(overviewCardTimer.current); |
| 22 | + setVisibility(true); |
| 23 | + overviewCardTimer.current = setTimeout(() => { |
| 24 | + setVisibility(false); |
| 25 | + }, timeout); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleMouseMove = (evt: MouseEvent) => { |
| 29 | + // Show card when mouse is on the top side of the screen and centered horizantally |
| 30 | + if ( |
| 31 | + !pinned && |
| 32 | + evt.clientY / window.innerHeight < 0.15 && |
| 33 | + evt.clientX / window.innerWidth > 0.3 && |
| 34 | + evt.clientX / window.innerWidth < 0.7 |
| 35 | + ) { |
| 36 | + hideCard(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + React.useEffect(() => { |
| 41 | + if (overviewCardTimer.current) clearTimeout(overviewCardTimer.current); |
| 42 | + if (!pinned) { |
| 43 | + hideCard(); |
| 44 | + } |
| 45 | + document.addEventListener("mousemove", handleMouseMove); |
| 46 | + |
| 47 | + return () => { |
| 48 | + document.removeEventListener("mousemove", handleMouseMove); |
| 49 | + }; |
| 50 | + }, [pinned]); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div |
| 54 | + id="fsd-overview-card" |
| 55 | + className={classNames({ |
| 56 | + "c-hidden": !visibility, |
| 57 | + "card-pinned": pinned, |
| 58 | + })}> |
| 59 | + <Spicetify.ReactComponent.TooltipWrapper |
| 60 | + label={pinned ? "Unpin Card" : "Pin Card"} |
| 61 | + placement="bottom"> |
| 62 | + <button |
| 63 | + id="fsd-overview-pin-button" |
| 64 | + onClick={() => { |
| 65 | + if (!pinned) { |
| 66 | + if (overviewCardTimer.current) clearTimeout(overviewCardTimer.current); |
| 67 | + } |
| 68 | + setPinned(!pinned); |
| 69 | + CFM.set("overviewCardPinned", !pinned); |
| 70 | + }}> |
| 71 | + {pinned ? <BsPinAngleFill /> : <BsPinAngle />} |
| 72 | + </button> |
| 73 | + </Spicetify.ReactComponent.TooltipWrapper> |
| 74 | + <ClockSection /> |
| 75 | + |
| 76 | + <div id="fsd-overview-button-container"> |
| 77 | + {/* <Spicetify.ReactComponent.TooltipWrapper label="Set Wake Lock" placement="bottom"> |
| 78 | + <button |
| 79 | + id="overview-lock-button" |
| 80 | + className="fsd-overview-button" |
| 81 | + onClick={() => {}}> |
| 82 | + <BsUnlock /> |
| 83 | + </button> |
| 84 | + </Spicetify.ReactComponent.TooltipWrapper> */} |
| 85 | + <Spicetify.ReactComponent.TooltipWrapper label="Toggle Mode" placement="bottom"> |
| 86 | + <button |
| 87 | + id="overview-toggle-button" |
| 88 | + className="fsd-overview-button" |
| 89 | + onClick={onToggle}> |
| 90 | + <MdAutorenew /> |
| 91 | + </button> |
| 92 | + </Spicetify.ReactComponent.TooltipWrapper> |
| 93 | + |
| 94 | + <Spicetify.ReactComponent.TooltipWrapper label="Exit" placement="bottom"> |
| 95 | + <button |
| 96 | + id="overview-exit-button" |
| 97 | + className="fsd-overview-button" |
| 98 | + dangerouslySetInnerHTML={{ |
| 99 | + __html: `<svg width="1.5em" height="1.5em" viewBox="0 0 16 16" fill="currentColor">${ |
| 100 | + Spicetify.SVGIcons.x |
| 101 | + }</svg>`, |
| 102 | + }} |
| 103 | + onClick={onExit} |
| 104 | + /> |
| 105 | + </Spicetify.ReactComponent.TooltipWrapper> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +const ClockSection = () => { |
| 112 | + const [currentTime, setCurrentTime] = React.useState(Utils.getTimeFormatted()); |
| 113 | + |
| 114 | + function updateTimeDisplay() { |
| 115 | + setCurrentTime(Utils.getTimeFormatted()); |
| 116 | + } |
| 117 | + |
| 118 | + React.useEffect(() => { |
| 119 | + const updateInterval = setInterval(updateTimeDisplay, 1000); |
| 120 | + |
| 121 | + return () => { |
| 122 | + clearInterval(updateInterval); |
| 123 | + }; |
| 124 | + }, []); |
| 125 | + |
| 126 | + return <div id="fsd-time-display">{currentTime}</div>; |
| 127 | +}; |
| 128 | +export default OverviewCard; |
0 commit comments