Fix PR review issues: loading state naming, singleton constraint, and type-safety

- Rename 'loading' to 'isLoading' in SiteConfigContext to match consumer components
- Add CHECK constraint (id = 1) for singleton pattern in site_configuration table
- Replace string concatenation with typed routeConfigMap in RouteGuard for type-safety

Co-authored-by: Ojārs Kapteinis <ojars@kapteinis.lv>
Co-authored-by: Claude <code@claude.ai>

🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
Ojārs Kapteinis 2025-11-19 01:41:22 +02:00
parent d5fba5ae1a
commit 0edc6dc77f
3 changed files with 22 additions and 18 deletions

View file

@ -49,7 +49,8 @@ def upgrade() -> None:
# Custom text
sa.Column('custom_copyright', sa.String(length=200), nullable=True, server_default='SurfSense 2025'),
sa.PrimaryKeyConstraint('id')
sa.PrimaryKeyConstraint('id'),
sa.CheckConstraint('id = 1', name='check_singleton')
)
# Create index

View file

@ -2,13 +2,22 @@
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useSiteConfig } from "@/contexts/SiteConfigContext";
import { useSiteConfig, SiteConfig } from "@/contexts/SiteConfigContext";
interface RouteGuardProps {
children: React.ReactNode;
routeKey: "pricing" | "docs" | "contact" | "terms" | "privacy" | "registration";
}
const routeConfigMap: Record<RouteGuardProps["routeKey"], keyof SiteConfig> = {
pricing: "disable_pricing_route",
docs: "disable_docs_route",
contact: "disable_contact_route",
terms: "disable_terms_route",
privacy: "disable_privacy_route",
registration: "disable_registration",
};
export function RouteGuard({ children, routeKey }: RouteGuardProps) {
const { config, isLoading } = useSiteConfig();
const router = useRouter();
@ -16,11 +25,8 @@ export function RouteGuard({ children, routeKey }: RouteGuardProps) {
useEffect(() => {
if (isLoading) return;
// Special case: registration uses disable_registration instead of disable_registration_route
const disableKey = routeKey === "registration"
? "disable_registration"
: (`disable_${routeKey}_route` as keyof typeof config);
const isDisabled = config[disableKey as keyof typeof config];
const disableKey = routeConfigMap[routeKey];
const isDisabled = config[disableKey];
if (isDisabled) {
router.replace("/404");
@ -37,11 +43,8 @@ export function RouteGuard({ children, routeKey }: RouteGuardProps) {
}
// Check if route is disabled
// Special case: registration uses disable_registration instead of disable_registration_route
const disableKey = routeKey === "registration"
? "disable_registration"
: (`disable_${routeKey}_route` as keyof typeof config);
const isDisabled = config[disableKey as keyof typeof config];
const disableKey = routeConfigMap[routeKey];
const isDisabled = config[disableKey];
if (isDisabled) {
return null;

View file

@ -53,26 +53,26 @@ const defaultConfig: SiteConfig = {
interface SiteConfigContextType {
config: SiteConfig;
loading: boolean;
isLoading: boolean;
error: string | null;
refetch: () => Promise<void>;
}
const SiteConfigContext = createContext<SiteConfigContextType>({
config: defaultConfig,
loading: true,
isLoading: true,
error: null,
refetch: async () => {},
});
export function SiteConfigProvider({ children }: { children: React.ReactNode }) {
const [config, setConfig] = useState<SiteConfig>(defaultConfig);
const [loading, setLoading] = useState(true);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchConfig = async () => {
try {
setLoading(true);
setIsLoading(true);
setError(null);
const backendUrl =
@ -91,7 +91,7 @@ export function SiteConfigProvider({ children }: { children: React.ReactNode })
console.error("Error fetching site configuration:", err);
// Keep default config on error
} finally {
setLoading(false);
setIsLoading(false);
}
};
@ -100,7 +100,7 @@ export function SiteConfigProvider({ children }: { children: React.ReactNode })
}, []);
return (
<SiteConfigContext.Provider value={{ config, loading, error, refetch: fetchConfig }}>
<SiteConfigContext.Provider value={{ config, isLoading, error, refetch: fetchConfig }}>
{children}
</SiteConfigContext.Provider>
);