diff options
| author | Wolfboy248 <georgejvindkarlsen@gmail.com> | 2024-11-25 09:20:01 +0100 |
|---|---|---|
| committer | Wolfboy248 <georgejvindkarlsen@gmail.com> | 2024-11-25 09:20:01 +0100 |
| commit | 207a2540101b2f216bde94ae53286d2e52f044e3 (patch) | |
| tree | 0d36c7d3ea8b87f654aa47384d16f1f8f96a0157 /frontend/src/components/Sidebar_old.tsx | |
| parent | feat/rankings: optimize Steam ID comparison (#236) (diff) | |
| download | lphub-207a2540101b2f216bde94ae53286d2e52f044e3.tar.gz lphub-207a2540101b2f216bde94ae53286d2e52f044e3.tar.bz2 lphub-207a2540101b2f216bde94ae53286d2e52f044e3.zip | |
frontend: begin port to css modules, sidebar refactor
Diffstat (limited to 'frontend/src/components/Sidebar_old.tsx')
| -rw-r--r-- | frontend/src/components/Sidebar_old.tsx | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/frontend/src/components/Sidebar_old.tsx b/frontend/src/components/Sidebar_old.tsx new file mode 100644 index 0000000..4d1cd7a --- /dev/null +++ b/frontend/src/components/Sidebar_old.tsx | |||
| @@ -0,0 +1,210 @@ | |||
| 1 | import React, { useRef } from 'react'; | ||
| 2 | import { Link, useLocation } from 'react-router-dom'; | ||
| 3 | |||
| 4 | import btn from "@css/Button.module.css"; | ||
| 5 | import { BookIcon, FlagIcon, HelpIcon, HomeIcon, LogoIcon, PortalIcon, SearchIcon, UploadIcon } from '@images/Images'; | ||
| 6 | import Login from '@components/Login'; | ||
| 7 | import { UserProfile } from '@customTypes/Profile'; | ||
| 8 | import { Search } from '@customTypes/Search'; | ||
| 9 | import { API } from '@api/Api'; | ||
| 10 | import "@css/Sidebar.css"; | ||
| 11 | |||
| 12 | interface SidebarProps { | ||
| 13 | setToken: React.Dispatch<React.SetStateAction<string | undefined>>; | ||
| 14 | profile?: UserProfile; | ||
| 15 | setProfile: React.Dispatch<React.SetStateAction<UserProfile | undefined>>; | ||
| 16 | onUploadRun: () => void; | ||
| 17 | }; | ||
| 18 | |||
| 19 | const Sidebar: React.FC<SidebarProps> = ({ setToken, profile, setProfile, onUploadRun }) => { | ||
| 20 | |||
| 21 | const btnRef = useRef(null); | ||
| 22 | const [searchData, setSearchData] = React.useState<Search | undefined>(undefined); | ||
| 23 | const [isSidebarLocked, setIsSidebarLocked] = React.useState<boolean>(false); | ||
| 24 | const [isSidebarOpen, setSidebarOpen] = React.useState<boolean>(true); | ||
| 25 | |||
| 26 | const location = useLocation(); | ||
| 27 | const path = location.pathname; | ||
| 28 | |||
| 29 | const handle_sidebar_click = (clicked_sidebar_idx: number) => { | ||
| 30 | const btn = document.querySelectorAll("#sidebarBtn"); | ||
| 31 | if (isSidebarOpen) { setSidebarOpen(false); _handle_sidebar_hide() } | ||
| 32 | // clusterfuck | ||
| 33 | btn.forEach((e, i) => { | ||
| 34 | btn[i].classList.remove("sidebar-button-selected") | ||
| 35 | btn[i].classList.add("sidebar-button-deselected") | ||
| 36 | }) | ||
| 37 | btn[clicked_sidebar_idx].classList.add("sidebar-button-selected") | ||
| 38 | btn[clicked_sidebar_idx].classList.remove("sidebar-button-deselected") | ||
| 39 | }; | ||
| 40 | |||
| 41 | const _handle_sidebar_hide = () => { | ||
| 42 | var btn = document.querySelectorAll("button.sidebar-button") as NodeListOf<HTMLElement> | ||
| 43 | const span = document.querySelectorAll("button.sidebar-button>span") as NodeListOf<HTMLElement> | ||
| 44 | const side = document.querySelector("#sidebar-list") as HTMLElement; | ||
| 45 | const searchbar = document.querySelector("#searchbar") as HTMLInputElement; | ||
| 46 | const uploadRunBtn = document.querySelector("#upload-run") as HTMLInputElement; | ||
| 47 | const uploadRunSpan = document.querySelector("#upload-run>span") as HTMLInputElement; | ||
| 48 | |||
| 49 | if (isSidebarOpen) { | ||
| 50 | if (profile) { | ||
| 51 | const login = document.querySelectorAll(".login>button")[1] as HTMLElement; | ||
| 52 | login.style.opacity = "1" | ||
| 53 | uploadRunBtn.style.width = "310px" | ||
| 54 | uploadRunBtn.style.padding = "0.4em 0 0 11px" | ||
| 55 | uploadRunSpan.style.opacity = "0" | ||
| 56 | setTimeout(() => { | ||
| 57 | uploadRunSpan.style.opacity = "1" | ||
| 58 | }, 100) | ||
| 59 | } | ||
| 60 | setSidebarOpen(false); | ||
| 61 | side.style.width = "320px" | ||
| 62 | btn.forEach((e, i) => { | ||
| 63 | e.style.width = "310px" | ||
| 64 | e.style.padding = "0.4em 0 0 11px" | ||
| 65 | setTimeout(() => { | ||
| 66 | span[i].style.opacity = "1" | ||
| 67 | }, 100) | ||
| 68 | }); | ||
| 69 | side.style.zIndex = "2" | ||
| 70 | } else { | ||
| 71 | if (profile) { | ||
| 72 | const login = document.querySelectorAll(".login>button")[1] as HTMLElement; | ||
| 73 | login.style.opacity = "0" | ||
| 74 | uploadRunBtn.style.width = "40px" | ||
| 75 | uploadRunBtn.style.padding = "0.4em 0 0 5px" | ||
| 76 | uploadRunSpan.style.opacity = "0" | ||
| 77 | } | ||
| 78 | setSidebarOpen(true); | ||
| 79 | side.style.width = "40px"; | ||
| 80 | searchbar.focus(); | ||
| 81 | btn.forEach((e, i) => { | ||
| 82 | e.style.width = "40px" | ||
| 83 | e.style.padding = "0.4em 0 0 5px" | ||
| 84 | span[i].style.opacity = "0" | ||
| 85 | }) | ||
| 86 | setTimeout(() => { | ||
| 87 | side.style.zIndex = "0" | ||
| 88 | }, 300); | ||
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | const _handle_sidebar_lock = () => { | ||
| 93 | if (!isSidebarLocked) { | ||
| 94 | _handle_sidebar_hide() | ||
| 95 | setIsSidebarLocked(true); | ||
| 96 | setTimeout(() => setIsSidebarLocked(false), 300); | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | |||
| 100 | const _handle_search_change = async (q: string) => { | ||
| 101 | const searchResponse = await API.get_search(q); | ||
| 102 | setSearchData(searchResponse); | ||
| 103 | }; | ||
| 104 | |||
| 105 | React.useEffect(() => { | ||
| 106 | if (path === "/") { handle_sidebar_click(1) } | ||
| 107 | else if (path.includes("games")) { handle_sidebar_click(2) } | ||
| 108 | else if (path.includes("rankings")) { handle_sidebar_click(3) } | ||
| 109 | // else if (path.includes("news")) { handle_sidebar_click(4) } | ||
| 110 | // else if (path.includes("scorelog")) { handle_sidebar_click(5) } | ||
| 111 | else if (path.includes("profile")) { handle_sidebar_click(4) } | ||
| 112 | else if (path.includes("rules")) { handle_sidebar_click(5) } | ||
| 113 | else if (path.includes("about")) { handle_sidebar_click(6) } | ||
| 114 | }, [path]); | ||
| 115 | |||
| 116 | React.useEffect(() => { | ||
| 117 | const btns = document.querySelectorAll("#sidebarBtn"); | ||
| 118 | btns.forEach((e, num) => { | ||
| 119 | e.setAttribute("num", num.toString()); | ||
| 120 | }); | ||
| 121 | }) | ||
| 122 | |||
| 123 | return ( | ||
| 124 | <div id='sidebar'> | ||
| 125 | <Link to="/" tabIndex={-1}> | ||
| 126 | <div id='logo'> {/* logo */} | ||
| 127 | <img src={LogoIcon} alt="" height={"80px"} /> | ||
| 128 | <div id='logo-text'> | ||
| 129 | <span><b>PORTAL 2</b></span><br /> | ||
| 130 | <span>Least Portals Hub</span> | ||
| 131 | </div> | ||
| 132 | </div> | ||
| 133 | </Link> | ||
| 134 | <div id='sidebar-list'> {/* List */} | ||
| 135 | <div id='sidebar-toplist'> {/* Top */} | ||
| 136 | |||
| 137 | <button id="sidebarBtn" className='sidebar-button' onClick={() => _handle_sidebar_lock()}><img src={SearchIcon} alt="" /><span>Search</span></button> | ||
| 138 | |||
| 139 | <span></span> | ||
| 140 | |||
| 141 | <Link to="/" tabIndex={-1}> | ||
| 142 | <button ref={btnRef} id="sidebarBtn" className={`${btn.sidebar}`}><img src={HomeIcon} alt="homepage" /><span>Home Page</span></button> | ||
| 143 | </Link> | ||
| 144 | |||
| 145 | <Link to="/games" tabIndex={-1}> | ||
| 146 | <button id="sidebarBtn" className='sidebar-button'><img src={PortalIcon} alt="games" /><span>Games</span></button> | ||
| 147 | </Link> | ||
| 148 | |||
| 149 | <Link to="/rankings" tabIndex={-1}> | ||
| 150 | <button id="sidebarBtn" className='sidebar-button'><img src={FlagIcon} alt="rankings" /><span>Rankings</span></button> | ||
| 151 | </Link> | ||
| 152 | |||
| 153 | {/* <Link to="/news" tabIndex={-1}> | ||
| 154 | <button className='sidebar-button'><img src={NewsIcon} alt="news" /><span>News</span></button> | ||
| 155 | </Link> */} | ||
| 156 | |||
| 157 | {/* <Link to="/scorelog" tabIndex={-1}> | ||
| 158 | <button className='sidebar-button'><img src={TableIcon} alt="scorelogs" /><span>Score Logs</span></button> | ||
| 159 | </Link> */} | ||
| 160 | </div> | ||
| 161 | <div id='sidebar-bottomlist'> | ||
| 162 | <span></span> | ||
| 163 | |||
| 164 | { | ||
| 165 | profile && profile.profile ? | ||
| 166 | <button id='upload-run' className='submit-run-button' onClick={() => onUploadRun()}><img src={UploadIcon} alt="upload" /><span>Upload Record</span></button> | ||
| 167 | : | ||
| 168 | <span></span> | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | <Link to="/rules" tabIndex={-1}> | ||
| 173 | <button id="sidebarBtn" className='sidebar-button'><img src={BookIcon} alt="rules" /><span>Leaderboard Rules</span></button> | ||
| 174 | </Link> | ||
| 175 | |||
| 176 | <Link to="/about" tabIndex={-1}> | ||
| 177 | <button id="sidebarBtn" className='sidebar-button'><img src={HelpIcon} alt="about" /><span>About LPHUB</span></button> | ||
| 178 | </Link> | ||
| 179 | </div> | ||
| 180 | </div> | ||
| 181 | <div> | ||
| 182 | <input type="text" id='searchbar' placeholder='Search for map or a player...' onChange={(e) => _handle_search_change(e.target.value)} /> | ||
| 183 | |||
| 184 | <div id='search-data'> | ||
| 185 | |||
| 186 | {searchData?.maps.map((q, index) => ( | ||
| 187 | <Link to={`/maps/${q.id}`} className='search-map' key={index}> | ||
| 188 | <span>{q.game}</span> | ||
| 189 | <span>{q.chapter}</span> | ||
| 190 | <span>{q.map}</span> | ||
| 191 | </Link> | ||
| 192 | ))} | ||
| 193 | {searchData?.players.map((q, index) => | ||
| 194 | ( | ||
| 195 | <Link to={ | ||
| 196 | profile && q.steam_id === profile.steam_id ? `/profile` : | ||
| 197 | `/users/${q.steam_id}` | ||
| 198 | } className='search-player' key={index}> | ||
| 199 | <img src={q.avatar_link} alt='pfp'></img> | ||
| 200 | <span style={{ fontSize: `${36 - q.user_name.length * 0.8}px` }}>{q.user_name}</span> | ||
| 201 | </Link> | ||
| 202 | ))} | ||
| 203 | |||
| 204 | </div> | ||
| 205 | </div> | ||
| 206 | </div> | ||
| 207 | ); | ||
| 208 | }; | ||
| 209 | |||
| 210 | export default Sidebar; | ||