Refactor DashboardBreadcrumb component to enhance breadcrumb functionality

- Introduced a new segmentToModel function to dynamically retrieve model names based on URL segments.
- Simplified breadcrumb name mapping by removing unused entries and adding support for pluralized model labels.
- Improved breadcrumb item rendering logic for better clarity and maintainability.
This commit is contained in:
Tom Butcher 2025-08-18 00:59:47 +01:00
parent 177b439c48
commit 653e4e6ab2

View File

@ -4,6 +4,7 @@ import { Breadcrumb, Button, Flex, Space } from 'antd'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import ArrowLeftIcon from '../../Icons/ArrowLeftIcon'
import ArrowRightIcon from '../../Icons/ArrowRightIcon'
import { getModelByName } from '../../../database/ObjectModels'
const breadcrumbNameMap = {
production: 'Production',
@ -11,30 +12,8 @@ const breadcrumbNameMap = {
management: 'Management',
developer: 'Developer',
overview: 'Overview',
printers: 'Printers',
hosts: 'Hosts',
control: 'Control',
info: 'Info',
jobs: 'Jobs',
subjobs: 'Sub Jobs',
gcodefiles: 'G Code Files',
filaments: 'Filaments',
parts: 'Parts',
products: 'Products',
vendors: 'Vendors',
materials: 'Materials',
notetypes: 'Note Types',
users: 'Users',
settings: 'Settings',
auditlogs: 'Audit Logs',
filamentstocks: 'Filament Stocks',
partstocks: 'Part Stocks',
productstocks: 'Products',
stockevents: 'Stock Events',
stockaudits: 'Stock Audits',
sessionstorage: 'Session Storage',
authcontextdebug: 'Auth Context Debug',
printservercontextdebug: 'Print Server Context Debug'
design: 'Design'
}
const mainSections = ['production', 'inventory', 'management', 'developer']
@ -44,15 +23,39 @@ const DashboardBreadcrumb = () => {
const navigate = useNavigate()
const pathSnippets = location.pathname.split('/').filter((i) => i)
function segmentToModel(segment) {
if (segment) {
// If segment ends with 's', remove it and get the model
if (segment.endsWith('s')) {
const singularSegment = segment.slice(0, -1)
return getModelByName(singularSegment, true)
}
// Otherwise, get the model as is
return getModelByName(segment, true)
}
return null
}
const breadcrumbItems = pathSnippets.map((segment, index) => {
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`
if (segment !== 'dashboard') {
const isMainSection = mainSections.includes(segment)
const name = breadcrumbNameMap[segment] || segment
const model = segmentToModel(segment)
const modelLabelPlural = model?.label ? `${model.label}s` : null
const name = breadcrumbNameMap[segment] || modelLabelPlural || segment
if (isMainSection) {
return {
title: (
<span style={{ padding: '0 12px' }} key={segment}>
{name}
</span>
),
key: segment
}
}
return {
title: isMainSection ? (
<span style={{ padding: '0 12px' }}>{name}</span>
) : (
title: (
<Link to={url} style={{ padding: '0 12px' }}>
{name}
</Link>