forked from RedHatInsights/frontend-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Insights wrapper hooks and components for router v6
- Loading branch information
Showing
16 changed files
with
139 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import { Link, LinkProps } from 'react-router-dom'; | ||
import useChrome from '../useChrome'; | ||
import { buildInsightsPath } from '@redhat-cloud-services/frontend-components-utilities/helpers/urlPathHelpers'; | ||
|
||
interface InsightsLinkProps { | ||
to: LinkProps['to']; | ||
app: string; | ||
} | ||
|
||
const InsightsLink: React.FC<InsightsLinkProps> = ({ to, app, ...props }) => { | ||
const chrome = useChrome(); | ||
const toPath = buildInsightsPath(chrome, app, to); | ||
|
||
return ( | ||
<Link to={toPath} {...props}> | ||
{props.children} | ||
</Link> | ||
); | ||
}; | ||
|
||
export default InsightsLink; |
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,2 @@ | ||
export { default } from './InsightsLink'; | ||
export { default as InsightsLink } from './InsightsLink'; |
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @ts-nocheck | ||
import React, { useMemo } from 'react'; | ||
import * as reactRouter from 'react-router-dom'; | ||
import useChrome from '../useChrome'; | ||
|
||
const WithReactRouterHistory = ({ Component, ...props }) => { | ||
const history = reactRouter.useHistory(); | ||
|
||
return <Component history={history} {...props} />; | ||
}; | ||
|
||
const WithChromeHistory = ({ Component, ...props }) => { | ||
const { chromeHistory } = useChrome(); | ||
|
||
return <Component history={chromeHistory} {...props} />; | ||
}; | ||
|
||
const WithHistory = ({ Component, ...props }, ref) => { | ||
const HistoryComponent = useMemo( | ||
() => (typeof reactRouter.useHistory === 'function' ? WithReactRouterHistory : WithChromeHistory), | ||
[Component, props] | ||
); | ||
|
||
return <HistoryComponent innerRef={ref} Component={Component} {...props} />; | ||
}; | ||
|
||
export default React.forwardRef(WithHistory); |
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,3 +1,2 @@ | ||
export * from './useChrome'; | ||
export { default } from './useChrome'; | ||
export { default as useChrome } from './useChrome'; |
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,2 +1,3 @@ | ||
export * as default from './helpers'; | ||
export * from './helpers'; | ||
export * from './urlPathHelpers'; |
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,18 @@ | ||
import { buildInsightsPath } from './urlPathHelpers'; | ||
|
||
const fakeChrome = { | ||
isBeta: jest.fn(() => false), | ||
getBundle: jest.fn(() => 'insights'), | ||
getApp: jest.fn(() => 'compliance'), | ||
}; | ||
|
||
describe('buildInsightsPath', () => { | ||
it('should return a path', () => { | ||
expect(buildInsightsPath(fakeChrome, 'compliance', '/scappolicies')).toEqual('/insights/compliance/scappolicies'); | ||
}); | ||
|
||
it('should return a preview path when isBeta is true', () => { | ||
fakeChrome.isBeta.mockImplementation(() => true); | ||
expect(buildInsightsPath(fakeChrome, 'compliance', '/scappolicies')).toEqual('/preview/insights/compliance/scappolicies'); | ||
}); | ||
}); |
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,17 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import { ChromeAPI } from '@redhat-cloud-services/types'; | ||
|
||
export const buildInsightsPath = (chrome: ChromeAPI, app: string, toProp: any) => { | ||
const inAppPath = (typeof toProp === 'object' ? toProp.pathname : toProp) || ''; | ||
const isAbsolutePath = /^\//.test(inAppPath); | ||
const environmentPath = chrome.isBeta() ? '/preview' : ''; | ||
const appPath = app || chrome.getApp(); | ||
const pathname = isAbsolutePath ? [environmentPath, chrome.getBundle(), appPath, inAppPath.replace(/^\//, '')].join('/') : inAppPath; | ||
|
||
return typeof toProp === 'object' | ||
? { | ||
...toProp, | ||
pathname, | ||
} | ||
: pathname; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './useInsightsNavigate'; | ||
export * from './useInsightsNavigate'; |
14 changes: 14 additions & 0 deletions
14
packages/utils/src/useInsightsNavigate/useInsightsNavigate.ts
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,14 @@ | ||
// @ts-ignore | ||
import { useNavigate } from 'react-router-dom'; | ||
// @ts-ignore | ||
import useChrome from '@redhat-cloud-services/frontend-components/useChrome'; | ||
import { buildInsightsPath } from '../helpers/urlPathHelpers'; | ||
|
||
const useInsightsNavigate = (app: string) => { | ||
const navigate = useNavigate(); | ||
const chrome = useChrome(); | ||
|
||
return (to: any) => navigate(buildInsightsPath(chrome, app, to)); | ||
}; | ||
|
||
export default useInsightsNavigate; |