Files
Stirling-PDF/frontend/src/components/shared/TopControls.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-07-16 17:53:50 +01:00
import React, { useState, useCallback } from "react";
import { SegmentedControl, Loader } from "@mantine/core";
2025-06-09 18:38:06 +01:00
import { useRainbowThemeContext } from "./RainbowThemeProvider";
2025-06-19 22:41:05 +01:00
import rainbowStyles from '../../styles/rainbow.module.css';
2025-06-05 21:59:18 +01:00
import VisibilityIcon from "@mui/icons-material/Visibility";
import EditNoteIcon from "@mui/icons-material/EditNote";
2025-06-20 17:51:24 +01:00
import FolderIcon from "@mui/icons-material/Folder";
2025-08-22 14:55:41 +01:00
import { ModeType, isValidMode } from '../../contexts/NavigationContext';
2025-06-05 21:59:18 +01:00
const viewOptionStyle = {
display: 'inline-flex',
flexDirection: 'row',
alignItems: 'center',
gap: 6,
whiteSpace: 'nowrap',
paddingTop: '0.3rem',
}
// Create view options with icons and loading states
2025-08-22 14:55:41 +01:00
const createViewOptions = (switchingTo: ModeType | null) => [
2025-06-05 21:59:18 +01:00
{
label: (
<div style={viewOptionStyle as React.CSSProperties}>
2025-07-16 17:53:50 +01:00
{switchingTo === "viewer" ? (
<Loader size="xs" />
) : (
<VisibilityIcon fontSize="small" />
)}
<span>Read</span>
</div>
2025-06-05 21:59:18 +01:00
),
value: "viewer",
},
{
label: (
<div style={viewOptionStyle as React.CSSProperties}>
2025-07-16 17:53:50 +01:00
{switchingTo === "pageEditor" ? (
<Loader size="xs" />
) : (
<EditNoteIcon fontSize="small" />
)}
<span>Page Editor</span>
</div>
2025-06-05 21:59:18 +01:00
),
value: "pageEditor",
},
{
label: (
<div style={viewOptionStyle as React.CSSProperties}>
2025-07-16 17:53:50 +01:00
{switchingTo === "fileEditor" ? (
<Loader size="xs" />
) : (
<FolderIcon fontSize="small" />
)}
<span>File Manager</span>
</div>
2025-06-20 17:51:24 +01:00
),
value: "fileEditor",
},
2025-06-05 21:59:18 +01:00
];
interface TopControlsProps {
2025-08-22 14:55:41 +01:00
currentView: ModeType;
setCurrentView: (view: ModeType) => void;
2025-07-16 17:53:50 +01:00
selectedToolKey?: string | null;
2025-06-05 21:59:18 +01:00
}
2025-06-19 19:47:44 +01:00
const TopControls = ({
2025-06-05 21:59:18 +01:00
currentView,
setCurrentView,
2025-07-16 17:53:50 +01:00
selectedToolKey,
2025-06-19 19:47:44 +01:00
}: TopControlsProps) => {
const { isRainbowMode } = useRainbowThemeContext();
2025-08-22 14:55:41 +01:00
const [switchingTo, setSwitchingTo] = useState<ModeType | null>(null);
2025-08-11 09:16:16 +01:00
2025-07-16 17:53:50 +01:00
const isToolSelected = selectedToolKey !== null;
const handleViewChange = useCallback((view: string) => {
2025-08-22 14:55:41 +01:00
if (!isValidMode(view)) {
// Ignore invalid values defensively
return;
}
const mode = view as ModeType;
2025-07-16 17:53:50 +01:00
// Show immediate feedback
2025-08-22 14:59:18 +01:00
setSwitchingTo(mode as ModeType);
2025-08-11 09:16:16 +01:00
2025-07-16 17:53:50 +01:00
// Defer the heavy view change to next frame so spinner can render
requestAnimationFrame(() => {
// Give the spinner one more frame to show
requestAnimationFrame(() => {
2025-08-22 14:59:18 +01:00
setCurrentView(mode as ModeType);
2025-08-11 09:16:16 +01:00
2025-07-16 17:53:50 +01:00
// Clear the loading state after view change completes
setTimeout(() => setSwitchingTo(null), 300);
});
});
}, [setCurrentView]);
2025-06-09 18:38:06 +01:00
2025-06-05 21:59:18 +01:00
return (
2025-06-09 18:38:06 +01:00
<div className="absolute left-0 w-full top-0 z-[100] pointer-events-none">
2025-07-16 17:53:50 +01:00
{!isToolSelected && (
<div className="flex justify-center mt-[0.5rem]">
2025-07-16 17:53:50 +01:00
<SegmentedControl
data={createViewOptions(switchingTo)}
value={currentView}
onChange={handleViewChange}
color="blue"
radius="xl"
fullWidth
className={isRainbowMode ? rainbowStyles.rainbowSegmentedControl : ''}
style={{
transition: 'all 0.2s ease',
opacity: switchingTo ? 0.8 : 1,
pointerEvents: 'auto'
2025-07-16 17:53:50 +01:00
}}
styles={{
root: {
borderRadius: 9999,
},
control: {
borderRadius: 9999,
},
indicator: {
borderRadius: 9999,
},
}}
2025-07-16 17:53:50 +01:00
/>
</div>
)}
2025-06-05 21:59:18 +01:00
</div>
);
};
2025-06-08 13:45:45 +01:00
export default TopControls;