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

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

258 lines
9.0 KiB
TypeScript
Raw Normal View History

2025-08-19 13:31:09 +01:00
import React, { useState, useRef, forwardRef, useEffect } from "react";
import { ActionIcon, Stack, Divider } from "@mantine/core";
import { useTranslation } from 'react-i18next';
import LocalIcon from './LocalIcon';
import { useRainbowThemeContext } from "./RainbowThemeProvider";
2025-07-25 12:27:30 +01:00
import { useIsOverflowing } from '../../hooks/useIsOverflowing';
2025-08-04 15:01:36 +01:00
import { useFilesModalContext } from '../../contexts/FilesModalContext';
2025-08-08 15:56:20 +01:00
import { useToolWorkflow } from '../../contexts/ToolWorkflowContext';
import { useSidebarNavigation } from '../../hooks/useSidebarNavigation';
import { handleUnlessSpecialClick } from '../../utils/clickHandlers';
2025-08-08 15:56:20 +01:00
import { ButtonConfig } from '../../types/sidebar';
2025-08-19 13:31:09 +01:00
import './quickAccessBar/QuickAccessBar.css';
import AllToolsNavButton from './AllToolsNavButton';
import ActiveToolButton from "./quickAccessBar/ActiveToolButton";
2025-10-01 20:22:04 +01:00
import AppConfigModal from './AppConfigModal';
import { useAppConfig } from '../../hooks/useAppConfig';
import {
isNavButtonActive,
getNavButtonStyle,
2025-08-19 13:31:09 +01:00
getActiveNavButton,
} from './quickAccessBar/QuickAccessBar';
2025-07-25 12:27:30 +01:00
2025-09-05 10:55:03 +01:00
const QuickAccessBar = forwardRef<HTMLDivElement>((_, ref) => {
2025-08-19 13:31:09 +01:00
const { t } = useTranslation();
const { isRainbowMode } = useRainbowThemeContext();
2025-08-04 15:01:36 +01:00
const { openFilesModal, isFilesModalOpen } = useFilesModalContext();
2025-08-26 16:59:03 +01:00
const { handleReaderToggle, handleBackToTools, handleToolSelect, selectedToolKey, leftPanelView, toolRegistry, readerMode, resetTool } = useToolWorkflow();
const { getToolNavigation } = useSidebarNavigation();
const { config } = useAppConfig();
const [configModalOpen, setConfigModalOpen] = useState(false);
2025-07-25 12:27:30 +01:00
const [activeButton, setActiveButton] = useState<string>('tools');
const scrollableRef = useRef<HTMLDivElement>(null);
const isOverflow = useIsOverflowing(scrollableRef);
2025-08-19 13:31:09 +01:00
useEffect(() => {
const next = getActiveNavButton(selectedToolKey, readerMode);
setActiveButton(next);
}, [leftPanelView, selectedToolKey, toolRegistry, readerMode]);
2025-08-04 15:01:36 +01:00
const handleFilesButtonClick = () => {
openFilesModal();
};
// Helper function to render navigation buttons with URL support
const renderNavButton = (config: ButtonConfig, index: number) => {
const isActive = isNavButtonActive(config, activeButton, isFilesModalOpen, configModalOpen, selectedToolKey, leftPanelView);
// Check if this button has URL navigation support
const navProps = config.type === 'navigation' && (config.id === 'read' || config.id === 'automate')
? getToolNavigation(config.id)
: null;
const handleClick = (e?: React.MouseEvent) => {
if (navProps && e) {
handleUnlessSpecialClick(e, config.onClick);
} else {
config.onClick();
}
};
// Render navigation button with conditional URL support
return (
<div key={config.id} className="flex flex-col items-center gap-1" style={{ marginTop: index === 0 ? '0.5rem' : "0rem" }}>
<ActionIcon
{...(navProps ? {
component: "a" as const,
href: navProps.href,
onClick: (e: React.MouseEvent) => handleClick(e),
'aria-label': config.name
} : {
onClick: () => handleClick()
})}
size={isActive ? (config.size || 'lg') : 'lg'}
variant="subtle"
style={getNavButtonStyle(config, activeButton, isFilesModalOpen, configModalOpen, selectedToolKey, leftPanelView)}
className={isActive ? 'activeIconScale' : ''}
data-testid={`${config.id}-button`}
>
<span className="iconContainer">
{config.icon}
</span>
</ActionIcon>
<span className={`button-text ${isActive ? 'active' : 'inactive'}`}>
{config.name}
</span>
</div>
);
};
2025-07-25 12:27:30 +01:00
const buttonConfigs: ButtonConfig[] = [
{
id: 'read',
2025-08-19 13:31:09 +01:00
name: t("quickAccess.read", "Read"),
icon: <LocalIcon icon="menu-book-rounded" width="1.5rem" height="1.5rem" />,
2025-07-25 12:27:30 +01:00
size: 'lg',
isRound: false,
2025-08-04 15:01:36 +01:00
type: 'navigation',
2025-07-25 12:27:30 +01:00
onClick: () => {
setActiveButton('read');
2025-08-19 13:31:09 +01:00
handleBackToTools();
2025-08-08 15:56:20 +01:00
handleReaderToggle();
2025-07-25 12:27:30 +01:00
}
},
// TODO: Add sign
//{
// id: 'sign',
// name: t("quickAccess.sign", "Sign"),
// icon: <LocalIcon icon="signature-rounded" width="1.25rem" height="1.25rem" />,
// size: 'lg',
// isRound: false,
// type: 'navigation',
// onClick: () => {
// setActiveButton('sign');
// handleToolSelect('sign');
// }
//},
2025-07-25 12:27:30 +01:00
{
id: 'automate',
2025-08-19 13:31:09 +01:00
name: t("quickAccess.automate", "Automate"),
2025-08-26 16:31:20 +01:00
icon: <LocalIcon icon="automation-outline" width="1.6rem" height="1.6rem" />,
2025-07-25 12:27:30 +01:00
size: 'lg',
isRound: false,
2025-08-04 15:01:36 +01:00
type: 'navigation',
2025-08-19 13:31:09 +01:00
onClick: () => {
setActiveButton('automate');
2025-08-26 16:59:03 +01:00
// If already on automate tool, reset it directly
if (selectedToolKey === 'automate') {
resetTool('automate');
} else {
handleToolSelect('automate');
}
2025-08-19 13:31:09 +01:00
}
2025-07-25 12:27:30 +01:00
},
{
id: 'files',
2025-08-19 13:31:09 +01:00
name: t("quickAccess.files", "Files"),
2025-08-26 16:31:20 +01:00
icon: <LocalIcon icon="folder-rounded" width="1.6rem" height="1.6rem" />,
2025-07-25 12:27:30 +01:00
isRound: true,
size: 'lg',
2025-08-04 15:01:36 +01:00
type: 'modal',
onClick: handleFilesButtonClick
2025-07-25 12:27:30 +01:00
},
//TODO: Activity
//{
// id: 'activity',
// name: t("quickAccess.activity", "Activity"),
// icon: <LocalIcon icon="vital-signs-rounded" width="1.25rem" height="1.25rem" />,
// isRound: true,
// size: 'lg',
// type: 'navigation',
// onClick: () => setActiveButton('activity')
//},
2025-07-25 12:27:30 +01:00
{
id: 'config',
name: config?.enableLogin ? t("quickAccess.account", "Account") : t("quickAccess.config", "Config"),
icon: config?.enableLogin ? <LocalIcon icon="person-rounded" width="1.25rem" height="1.25rem" /> : <LocalIcon icon="settings-rounded" width="1.25rem" height="1.25rem" />,
2025-07-25 12:27:30 +01:00
size: 'lg',
2025-08-04 15:01:36 +01:00
type: 'modal',
2025-07-25 12:27:30 +01:00
onClick: () => {
setConfigModalOpen(true);
}
}
];
2025-08-04 15:01:36 +01:00
return (
<div
2025-08-08 12:09:41 +01:00
ref={ref}
data-sidebar="quick-access"
2025-07-25 12:27:30 +01:00
className={`h-screen flex flex-col w-20 quick-access-bar-main ${isRainbowMode ? 'rainbow-mode' : ''}`}
2025-08-19 13:31:09 +01:00
style={{
borderRight: '1px solid var(--border-default)'
}}
>
2025-07-25 12:27:30 +01:00
{/* Fixed header outside scrollable area */}
<div className="quick-access-header">
2025-08-19 13:31:09 +01:00
<ActiveToolButton activeButton={activeButton} setActiveButton={setActiveButton} />
<AllToolsNavButton activeButton={activeButton} setActiveButton={setActiveButton} />
2025-07-25 12:27:30 +01:00
</div>
2025-07-25 12:27:30 +01:00
{/* Conditional divider when overflowing */}
{isOverflow && (
2025-08-19 13:31:09 +01:00
<Divider
size="xs"
2025-07-25 12:27:30 +01:00
className="overflow-divider"
/>
)}
2025-07-25 12:27:30 +01:00
{/* Scrollable content area */}
<div
ref={scrollableRef}
className="quick-access-bar flex-1"
onWheel={(e) => {
// Prevent the wheel event from bubbling up to parent containers
e.stopPropagation();
}}
>
<div className="scrollable-content">
{/* Top section with main buttons */}
<Stack gap="lg" align="center">
{buttonConfigs.slice(0, -1).map((config, index) => (
<React.Fragment key={config.id}>
{renderNavButton(config, index)}
2025-08-19 13:31:09 +01:00
{/* Add divider after Automate button (index 1) and Files button (index 2) */}
{index === 1 && (
2025-08-19 13:31:09 +01:00
<Divider
size="xs"
className="content-divider"
/>
2025-07-25 12:27:30 +01:00
)}
</React.Fragment>
))}
</Stack>
2025-08-19 13:31:09 +01:00
2025-07-25 12:27:30 +01:00
{/* Spacer to push Config button to bottom */}
<div className="spacer" />
2025-08-19 13:31:09 +01:00
2025-07-25 12:27:30 +01:00
{/* Config button at the bottom */}
2025-10-01 20:22:04 +01:00
{buttonConfigs
2025-08-04 15:01:36 +01:00
.filter(config => config.id === 'config')
.map(config => (
<div key={config.id} className="flex flex-col items-center gap-1">
2025-08-04 15:01:36 +01:00
<ActionIcon
size={config.size || 'lg'}
variant="subtle"
onClick={config.onClick}
2025-08-19 13:31:09 +01:00
style={getNavButtonStyle(config, activeButton, isFilesModalOpen, configModalOpen, selectedToolKey, leftPanelView)}
className={isNavButtonActive(config, activeButton, isFilesModalOpen, configModalOpen, selectedToolKey, leftPanelView) ? 'activeIconScale' : ''}
2025-08-04 15:01:36 +01:00
data-testid={`${config.id}-button`}
>
<span className="iconContainer">
{config.icon}
</span>
</ActionIcon>
2025-08-19 13:31:09 +01:00
<span className={`button-text ${isNavButtonActive(config, activeButton, isFilesModalOpen, configModalOpen, selectedToolKey, leftPanelView) ? 'active' : 'inactive'}`}>
2025-08-04 15:01:36 +01:00
{config.name}
</span>
</div>
2025-10-01 20:22:04 +01:00
))}
</div>
2025-07-25 12:27:30 +01:00
</div>
2025-10-01 20:22:04 +01:00
<AppConfigModal
opened={configModalOpen}
onClose={() => setConfigModalOpen(false)}
2025-10-01 20:22:04 +01:00
/>
</div>
);
2025-08-08 12:09:41 +01:00
});
QuickAccessBar.displayName = 'QuickAccessBar';
export default QuickAccessBar;