diff options
| author | Leitwolf11 | 2026-11-06 13:28:25 +0300 |
|---|---|---|
| committer | Leitwolf11 | 2026-11-06 13:28:25 +0300 |
| commit | 39a301172c3752e7b22f9ea2c3a4b1d4c73cfeb1 (patch) | |
| tree | 0e3eb0e988b21135406862ec5318ea0afd76fb61 | |
| parent | 53fb8b8bde857025d12655ae53857fc30226b186 (diff) | |
| download | progmaweb-39a301172c3752e7b22f9ea2c3a4b1d4c73cfeb1.tar.gz | |
Pick a policy by a condition rather than by an index
Codacy reads messages[kind] and policyUrls[kind] as object injection
sinks. kind is a PolicyKind the pages pass, never input, but choosing
the policy and its counterpart with one condition reads as plainly and
leaves the analysis nothing to report.
| -rw-r--r-- | apps/web/components/PolicyPage.tsx | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/apps/web/components/PolicyPage.tsx b/apps/web/components/PolicyPage.tsx index c0e8eda..235a265 100644 --- a/apps/web/components/PolicyPage.tsx +++ b/apps/web/components/PolicyPage.tsx @@ -5,16 +5,31 @@ import type { Metadata } from "next"; import { Footer } from "@/components/Footer"; import { SiteHeader } from "@/components/SiteHeader"; import { getLocale } from "@/lib/locale.server"; -import { type PolicyKind, policyUrls, policyVersion } from "@/lib/policies"; +import { + type PolicyKind, + type PolicyMessages, + type PolicyText, + policyUrls, + policyVersion, +} from "@/lib/policies"; import { getPolicyMessages } from "@/lib/policy-messages"; +/** One policy and the other, with their addresses. */ +function select(messages: PolicyMessages, kind: PolicyKind) { + const privacy = { text: messages.privacy, url: policyUrls.privacy }; + const security = { text: messages.security, url: policyUrls.security }; + return kind === "privacy" + ? { policy: privacy, other: { ...security, path: "/security" } } + : { policy: security, other: { ...privacy, path: "/privacy" } }; +} + /** The title, description and canonical address of a policy page. */ export async function policyMetadata(kind: PolicyKind): Promise<Metadata> { - const policy = getPolicyMessages(await getLocale())[kind]; + const { policy } = select(getPolicyMessages(await getLocale()), kind); return { - title: policy.title, - description: policy.description, - alternates: { canonical: policyUrls[kind] }, + title: policy.text.title, + description: policy.text.description, + alternates: { canonical: policy.url }, }; } @@ -22,8 +37,9 @@ export async function policyMetadata(kind: PolicyKind): Promise<Metadata> { export async function PolicyPage({ kind }: { kind: PolicyKind }) { const locale = await getLocale(); const messages = getPolicyMessages(locale); - const policy = messages[kind]; - const other: PolicyKind = kind === "privacy" ? "security" : "privacy"; + const selected = select(messages, kind); + const policy: PolicyText = selected.policy.text; + const other = selected.other; return ( <div className="pm-page"> <SiteHeader locale={locale} /> @@ -49,8 +65,7 @@ export async function PolicyPage({ kind }: { kind: PolicyKind }) { </section> ))} <p className="pm-policy-see-also"> - {messages.seeAlso}:{" "} - <a href={`/${other}`}>{messages[other].title}</a> + {messages.seeAlso}: <a href={other.path}>{other.text.title}</a> </p> </article> </section> |