-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hycu): breadcrumb and order links
ref: MANAGER-15780 Signed-off-by: Thibaud Crespin <[email protected]>
- Loading branch information
Thibaud Crespin
committed
Nov 13, 2024
1 parent
7414cd5
commit d9ddf23
Showing
7 changed files
with
70 additions
and
37 deletions.
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
62 changes: 32 additions & 30 deletions
62
packages/manager/apps/hycu/src/hooks/breadcrumb/useBreadcrumb.tsx
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,49 +1,51 @@ | ||
import { useEffect, useState, useContext } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { ShellContext } from '@ovh-ux/manager-react-shell-client'; | ||
import { useEffect, useState } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { urls } from '@/routes/routes.constant'; | ||
|
||
export type BreadcrumbItem = { | ||
id: string; | ||
label: string | undefined; | ||
href?: string; | ||
onClick?: (event?: BreadcrumbEvent) => void; | ||
}; | ||
|
||
type BreadcrumbEvent = Event & { | ||
target: { isCollapsed?: boolean; isLast?: boolean }; | ||
}; | ||
|
||
export interface BreadcrumbProps { | ||
rootLabel?: string; | ||
appName?: string; | ||
projectId?: string; | ||
items?: BreadcrumbItem[]; | ||
} | ||
|
||
export const useBreadcrumb = ({ rootLabel, appName }: BreadcrumbProps) => { | ||
const { shell } = useContext(ShellContext); | ||
const [root, setRoot] = useState<BreadcrumbItem[]>([]); | ||
export const useBreadcrumb = ({ rootLabel, items }: BreadcrumbProps) => { | ||
const [paths, setPaths] = useState<BreadcrumbItem[]>([]); | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
const pathnames = location.pathname.split('/').filter((x) => x); | ||
|
||
useEffect(() => { | ||
const fetchRoot = async () => { | ||
try { | ||
const response = await shell?.navigation.getURL(appName, '#/', {}); | ||
const rootItem = { | ||
label: rootLabel, | ||
href: String(response), | ||
}; | ||
setRoot([rootItem]); | ||
} catch { | ||
// Fetch navigation error | ||
} | ||
}; | ||
fetchRoot(); | ||
}, [rootLabel, appName, shell?.navigation]); | ||
const root: BreadcrumbItem = { | ||
id: rootLabel, | ||
label: rootLabel, | ||
onClick: () => navigate(urls.root), | ||
}; | ||
|
||
useEffect(() => { | ||
const pathsTab = pathnames.map((value) => ({ | ||
label: value, | ||
href: `/#/${appName}/${value}`, | ||
})); | ||
const pathsTab = pathnames.map((value, index) => { | ||
const item = items?.find(({ id }) => id === value); | ||
|
||
return { | ||
id: item?.id ?? value, | ||
label: item?.label ?? value, | ||
onClick: (event: BreadcrumbEvent) => { | ||
const { isCollapsed, isLast } = event.target; | ||
if (!isCollapsed && !isLast) { | ||
navigate(`/${pathnames.slice(0, index + 1).join('/')}`); | ||
} | ||
}, | ||
}; | ||
}); | ||
setPaths(pathsTab); | ||
}, [location]); | ||
}, [location, items]); | ||
|
||
return [...root, ...paths]; | ||
return [root, ...paths]; | ||
}; |
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
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,13 +1,13 @@ | ||
export const subRoutes = { | ||
onboarding: 'onboarding', | ||
order: 'order', | ||
serviceName: ':serviceName', | ||
}; | ||
|
||
export const urls = { | ||
root: '/', | ||
onboarding: `/${subRoutes.onboarding}`, | ||
listing: '/', | ||
onboarding: `/${subRoutes.onboarding}`, | ||
order: `/${subRoutes.order}`, | ||
dashboard: `/${subRoutes.serviceName}`, | ||
tab2: 'Tab2', | ||
order: '/order', | ||
} as const; |