@ploykit/ui 0.5.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AppProviders.js +2 -2
- package/dist/components/ConfirmDialog.js +1 -1
- package/dist/components/CronInput.js +3 -3
- package/dist/components/DataTable.js +4 -4
- package/dist/components/FormField.js +1 -1
- package/dist/components/ImpersonationBanner.js +1 -1
- package/dist/components/NotificationBell.js +1 -1
- package/dist/components/OnboardingChecklist.js +3 -3
- package/dist/components/Page.js +2 -2
- package/dist/components/ReauthDialog.js +4 -4
- package/dist/components/SecretModal.js +2 -2
- package/dist/components/SettingsCard.js +2 -2
- package/dist/components/SiteBanner.js +1 -1
- package/dist/components/SiteBannerLayer.js +1 -1
- package/dist/components/StatusBadge.js +1 -1
- package/dist/components/UserMenu.js +3 -3
- package/dist/components/WorkspaceSwitcher.js +7 -7
- package/dist/components/ui/alert-dialog.js +2 -2
- package/dist/components/ui/avatar.js +1 -1
- package/dist/components/ui/badge.js +1 -1
- package/dist/components/ui/breadcrumb.js +1 -1
- package/dist/components/ui/button.js +1 -1
- package/dist/components/ui/card.js +1 -1
- package/dist/components/ui/checkbox.js +1 -1
- package/dist/components/ui/dialog.js +2 -2
- package/dist/components/ui/dropdown-menu.js +1 -1
- package/dist/components/ui/empty.js +1 -1
- package/dist/components/ui/field.js +3 -3
- package/dist/components/ui/input.js +1 -1
- package/dist/components/ui/label.js +1 -1
- package/dist/components/ui/popover.js +1 -1
- package/dist/components/ui/progress.js +1 -1
- package/dist/components/ui/select.js +1 -1
- package/dist/components/ui/separator.js +1 -1
- package/dist/components/ui/sheet.js +2 -2
- package/dist/components/ui/sidebar.js +8 -8
- package/dist/components/ui/skeleton.js +1 -1
- package/dist/components/ui/switch.js +1 -1
- package/dist/components/ui/tabs.js +1 -1
- package/dist/components/ui/textarea.js +1 -1
- package/dist/components/ui/tooltip.js +1 -1
- package/dist/index.js +78 -78
- package/dist/layouts/AppShell.js +4 -4
- package/dist/layouts/SettingsShell.js +4 -4
- package/dist/pages/LoginPage.js +5 -5
- package/dist/pages/account/NotificationPreferencesPage.js +3 -3
- package/dist/pages/account/ProfileSettingsPage.js +9 -9
- package/dist/pages/account/SecuritySettingsPage.js +13 -13
- package/dist/pages/account/TokensPage.js +12 -12
- package/dist/pages/admin/AdminSettingsPage.js +11 -11
- package/dist/pages/admin/AdminShell.js +3 -3
- package/dist/pages/admin/AuditPage.js +8 -8
- package/dist/pages/admin/OrdersPage.js +12 -12
- package/dist/pages/admin/OverviewPage.js +13 -13
- package/dist/pages/admin/SSOPage.js +6 -6
- package/dist/pages/admin/UserDetailPage.js +13 -13
- package/dist/pages/admin/UsersPage.js +13 -13
- package/dist/pages/admin/WebhookDeliveriesPage.js +9 -9
- package/dist/pages/admin/WorkspaceDetailPage.js +14 -14
- package/dist/pages/admin/WorkspacesPage.js +9 -9
- package/dist/pages/admin/shared.js +1 -1
- package/dist/pages/auth/ForgotPasswordPage.js +4 -4
- package/dist/pages/auth/InviteAcceptPage.js +6 -6
- package/dist/pages/auth/RegisterPage.js +7 -7
- package/dist/pages/auth/ResetPasswordPage.js +5 -5
- package/dist/pages/auth/VerifyEmailPage.js +3 -3
- package/dist/pages/workspace/BillingPage.js +11 -11
- package/dist/pages/workspace/BillingSuccessPage.js +2 -2
- package/dist/pages/workspace/MembersPage.js +15 -15
- package/dist/pages/workspace/RolePermissionsPage.js +9 -9
- package/dist/pages/workspace/SchedulesPage.js +12 -12
- package/dist/pages/workspace/UsagePage.js +6 -6
- package/dist/pages/workspace/WebhooksPage.js +16 -16
- package/dist/pages/workspace/WorkspaceAuditPage.js +7 -7
- package/dist/pages/workspace/WorkspaceGeneralPage.js +9 -9
- package/package.json +6 -5
- package/src/AppProviders.tsx +29 -0
- package/src/components/ConfirmDialog.tsx +77 -0
- package/src/components/CronInput.tsx +195 -0
- package/src/components/DataTable.tsx +152 -0
- package/src/components/ErrorBoundary.tsx +46 -0
- package/src/components/FormField.tsx +56 -0
- package/src/components/ImpersonationBanner.tsx +23 -0
- package/src/components/NotificationBell.tsx +135 -0
- package/src/components/OnboardingChecklist.tsx +114 -0
- package/src/components/Page.tsx +67 -0
- package/src/components/ReauthDialog.tsx +94 -0
- package/src/components/SecretModal.tsx +147 -0
- package/src/components/SettingsCard.tsx +39 -0
- package/src/components/SiteBanner.tsx +32 -0
- package/src/components/SiteBannerLayer.tsx +29 -0
- package/src/components/StatusBadge.tsx +12 -0
- package/src/components/UserMenu.tsx +103 -0
- package/src/components/WorkspaceSwitcher.tsx +159 -0
- package/src/components/__tests__/AppProviders.test.tsx +111 -0
- package/src/components/__tests__/CronInput.test.tsx +180 -0
- package/src/components/__tests__/DataTable.test.tsx +208 -0
- package/src/components/__tests__/ErrorBoundary.test.tsx +61 -0
- package/src/components/__tests__/ImpersonationBanner.test.tsx +59 -0
- package/src/components/__tests__/NotificationBell.test.tsx +207 -0
- package/src/components/__tests__/OnboardingChecklist.test.tsx +97 -0
- package/src/components/__tests__/SecretModal.test.tsx +112 -0
- package/src/components/__tests__/SiteBannerLayer.test.tsx +44 -0
- package/src/components/__tests__/UserMenu.test.tsx +98 -0
- package/src/components/__tests__/WorkspaceSwitcher.test.tsx +162 -0
- package/src/components/__tests__/form.test.tsx +133 -0
- package/src/components/__tests__/semantic-tokens.test.ts +41 -0
- package/src/components/__tests__/smoke.test.tsx +11 -0
- package/src/components/__tests__/toast.test.tsx +27 -0
- package/src/components/toast.tsx +23 -0
- package/src/components/ui/__tests__/alert-dialog.test.tsx +66 -0
- package/src/components/ui/__tests__/avatar.test.tsx +30 -0
- package/src/components/ui/__tests__/checkbox.test.tsx +18 -0
- package/src/components/ui/__tests__/dropdown-menu.test.tsx +33 -0
- package/src/components/ui/__tests__/field.test.tsx +43 -0
- package/src/components/ui/__tests__/overlay-size.test.tsx +84 -0
- package/src/components/ui/__tests__/popover.test.tsx +32 -0
- package/src/components/ui/__tests__/progress.test.tsx +24 -0
- package/src/components/ui/__tests__/select-callback.test.tsx +72 -0
- package/src/components/ui/__tests__/select.test.tsx +48 -0
- package/src/components/ui/__tests__/sidebar.test.tsx +121 -0
- package/src/components/ui/__tests__/stock-mirror.test.ts +39 -0
- package/src/components/ui/__tests__/subpath.test.tsx +38 -0
- package/src/components/ui/__tests__/switch.test.tsx +24 -0
- package/src/components/ui/__tests__/tabs.test.tsx +24 -0
- package/src/components/ui/__tests__/textarea.test.tsx +41 -0
- package/src/components/ui/alert-dialog.tsx +187 -0
- package/src/components/ui/avatar.tsx +108 -0
- package/src/components/ui/badge.tsx +51 -0
- package/src/components/ui/breadcrumb.tsx +124 -0
- package/src/components/ui/button.tsx +57 -0
- package/src/components/ui/card.tsx +102 -0
- package/src/components/ui/checkbox.tsx +28 -0
- package/src/components/ui/dialog.tsx +166 -0
- package/src/components/ui/dropdown-menu.tsx +265 -0
- package/src/components/ui/empty.tsx +103 -0
- package/src/components/ui/field.tsx +238 -0
- package/src/components/ui/input.tsx +19 -0
- package/src/components/ui/label.tsx +17 -0
- package/src/components/ui/popover.tsx +89 -0
- package/src/components/ui/progress.tsx +82 -0
- package/src/components/ui/select.tsx +235 -0
- package/src/components/ui/separator.tsx +24 -0
- package/src/components/ui/sheet.tsx +146 -0
- package/src/components/ui/sidebar.tsx +723 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/tabs.tsx +79 -0
- package/src/components/ui/textarea.tsx +26 -0
- package/src/components/ui/tooltip.tsx +63 -0
- package/src/dev.css +1 -0
- package/src/hooks/use-mobile.ts +19 -0
- package/src/index.ts +150 -0
- package/src/layouts/AppShell.tsx +163 -0
- package/src/layouts/AuthShell.tsx +24 -0
- package/src/layouts/SettingsShell.tsx +129 -0
- package/src/layouts/__tests__/AppShell.test.tsx +163 -0
- package/src/layouts/__tests__/SettingsShell.test.tsx +146 -0
- package/src/lib/__tests__/api-error.test.ts +163 -0
- package/src/lib/__tests__/utils.test.ts +21 -0
- package/src/lib/api-error.ts +98 -0
- package/src/lib/utils.ts +109 -0
- package/src/pages/LandingPage.tsx +88 -0
- package/src/pages/LoginPage.tsx +291 -0
- package/src/pages/__tests__/LandingPage.test.tsx +58 -0
- package/src/pages/__tests__/LoginPage.test.tsx +229 -0
- package/src/pages/__tests__/RegisterPage.test.tsx +138 -0
- package/src/pages/__tests__/ssr-smoke.test.tsx +99 -0
- package/src/pages/account/NotificationPreferencesPage.tsx +138 -0
- package/src/pages/account/ProfileSettingsPage.tsx +189 -0
- package/src/pages/account/SecuritySettingsPage.tsx +214 -0
- package/src/pages/account/TokensPage.tsx +211 -0
- package/src/pages/account/__tests__/NotificationPreferencesPage.test.tsx +150 -0
- package/src/pages/account/__tests__/ProfileSettingsPage.test.tsx +173 -0
- package/src/pages/account/__tests__/SecuritySettingsPage.test.tsx +212 -0
- package/src/pages/account/__tests__/TokensPage.test.tsx +180 -0
- package/src/pages/admin/AdminSettingsPage.tsx +564 -0
- package/src/pages/admin/AdminShell.tsx +199 -0
- package/src/pages/admin/AuditPage.tsx +338 -0
- package/src/pages/admin/OrdersPage.tsx +642 -0
- package/src/pages/admin/OverviewPage.tsx +559 -0
- package/src/pages/admin/SSOPage.tsx +166 -0
- package/src/pages/admin/UserDetailPage.tsx +641 -0
- package/src/pages/admin/UsersPage.tsx +462 -0
- package/src/pages/admin/WebhookDeliveriesPage.tsx +318 -0
- package/src/pages/admin/WorkspaceDetailPage.tsx +551 -0
- package/src/pages/admin/WorkspacesPage.tsx +299 -0
- package/src/pages/admin/__tests__/AdminSettingsPage.test.tsx +260 -0
- package/src/pages/admin/__tests__/OrdersPageMarkPaid.test.tsx +207 -0
- package/src/pages/admin/__tests__/OverviewPageActivity.test.tsx +213 -0
- package/src/pages/admin/__tests__/SSOPage.test.tsx +128 -0
- package/src/pages/admin/__tests__/UsersPageActions.test.tsx +188 -0
- package/src/pages/admin/__tests__/WorkspaceDetailPageTransfer.test.tsx +170 -0
- package/src/pages/admin/__tests__/WorkspacesPagePlans.test.tsx +138 -0
- package/src/pages/admin/shared.ts +42 -0
- package/src/pages/auth/ForgotPasswordPage.tsx +83 -0
- package/src/pages/auth/InviteAcceptPage.tsx +105 -0
- package/src/pages/auth/RegisterPage.tsx +204 -0
- package/src/pages/auth/ResetPasswordPage.tsx +150 -0
- package/src/pages/auth/VerifyEmailPage.tsx +76 -0
- package/src/pages/auth/__tests__/ForgotPasswordPage.test.tsx +67 -0
- package/src/pages/auth/__tests__/InviteAcceptPage.test.tsx +86 -0
- package/src/pages/auth/__tests__/ResetPasswordPage.test.tsx +115 -0
- package/src/pages/auth/__tests__/VerifyEmailPage.test.tsx +74 -0
- package/src/pages/workspace/BillingPage.tsx +361 -0
- package/src/pages/workspace/BillingSuccessPage.tsx +115 -0
- package/src/pages/workspace/MembersPage.tsx +568 -0
- package/src/pages/workspace/RolePermissionsPage.tsx +202 -0
- package/src/pages/workspace/SchedulesPage.tsx +387 -0
- package/src/pages/workspace/UsagePage.tsx +207 -0
- package/src/pages/workspace/WebhooksPage.tsx +506 -0
- package/src/pages/workspace/WorkspaceAuditPage.tsx +229 -0
- package/src/pages/workspace/WorkspaceGeneralPage.tsx +184 -0
- package/src/pages/workspace/__tests__/BillingPage.test.tsx +302 -0
- package/src/pages/workspace/__tests__/BillingSuccessPage.test.tsx +134 -0
- package/src/pages/workspace/__tests__/MembersPage.test.tsx +347 -0
- package/src/pages/workspace/__tests__/RolePermissionsPage.test.tsx +138 -0
- package/src/pages/workspace/__tests__/SchedulesPage.test.tsx +239 -0
- package/src/pages/workspace/__tests__/UsagePage.test.tsx +223 -0
- package/src/pages/workspace/__tests__/WebhooksPage.test.tsx +309 -0
- package/src/pages/workspace/__tests__/WorkspaceAuditPage.test.tsx +112 -0
- package/src/pages/workspace/__tests__/WorkspaceGeneralPage.test.tsx +164 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { Link, useSearchParams } from 'react-router';
|
|
4
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
5
|
+
import type { components } from '@ploykit/client';
|
|
6
|
+
import { DataTable, type Column } from '../../components/DataTable';
|
|
7
|
+
import { PageEmpty, PageError, PageHeader } from '../../components/Page';
|
|
8
|
+
import { toast } from '../../components/toast';
|
|
9
|
+
import { Badge } from '../../components/ui/badge';
|
|
10
|
+
import { Button } from '../../components/ui/button';
|
|
11
|
+
import { Input } from '../../components/ui/input';
|
|
12
|
+
import {
|
|
13
|
+
Select,
|
|
14
|
+
SelectContent,
|
|
15
|
+
SelectItem,
|
|
16
|
+
SelectTrigger,
|
|
17
|
+
SelectValue,
|
|
18
|
+
} from '../../components/ui/select';
|
|
19
|
+
import { useApi } from '@ploykit/hooks';
|
|
20
|
+
import { formatDate } from '../../lib/utils';
|
|
21
|
+
import {
|
|
22
|
+
ADMIN_PAGE_SIZE,
|
|
23
|
+
adminError,
|
|
24
|
+
adminListQuery,
|
|
25
|
+
type AdminListEnvelope,
|
|
26
|
+
} from './shared';
|
|
27
|
+
|
|
28
|
+
type AdminWorkspace = components['schemas']['AdminWorkspace'];
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
interface AdminPlanView {
|
|
32
|
+
code: string;
|
|
33
|
+
name: string;
|
|
34
|
+
currency: string;
|
|
35
|
+
trial_days: number;
|
|
36
|
+
sort_no: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
const FALLBACK_PLAN_CODES = ['free'];
|
|
41
|
+
|
|
42
|
+
export function WorkspacesPage() {
|
|
43
|
+
const api = useApi();
|
|
44
|
+
const qc = useQueryClient();
|
|
45
|
+
|
|
46
|
+
const [searchParams, setSearchParams] = useSearchParams();
|
|
47
|
+
const q = searchParams.get('q') ?? '';
|
|
48
|
+
const page = Math.max(
|
|
49
|
+
1,
|
|
50
|
+
Number.parseInt(searchParams.get('page') ?? '1', 10) || 1,
|
|
51
|
+
);
|
|
52
|
+
const applyFilter = (nextQ: string, nextPage: number) => {
|
|
53
|
+
const sp = new URLSearchParams(searchParams);
|
|
54
|
+
if (nextQ) sp.set('q', nextQ);
|
|
55
|
+
else sp.delete('q');
|
|
56
|
+
if (nextPage > 1) sp.set('page', String(nextPage));
|
|
57
|
+
else sp.delete('page');
|
|
58
|
+
setSearchParams(sp, { replace: true });
|
|
59
|
+
};
|
|
60
|
+
const setPage = (p: number) => applyFilter(q, p);
|
|
61
|
+
|
|
62
|
+
const [searchInput, setSearchInput] = useState(q);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const t = setTimeout(() => {
|
|
65
|
+
const next = searchInput.trim();
|
|
66
|
+
if (next !== q) applyFilter(next, 1);
|
|
67
|
+
}, 300);
|
|
68
|
+
return () => clearTimeout(t);
|
|
69
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
}, [searchInput]);
|
|
71
|
+
|
|
72
|
+
const wsQ = useQuery({
|
|
73
|
+
queryKey: ['admin', 'workspaces', page, q],
|
|
74
|
+
queryFn: () =>
|
|
75
|
+
api.get<AdminListEnvelope<AdminWorkspace>>(
|
|
76
|
+
`/api/admin/workspaces${adminListQuery(page, ADMIN_PAGE_SIZE, { q })}`,
|
|
77
|
+
),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
const plansQ = useQuery({
|
|
85
|
+
queryKey: ['admin', 'plans'],
|
|
86
|
+
queryFn: () =>
|
|
87
|
+
api.get<{ items: string[]; plans?: AdminPlanView[] }>('/api/admin/plans'),
|
|
88
|
+
});
|
|
89
|
+
const planCodes = plansQ.data?.items?.length
|
|
90
|
+
? plansQ.data.items
|
|
91
|
+
: FALLBACK_PLAN_CODES;
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
const planName = new Map(
|
|
95
|
+
(plansQ.data?.plans ?? [])
|
|
96
|
+
.filter((p) => p.name)
|
|
97
|
+
.map((p) => [p.code, p.name] as const),
|
|
98
|
+
);
|
|
99
|
+
const planTrial = new Map(
|
|
100
|
+
(plansQ.data?.plans ?? [])
|
|
101
|
+
.filter((p) => p.trial_days > 0)
|
|
102
|
+
.map((p) => [p.code, p.trial_days] as const),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
const planLabel = (code: string) => {
|
|
107
|
+
const name = planName.get(code) ?? code;
|
|
108
|
+
return planTrial.has(code)
|
|
109
|
+
? `${name}(新订阅享 ${planTrial.get(code)} 天试用)`
|
|
110
|
+
: name;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const planM = useMutation({
|
|
114
|
+
mutationFn: (v: { ws: AdminWorkspace; planCode: string }) =>
|
|
115
|
+
api.patch(`/api/admin/workspaces/${v.ws.id}/plan`, {
|
|
116
|
+
plan_code: v.planCode,
|
|
117
|
+
}),
|
|
118
|
+
onSuccess: (_d, v) => {
|
|
119
|
+
toast.success(
|
|
120
|
+
`${v.ws.name} 套餐已变更为 ${planLabel(v.planCode)}`,
|
|
121
|
+
);
|
|
122
|
+
void qc.invalidateQueries({ queryKey: ['admin', 'workspaces'] });
|
|
123
|
+
|
|
124
|
+
void qc.invalidateQueries({ queryKey: ['admin', 'workspace'] });
|
|
125
|
+
void qc.invalidateQueries({ queryKey: ['admin', 'stats'] });
|
|
126
|
+
},
|
|
127
|
+
onError: (e) => toast.error(adminError(e, '套餐变更失败')),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const raw = wsQ.data?.items ?? [];
|
|
131
|
+
const total = wsQ.data?.total ?? 0;
|
|
132
|
+
|
|
133
|
+
const pastEnd = !wsQ.isPending && raw.length === 0 && page > 1;
|
|
134
|
+
|
|
135
|
+
const columns: Column<AdminWorkspace>[] = [
|
|
136
|
+
{
|
|
137
|
+
key: 'name',
|
|
138
|
+
header: '工作区',
|
|
139
|
+
render: (ws) => (
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
<div className="min-w-0 max-w-[24rem]">
|
|
143
|
+
<p className="truncate font-medium">
|
|
144
|
+
{/* 工作区名 = 详情入口(/admin/workspaces/:id:成员/配额/删除操作面) */}
|
|
145
|
+
<Link
|
|
146
|
+
to={`/admin/workspaces/${ws.id}`}
|
|
147
|
+
className="transition-colors hover:text-primary hover:underline"
|
|
148
|
+
>
|
|
149
|
+
{ws.name}
|
|
150
|
+
</Link>
|
|
151
|
+
</p>
|
|
152
|
+
<p className="truncate font-mono text-xs text-muted-foreground">
|
|
153
|
+
{ws.slug}
|
|
154
|
+
</p>
|
|
155
|
+
</div>
|
|
156
|
+
),
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
key: 'owner_email',
|
|
160
|
+
header: '属主',
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
className: 'w-48',
|
|
164
|
+
render: (ws) => (
|
|
165
|
+
<span className="truncate text-muted-foreground" title={ws.owner_email ?? undefined}>
|
|
166
|
+
{ws.owner_email ?? '—'}
|
|
167
|
+
</span>
|
|
168
|
+
),
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
key: 'plan_code',
|
|
172
|
+
header: '套餐',
|
|
173
|
+
className: 'w-44',
|
|
174
|
+
render: (ws) => {
|
|
175
|
+
const mutating = planM.isPending && planM.variables?.ws.id === ws.id;
|
|
176
|
+
const known = planCodes.includes(ws.plan_code);
|
|
177
|
+
return (
|
|
178
|
+
<div className="flex items-center gap-2">
|
|
179
|
+
<Select
|
|
180
|
+
value={ws.plan_code}
|
|
181
|
+
onValueChange={(v) => {
|
|
182
|
+
if (typeof v === 'string' && v !== ws.plan_code)
|
|
183
|
+
planM.mutate({ ws, planCode: v });
|
|
184
|
+
}}
|
|
185
|
+
>
|
|
186
|
+
<SelectTrigger
|
|
187
|
+
size="sm"
|
|
188
|
+
className="max-md:h-10"
|
|
189
|
+
disabled={mutating}
|
|
190
|
+
aria-label={`变更 ${ws.name} 套餐`}
|
|
191
|
+
>
|
|
192
|
+
<SelectValue>{planLabel(ws.plan_code)}</SelectValue>
|
|
193
|
+
</SelectTrigger>
|
|
194
|
+
<SelectContent align="start">
|
|
195
|
+
{planCodes.map((code) => (
|
|
196
|
+
<SelectItem key={code} value={code}>
|
|
197
|
+
{planLabel(code)}
|
|
198
|
+
</SelectItem>
|
|
199
|
+
))}
|
|
200
|
+
</SelectContent>
|
|
201
|
+
</Select>
|
|
202
|
+
{/* 非目录码:原值仍可见(Select 显示原 plan_code),打标提示 */}
|
|
203
|
+
{!known && (
|
|
204
|
+
<Badge variant="outline" className="text-muted-foreground">
|
|
205
|
+
未知套餐
|
|
206
|
+
</Badge>
|
|
207
|
+
)}
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
key: 'member_count',
|
|
214
|
+
header: '成员数',
|
|
215
|
+
className: 'w-20',
|
|
216
|
+
render: (ws) => <span className="tabular-nums">{ws.member_count}</span>,
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
key: 'created_at',
|
|
220
|
+
header: '创建时间',
|
|
221
|
+
className: 'w-28',
|
|
222
|
+
render: (ws) => (
|
|
223
|
+
<span className="whitespace-nowrap text-muted-foreground">
|
|
224
|
+
{formatDate(ws.created_at)}
|
|
225
|
+
</span>
|
|
226
|
+
),
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
return (
|
|
231
|
+
<div>
|
|
232
|
+
<PageHeader
|
|
233
|
+
title="工作区"
|
|
234
|
+
description="平台全部工作区;支持名称/slug 搜索,行内可直接变更套餐(审计留痕)"
|
|
235
|
+
/>
|
|
236
|
+
|
|
237
|
+
{/* B-workspaces-4:跨页找工作区不再只能逐页手翻(服务端 ilike 检索)。 */}
|
|
238
|
+
<div className="mb-4 flex flex-wrap items-center gap-2">
|
|
239
|
+
<Input
|
|
240
|
+
className="w-72"
|
|
241
|
+
placeholder="按名称 / slug 前缀搜索…"
|
|
242
|
+
aria-label="搜索工作区"
|
|
243
|
+
value={searchInput}
|
|
244
|
+
onChange={(e) => setSearchInput(e.target.value)}
|
|
245
|
+
onKeyDown={(e) => {
|
|
246
|
+
if (e.key === 'Enter') applyFilter(searchInput.trim(), 1);
|
|
247
|
+
}}
|
|
248
|
+
/>
|
|
249
|
+
{q && (
|
|
250
|
+
<Button
|
|
251
|
+
variant="ghost"
|
|
252
|
+
size="sm"
|
|
253
|
+
onClick={() => {
|
|
254
|
+
setSearchInput('');
|
|
255
|
+
applyFilter('', 1);
|
|
256
|
+
}}
|
|
257
|
+
>
|
|
258
|
+
清除搜索
|
|
259
|
+
</Button>
|
|
260
|
+
)}
|
|
261
|
+
</div>
|
|
262
|
+
|
|
263
|
+
{wsQ.isError ? (
|
|
264
|
+
<PageError
|
|
265
|
+
message={adminError(wsQ.error, '工作区列表加载失败')}
|
|
266
|
+
onRetry={() => void wsQ.refetch()}
|
|
267
|
+
/>
|
|
268
|
+
) : (
|
|
269
|
+
<DataTable
|
|
270
|
+
columns={columns}
|
|
271
|
+
rows={raw}
|
|
272
|
+
rowKey={(ws) => ws.id}
|
|
273
|
+
loading={wsQ.isPending}
|
|
274
|
+
pagination={{
|
|
275
|
+
page,
|
|
276
|
+
pageSize: ADMIN_PAGE_SIZE,
|
|
277
|
+
total,
|
|
278
|
+
onPageChange: setPage,
|
|
279
|
+
}}
|
|
280
|
+
empty={
|
|
281
|
+
|
|
282
|
+
pastEnd ? (
|
|
283
|
+
<div className="flex flex-col items-center gap-2">
|
|
284
|
+
<PageEmpty label="没有更多工作区了" hint="当前页数据可能已被删除" />
|
|
285
|
+
<Button variant="outline" size="sm" onClick={() => setPage(1)}>
|
|
286
|
+
返回第一页
|
|
287
|
+
</Button>
|
|
288
|
+
</div>
|
|
289
|
+
) : q ? (
|
|
290
|
+
<PageEmpty label="没有匹配的工作区" hint="换个关键词或清除搜索" />
|
|
291
|
+
) : (
|
|
292
|
+
<PageEmpty label="暂无工作区" />
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
/>
|
|
296
|
+
)}
|
|
297
|
+
</div>
|
|
298
|
+
);
|
|
299
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
|
|
2
|
+
import '@testing-library/jest-dom/vitest';
|
|
3
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
4
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
5
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
6
|
+
import { AdminSettingsPage } from '../AdminSettingsPage';
|
|
7
|
+
|
|
8
|
+
const mockedGet = vi.hoisted(() => vi.fn());
|
|
9
|
+
const mockedPut = vi.hoisted(() => vi.fn());
|
|
10
|
+
const mockedPost = vi.hoisted(() => vi.fn());
|
|
11
|
+
const toastMocks = vi.hoisted(() => ({
|
|
12
|
+
success: vi.fn(),
|
|
13
|
+
error: vi.fn(),
|
|
14
|
+
info: vi.fn(),
|
|
15
|
+
}));
|
|
16
|
+
vi.mock('@ploykit/client', () => ({
|
|
17
|
+
api: {
|
|
18
|
+
get: mockedGet,
|
|
19
|
+
put: mockedPut,
|
|
20
|
+
post: mockedPost,
|
|
21
|
+
patch: vi.fn(),
|
|
22
|
+
delete: vi.fn(),
|
|
23
|
+
},
|
|
24
|
+
ApiError: class ApiError extends Error {
|
|
25
|
+
status: number;
|
|
26
|
+
code: string;
|
|
27
|
+
constructor(status: number, code: string, message: string) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.status = status;
|
|
30
|
+
this.code = code;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
vi.mock('../../../components/toast', () => ({ toast: toastMocks }));
|
|
35
|
+
|
|
36
|
+
const ENVELOPE = {
|
|
37
|
+
items: { banner_text: '今晚例行维护', banner_kind: 'warn' },
|
|
38
|
+
effective: {
|
|
39
|
+
banner_text: '今晚例行维护',
|
|
40
|
+
banner_kind: 'warn',
|
|
41
|
+
maintenance_mode: '0',
|
|
42
|
+
allow_signup: '1',
|
|
43
|
+
allowed_domains: '',
|
|
44
|
+
},
|
|
45
|
+
defaults: {
|
|
46
|
+
banner_text: '',
|
|
47
|
+
banner_kind: 'info',
|
|
48
|
+
maintenance_mode: '0',
|
|
49
|
+
allow_signup: '1',
|
|
50
|
+
allowed_domains: '',
|
|
51
|
+
},
|
|
52
|
+
descriptions: {
|
|
53
|
+
banner_text: '站点公告文本(空 = 无公告)',
|
|
54
|
+
banner_kind: '公告样式',
|
|
55
|
+
maintenance_mode: '维护模式说明',
|
|
56
|
+
allow_signup: '注册开关说明',
|
|
57
|
+
allowed_domains: '域名白名单说明',
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function renderPage() {
|
|
62
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
63
|
+
return render(
|
|
64
|
+
<QueryClientProvider client={qc}>
|
|
65
|
+
<AdminSettingsPage />
|
|
66
|
+
</QueryClientProvider>,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
mockedGet.mockReset().mockResolvedValue(ENVELOPE);
|
|
72
|
+
mockedPut.mockReset().mockResolvedValue({ key: '', value: '' });
|
|
73
|
+
mockedPost.mockReset().mockResolvedValue({ mode: 'smtp', status: 'sent' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('AdminSettingsPage', () => {
|
|
77
|
+
it('渲染四卡并按 effective 回填表单(公告预览同步)', async () => {
|
|
78
|
+
renderPage();
|
|
79
|
+
|
|
80
|
+
expect(await screen.findByText('顶部公告横条')).toBeInTheDocument();
|
|
81
|
+
expect(screen.getByText('全站"维护中"提示条')).toBeInTheDocument();
|
|
82
|
+
expect(screen.getByText('注册开关与域名白名单')).toBeInTheDocument();
|
|
83
|
+
expect(screen.getByText('发送测试邮件')).toBeInTheDocument();
|
|
84
|
+
|
|
85
|
+
expect(screen.getByLabelText('公告文本')).toHaveValue('今晚例行维护');
|
|
86
|
+
expect(screen.getByRole('status')).toHaveTextContent('今晚例行维护');
|
|
87
|
+
expect(screen.getByText('展示中')).toBeInTheDocument();
|
|
88
|
+
expect(screen.getByText('开放注册')).toBeInTheDocument();
|
|
89
|
+
|
|
90
|
+
expect(mockedGet).toHaveBeenCalledWith('/api/admin/settings');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('修改公告并保存 → PUT banner_text + banner_kind 两键', async () => {
|
|
94
|
+
renderPage();
|
|
95
|
+
await screen.findByText('顶部公告横条');
|
|
96
|
+
fireEvent.change(screen.getByLabelText('公告文本'), {
|
|
97
|
+
target: { value: '新公告:本周日停机' },
|
|
98
|
+
});
|
|
99
|
+
fireEvent.click(screen.getByRole('button', { name: '保存公告' }));
|
|
100
|
+
|
|
101
|
+
await waitFor(() => expect(mockedPut).toHaveBeenCalledTimes(2));
|
|
102
|
+
expect(mockedPut).toHaveBeenCalledWith('/api/admin/settings/banner_text', {
|
|
103
|
+
value: '新公告:本周日停机',
|
|
104
|
+
});
|
|
105
|
+
expect(mockedPut).toHaveBeenCalledWith('/api/admin/settings/banner_kind', {
|
|
106
|
+
value: 'warn',
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(screen.getByRole('status')).toHaveTextContent('新公告:本周日停机');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('保存域名白名单 → PUT allowed_domains', async () => {
|
|
113
|
+
renderPage();
|
|
114
|
+
await screen.findByText('注册开关与域名白名单');
|
|
115
|
+
fireEvent.change(screen.getByLabelText(/域名白名单/), {
|
|
116
|
+
target: { value: 'corp.com, edu.cn' },
|
|
117
|
+
});
|
|
118
|
+
fireEvent.click(screen.getByRole('button', { name: '保存白名单' }));
|
|
119
|
+
|
|
120
|
+
await waitFor(() => expect(mockedPut).toHaveBeenCalledTimes(1));
|
|
121
|
+
expect(mockedPut).toHaveBeenCalledWith(
|
|
122
|
+
'/api/admin/settings/allowed_domains',
|
|
123
|
+
{
|
|
124
|
+
value: 'corp.com, edu.cn',
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('发送测试邮件 → POST test-email;dev 通道提示看日志', async () => {
|
|
130
|
+
mockedPost.mockResolvedValueOnce({
|
|
131
|
+
mode: 'dev',
|
|
132
|
+
hint: '当前为开发邮件通道(emaildev):邮件内容已打日志,请到后端日志查看,真实邮箱不会收到',
|
|
133
|
+
});
|
|
134
|
+
renderPage();
|
|
135
|
+
await screen.findByText('发送测试邮件');
|
|
136
|
+
fireEvent.change(screen.getByLabelText('测试邮件收件邮箱'), {
|
|
137
|
+
target: { value: 'ops@example.com' },
|
|
138
|
+
});
|
|
139
|
+
fireEvent.click(screen.getByRole('button', { name: '发送' }));
|
|
140
|
+
|
|
141
|
+
await waitFor(() => expect(mockedPost).toHaveBeenCalledTimes(1));
|
|
142
|
+
expect(mockedPost).toHaveBeenCalledWith('/api/admin/settings/test-email', {
|
|
143
|
+
to: 'ops@example.com',
|
|
144
|
+
});
|
|
145
|
+
expect(await screen.findByText(/后端日志查看/)).toBeInTheDocument();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('测试邮件失败 → 展示错误结果,不静默', async () => {
|
|
149
|
+
mockedPost.mockRejectedValueOnce(new Error('smtp connect refused'));
|
|
150
|
+
renderPage();
|
|
151
|
+
await screen.findByText('发送测试邮件');
|
|
152
|
+
fireEvent.change(screen.getByLabelText('测试邮件收件邮箱'), {
|
|
153
|
+
target: { value: 'ops@example.com' },
|
|
154
|
+
});
|
|
155
|
+
fireEvent.click(screen.getByRole('button', { name: '发送' }));
|
|
156
|
+
|
|
157
|
+
expect(await screen.findByText(/发送失败/)).toBeInTheDocument();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
it('限流卡:按 effective 回填三键', async () => {
|
|
162
|
+
mockedGet.mockResolvedValueOnce({
|
|
163
|
+
...ENVELOPE,
|
|
164
|
+
effective: {
|
|
165
|
+
...ENVELOPE.effective,
|
|
166
|
+
rate_limit_per_user_per_min: '600',
|
|
167
|
+
rate_limit_per_ip_per_min: '60',
|
|
168
|
+
rate_limit_allowlist: '10.0.0.5, 192.168.0.0/16',
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
renderPage();
|
|
172
|
+
expect(
|
|
173
|
+
await screen.findByText('每用户 / 每 IP 限流与豁免名单'),
|
|
174
|
+
).toBeInTheDocument();
|
|
175
|
+
expect(screen.getByLabelText('每用户请求/分(0 = 关闭)')).toHaveValue('600');
|
|
176
|
+
expect(
|
|
177
|
+
screen.getByLabelText('每 IP 请求/分(认证路径;0 = 关闭)'),
|
|
178
|
+
).toHaveValue('60');
|
|
179
|
+
expect(screen.getByLabelText(/豁免名单/)).toHaveValue(
|
|
180
|
+
'10.0.0.5, 192.168.0.0/16',
|
|
181
|
+
);
|
|
182
|
+
expect(screen.getByText('启用中')).toBeInTheDocument();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('保存限流配置 → PUT 三键(user/ip/allowlist)', async () => {
|
|
186
|
+
renderPage();
|
|
187
|
+
await screen.findByText('每用户 / 每 IP 限流与豁免名单');
|
|
188
|
+
|
|
189
|
+
fireEvent.change(screen.getByLabelText('每用户请求/分(0 = 关闭)'), {
|
|
190
|
+
target: { value: '600' },
|
|
191
|
+
});
|
|
192
|
+
fireEvent.change(
|
|
193
|
+
screen.getByLabelText('每 IP 请求/分(认证路径;0 = 关闭)'),
|
|
194
|
+
{ target: { value: '30' } },
|
|
195
|
+
);
|
|
196
|
+
fireEvent.change(screen.getByLabelText(/豁免名单/), {
|
|
197
|
+
target: { value: '10.0.0.5' },
|
|
198
|
+
});
|
|
199
|
+
fireEvent.click(screen.getByRole('button', { name: '保存限流配置' }));
|
|
200
|
+
|
|
201
|
+
await waitFor(() => expect(mockedPut).toHaveBeenCalledTimes(3));
|
|
202
|
+
expect(mockedPut).toHaveBeenCalledWith(
|
|
203
|
+
'/api/admin/settings/rate_limit_per_user_per_min',
|
|
204
|
+
{ value: '600' },
|
|
205
|
+
);
|
|
206
|
+
expect(mockedPut).toHaveBeenCalledWith(
|
|
207
|
+
'/api/admin/settings/rate_limit_per_ip_per_min',
|
|
208
|
+
{ value: '30' },
|
|
209
|
+
);
|
|
210
|
+
expect(mockedPut).toHaveBeenCalledWith(
|
|
211
|
+
'/api/admin/settings/rate_limit_allowlist',
|
|
212
|
+
{ value: '10.0.0.5' },
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('B-settings-10:限流空值/非法值 → 拒绝保存并就地提示(不再静默按 0 落库)', async () => {
|
|
217
|
+
renderPage();
|
|
218
|
+
await screen.findByText('每用户 / 每 IP 限流与豁免名单');
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
fireEvent.change(screen.getByLabelText('每用户请求/分(0 = 关闭)'), {
|
|
222
|
+
target: { value: '' },
|
|
223
|
+
});
|
|
224
|
+
fireEvent.click(screen.getByRole('button', { name: '保存限流配置' }));
|
|
225
|
+
|
|
226
|
+
expect(
|
|
227
|
+
await screen.findByText('每用户限流:不能留空:填写 0 表示关闭限流'),
|
|
228
|
+
).toBeInTheDocument();
|
|
229
|
+
expect(mockedPut).not.toHaveBeenCalled();
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
fireEvent.change(screen.getByLabelText('每用户请求/分(0 = 关闭)'), {
|
|
233
|
+
target: { value: 'abc' },
|
|
234
|
+
});
|
|
235
|
+
fireEvent.click(screen.getByRole('button', { name: '保存限流配置' }));
|
|
236
|
+
expect(
|
|
237
|
+
await screen.findByText(/每用户限流:必须是 0\.\.100000 的整数/),
|
|
238
|
+
).toBeInTheDocument();
|
|
239
|
+
expect(mockedPut).not.toHaveBeenCalled();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('B-settings-1:限流三键部分失败 → 如实汇报部分写入(成功键已生效)', async () => {
|
|
243
|
+
mockedPut.mockImplementation(async (url: string) => {
|
|
244
|
+
if (url.endsWith('rate_limit_per_ip_per_min')) {
|
|
245
|
+
throw new Error('必须是 0..100000 的整数');
|
|
246
|
+
}
|
|
247
|
+
return {};
|
|
248
|
+
});
|
|
249
|
+
renderPage();
|
|
250
|
+
await screen.findByText('每用户 / 每 IP 限流与豁免名单');
|
|
251
|
+
|
|
252
|
+
fireEvent.click(screen.getByRole('button', { name: '保存限流配置' }));
|
|
253
|
+
await waitFor(() => expect(mockedPut).toHaveBeenCalledTimes(3));
|
|
254
|
+
await waitFor(() => expect(toastMocks.error).toHaveBeenCalled());
|
|
255
|
+
const msg = toastMocks.error.mock.calls[0][0] as string;
|
|
256
|
+
expect(msg).toContain('部分保存');
|
|
257
|
+
expect(msg).toContain('成功 2/3');
|
|
258
|
+
expect(msg).toContain('每 IP 限流');
|
|
259
|
+
});
|
|
260
|
+
});
|