aboutsummaryrefslogtreecommitdiff
path: root/pages/admin/index.js
blob: 6cb9a768e993e6d99b7e92d32dc8b77a52c52466 (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
import AdminDashboard from "@/components/admin/dashboard";
import AdminLayout from "@/components/admin/layout";
import AppendMeta from "@/components/admin/meta/AppendMeta";
import {
  countKeysWithPrefix,
  countNumericKeys,
  getValuesWithPrefix,
} from "@/utils/getRedisWithPrefix";
import { getServerSession } from "next-auth";
import { authOptions } from "pages/api/auth/[...nextauth]";
import React, { useState } from "react";

export async function getServerSideProps(context) {
  const sessions = await getServerSession(
    context.req,
    context.res,
    authOptions
  );

  if (!sessions) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  }

  const admin = sessions?.user?.name === process.env.ADMIN_USERNAME;

  let api;
  api = process.env.API_URI || null;
  if (api && api.endsWith("/")) {
    api = api.slice(0, -1);
  }

  if (!admin) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  }

  const [anime, info, meta, report] = await Promise.all([
    countNumericKeys(),
    countKeysWithPrefix("anime:"),
    countKeysWithPrefix("meta:"),
    getValuesWithPrefix("report:"),
  ]);

  return {
    props: {
      session: sessions,
      animeCount: anime || 0,
      infoCount: info || 0,
      metaCount: meta || 0,
      report: report || [],
      api,
    },
  };
}

export default function Admin({
  animeCount,
  infoCount,
  metaCount,
  report,
  api,
}) {
  const [page, setPage] = useState(1);

  return (
    <AdminLayout page={page} setPage={setPage}>
      <div className="h-full">
        {page == 1 && (
          <AdminDashboard
            animeCount={animeCount}
            infoCount={infoCount}
            metaCount={metaCount}
            report={report}
          />
        )}
        {page == 2 && <AppendMeta api={api} />}
        {page == 3 && <p className="flex-center h-full">Coming Soon!</p>}
        {page == 4 && <p className="flex-center h-full">Coming Soon!</p>}
      </div>
    </AdminLayout>
  );
}