-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: update breadcrumbs #432
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import React from 'react'; | ||
import {Switch, Route, Redirect, Router} from 'react-router-dom'; | ||
import {Switch, Route, Redirect, Router, useLocation} from 'react-router-dom'; | ||
import cn from 'bem-cn-lite'; | ||
import {connect} from 'react-redux'; | ||
|
||
|
@@ -28,6 +28,8 @@ import {clusterTabsIds} from '../Cluster/utils'; | |
const b = cn('app'); | ||
|
||
export function Content(props) { | ||
const location = useLocation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Location was used but was not declared |
||
|
||
const {singleClusterMode} = props; | ||
const isClustersPage = | ||
location.pathname.includes('/clusters') || | ||
|
@@ -54,7 +56,7 @@ export function Content(props) { | |
}; | ||
return ( | ||
<React.Fragment> | ||
{!isClustersPage && <Header clusterName={props.clusterName} />} | ||
{!isClustersPage && <Header mainPage={props.mainPage} />} | ||
<main className={b('main')}>{renderRoute()}</main> | ||
<ReduxTooltip /> | ||
<AppIcons /> | ||
|
@@ -66,6 +68,7 @@ Content.propTypes = { | |
singleClusterMode: PropTypes.bool, | ||
children: PropTypes.node, | ||
clusterName: PropTypes.string, | ||
mainPage: PropTypes.object, | ||
}; | ||
|
||
function ContentWrapper(props) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import {useEffect, useMemo} from 'react'; | ||
import {useHistory, useLocation} from 'react-router'; | ||
import {useDispatch} from 'react-redux'; | ||
import block from 'bem-cn-lite'; | ||
import {useHistory} from 'react-router'; | ||
|
||
import {Breadcrumbs, BreadcrumbsItem} from '@gravity-ui/uikit'; | ||
import {Breadcrumbs, Icon} from '@gravity-ui/uikit'; | ||
|
||
import {ExternalLinkWithIcon} from '../../components/ExternalLinkWithIcon/ExternalLinkWithIcon'; | ||
|
||
import {backend, customBackend} from '../../store'; | ||
import {HeaderItemType} from '../../store/reducers/header'; | ||
import {getClusterInfo} from '../../store/reducers/cluster/cluster'; | ||
import {useTypedSelector} from '../../utils/hooks'; | ||
import {DEVELOPER_UI} from '../../utils/constants'; | ||
import {parseQuery} from '../../routes'; | ||
|
||
import {RawBreadcrumbItem, getBreadcrumbs} from './breadcrumbs'; | ||
|
||
import './Header.scss'; | ||
|
||
|
@@ -22,30 +27,80 @@ const getInternalLink = (singleClusterMode: boolean) => { | |
return backend + '/internal'; | ||
}; | ||
|
||
function Header() { | ||
const {singleClusterMode, header}: {singleClusterMode: boolean; header: HeaderItemType[]} = | ||
useTypedSelector((state) => state); | ||
interface HeaderProps { | ||
mainPage?: RawBreadcrumbItem; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function Header({mainPage}: HeaderProps) { | ||
const dispatch = useDispatch(); | ||
const history = useHistory(); | ||
const location = useLocation(); | ||
|
||
const renderHeader = () => { | ||
const breadcrumbItems = header.reduce((acc, el) => { | ||
const singleClusterMode = useTypedSelector((state) => state.singleClusterMode); | ||
const {page, pageBreadcrumbsOptions} = useTypedSelector((state) => state.header); | ||
const {data} = useTypedSelector((state) => state.cluster); | ||
|
||
const queryParams = parseQuery(location); | ||
|
||
const clusterNameFromQuery = queryParams.clusterName?.toString(); | ||
|
||
const clusterNameFinal = data?.Name || clusterNameFromQuery; | ||
|
||
useEffect(() => { | ||
dispatch(getClusterInfo(clusterNameFromQuery)); | ||
}, [dispatch, clusterNameFromQuery]); | ||
|
||
const breadcrumbItems = useMemo(() => { | ||
const rawBreadcrumbs: RawBreadcrumbItem[] = []; | ||
let options = pageBreadcrumbsOptions; | ||
|
||
if (mainPage) { | ||
rawBreadcrumbs.push(mainPage); | ||
} | ||
|
||
if (clusterNameFinal) { | ||
options = { | ||
...pageBreadcrumbsOptions, | ||
clusterName: clusterNameFinal, | ||
}; | ||
} | ||
|
||
const breadcrumbs = getBreadcrumbs(page, options, rawBreadcrumbs, queryParams); | ||
|
||
return breadcrumbs.map((item) => { | ||
const action = () => { | ||
if (el.link) { | ||
history.push(el.link); | ||
if (item.link) { | ||
history.push(item.link); | ||
} | ||
}; | ||
acc.push({text: el.text, action}); | ||
return acc; | ||
}, [] as BreadcrumbsItem[]); | ||
return {...item, action}; | ||
}); | ||
}, [clusterNameFinal, mainPage, history, queryParams, page, pageBreadcrumbsOptions]); | ||
|
||
const renderHeader = () => { | ||
return ( | ||
<header className={b()}> | ||
<div> | ||
<Breadcrumbs | ||
items={breadcrumbItems} | ||
lastDisplayedItemsCount={1} | ||
firstDisplayedItemsCount={1} | ||
renderItemContent={({icon, text}) => { | ||
if (!icon) { | ||
return text; | ||
} | ||
return ( | ||
<span className={b('breadcrumb')}> | ||
<Icon | ||
width={16} | ||
height={16} | ||
data={icon} | ||
className={b('breadcrumb__icon')} | ||
/> | ||
{text} | ||
</span> | ||
); | ||
}} | ||
/> | ||
</div> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import nodesRightIcon from '@gravity-ui/icons/svgs/nodes-right.svg'; | ||
import databaseIcon from '@gravity-ui/icons/svgs/database.svg'; | ||
|
||
import type { | ||
BreadcrumbsOptions, | ||
ClusterBreadcrumbsOptions, | ||
NodeBreadcrumbsOptions, | ||
Page, | ||
TabletBreadcrumbsOptions, | ||
TabletsBreadcrumbsOptions, | ||
TenantBreadcrumbsOptions, | ||
} from '../../store/reducers/header/types'; | ||
import routes, {createHref} from '../../routes'; | ||
|
||
import {getClusterPath} from '../Cluster/utils'; | ||
import {getTenantPath} from '../Tenant/TenantPages'; | ||
import {getDefaultNodePath} from '../Node/NodePages'; | ||
|
||
const prepareTenantName = (tenantName: string) => { | ||
return tenantName.startsWith('/') ? tenantName.slice(1) : tenantName; | ||
}; | ||
|
||
export interface RawBreadcrumbItem { | ||
text: string; | ||
link?: string; | ||
icon?: SVGIconData; | ||
} | ||
|
||
const getClusterBreadcrumbs = ( | ||
options: ClusterBreadcrumbsOptions, | ||
query = {}, | ||
): RawBreadcrumbItem[] => { | ||
const {clusterName, clusterTab} = options; | ||
|
||
return [ | ||
{ | ||
text: clusterName || 'Cluster', | ||
link: getClusterPath(clusterTab, query), | ||
icon: nodesRightIcon, | ||
}, | ||
]; | ||
}; | ||
|
||
const getTenantBreadcrumbs = ( | ||
options: TenantBreadcrumbsOptions, | ||
query = {}, | ||
): RawBreadcrumbItem[] => { | ||
const {tenantName} = options; | ||
|
||
const text = tenantName ? prepareTenantName(tenantName) : 'Tenant'; | ||
const link = tenantName ? getTenantPath({...query, name: tenantName}) : undefined; | ||
|
||
return [...getClusterBreadcrumbs(options, query), {text, link, icon: databaseIcon}]; | ||
}; | ||
|
||
const getNodeBreadcrumbs = (options: NodeBreadcrumbsOptions, query = {}): RawBreadcrumbItem[] => { | ||
const {tenantName, nodeId} = options; | ||
|
||
let breadcrumbs: RawBreadcrumbItem[]; | ||
|
||
// Compute nodes have tenantName, storage nodes doesn't | ||
const isStorageNode = !tenantName; | ||
|
||
if (isStorageNode) { | ||
breadcrumbs = getClusterBreadcrumbs(options, query); | ||
} else { | ||
breadcrumbs = getTenantBreadcrumbs(options, query); | ||
} | ||
|
||
const text = nodeId ? `Node ${nodeId}` : 'Node'; | ||
const link = nodeId ? getDefaultNodePath(nodeId, query) : undefined; | ||
|
||
breadcrumbs.push({text, link}); | ||
|
||
return breadcrumbs; | ||
}; | ||
|
||
const getTabletsBreadcrubms = ( | ||
options: TabletsBreadcrumbsOptions, | ||
query = {}, | ||
): RawBreadcrumbItem[] => { | ||
const {tenantName, nodeIds, state, type} = options; | ||
|
||
let breadcrumbs: RawBreadcrumbItem[]; | ||
|
||
// Cluster system tablets don't have tenantName | ||
if (tenantName) { | ||
breadcrumbs = getTenantBreadcrumbs(options, query); | ||
} else { | ||
breadcrumbs = getClusterBreadcrumbs(options, query); | ||
} | ||
|
||
const link = createHref(routes.tabletsFilters, undefined, { | ||
nodeIds, | ||
state, | ||
type, | ||
path: tenantName, | ||
}); | ||
|
||
breadcrumbs.push({text: 'Tablets', link}); | ||
|
||
return breadcrumbs; | ||
}; | ||
|
||
const getTabletBreadcrubms = ( | ||
options: TabletBreadcrumbsOptions, | ||
query = {}, | ||
): RawBreadcrumbItem[] => { | ||
const {tabletId} = options; | ||
|
||
const breadcrumbs = getTabletsBreadcrubms(options, query); | ||
|
||
breadcrumbs.push({ | ||
text: tabletId || 'Tablet', | ||
}); | ||
|
||
return breadcrumbs; | ||
}; | ||
|
||
export const getBreadcrumbs = ( | ||
page: Page, | ||
options: BreadcrumbsOptions, | ||
rawBreadcrumbs: RawBreadcrumbItem[] = [], | ||
query = {}, | ||
) => { | ||
switch (page) { | ||
case 'cluster': { | ||
return [...rawBreadcrumbs, ...getClusterBreadcrumbs(options, query)]; | ||
} | ||
case 'tenant': { | ||
return [...rawBreadcrumbs, ...getTenantBreadcrumbs(options, query)]; | ||
} | ||
case 'node': { | ||
return [...rawBreadcrumbs, ...getNodeBreadcrumbs(options, query)]; | ||
} | ||
case 'tablets': { | ||
return [...rawBreadcrumbs, ...getTabletsBreadcrubms(options, query)]; | ||
} | ||
case 'tablet': { | ||
return [...rawBreadcrumbs, ...getTabletBreadcrubms(options, query)]; | ||
} | ||
default: { | ||
return rawBreadcrumbs; | ||
} | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass these params as query for immediate breadcrumbs render on
Tablet
page