Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions assets/src/settings/procaptcha/procaptchaSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,31 @@ import {
} from "#settings/procaptcha/procaptchaAccount.js";
import { z, type ZodType } from "zod";

/**
* Only the parts of the site response the statistics page actually renders:
* the monthly captcha counts and the account tier the traffic chart gates on.
*
* `settings` is deliberately absent. Zod strips keys a schema does not
* declare, so whatever shape the portal sends for the captcha settings — and
* `frictionlessThreshold` has already changed shape once — is ignored rather
* than validated. The whole response is a single parse, so a field the page
* does not display has no business being able to fail it.
*/
export interface ProcaptchaSite {
account: ProcaptchaAccount;
name: string;
settings: SiteSettings;
monthlyUsage: {
limit: number;
image: CaptchaUsage;
pow: CaptchaUsage;
};
}

export interface SiteSettings {
frictionlessThreshold: number;
powDifficulty: number;
captchaType: string;
domains: string[];
}

export interface CaptchaUsage {
submissions: number;
verifications: number;
total: number;
}

export const siteSettingsSchema = z.object({
frictionlessThreshold: z.number(),
powDifficulty: z.number(),
captchaType: z.string(),
domains: z.string().array(),
}) satisfies ZodType<SiteSettings>;

export const captchaUsageSchema = z.object({
submissions: z.number(),
verifications: z.number(),
Expand All @@ -43,8 +37,6 @@ export const captchaUsageSchema = z.object({

export const procaptchaSiteSchema = z.object({
account: procaptchaAccountSchema,
name: z.string(),
settings: siteSettingsSchema,
monthlyUsage: z.object({
limit: z.number(),
image: captchaUsageSchema,
Expand Down
167 changes: 4 additions & 163 deletions assets/src/settings/statistics/components/appComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
AppStatusComponentProperties,
StatCurrentState,
} from "./appStatusComponent.js";
import { ListComponent, ListComponentProperties } from "./listComponent.js";
import {
TrafficAnalyticsComponent,
TrafficAnalyticsComponentProperties,
Expand All @@ -21,10 +20,7 @@ import {
import type Logger from "#utils/logger/logger.js";
import { type Config, ConfigClass } from "#settings/statistics/config.js";
import CaptchaUsageNumberUtils from "#settings/statistics/captchaUsage/captchaUsageNumberUtils.js";
import type {
ProcaptchaSite,
SiteSettings,
} from "#settings/procaptcha/procaptchaSite.js";
import type { ProcaptchaSite } from "#settings/procaptcha/procaptchaSite.js";

import type { ProcaptchaAccount } from "#settings/procaptcha/procaptchaAccount.js";
import {
Expand All @@ -39,9 +35,6 @@ interface AppComponentProperties {
interface AppState {
statState: AppStatusComponentProperties;
usageInfo: CaptchaUsageComponentProperties;
accountInformation: ListComponentProperties;
captchaSettings: ListComponentProperties;
domains: ListComponentProperties;
trafficData: TrafficAnalyticsComponentProperties;
}

Expand Down Expand Up @@ -95,45 +88,6 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
},
labels: this.config.getUsageLabels(),
},
accountInformation: {
title: this.config.getAccountLabels().title,
icon: "icon-[material-symbols--account-circle]",
items: [
{
label: this.config.getAccountLabels().tier,
value: "...",
},
{
label: this.config.getAccountLabels().name,
value: "...",
},
],
},
captchaSettings: {
title: this.config.getCaptchaSettingsLabels().title,
icon: "icon-[material-symbols--settings]",
items: [
{
label: this.config.getCaptchaSettingsLabels().type,
value: "...",
},
{
label: this.config.getCaptchaSettingsLabels()
.frictionlessThreshold,
value: "...",
},
{
label: this.config.getCaptchaSettingsLabels()
.powDifficulty,
value: "...",
},
],
},
domains: {
title: this.config.getDomainLabels().title,
icon: "icon-[material-symbols--domain]",
items: [],
},
trafficData: {
accountTier: "",
logger: this.logger,
Expand Down Expand Up @@ -164,22 +118,9 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
}));
}

protected refreshUserData(site: ProcaptchaSite): void {
protected refreshUsage(site: ProcaptchaSite): void {
this.setState((actualState) => ({
...actualState,
accountInformation: {
...actualState.accountInformation,
items: [
{
label: this.config.getAccountLabels().tier,
value: site.account.tier.toUpperCase(),
},
{
label: this.config.getAccountLabels().name,
value: site.name,
},
],
},
usageInfo: {
...actualState.usageInfo,
limits: {
Expand All @@ -199,83 +140,6 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
}));
}

protected getPowDifficultyLabel(powDifficulty: number): string {
const levelLabels = this.config.getCaptchaSettingsLabels().level;

if (powDifficulty === 4) {
return levelLabels.normal;
}

return powDifficulty < 4 ? levelLabels.low : levelLabels.high;
}

protected getFrictionlessThresholdLabel(
frictionlessThreshold: number,
): string {
const levelLabels = this.config.getCaptchaSettingsLabels().level;

if (frictionlessThreshold >= 0.4 && frictionlessThreshold <= 0.6) {
return levelLabels.normal;
}

return frictionlessThreshold < 0.4 ? levelLabels.high : levelLabels.low;
}

protected getTypeLabel(type: string): string {
const typeLabels = this.config.getCaptchaSettingsLabels().types;

switch (type) {
case "image":
return typeLabels.image;
case "pow":
return typeLabels.proofOfWork;
default:
return typeLabels.frictionless;
}
}

protected refreshSiteSettings(siteSettings: SiteSettings): void {
const domains = siteSettings.domains
.filter((domain) => "*" !== domain)
.map((domain, index) => {
return {
label: "#" + (index + 1),
value: domain,
};
});

this.setState((actualState) => ({
...actualState,
captchaSettings: {
...actualState.captchaSettings,
items: [
{
label: this.config.getCaptchaSettingsLabels().type,
value: this.getTypeLabel(siteSettings.captchaType),
},
{
label: this.config.getCaptchaSettingsLabels()
.frictionlessThreshold,
value: this.getFrictionlessThresholdLabel(
siteSettings.frictionlessThreshold,
),
},
{
label: this.config.getCaptchaSettingsLabels()
.powDifficulty,
value: this.getPowDifficultyLabel(
siteSettings.powDifficulty,
),
},
],
},
domains: {
...actualState.domains,
items: domains,
},
}));
}

protected refreshTrafficData(account: ProcaptchaAccount): void {
this.setState((actualState) => ({
...actualState,
Expand All @@ -295,8 +159,7 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
);

if (site) {
this.refreshUserData(site);
this.refreshSiteSettings(site.settings);
this.refreshUsage(site);
this.refreshTrafficData(site.account);

this.markAsLoaded();
Expand All @@ -318,14 +181,7 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
}

public render() {
const {
statState,
usageInfo,
accountInformation,
captchaSettings,
domains,
trafficData,
} = this.state;
const { statState, usageInfo, trafficData } = this.state;

return (
<div className="flex flex-col gap-5">
Expand All @@ -343,21 +199,6 @@ class AppComponent extends React.Component<AppComponentProperties, AppState> {
image={usageInfo.image}
pow={usageInfo.pow}
/>
<ListComponent
title={accountInformation.title}
icon={accountInformation.icon}
items={accountInformation.items}
/>
<ListComponent
title={captchaSettings.title}
icon={captchaSettings.icon}
items={captchaSettings.items}
/>
<ListComponent
title={domains.title}
icon={domains.icon}
items={domains.items}
/>
<TrafficAnalyticsComponent
classes="col-span-2"
logger={trafficData.logger}
Expand Down
38 changes: 0 additions & 38 deletions assets/src/settings/statistics/components/listComponent.tsx

This file was deleted.

Loading
Loading