Skip to content
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

Use OpenShift console timestamp component #4117

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions frontend/src/components/ACMNotReadyWarning.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { clickByRole, waitForText } from '../lib/test-util'
import { SubscriptionOperator, SubscriptionOperatorApiVersion, SubscriptionOperatorKind } from '../resources'
import { nockIgnoreOperatorCheck } from '../lib/nock-util'
import { ACMNotReadyWarning } from './ACMNotReadyWarning'
import { PluginDataContext } from '../lib/PluginDataContext'
import { PluginContext } from '../lib/PluginContext'
import { defaultPlugin, PluginContext } from '../lib/PluginContext'

const acm_unhealthy: SubscriptionOperator = {
apiVersion: SubscriptionOperatorApiVersion,
Expand Down Expand Up @@ -88,17 +87,8 @@ describe('ACMNotReadyWarning', () => {
render(
<PluginContext.Provider
value={{
...defaultPlugin,
isACMAvailable: false,
isOverviewAvailable: true,
isSubmarinerAvailable: true,
isApplicationsAvailable: true,
isGovernanceAvailable: true,
isSearchAvailable: true,
dataContext: PluginDataContext,
acmExtensions: {},
ocpApi: {
useK8sWatchResource: () => [[] as any, true, undefined],
},
}}
>
<WrappedACMNotReadyWarning acmOperators={[acm]} />
Expand All @@ -125,17 +115,8 @@ describe('ACMNotReadyWarning', () => {
render(
<PluginContext.Provider
value={{
...defaultPlugin,
isACMAvailable: false,
isOverviewAvailable: true,
isSubmarinerAvailable: true,
isApplicationsAvailable: true,
isGovernanceAvailable: true,
isSearchAvailable: true,
dataContext: PluginDataContext,
acmExtensions: {},
ocpApi: {
useK8sWatchResource: () => [[] as any, true, undefined],
},
}}
>
<WrappedACMNotReadyWarning acmOperators={[acm]} />
Expand Down
28 changes: 8 additions & 20 deletions frontend/src/components/PluginContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* Copyright Contributors to the Open Cluster Management project */
import { isHrefNavItem, useResolvedExtensions, UseK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'
import {
isHrefNavItem,
Timestamp,
useK8sWatchResource,
useResolvedExtensions,
} from '@openshift-console/dynamic-plugin-sdk'
import { AcmTablePaginationContextProvider, AcmToastGroup, AcmToastProvider } from '../ui-components'
import { ReactNode, useMemo, useEffect, useState } from 'react'
import { ReactNode, useMemo } from 'react'
import { PluginContext } from '../lib/PluginContext'
import { useAcmExtension } from '../plugin-extensions/handler'
import { LoadingPage } from './LoadingPage'
Expand All @@ -15,9 +20,6 @@ const isPluginDataContext = (e: Extension): e is SharedContext<PluginData> =>
isSharedContext(e) && e.properties.id === 'mce-data-context'

export function PluginContextProvider(props: { children?: ReactNode }) {
const [ocpApi, setOcpApi] = useState<{ useK8sWatchResource: UseK8sWatchResource }>({
useK8sWatchResource: () => [[] as any, false, undefined],
})
const [hrefs] = useResolvedExtensions(isHrefNavItem)

const [pluginDataContexts, extensionsReady] = useResolvedExtensions(isPluginDataContext)
Expand All @@ -38,20 +40,6 @@ export function PluginContextProvider(props: { children?: ReactNode }) {
const isACMAvailable = isOverviewAvailable
const isSubmarinerAvailable = isOverviewAvailable

useEffect(() => {
const loadOCPAPI = async () => {
try {
const api = await import('@openshift-console/dynamic-plugin-sdk')
setOcpApi({
useK8sWatchResource: api.useK8sWatchResource,
})
} catch (err) {
console.error('Failed to load OCP API', err)
}
}
loadOCPAPI()
}, [])

// ACM Custom extensions
const acmExtensions = useAcmExtension()

Expand All @@ -66,7 +54,7 @@ export function PluginContextProvider(props: { children?: ReactNode }) {
isSubmarinerAvailable,
dataContext: pluginDataContext.properties.context,
acmExtensions,
ocpApi,
ocpApi: { Timestamp, useK8sWatchResource },
}}
>
<AcmFeedbackModal />
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/lib/AcmTimestamp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright Contributors to the Open Cluster Management project */

import { css } from '@emotion/css'
import { SimpleTimestamp } from './SimpleTimestamp'
import { PluginContext } from './PluginContext'
import { useContext } from 'react'
import { TimestampProps } from '@openshift-console/dynamic-plugin-sdk'

type AcmTimestampProps = TimestampProps & {
showIcon?: boolean
}

const AcmTimestamp: React.FC<AcmTimestampProps> = ({
timestamp,
simple,
omitSuffix,
className = '',
showIcon = false,
}) => {
const {
ocpApi: { Timestamp },
} = useContext(PluginContext)

return Timestamp ? (
<Timestamp
timestamp={timestamp}
simple={simple}
omitSuffix={omitSuffix}
className={`${className}${
!showIcon
? ` ${css({
'.co-icon-and-text__icon': {
display: 'none',
},
})}`
: ''
}`}
/>
) : (
<SimpleTimestamp timestamp={timestamp} />
)
}

export default AcmTimestamp
29 changes: 17 additions & 12 deletions frontend/src/lib/PluginContext.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
/* Copyright Contributors to the Open Cluster Management project */
import { Context, createContext } from 'react'
import { Context, createContext, FC } from 'react'
import { AcmExtension } from '../plugin-extensions/types'
import { PluginData, PluginDataContext } from './PluginDataContext'
import { UseK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk/lib/extensions/console-types'
import { TimestampProps, UseK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk/lib/extensions/console-types'

export const PluginContext = createContext<{
isACMAvailable?: boolean
isOverviewAvailable?: boolean
isSubmarinerAvailable?: boolean
isApplicationsAvailable?: boolean
isGovernanceAvailable?: boolean
isSearchAvailable?: boolean
export type Plugin = {
isACMAvailable: boolean
isOverviewAvailable: boolean
isSubmarinerAvailable: boolean
isApplicationsAvailable: boolean
isGovernanceAvailable: boolean
isSearchAvailable: boolean
dataContext: Context<PluginData>
acmExtensions?: AcmExtension
acmExtensions: AcmExtension
ocpApi: {
Timestamp?: FC<TimestampProps>
useK8sWatchResource: UseK8sWatchResource
}
}>({
}

export const defaultPlugin: Plugin = {
isACMAvailable: true,
isOverviewAvailable: true,
isSubmarinerAvailable: true,
Expand All @@ -28,4 +31,6 @@ export const PluginContext = createContext<{
ocpApi: {
useK8sWatchResource: () => [[] as any, true, undefined],
},
})
}

export const PluginContext = createContext<Plugin>(defaultPlugin)
24 changes: 24 additions & 0 deletions frontend/src/lib/SimpleTimestamp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright Contributors to the Open Cluster Management project */

import React from 'react'

interface SimpleTimestampProps {
timestamp: string | number | Date
}

export const SimpleTimestamp: React.FC<SimpleTimestampProps> = ({ timestamp }) => {
const date = new Date(timestamp)

let formattedDate = date.toLocaleString('en-US', {
day: '2-digit',
month: 'short',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
})

formattedDate = formattedDate.replace(' am', ' AM').replace(' pm', ' PM')
return <>{formattedDate}</>
}

export default SimpleTimestamp
6 changes: 0 additions & 6 deletions frontend/src/lib/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { act, ByRoleMatcher, ByRoleOptions, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Scope } from 'nock/types'
import { UseK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'

export const waitTimeout = 5 * 1000

Expand Down Expand Up @@ -375,11 +374,6 @@ export function isCardEnabled(card: HTMLElement) {
return card.style.cursor === 'pointer'
}

export const ocpApi: {
useK8sWatchResource: UseK8sWatchResource
} = {
useK8sWatchResource: () => [[] as any, true, undefined],
}
export const getCSVExportSpies = () => {
const blobConstructorSpy = jest.fn()
jest.spyOn(global, 'Blob').mockImplementationOnce(blobConstructorSpy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import {
subscriptionsState,
} from '../../../atoms'
import { nockIgnoreApiPaths, nockIgnoreRBAC, nockSearch } from '../../../lib/nock-util'
import { PluginContext } from '../../../lib/PluginContext'
import { PluginDataContext } from '../../../lib/PluginDataContext'
import { ocpApi, waitForText } from '../../../lib/test-util'
import { defaultPlugin, PluginContext } from '../../../lib/PluginContext'
import { waitForText } from '../../../lib/test-util'
import { ApplicationActionProps } from '../../../plugin-extensions/properties'
import { AcmExtension } from '../../../plugin-extensions/types'
import { GetMessagesDocument, SearchSchemaDocument } from '../../Search/search-sdk/search-sdk'
Expand Down Expand Up @@ -372,9 +371,8 @@ describe('Applications Page', () => {
<MockedProvider mocks={mocks}>
<PluginContext.Provider
value={{
...defaultPlugin,
acmExtensions: acmExtension,
dataContext: PluginDataContext,
ocpApi,
}}
>
<ApplicationDetailsPage />
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/routes/Applications/Overview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import {
nockSearch,
nockAggegateRequest,
} from '../../lib/nock-util'
import { PluginContext } from '../../lib/PluginContext'
import { PluginDataContext } from '../../lib/PluginDataContext'
import { ocpApi, waitForText } from '../../lib/test-util'
import { defaultPlugin, PluginContext } from '../../lib/PluginContext'
import { waitForText } from '../../lib/test-util'
import {
ApplicationKind,
ApplicationSetKind,
Expand Down Expand Up @@ -116,9 +115,8 @@ describe('Applications Page', () => {
<MemoryRouter>
<PluginContext.Provider
value={{
...defaultPlugin,
acmExtensions: acmExtension,
dataContext: PluginDataContext,
ocpApi,
}}
>
<Routes>
Expand Down
23 changes: 3 additions & 20 deletions frontend/src/routes/Home/Overview/Overview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { render } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom-v5-compat'
import { RecoilRoot } from 'recoil'
import { nockGet, nockIgnoreApiPaths } from '../../../lib/nock-util'
import { PluginContext } from '../../../lib/PluginContext'
import { PluginDataContext } from '../../../lib/PluginDataContext'
import { defaultPlugin, PluginContext } from '../../../lib/PluginContext'
import { clickByText, waitForNocks, waitForText } from '../../../lib/test-util'
import Overview from './Overview'
import { getAddonRequest, getAddonResponse } from './Overview.sharedmocks'
Expand All @@ -29,13 +28,8 @@ it('should render overview page with extension', async () => {
<MockedProvider mocks={[]}>
<PluginContext.Provider
value={{
...defaultPlugin,
isACMAvailable: false,
isOverviewAvailable: true,
isSubmarinerAvailable: true,
isApplicationsAvailable: true,
isGovernanceAvailable: true,
isSearchAvailable: true,
dataContext: PluginDataContext,
acmExtensions: {
overviewTab: [
{
Expand All @@ -50,9 +44,6 @@ it('should render overview page with extension', async () => {
},
],
},
ocpApi: {
useK8sWatchResource: () => [[] as any, true, undefined],
},
}}
>
<Overview />
Expand Down Expand Up @@ -81,13 +72,8 @@ it('should render overview page layout when extension tab crashes', async () =>
<MockedProvider mocks={[]}>
<PluginContext.Provider
value={{
...defaultPlugin,
isACMAvailable: false,
isOverviewAvailable: true,
isSubmarinerAvailable: true,
isApplicationsAvailable: true,
isGovernanceAvailable: true,
isSearchAvailable: true,
dataContext: PluginDataContext,
acmExtensions: {
overviewTab: [
{
Expand All @@ -104,9 +90,6 @@ it('should render overview page layout when extension tab crashes', async () =>
},
],
},
ocpApi: {
useK8sWatchResource: () => [[] as any, true, undefined],
},
}}
>
<Overview />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import {
nockNamespacedList,
nockPatch,
} from '../../../../../lib/nock-util'
import { PluginContext } from '../../../../../lib/PluginContext'
import { PluginDataContext } from '../../../../../lib/PluginDataContext'
import { defaultPlugin, PluginContext } from '../../../../../lib/PluginContext'
import { mockGlobalManagedClusterSet, mockManagedClusterSet } from '../../../../../lib/test-metadata'
import {
clearByTestId,
Expand All @@ -39,7 +38,6 @@ import {
waitForNotText,
waitForTestId,
waitForText,
ocpApi,
} from '../../../../../lib/test-util'
import { NavigationPath } from '../../../../../NavigationPath'
import {
Expand Down Expand Up @@ -2084,7 +2082,7 @@ describe('ClusterSetDetails page without Submariner', () => {
nockIgnoreRBAC()
nockIgnoreApiPaths()
render(
<PluginContext.Provider value={{ isSubmarinerAvailable: false, dataContext: PluginDataContext, ocpApi }}>
<PluginContext.Provider value={{ ...defaultPlugin, isSubmarinerAvailable: false }}>
<Component />
</PluginContext.Provider>
)
Expand All @@ -2108,7 +2106,7 @@ describe('ClusterSetDetails page global clusterset', () => {
nockIgnoreRBAC()
nockIgnoreApiPaths()
render(
<PluginContext.Provider value={{ isSubmarinerAvailable: false, dataContext: PluginDataContext, ocpApi }}>
<PluginContext.Provider value={{ ...defaultPlugin, isSubmarinerAvailable: false }}>
<Component isGlobal />
</PluginContext.Provider>
)
Expand Down
Loading