aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/SubscriberTable/TwitchDataTable.tsx
blob: 2294be30accd76f16a588de87b6efdff5f98fa3a (plain) (blame)
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
"use client";
import React, { useState } from "react";
import ChannelRow from "./TwitchTableRow";

interface TwitchChannelDataProp {
    channel_name: string;
    profile_pic: string;
    subscribers: number;
    sub_org: string;
    twitch_followers: number;
    total_sum: number;
    max_following?: number;
}


interface TwitchDataTableProp {
    channel_data: TwitchChannelDataProp[];
    timestamp: string;
}

type SortKey = keyof TwitchChannelDataProp | "rank" | "max_following";

const TwitchDataTable = ({ channel_data, timestamp }: TwitchDataTableProp) => {
    const [sortKey, setSortKey] = useState<SortKey>("twitch_followers");
    const [sortOrder, setSortOrder] = useState<"asc" | "desc">("desc");
    const [indexName, setIndexName] = useState<string>("RANK");

    const handleSort = (key: SortKey) => {
        if (sortKey === key) {
            setSortOrder(sortOrder === "asc" ? "desc" : "asc");
        } else {
            setSortKey(key);
            setSortOrder("desc");
        }
        setIndexName(key === "sub_org" ? "INDEX" : "RANK");
    };

    const dataWithMax = channel_data.map((channel) => ({
        ...channel,
        max_following: Math.max(
            channel.subscribers || 0,
            channel.twitch_followers || 0
        ),
    }));

    const sortedData = [...dataWithMax].sort((a, b) => {
        let aValue: any, bValue: any;
        if (sortKey === "rank") {
            aValue = dataWithMax.indexOf(a) + 1;
            bValue = dataWithMax.indexOf(b) + 1;
        } else {
            aValue = a[sortKey];
            bValue = b[sortKey];
        }
        if (typeof aValue === "string") {
            return sortOrder === "asc"
                ? aValue.localeCompare(bValue)
                : bValue.localeCompare(aValue);
        }
        return sortOrder === "asc" ? aValue - bValue : bValue - aValue;
    });

    return (
        <>
            <div className="sm:hidden text-center text-red-600 font-semibold my-2">
                Limited data shown on mobile view!
            </div>
            <div className="text-center sm:mt-5" style={{ fontFamily: "Quantico, sans-serif" }}>
                <h1 className="text-2xl font-bold text-gray-800">The Twitch Table</h1>
                <p className="text-gray-500 text-sm">Updated Hourly. Retrieved at: {timestamp}</p>
            </div>

            <div className="flex justify-center mt-2 mb-4">
              <div className="bg-gray-100 rounded-lg p-4 shadow-sm text-sm md:text-base max-w-2xl">
                {/* Legend wrapper - stacked sections on all screen sizes */}
                <div className="flex flex-col gap-3">

                  {/* Column explanations */}
                  <div>
                    <h3 className="font-bold text-gray-800 mb-1">Column Explanations</h3>
                    <div>
                      <div className="flex items-start mb-1">
                        <span className="font-medium text-gray-700 mr-2 whitespace-nowrap">SUM(YT+TTV):</span>
                        <span className="text-gray-600">Total combined followers across both platforms</span>
                      </div>
                      <div className="flex items-start">
                        <span className="font-medium text-gray-700 mr-2 whitespace-nowrap">MAX(YT,TTV):</span>
                        <span className="text-gray-600">Highest follower count between platforms</span>
                      </div>
                    </div>
                  </div>

                  {/* Horizontal divider */}
                  <div className="border-t border-gray-300 w-full"></div>

                  {/* Color key */}
                  <div>
                    <h3 className="font-bold text-gray-800 mb-1">MAX Column Color Key</h3>
                    <div>
                      <div className="flex items-center mb-1">
                        <div className="h-3 w-3 bg-red-600 rounded-full mr-2 flex-shrink-0"></div>
                        <span className="text-gray-600">YouTube subscriber count is higher</span>
                      </div>
                      <div className="flex items-center">
                        <div className="h-3 w-3 bg-purple-600 rounded-full mr-2 flex-shrink-0"></div>
                        <span className="text-gray-600">Twitch follower count is higher</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div className="px-2 sm:px-48 py-4 sm:py-8 relative rounded-l text-left overflow-auto">
                <table className="w-full text-m sm:text-xl text-black bg-white">
                    <thead
                        className="text-m sm:text-lg text-white rounded-md select-none"
                        style={{ backgroundColor: "black" }}
                    >
                        <tr>
                            <th className="py-1 px-1 sm:px-3 hidden sm:table-cell">{indexName}</th>
                            <th className="py-1 px-1 sm:px-3">CHANNEL</th>
                            <th
                                className="py-1 px-1 sm:px-3 hidden sm:table-cell cursor-pointer"
                                onClick={() => handleSort("sub_org")}
                            >
                                GROUP
                                {sortKey === "sub_org" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                className="py-1 px-1 sm:px-3 cursor-pointer"
                                onClick={() => handleSort("subscribers")}
                            >
                                YOUTUBE SUBS
                                {sortKey === "subscribers" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                className="py-1 px-1 sm:px-3 cursor-pointer"
                                onClick={() => handleSort("twitch_followers")}
                            >
                                TWITCH FOLLOWS
                                {sortKey === "twitch_followers" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                className="py-1 px-1 sm:px-3 cursor-pointer hidden sm:table-cell"
                                onClick={() => handleSort("total_sum")}
                            >
                              SUM(YT+TTV)
                                {sortKey === "total_sum" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                className="py-1 px-1 sm:px-3 cursor-pointer"
                                onClick={() => handleSort("max_following")}
                            >
                                MAX(YT, TTV)
                                {sortKey === "max_following" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {sortedData.map((channel, index) => (
                            <ChannelRow key={index} channel={channel} index={index} />
                        ))}
                    </tbody>
                </table>
            </div>
        </>
    );
};

export default TwitchDataTable;
export type { TwitchDataTableProp, TwitchChannelDataProp };
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage