refactor: extract various constants

This commit is contained in:
2025-11-25 10:10:42 +01:00
parent 4d30e47ee8
commit ccc22beda2
40 changed files with 326 additions and 216 deletions
+12 -32
View File
@@ -10,13 +10,16 @@ import React, {
} from 'react';
import type { CSSProperties, PointerEvent as ReactPointerEvent, RefObject } from 'react';
import { useSidebarContext } from '../sidebar/SidebarContext';
type PanelKey = 'sidebar' | 'detail';
interface PanelLimits {
maxRatio: number;
minPx: number;
}
import {
DEFAULT_DETAIL_WIDTH,
DEFAULT_SIDEBAR_WIDTH,
MINIMAL_FREE_RATIO,
MINIMUM_MAIN_CONTENT_WIDTH,
PANEL_LIMITS,
PANEL_STORAGE_KEYS,
SIDEBAR_SOLO_THRESHOLD,
type PanelKey,
} from '../constants/layout';
interface SetPanelWidthOptions {
commit?: boolean;
@@ -50,29 +53,6 @@ type PanelResizeBindings = {
const PanelManagerContext = createContext<PanelManagerContextValue | null>(null);
const PANEL_LIMITS: Record<PanelKey, PanelLimits> = {
sidebar: {
maxRatio: 1 / 3,
minPx: 280,
},
detail: {
maxRatio: 2 / 3,
minPx: 320,
},
};
const STORAGE_KEYS: Record<PanelKey, string> = {
sidebar: 'papercrate_sidebar_width',
detail: 'papercrate_detail_width',
};
const DEFAULT_SIDEBAR_WIDTH = 320;
const DEFAULT_DETAIL_WIDTH = 420;
const MINIMAL_FREE_RATIO = 1 / 3;
const SIDEBAR_SOLO_THRESHOLD = 1 / 2;
const MINIMUM_MAIN_CONTENT_WIDTH = 160;
const clampPanelWidth = (panel: PanelKey, value: number): number => {
const numeric = Number(value);
const limits = PANEL_LIMITS[panel];
@@ -86,7 +66,7 @@ const clampPanelWidth = (panel: PanelKey, value: number): number => {
};
const readStoredWidth = (panel: PanelKey, fallback: number): number => {
const raw = window.localStorage.getItem(STORAGE_KEYS[panel]);
const raw = window.localStorage.getItem(PANEL_STORAGE_KEYS[panel]);
if (!raw) {
return fallback;
}
@@ -95,7 +75,7 @@ const readStoredWidth = (panel: PanelKey, fallback: number): number => {
};
const persistWidth = (panel: PanelKey, value: number): void => {
window.localStorage.setItem(STORAGE_KEYS[panel], String(Math.round(value)));
window.localStorage.setItem(PANEL_STORAGE_KEYS[panel], String(Math.round(value)));
};
const applyPanelWidthToRoot = (panel: PanelKey, width: number, active: boolean): void => {