File size: 5,761 Bytes
ea775bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useQuery } from "@tanstack/react-query";
import {
  AlertTriangle,
  Database,
  LayoutDashboard,
  LogOut,
  Logs,
  Network,
  Regex,
  Rss,
  Server,
  Settings,
} from "lucide-react";
import { NavLink, Outlet, useNavigate } from "react-router-dom";
import { motion } from "framer-motion";
import { Button } from "./ui/Button";
import { cn } from "../lib/cn";
import { apiRequest } from "../lib/api-client";
import { useAuthStore } from "../features/auth/auth-store";
import { getEnvConfig } from "../features/systemConfig/api";
import { useI18n } from "../i18n";
import { LanguageSwitcher } from "./LanguageSwitcher";

type NavItem = {
  label: string;
  path: string;
  icon: typeof LayoutDashboard;
};

const navItems: NavItem[] = [
  { label: "总览看板", path: "/dashboard", icon: LayoutDashboard },
  { label: "平台管理", path: "/platforms", icon: Server },
  { label: "订阅管理", path: "/subscriptions", icon: Rss },
  { label: "节点池", path: "/nodes", icon: Network },
  { label: "请求头规则", path: "/rules", icon: Regex },
  { label: "请求日志", path: "/request-logs", icon: Logs },
  { label: "资源", path: "/resources", icon: Database },
  { label: "系统配置", path: "/system-config", icon: Settings },
];

type SystemInfoResponse = {
  version: string;
};

export function AppShell() {
  const { t } = useI18n();
  const clearToken = useAuthStore((state) => state.clearToken);
  const token = useAuthStore((state) => state.token);
  const navigate = useNavigate();
  const envConfigQuery = useQuery({
    queryKey: ["system-config-env", "shell"],
    queryFn: getEnvConfig,
    staleTime: 30_000,
  });
  const systemInfoQuery = useQuery({
    queryKey: ["system-info", "shell"],
    queryFn: () => apiRequest<SystemInfoResponse>("/api/v1/system/info"),
    staleTime: 300_000,
  });
  const logoSrc = `${import.meta.env.BASE_URL}vite.svg`;
  const version = systemInfoQuery.data?.version?.trim();

  const envConfig = envConfigQuery.data;
  const authWarnings: string[] = [];
  if (envConfig && !envConfig.admin_token_set) {
    authWarnings.push(t("RESIN_ADMIN_TOKEN 为空,控制面 API 免认证"));
  }
  if (envConfig && !envConfig.proxy_token_set) {
    authWarnings.push(t("RESIN_PROXY_TOKEN 为空,正/反向代理免认证"));
  }
  if (envConfig && envConfig.admin_token_set && envConfig.admin_token_weak) {
    authWarnings.push(t("RESIN_ADMIN_TOKEN 强度较弱,建议更换为更高熵随机令牌"));
  }
  if (envConfig && envConfig.proxy_token_set && envConfig.proxy_token_weak) {
    authWarnings.push(t("RESIN_PROXY_TOKEN 强度较弱,建议更换为更高熵随机令牌"));
  }
  const showAuthWarning = authWarnings.length > 0;

  const logout = () => {
    clearToken();
    navigate("/login", { replace: true });
  };

  return (
    <div className="app-layout">
      <aside className="sidebar">
        <div className="brand">
          <div className="brand-logo" aria-hidden="true">
            <img src={logoSrc} alt="Resin Logo" style={{ width: 20, height: 20 }} />
          </div>
          <div className="brand-copy">
            <div className="brand-title-row">
              <p className="brand-title">Resin</p>
              {version ? (
                <span className="brand-version" title={version} aria-label={`Version ${version}`}>
                  {version}
                </span>
              ) : null}
            </div>
            <p className="brand-subtitle">{t("高性能粘性代理池 · 管理面板")}</p>
          </div>
        </div>

        <div className="sidebar-main">
          <nav className="nav-list" aria-label={t("主导航")}>
            {navItems.map((item) => {
              const Icon = item.icon;
              return (
                <NavLink
                  key={item.path}
                  to={item.path}
                  className={({ isActive }) => cn("nav-item", isActive && "nav-item-active")}
                >
                  <Icon size={16} />
                  <span>{t(item.label)}</span>
                </NavLink>
              );
            })}
          </nav>
        </div>

        <div className="sidebar-bottom">
          {showAuthWarning ? (
            <div className="callout callout-warning sidebar-warning" role="alert">
              <AlertTriangle size={16} />
              <div className="sidebar-warning-copy">
                <strong>{t("安全警告")}</strong>
                <div className="sidebar-warning-list">
                  {authWarnings.map((warning) => (
                    <span key={warning}>{warning}</span>
                  ))}
                </div>
              </div>
            </div>
          ) : null}

          {!token ? <p className="sidebar-hint">{t("当前为免认证访问模式")}</p> : null}

          <div className="sidebar-tools">
            {token ? (
              <Button
                variant="secondary"
                size="sm"
                className="sidebar-icon-btn"
                onClick={logout}
                aria-label={t("退出登录")}
                title={t("退出登录")}
              >
                <LogOut size={16} />
              </Button>
            ) : (
              <span className="sidebar-tool-spacer" aria-hidden="true" />
            )}
            <LanguageSwitcher className="sidebar-locale" compact />
          </div>
        </div>
      </aside>

      <main className="main">
        <motion.div
          key="content"
          initial={{ opacity: 0, y: 8 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.24, ease: "easeOut" }}
          className="content"
        >
          <Outlet />
        </motion.div>
      </main>
    </div>
  );
}