Files
youtube-generator/src/utils/editor-issues.ts
T
poli0981andClaude Opus 5.5 116dc1c7e6 feat(editor): copyright email and purpose words for split contact emails
With "Split contact email by purpose" on, the contact block gains a fourth
line, Copyright / DMCA, and every purpose address has to say what it is
for: the part before @ must contain one of the field's words
(src/config/contact-emails.ts), matched case-insensitively anywhere in it,
so name+dmca@gmail.com works. A missing word is an error like a malformed
address: the value is not saved and Strict Mode blocks. Addresses saved
before the rule are flagged, never rewritten.

- validatePurposeEmails() on top of validateEmails(); Strict Mode and the
  three purpose inputs use it, the general Contact field does not
- copyrightEmail: EditorData, defaults, field cap, PROFILE_FIELDS (optional
  on Profile, like the v1.0.0 additions); editor store v19 -> v20
- description: "©️ Copyright / DMCA: …" after the game-keys line, 8 locales
- help texts list each field's words; the Spanish examples now carry one
  (sponsors@, games@), and a test keeps every locale's examples valid

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:13:45 +07:00

200 lines
7.0 KiB
TypeScript

import type { EditorData } from "@store/editor-store";
import { PLATFORMS } from "@config/platforms";
import { SOCIAL_FIELDS } from "@config/social-fields";
import { PLAYTEST_PLATFORMS } from "@config/playtest-platforms";
import {
validateEmails,
validatePurposeEmails,
validateUrl,
validateUrlWithPattern,
validateUrlWithPrefix,
validatePlaylistUrl,
validateMessengerUrl,
validateZaloGroupUrl,
validateSignalGroupUrl,
validateInstagramInviteUrl,
validateFacebookGroupUrl,
type ValidationResult,
} from "./validation";
/**
* Everything currently wrong with the editor's data, derived from the store.
*
* The first cut of Strict Mode had each input register its own error into a
* shared store as it rendered. That looked reasonable and was quietly useless:
* the fields live on the Editor page, the gates live on Batch / Social /
* Output / Profiles, and unmounting the Editor took every issue with it. Strict
* Mode could never block anything on the page you were actually on.
*
* Deriving from state instead of from mounted components fixes that, and is
* also simply more honest about what the setting promises. Two things follow:
*
* - It works from any page, with no mount ordering or effect-loop hazards.
* - It catches invalid data that never passed through an input at all —
* an imported profile, a preset shared by someone else, a draft persisted
* before a validator got stricter. Since `ValidatedInput` refuses to commit
* invalid text, imports are in fact the main way bad data gets in.
*
* Pure and React-free, so the whole rule set is unit-testable.
*/
export type IssueSeverity = "error" | "warning";
export interface EditorIssue {
/** Stable id — `storeLinks.steam`, `social.twitch`, `contactEmail`. */
id: string;
/** i18n key naming the field, for the banner. */
labelKey: string;
/** i18n key of the failure message. */
messageKey: string;
params?: Record<string, string | number>;
/**
* Only `"error"` blocks. `validateUrlWithPrefix` reports a *working* link
* that merely doesn't match a platform's usual prefix as
* `{ valid: true, error }` — a vanity domain or a regional mirror. Blocking
* on that would strand the people most likely to have one.
*/
severity: IssueSeverity;
}
/** Context that decides which optional fields are actually in play. */
export interface EditorIssueContext {
/**
* When false, `adEmail` / `gameKeyEmail` / `copyrightEmail` are not
* rendered, so they cannot block.
*/
splitContactEmail: boolean;
/** Output language — Zalo is Vietnamese-only. */
language: string;
}
/**
* Is this field currently shown, and therefore allowed to block?
*
* Applied to *both* saved and typed issues. A value the user cannot see or
* reach — `adEmail` after the split toggle goes off, `zaloGroupLink` after
* switching away from Vietnamese — must never gate anything, or Strict Mode
* becomes an unresolvable dead end.
*/
export function isRelevantIssueId(id: string, context: EditorIssueContext): boolean {
if (id === "adEmail" || id === "gameKeyEmail" || id === "copyrightEmail") {
return context.splitContactEmail;
}
if (id === "zaloGroupLink") return context.language === "vi";
return true;
}
function toIssue(id: string, labelKey: string, result: ValidationResult): EditorIssue | null {
if (result.valid && !result.error) return null;
const issue: EditorIssue = {
id,
labelKey,
messageKey: result.error ?? "validation.urlInvalid",
severity: result.valid ? "warning" : "error",
};
if (result.errorParams) issue.params = result.errorParams;
return issue;
}
export function collectEditorIssues(
editor: EditorData,
context: EditorIssueContext,
): EditorIssue[] {
const { splitContactEmail } = context;
const issues: EditorIssue[] = [];
const add = (id: string, labelKey: string, result: ValidationResult) => {
const issue = toIssue(id, labelKey, result);
if (issue) issues.push(issue);
};
// Store links — one per platform, each with its own authoritative pattern.
for (const platform of PLATFORMS) {
const url = editor.storeLinks?.[platform.id];
if (url)
add(
`storeLinks.${platform.id}`,
platform.label,
validateUrlWithPattern(url, platform.urlPattern),
);
}
// Social + donate links. Same rule the editor uses: a declared prefix gets
// the soft prefix check, everything else just has to be a URL.
for (const field of SOCIAL_FIELDS) {
const url = editor.social?.[field.id];
if (!url) continue;
add(
`social.${field.id}`,
field.labelKey,
field.urlPrefix ? validateUrlWithPrefix(url, field.urlPrefix) : validateUrl(url),
);
}
// Community invites. Zalo is Vietnamese-audience only — it neither renders
// nor is editable in another output language, so it must not block there.
if (editor.messengerCommunityLink)
add(
"messengerCommunityLink",
"editor.messengerCommunityLink",
validateMessengerUrl(editor.messengerCommunityLink),
);
if (editor.signalGroupLink)
add(
"signalGroupLink",
"editor.signalGroupLink",
validateSignalGroupUrl(editor.signalGroupLink),
);
if (editor.instagramGroupLink)
add(
"instagramGroupLink",
"editor.instagramGroupLink",
validateInstagramInviteUrl(editor.instagramGroupLink),
);
if (editor.facebookGroupLink)
add(
"facebookGroupLink",
"editor.facebookGroupLink",
validateFacebookGroupUrl(editor.facebookGroupLink),
);
if (isRelevantIssueId("zaloGroupLink", context) && editor.zaloGroupLink)
add("zaloGroupLink", "editor.zaloGroupLink", validateZaloGroupUrl(editor.zaloGroupLink));
// Contact emails. The three purpose-specific fields only exist — and only
// render — while the split toggle is on, and each needs a purpose word
// before `@` (v1.1.0). A value saved before that rule existed is flagged
// here, never rewritten.
if (editor.contactEmail)
add("contactEmail", "editor.contactEmail", validateEmails(editor.contactEmail));
if (splitContactEmail) {
for (const field of ["adEmail", "gameKeyEmail", "copyrightEmail"] as const) {
if (editor[field]) add(field, `editor.${field}`, validatePurposeEmails(field, editor[field]));
}
}
if (editor.playlistLink)
add("playlistLink", "editor.playlistLink", validatePlaylistUrl(editor.playlistLink));
if (editor.playtestLink) {
const platform = PLAYTEST_PLATFORMS.find((p) => p.id === editor.playtestPlatform);
add(
"playtestLink",
"editor.playtest.linkLabel",
platform?.urlPattern
? validateUrlWithPattern(editor.playtestLink, platform.urlPattern)
: validateUrl(editor.playtestLink),
);
}
if (editor.liveUrl) add("liveUrl", "editor.liveUrl", validateUrl(editor.liveUrl));
return issues;
}
/** Just the blocking subset — warnings are surfaced but never gate. */
export function collectEditorErrors(
editor: EditorData,
context: EditorIssueContext,
): EditorIssue[] {
return collectEditorIssues(editor, context).filter((i) => i.severity === "error");
}