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

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

358 lines
13 KiB
TypeScript
Raw Normal View History

2025-08-19 13:31:09 +01:00
import React, { useState, useRef, forwardRef, useEffect } from "react";
2025-11-25 13:45:02 +00:00
import { ActionIcon, Stack, Divider, Menu, Indicator } from "@mantine/core";
2025-08-19 13:31:09 +01:00
import { useTranslation } from 'react-i18next';
import { useNavigate, useLocation } from 'react-router-dom';
import LocalIcon from '@app/components/shared/LocalIcon';
import { useRainbowThemeContext } from "@app/components/shared/RainbowThemeProvider";
import { useIsOverflowing } from '@app/hooks/useIsOverflowing';
import { useFilesModalContext } from '@app/contexts/FilesModalContext';
import { useToolWorkflow } from '@app/contexts/ToolWorkflowContext';
import { useSidebarNavigation } from '@app/hooks/useSidebarNavigation';
import { handleUnlessSpecialClick } from '@app/utils/clickHandlers';
import { ButtonConfig } from '@app/types/sidebar';
import '@app/components/shared/quickAccessBar/QuickAccessBar.css';
import AllToolsNavButton from '@app/components/shared/AllToolsNavButton';
import ActiveToolButton from "@app/components/shared/quickAccessBar/ActiveToolButton";
import AppConfigModal from '@app/components/shared/AppConfigModal';
import { useAppConfig } from '@app/contexts/AppConfigContext';
import { useOnboarding } from '@app/contexts/OnboardingContext';
2025-11-25 13:45:02 +00:00
import { useLicenseAlert } from "@app/hooks/useLicenseAlert";
2025-11-14 14:52:32 +00:00
import {
isNavButtonActive,
getNavButtonStyle,
2025-08-19 13:31:09 +01:00
getActiveNavButton,
} from '@app/components/shared/quickAccessBar/QuickAccessBar';
2025-11-10 13:47:43 +00:00
import { Z_INDEX_OVER_FULLSCREEN_SURFACE } from '@app/styles/zIndex';
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 navigate = useNavigate();
const location = useLocation();
const { isRainbowMode } = useRainbowThemeContext();
2025-08-04 15:01:36 +01:00
const { openFilesModal, isFilesModalOpen } = useFilesModalContext();
2025-10-10 15:36:35 +01:00
const { handleReaderToggle, handleToolSelect, selectedToolKey, leftPanelView, toolRegistry, readerMode, resetTool } = useToolWorkflow();
const { getToolNavigation } = useSidebarNavigation();
const { config } = useAppConfig();
2025-10-20 15:07:40 +01:00
const { startTour } = useOnboarding();
2025-11-25 13:45:02 +00:00
const licenseAlert = useLicenseAlert();
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-11-24 16:57:36 +00:00
const isRTL = typeof document !== 'undefined' && document.documentElement.dir === 'rtl';
// Open modal if URL is at /settings/*
useEffect(() => {
const isSettings = location.pathname.startsWith('/settings');
setConfigModalOpen(isSettings);
}, [location.pathname]);
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 (
2025-10-20 15:07:40 +01:00
<div
key={config.id}
className="flex flex-col items-center gap-1"
style={{ marginTop: index === 0 ? '0.5rem' : "0rem" }}
data-tour={`${config.id}-button`}
>
<ActionIcon
{...(navProps ? {
component: "a" as const,
href: navProps.href,
onClick: (e: React.MouseEvent) => handleClick(e),
'aria-label': config.name
} : {
2025-10-10 15:36:35 +01:00
onClick: () => handleClick(),
'aria-label': config.name
})}
2025-11-14 14:52:32 +00:00
size={isActive ? 'lg' : 'md'}
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-10-20 15:07:40 +01:00
const mainButtons: ButtonConfig[] = [
2025-07-25 12:27:30 +01:00
{
id: 'read',
2025-11-14 14:52:32 +00:00
name: t("quickAccess.reader", "Reader"),
icon: <LocalIcon icon="menu-book-rounded" width="1.25rem" height="1.25rem" />,
size: 'md',
2025-07-25 12:27:30 +01:00
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-08 15:56:20 +01:00
handleReaderToggle();
2025-07-25 12:27:30 +01:00
}
},
{
id: 'automate',
2025-08-19 13:31:09 +01:00
name: t("quickAccess.automate", "Automate"),
2025-11-14 14:52:32 +00:00
icon: <LocalIcon icon="automation-outline" width="1.25rem" height="1.25rem" />,
size: 'md',
2025-07-25 12:27:30 +01:00
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-11-14 14:52:32 +00:00
icon: <LocalIcon icon="folder-rounded" width="1.25rem" height="1.25rem" />,
2025-07-25 12:27:30 +01:00
isRound: true,
2025-11-14 14:52:32 +00:00
size: 'md',
2025-08-04 15:01:36 +01:00
type: 'modal',
onClick: handleFilesButtonClick
2025-07-25 12:27:30 +01:00
},
2025-10-20 15:07:40 +01:00
];
2025-11-14 14:52:32 +00:00
const middleButtons: ButtonConfig[] = [];
//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-10-20 15:07:40 +01:00
const bottomButtons: ButtonConfig[] = [
{
id: 'help',
name: t("quickAccess.help", "Help"),
2025-11-14 14:52:32 +00:00
icon: <LocalIcon icon="help-rounded" width="1.25rem" height="1.25rem" />,
2025-10-20 15:07:40 +01:00
isRound: true,
2025-11-14 14:52:32 +00:00
size: 'md',
2025-10-20 15:07:40 +01:00
type: 'action',
onClick: () => {
2025-11-10 13:47:43 +00:00
// This will be overridden by the wrapper logic
2025-10-20 15:07:40 +01:00
},
},
2025-07-25 12:27:30 +01:00
{
id: 'config',
2025-11-14 14:52:32 +00:00
name: t("quickAccess.settings", "Settings"),
icon: <LocalIcon icon="settings-rounded" width="1.25rem" height="1.25rem" />,
size: 'md',
2025-08-04 15:01:36 +01:00
type: 'modal',
2025-07-25 12:27:30 +01:00
onClick: () => {
navigate('/settings/overview');
2025-07-25 12:27:30 +01:00
setConfigModalOpen(true);
}
}
];
return (
<div
2025-08-08 12:09:41 +01:00
ref={ref}
data-sidebar="quick-access"
2025-11-14 14:52:32 +00:00
className={`h-screen flex flex-col w-16 quick-access-bar-main ${isRainbowMode ? 'rainbow-mode' : ''}`}
>
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">
2025-10-20 15:07:40 +01:00
{/* Main navigation section */}
2025-07-25 12:27:30 +01:00
<Stack gap="lg" align="center">
2025-10-20 15:07:40 +01:00
{mainButtons.map((config, index) => (
2025-07-25 12:27:30 +01:00
<React.Fragment key={config.id}>
{renderNavButton(config, index)}
2025-10-20 15:07:40 +01:00
</React.Fragment>
))}
</Stack>
2025-08-19 13:31:09 +01:00
2025-11-14 14:52:32 +00:00
{/* Divider after main buttons (creates gap) */}
{middleButtons.length === 0 && (
<Divider
size="xs"
className="content-divider"
/>
)}
2025-10-20 15:07:40 +01:00
{/* Middle section */}
2025-11-14 14:52:32 +00:00
{middleButtons.length > 0 && (
<>
<Divider
size="xs"
className="content-divider"
/>
<Stack gap="lg" align="center">
{middleButtons.map((config, index) => (
<React.Fragment key={config.id}>
{renderNavButton(config, index)}
</React.Fragment>
))}
</Stack>
</>
)}
2025-08-19 13:31:09 +01:00
2025-10-20 15:07:40 +01:00
{/* Spacer to push bottom buttons to bottom */}
2025-07-25 12:27:30 +01:00
<div className="spacer" />
2025-08-19 13:31:09 +01:00
2025-10-20 15:07:40 +01:00
{/* Bottom section */}
<Stack gap="lg" align="center">
2025-11-10 13:47:43 +00:00
{bottomButtons.map((buttonConfig, index) => {
// Handle help button with menu or direct action
if (buttonConfig.id === 'help') {
const isAdmin = config?.isAdmin === true;
// If not admin, just show button that starts tools tour directly
if (!isAdmin) {
return (
<div
key={buttonConfig.id}
data-tour="help-button"
onClick={() => startTour('tools')}
>
{renderNavButton(buttonConfig, index)}
</div>
);
}
// If admin, show menu with both options
return (
<div key={buttonConfig.id} data-tour="help-button">
2025-11-24 16:57:36 +00:00
<Menu position={isRTL ? 'left' : 'right'} offset={10} zIndex={Z_INDEX_OVER_FULLSCREEN_SURFACE}>
2025-11-10 13:47:43 +00:00
<Menu.Target>
<div>{renderNavButton(buttonConfig, index)}</div>
</Menu.Target>
<Menu.Dropdown>
<Menu.Item
leftSection={<LocalIcon icon="view-carousel-rounded" width="1.25rem" height="1.25rem" />}
onClick={() => startTour('tools')}
>
<div>
<div style={{ fontWeight: 500 }}>
{t("quickAccess.helpMenu.toolsTour", "Tools Tour")}
</div>
<div style={{ fontSize: '0.875rem', opacity: 0.7 }}>
{t("quickAccess.helpMenu.toolsTourDesc", "Learn what the tools can do")}
</div>
</div>
</Menu.Item>
<Menu.Item
leftSection={<LocalIcon icon="admin-panel-settings-rounded" width="1.25rem" height="1.25rem" />}
onClick={() => startTour('admin')}
>
<div>
<div style={{ fontWeight: 500 }}>
{t("quickAccess.helpMenu.adminTour", "Admin Tour")}
</div>
<div style={{ fontSize: '0.875rem', opacity: 0.7 }}>
{t("quickAccess.helpMenu.adminTourDesc", "Explore admin settings & features")}
</div>
</div>
</Menu.Item>
</Menu.Dropdown>
</Menu>
</div>
);
}
2025-11-25 13:45:02 +00:00
const buttonNode = renderNavButton(buttonConfig, index);
const shouldShowSettingsBadge =
buttonConfig.id === 'config' &&
licenseAlert.active &&
licenseAlert.audience === 'admin';
2025-11-10 13:47:43 +00:00
return (
<React.Fragment key={buttonConfig.id}>
2025-11-25 13:45:02 +00:00
{shouldShowSettingsBadge ? (
<Indicator
inline
size={12}
color="orange"
position="top-end"
offset={4}
>
{buttonNode}
</Indicator>
) : (
buttonNode
)}
2025-11-10 13:47:43 +00:00
</React.Fragment>
);
})}
2025-10-20 15:07:40 +01:00
</Stack>
</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;