1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
"use client";
import type React from "react";
import "../TitleBar/TitleBarStyle.css";
import {
faHouse,
faBars,
faTimes,
faChevronDown,
faChevronUp,
faSpinner,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useState, useEffect } from "react";
import Link from "next/link";
interface TitleBarProps {
title: string;
redirectUrl?: string;
showHomeButton?: boolean;
backgroundColor?: string;
}
const TitleBar: React.FC<TitleBarProps> = ({
title,
redirectUrl,
showHomeButton,
backgroundColor,
}) => {
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [isMounted, setIsMounted] = useState(false);
const [groupingData, setPhaseData] = useState<{
[key: string]: string[];
} | null>(null);
const [collapsedSections, setCollapsedSections] = useState<{
[key: string]: boolean;
}>({});
const [loadingMember, setLoadingMember] = useState<string | null>(null);
useEffect(() => {
setIsMounted(true);
const fetchPhaseData = async () => {
const apiUrl = process.env.NEXT_PUBLIC_API_URL_TESTING;
try {
const response = await fetch(apiUrl + "/groups.json", {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache'
}
});
const data = await response.json();
setPhaseData(data);
const initialCollapsedState = Object.keys(data).reduce(
(acc, phase) => {
acc[phase] = true;
return acc;
},
{} as { [key: string]: boolean },
);
setCollapsedSections(initialCollapsedState);
} catch (error) {
console.error("Error fetching phase data:", error);
}
};
fetchPhaseData();
}, []);
const toggleSidebar = () => {
setIsSidebarOpen(!isSidebarOpen);
};
const toggleSection = (phase: string) => {
setCollapsedSections((prevState) => ({
...prevState,
[phase]: !prevState[phase],
}));
};
const handleMemberClick = (member: string) => {
setLoadingMember(member);
};
if (!isMounted) {
return null;
}
return (
<>
<div
className="title-bar p-5 shadow-md"
style={{ backgroundColor: backgroundColor || "#2D4B71" }}
>
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}
>
<button
onClick={toggleSidebar}
className="text-white text-3xl mr-4 focus:outline-none hover:cursor-pointer"
>
<FontAwesomeIcon icon={isSidebarOpen ? faTimes : faBars} />
</button>
<a href={redirectUrl}>
<span
className="text-white text-4xl font-bold"
style={{ fontFamily: "Quantico, sans-serif" }}
>
{title}
</span>
</a>
{showHomeButton && (
<Link href="/" className="ml-2 text-white text-3xl">
<FontAwesomeIcon icon={faHouse} />
</Link>
)}
</div>
</div>
{/* Sidebar */}
<div
className={`fixed top-0 left-0 h-screen bg-black text-white shadow-lg transition-transform transform ${
isSidebarOpen ? "translate-x-0" : "-translate-x-full"
} duration-500 ease-in-out z-50`}
style={{
width: "16rem",
fontFamily: "Quantico, sans-serif",
overflowY: "auto",
}}
>
<div className="p-4 text-3xl font-bold border-b border-gray-700">
PhaseTracker
</div>
<ul className="text-xl border-b border-gray-700">
<Link href="/">
<li className="p-4 hover:bg-gray-700 transition-colors duration-300">
Home
</li>
</Link>
<Link href="/twitch">
<li className="p-4 hover:bg-gray-700 transition-colors duration-300">
Twitch Table
</li>
</Link>
<Link href="/about">
<li className="p-4 hover:bg-gray-700 transition-colors duration-300">
About
</li>
</Link>
<Link href="https://github.com/pinapelz/Phase-Tracker-Data">
<li className="p-4 hover:bg-gray-700 transition-colors duration-300">
Data
</li>
</Link>
</ul>
<ul className="mt-4 text-xl">
{groupingData ? (
Object.entries(groupingData).map(([group, members]) => (
<li key={group} className="p-4">
<div
className="flex justify-between items-center cursor-pointer select-none"
onClick={() => toggleSection(group)}
>
<span className="font-bold text-lg">{group}</span>
<FontAwesomeIcon
icon={
collapsedSections[group] ? faChevronDown : faChevronUp
}
/>
</div>
{!collapsedSections[group] && (
<ul className="ml-4 mt-2">
{members
.map((member) => (
<a href={`/stats/${member}`} key={member}>
<li
className="p-1 hover:bg-gray-700 transition-colors duration-300 flex items-center"
onClick={() => handleMemberClick(member)}
>
{member}
{loadingMember === member && (
<FontAwesomeIcon
icon={faSpinner}
spin
className="ml-2"
/>
)}
</li>
</a>
))}
</ul>
)}
</li>
))
) : (
<li className="p-4">Loading...</li>
)}
</ul>
</div>
{isSidebarOpen && (
<div
className="fixed top-0 left-0 w-full h-full bg-black opacity-50 transition-opacity duration-500"
style={{ zIndex: 40 }}
onClick={toggleSidebar}
></div>
)}
</>
);
};
export default TitleBar;
|