#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { Suspense, lazy, useCallback, useMemo, useState } from "react";
import { Modal, Text } from "@mantine/core";
import { useTranslation } from "react-i18next";
import { Button } from "@editor/ui/Button";
import { useAuth } from "@editor/auth/UseSession";
import { usePortalAccessState } from "@editor/hooks/usePortalAccess";
import { isUserAnonymous } from "@editor/auth/supabase";
import Overview from "@editor/components/shared/config/configSections/Overview";
import { createSaasConfigNavSections } from "@editor/components/shared/config/saasConfigNavSections";
import { withBasePath } from "@editor/constants/app";
import { Z_INDEX_OVER_SETTINGS_MODAL } from "@editor/styles/zIndex";
import type { SettingsNav } from "@editor/components/settings/settingsNavTypes";
import {
buildPortalSettingsSections,
PORTAL_SECTION_ALIASES,
portalSupersededSectionKeys,
} from "@editor/components/settings/portalSettingsNav";
import { mergeSettingsGroups } from "@editor/components/settings/mergeSettingsGroups";
import { useSaaSTeam } from "@editor/contexts/SaaSTeamContext";
import { LoadingFallback } from "@editor/components/shared/LoadingFallback";
import { BASE_SECTION_ALIASES } from "@editor/data/settingsAliases";
import { useFeedback } from "@editor/hooks/useFeedback";
import type { NavKey } from "@editor/components/shared/config/types";
const ConnectedInstancesSection = lazy(
() => import("@editor/components/settings/ConnectedInstancesSection"),
);
export type { SettingsNav };
// SaaS accounts live under Overview rather than as cards on Preferences.
const SAAS_SECTION_ALIASES: Partial<Record<string, NavKey>> = {
...BASE_SECTION_ALIASES,
backendThirdPartyLicenses: "legal",
frontendThirdPartyLicenses: "legal",
account: "overview",
};
/**
* SaaS settings sections. The cloud nav is a plain factory rather than a hook,
* and its Overview tab owns the sign-out action, so this seam also supplies the
* confirm dialog as the page's overlay. Members with processor access also get
* the processor's former Users / Infrastructure / Usage tabs here, the same way
* self-hosted does; the processor's redirects for those routes rely on it.
*/
export function useSettingsNav(onLeave: () => void): SettingsNav {
const { t } = useTranslation();
const { signOut, user } = useAuth();
const { isTeamLeader, loading: teamLoading } = useSaaSTeam();
// Not from useAuth: the editor's Supabase context never carries permission
// flags, so only this seam knows. The processor has its own auth context.
const { granted: portalAccess, settled: accessSettled } =
usePortalAccessState();
const [confirmOpen, setConfirmOpen] = useState(false);
const openLogoutConfirm = useCallback(() => setConfirmOpen(true), []);
const isAnonymous = user ? isUserAnonymous(user) : false;
const feedbackAvailable = useFeedback() !== null;
const sections = useMemo(() => {
let own = createSaasConfigNavSections(Overview, openLogoutConfirm, {
isAnonymous,
t,
onRequestClose: onLeave,
feedbackAvailable,
});
if (isTeamLeader && !teamLoading && !isAnonymous) {
own = mergeSettingsGroups(
own,
[
{
id: "workspace",
title: t("settings.workspace.title", "Workspace"),
mergeAt: "append",
items: [
{
key: "account-link",
label: t(
"settings.connectedInstances.title",
"Connected instances",
),
icon: "link",
fullBleed: true,
component: (
<Suspense fallback={<LoadingFallback />}>
<ConnectedInstancesSection />
</Suspense>
),
},
],
},
],
[],
);
}
if (!portalAccess) return own;
const portal = buildPortalSettingsSections(t, {
includeAccountLink: false,
includeAudit: true,
});
if (portal.length === 0) return own;
return mergeSettingsGroups(
own,
portal,
portalSupersededSectionKeys(portal),
);
}, [
openLogoutConfirm,
isAnonymous,
t,
onLeave,
portalAccess,
isTeamLeader,
teamLoading,
feedbackAvailable,
]);
const overlay = (
<Modal
opened={confirmOpen}
onClose={() => setConfirmOpen(false)}
title={t("settings.signOut.title", "Sign out")}
centered
zIndex={Z_INDEX_OVER_SETTINGS_MODAL}
>
<div className="confirm-modal-content">
<Text>
{t("settings.signOut.confirm", "Are you sure you want to sign out?")}
</Text>
<div className="confirm-modal-buttons">
<Button variant="secondary" onClick={() => setConfirmOpen(false)}>
{t("common.cancel", "Cancel")}
</Button>
<Button
accent="danger"
onClick={async () => {
try {
await signOut();
} finally {
setConfirmOpen(false);
window.location.href = withBasePath("/login");
}
}}
>
{t("settings.signOut.submit", "Sign out")}
</Button>
</div>
</div>
</Modal>
);
return {
sections,
overlay,
aliases: portalAccess
? { ...SAAS_SECTION_ALIASES, ...PORTAL_SECTION_ALIASES }
: SAAS_SECTION_ALIASES,
pending: !accessSettled || teamLoading,
};
}