aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/SubscriberTable/SubscriberTable.tsx
blob: c308aee5eb0e62b37773a73037d17aade27a7255 (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
"use client";
import React, { useState } from "react";
import ChannelRow from "./SubscriberTableRow";

interface ChannelDataProp {
    channel_name: string;
    profile_pic: string;
    subscribers: number;
    sub_org: string;
    video_count: number;
    day_diff: number;
    views: number;
    diff_1d: number;
    diff_7d: number;
    diff_30d: number;
}

interface SubscriberDataTableProp {
    channel_data: ChannelDataProp[];
    timestamp: string;
}

type SortKey = keyof ChannelDataProp | "rank";

const DataTable = ({ channel_data, timestamp }: SubscriberDataTableProp) => {
    const [sortKey, setSortKey] = useState<SortKey>("subscribers");
    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");
        }
        if (key === "sub_org") {
            setIndexName("INDEX");
        } else {
            setIndexName("RANK");
        }
    };

    const sortedData = [...channel_data].sort((a, b) => {
        let aValue: any, bValue: any;
        if (sortKey === "rank") {
            aValue = channel_data.indexOf(a) + 1;
            bValue = channel_data.indexOf(b) + 1;
        } else {
            aValue = a[sortKey as keyof ChannelDataProp];
            bValue = b[sortKey as keyof ChannelDataProp];
        }
        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">
                    Subscriber Count
                </h1>
                <p className="text-gray-500 text-sm">
                    Updated Hourly. Retrieved at: {timestamp}
                </p>
            </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
                                scope="col"
                                className="py-1 px-1 sm:px-3 hidden sm:table-cell"
                            >
                                {indexName}
                            </th>
                            <th
                                scope="col"
                                className="py-1 px-1 sm:px-3"
                            >
                                CHANNEL
                            </th>
                            <th
                                scope="col"
                                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
                                scope="col"
                                className="py-1 px-1 sm:px-3 hidden sm:table-cell cursor-pointer"
                                onClick={() => handleSort("video_count")}
                            >
                                VIDEO COUNT
                                {sortKey === "video_count" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                scope="col"
                                className="py-1 px-1 sm:px-3 hidden sm:table-cell cursor-pointer"
                                onClick={() => handleSort("views")}
                            >
                                VIEW COUNT
                                {sortKey === "views" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                scope="col"
                                className="py-1 px-1 sm:px-3 cursor-pointer"
                                onClick={() => handleSort("subscribers")}
                            >
                                SUBSCRIBERS
                                {sortKey === "subscribers" && (
                                    <span className="ml-1">{sortOrder === "asc" ? "▲" : "▼"}</span>
                                )}
                            </th>
                            <th
                                scope="col"
                                className="py-1 px-1 sm:px-3"
                                onClick={() => handleSort("diff_1d")}
                            >
                                DIFF (24H)
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {sortedData.map((channel, index) => (
                            <ChannelRow
                                key={index}
                                channel={channel}
                                index={index}
                            />
                        ))}
                    </tbody>
                </table>
            </div>
        </>
    );
};

export default DataTable;
export type { SubscriberDataTableProp };
export type { ChannelDataProp };
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage