Files
Stirling-PDF/frontend/src/saas/components/shared/config/saasConfigNavSections.tsx
T
James BruntonandGitHub a3e45bc182 Add frontend autoformatting and set CI to require formatted code for all languages (#6052)
# Description of Changes
Changes the strategy for autoformatting to reject PRs if they are not
formatted correctly instead of allowing them to merge and then spawning
a new PR to fix the formatting. The old strategy just caused more work
for us because we'd have to manually approve the followup PR and get it
merged, which required 2 reviewers so in practice it rarely got done and
just meant everyone's PRs ended up containing reformatting for unrelated
files, which makes code review unnecessarily difficult. If the PR's code
is not formatted correctly after this PR, a comment will be added
automatically to tell the author how to run the formatter script to fix
their code so it can go in.

This also enables autoformatting for the frontend code, using Prettier.
I've enabled it for pretty much everything in the frontend folder, other
than 3rd party files and files it doesn't make sense for. I also
excluded Markdown because it sounds likely to be more annoying to have
to autoformat the Markdown in the frontend folder but nowhere else. Open
to changing this though if people disagree.

> [!note]
> 
> Advice to reviewers: The first commit contains all of the actual logic
I've introduced (CI changes, Prettier config, etc.)
> The second commit is just the reformatting of the entire frontend
folder.
> The first commit needs proper review, the second one just give it a
spot-check that it's doing what you'd expect.
2026-04-10 17:41:19 +01:00

147 lines
4.0 KiB
TypeScript

import React from "react";
import { type TFunction } from "i18next";
import {
createConfigNavSections as createCoreConfigNavSections,
type ConfigNavSection,
} from "@core/components/shared/config/configNavSections";
import HotkeysSection from "@app/components/shared/config/configSections/HotkeysSection";
import GeneralSection from "@app/components/shared/config/configSections/GeneralSection";
import PasswordSecurity from "@app/components/shared/config/configSections/PasswordSecurity";
import ApiKeys from "@app/components/shared/config/configSections/ApiKeys";
import Plan from "@app/components/shared/config/configSections/Plan";
type OverviewComponent = React.ComponentType<{ onLogoutClick: () => void }>;
interface CreateSaasConfigNavSectionsOptions {
isDev?: boolean;
isAnonymous?: boolean;
t: TFunction<"translation", undefined>;
}
function ensurePreferencesSection(sections: ConfigNavSection[]): ConfigNavSection[] {
const preferencesIndex = sections.findIndex((section) => section.title === "Preferences");
if (preferencesIndex === -1) {
return [
...sections,
{
title: "Preferences",
items: [
{
key: "general",
label: "General",
icon: "settings-rounded",
component: <GeneralSection />,
},
{
key: "hotkeys",
label: "Keyboard Shortcuts",
icon: "keyboard-rounded",
component: <HotkeysSection />,
},
],
},
];
}
return sections;
}
function appendDeveloperSection(sections: ConfigNavSection[]): ConfigNavSection[] {
const hasDeveloper = sections.some((section) =>
section.items.some((item) => item.key === "developer" || item.key === "api-keys"),
);
if (hasDeveloper) {
return sections;
}
return [
...sections,
{
title: "Developer",
items: [
{
key: "api-keys",
label: "API Keys",
icon: "key-rounded",
component: <ApiKeys />,
},
],
},
];
}
function appendBillingSection(sections: ConfigNavSection[], t: TFunction<"translation", undefined>): ConfigNavSection[] {
const hasPlan = sections.some((section) => section.items.some((item) => item.key === "plan"));
if (hasPlan) {
return sections;
}
return [
...sections,
{
title: "Billing",
items: [
{
key: "plan",
label: t("config.plan", "Plan"),
icon: "credit-card",
component: <Plan />,
},
],
},
];
}
export function createSaasConfigNavSections(
Overview: OverviewComponent,
onLogoutClick: () => void,
{ isDev = false, isAnonymous = false, t }: CreateSaasConfigNavSectionsOptions,
): ConfigNavSection[] {
const baseSections = createCoreConfigNavSections(false, false, false);
// Create Account section as the first section with Overview and Passwords & Security
const accountSection: ConfigNavSection = {
title: t("config.account.overview.title", "Account Settings"),
items: [
{
key: "overview",
label: t("config.account.overview.label", "Overview"),
icon: "account-circle",
component: <Overview onLogoutClick={onLogoutClick} />,
},
{
key: "security",
label: "Passwords & Security",
icon: "lock",
component: <PasswordSecurity />,
},
],
};
let sections = [accountSection, ...baseSections];
// Suppress OSS-only sections (update checker, login config banner) not relevant in SaaS
sections = sections.map((section) => ({
...section,
items: section.items.map((item) =>
item.key === "general" ? { ...item, component: <GeneralSection hideUpdateSection hideAdminBanner /> } : item,
),
}));
sections = ensurePreferencesSection(sections);
sections = appendDeveloperSection(sections);
if (!isAnonymous) {
sections = appendBillingSection(sections, t);
}
if (isDev) {
console.debug("[AppConfigModal] SaaS navigation sections", sections);
}
return sections;
}