pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 1 | import React, { useEffect, useState } from "react"; |
pineafan | e0283a8 | 2022-02-13 10:05:56 +0000 | [diff] [blame] | 2 | import Styles from '../styles/Components/header.module.css'; |
pineafan | b18f019 | 2022-10-27 22:08:36 +0100 | [diff] [blame] | 3 | import NavStyles from '../styles/Components/navbar.module.css'; |
pineafan | 876af7d | 2021-10-14 20:31:21 +0100 | [diff] [blame] | 4 | import Head from 'next/head'; |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 5 | import { useReward } from "react-rewards"; |
| 6 | import ScrollContainer from 'react-indiana-drag-scroll' |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 7 | |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 8 | const positive = [ |
| 9 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f389.svg", |
| 10 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f388.svg", |
| 11 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f973.svg", |
| 12 | ]; |
| 13 | |
| 14 | const negative = [ |
| 15 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f614.svg", |
| 16 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f61e.svg", |
| 17 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f613.svg", |
| 18 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f625.svg", |
| 19 | "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f623.svg", |
| 20 | ]; |
| 21 | |
| 22 | function preloadImage (src) { |
| 23 | return new Promise((resolve, reject) => { |
| 24 | const img = new Image() |
| 25 | img.onload = function() { |
| 26 | resolve(img) |
| 27 | } |
| 28 | img.onerror = img.onabort = function() { |
| 29 | reject(src) |
| 30 | } |
| 31 | img.src = src |
| 32 | }) |
| 33 | } |
| 34 | |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 35 | function Header(props) { |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 36 | const keys = Object.keys(props.effects ?? {}); |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 37 | |
| 38 | const { reward: reward, isAnimating: isAnimating } = useReward('headerConfetti', 'confetti', { |
| 39 | elementSize: 10, |
| 40 | elementCount: 150, |
| 41 | startVelocity: 35, |
| 42 | lifetime: 300, |
| 43 | decay: 0.95, |
| 44 | spread: 170, |
| 45 | position: "absolute", |
| 46 | colors: ["#F27878", "#E5AB71", "#E5DC71", "#A1CC65", "#68D49E", "#71AFE5", "#6576CC", "#8D58B2", "#BF5E9F"] |
| 47 | }); |
| 48 | const { reward: disappointment, isAnimating: isDisappointmentAnimating } = useReward('disappointmentConfetti', 'confetti', { |
| 49 | elementSize: 25, |
| 50 | elementCount: 1, |
| 51 | startVelocity: 25, |
| 52 | lifetime: 350, |
| 53 | decay: 0.95, |
| 54 | spread: 0, |
| 55 | position: "absolute", |
| 56 | colors: ["#E5AB71"] |
| 57 | }); |
| 58 | |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 59 | const all = positive.concat(negative) |
| 60 | const [imagesPreloaded, setImagesPreloaded] = useState(false) |
PineaFan | 93f540e | 2022-12-04 22:05:57 +0000 | [diff] [blame] | 61 | const [clickTotal, setClickTotal] = useState(0) |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 62 | useEffect(() => { |
| 63 | let isCancelled = false |
| 64 | async function effect() { |
| 65 | if (isCancelled) { return } |
| 66 | const imagesPromiseList = [] |
| 67 | for (const i of all) { imagesPromiseList.push(preloadImage(i)) } |
| 68 | await Promise.all(imagesPromiseList) |
| 69 | if (isCancelled) { return } |
| 70 | setImagesPreloaded(true) |
| 71 | }; |
| 72 | effect() |
| 73 | return () => { isCancelled = true } |
| 74 | }, [all]) |
| 75 | |
pineafan | b18f019 | 2022-10-27 22:08:36 +0100 | [diff] [blame] | 76 | function showSubBarWithUrl(url) { |
| 77 | const screenWidth = window.innerWidth; |
| 78 | let amount = screenWidth - 50; |
| 79 | amount = Math.floor(amount / 42) - 1; |
| 80 | props.showSubBar( |
| 81 | <div className={NavStyles.container}> |
| 82 | { |
| 83 | Array.from(Array(amount)).map((i, index) => { |
| 84 | return ( |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 85 | <a className={NavStyles.icon} key={index}><img alt="" className={NavStyles.icon} src={ |
pineafan | b18f019 | 2022-10-27 22:08:36 +0100 | [diff] [blame] | 86 | url[Math.floor(Math.random() * url.length)] |
| 87 | } /></a> |
| 88 | ) |
| 89 | }) |
| 90 | } |
| 91 | </div>, |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 92 | 5, "center" |
pineafan | b18f019 | 2022-10-27 22:08:36 +0100 | [diff] [blame] | 93 | ); |
| 94 | } |
| 95 | |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 96 | function confetti() { |
| 97 | if (!isAnimating && !isDisappointmentAnimating && props.index) { |
| 98 | setClickTotal(clickTotal + 1); |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame] | 99 | if (clickTotal > 0 && Math.floor(Math.random() * 3) === 0) { |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 100 | showSubBarWithUrl(negative); |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 101 | disappointment(); |
| 102 | } else { |
pineafan | b7c7974 | 2022-11-06 18:08:36 +0000 | [diff] [blame] | 103 | showSubBarWithUrl(positive); |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 104 | reward(); |
| 105 | } |
| 106 | } |
pineafan | 9b1b68c | 2021-11-05 17:47:27 +0000 | [diff] [blame] | 107 | } |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 108 | const season = props.season ?? { season: "normal", filePath: "normal" }; |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 109 | |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 110 | return ( |
PineaFan | a465f35 | 2023-02-05 16:45:01 +0000 | [diff] [blame] | 111 | <div className={Styles.header} style={{ |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 112 | margin: "0", |
| 113 | minHeight: props.fullscreen ? "calc(100vh - 42px)" : "calc(100vh - (4 * max(2em, 4vw)) - 1em)", |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 114 | transform: season.season === "aprilFools" ? "rotate(2.5deg)" : "none", |
PineaFan | a465f35 | 2023-02-05 16:45:01 +0000 | [diff] [blame] | 115 | transition: "transform 1s cubic-bezier(.47,1.64,.41,.8), background-color 0.3s ease-in-out" |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 116 | }} id={props.id ? props.id : null}> |
PineaFan | a465f35 | 2023-02-05 16:45:01 +0000 | [diff] [blame] | 117 | <div className={Styles.container} style={{minHeight: props.fullscreen ? "calc(100vh - 42px)" : "calc(100vh - (4 * max(2em, 4vw)) - 1em)"}}> |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 118 | {imagesPreloaded} |
| 119 | <div className={Styles.backgroundGradient} style={{ |
| 120 | backgroundImage: `linear-gradient(69.44deg, #${props.gradient[0]} 0%, #${props.gradient[1]} 100%)`, |
| 121 | }} /> |
| 122 | <Head> |
| 123 | <title>{props.name}</title> |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 124 | |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 125 | <meta name="apple-mobile-web-app-capable" content="yes" /> |
| 126 | <meta name="mobile-web-app-capable" content="yes" /> |
| 127 | <meta name="viewport" content="width=device-width, minimal-ui" /> |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 128 | |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 129 | <meta name="title" content={props.name} /> |
| 130 | <meta name="og:title" content={props.name} /> |
| 131 | <meta name="description" content={props.embedDescription ? props.embedDescription : props.subtext} /> |
| 132 | <meta name="og:description" content={props.embedDescription ? props.embedDescription : props.subtext} /> |
| 133 | <meta name="author" content="Clicks" /> |
| 134 | <meta name="og:author" content="Clicks" /> |
| 135 | <meta name="image" content={props.embedImage} /> |
| 136 | <meta name="og:image" content={props.embedImage} /> |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 137 | |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 138 | <meta name="theme-color" content={"#000000"} /> |
| 139 | <meta name="og-color" content={"#" + props.gradient[1]} /> |
| 140 | <meta name="msapplication-TileColor" content={"#000000"} /> |
| 141 | </Head> |
| 142 | <img draggable={false} alt="" className={Styles.backgroundImage} src={`https://assets.clicks.codes/${props.wave}.svg`} /> |
| 143 | <div id="headerConfetti" /> |
| 144 | <div id="disappointmentConfetti" /> |
| 145 | <div className={Styles.panel}> |
| 146 | <div className={Styles.titleContainer}> |
| 147 | <div onClick={confetti}> |
| 148 | { |
| 149 | props.customImage ? |
| 150 | <img height="64px" width="64px" alt={props.name} className={Styles.headerImage} style={{borderRadius: props.roundImage ? "100vw" : "0"}} src={props.customImage} /> |
| 151 | : <></> |
| 152 | } |
| 153 | </div> |
| 154 | <h1 className={Styles.title}>{props.name}</h1> |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 155 | </div> |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 156 | <div className={Styles.textBar}> |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 157 | { |
| 158 | props.subtext instanceof String ? |
| 159 | <p className={Styles.subtext + " " + (props.buttons.length ? Styles.subtextExtra : null)}>{props.subtext}</p> : |
| 160 | props.subtext |
| 161 | } |
pineafan | 74f1674 | 2022-11-07 21:57:55 +0000 | [diff] [blame] | 162 | </div> |
| 163 | <a href="#skipNav" id="skipNav" style={{display: "none"}} /> |
| 164 | { props.buttons.length ? |
| 165 | <div className={Styles.buttonOverflow}> |
| 166 | <img className={Styles.indicator + " " + Styles.leftArrow} draggable={false} alt="" src={`https://assets.clicks.codes/web/icons/arrow.svg`}/> |
| 167 | <ScrollContainer |
| 168 | vertical={false} |
| 169 | horizontal={true} |
| 170 | hideScrollbars={true} |
| 171 | nativeMobileScroll={true} |
| 172 | style={{borderRadius: "10px", height: "fit-content"}} |
| 173 | > |
| 174 | <div className={Styles.buttonLayout}> |
| 175 | { |
| 176 | props.buttons ? props.buttons.map((button, index) => { |
| 177 | return <a |
| 178 | key={index} |
| 179 | className={Styles.button} |
| 180 | style={{ backgroundColor: `#${button.color}`, color: `#${button.buttonText}` }} |
| 181 | href={button.link} |
| 182 | onClick={ |
| 183 | button.onClick ? button.onClick : () => { if (button.id) { props.callback(button.id) } }} |
| 184 | target={button.target ? "_blank" : null} |
| 185 | draggable={false} |
| 186 | rel="noreferrer"> |
| 187 | {button.text} |
| 188 | </a> |
| 189 | }) : null |
| 190 | } |
| 191 | </div> |
| 192 | </ScrollContainer> |
| 193 | <img className={Styles.indicator + " " + Styles.rightArrow} draggable={false} alt="" src={`https://assets.clicks.codes/web/icons/arrow.svg`}/> |
| 194 | </div> : <></> |
| 195 | } |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 196 | </div> |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 197 | </div> |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 198 | </div> |
| 199 | ) |
pineafan | a5ce910 | 2021-09-02 17:21:31 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | export default Header; |