-
Notifications
You must be signed in to change notification settings - Fork 24
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
CNV-29444: Feature guide user to pipelines #1911
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { | ||
getBestMatch, | ||
groupQuickStartsByName, | ||
} from '@kubevirt-utils/hooks/useQuickStarts/utils/utils'; | ||
import { QuickStartModel } from '@kubevirt-utils/models'; | ||
import { | ||
getGroupVersionKindForModel, | ||
useK8sWatchResource, | ||
WatchK8sResult, | ||
} from '@openshift-console/dynamic-plugin-sdk'; | ||
import { getDisabledQuickStarts, QuickStart } from '@patternfly/quickstarts'; | ||
|
||
const useQuickStarts = (filterDisabledQuickStarts = true): WatchK8sResult<QuickStart[]> => { | ||
const preferredLanguage = useTranslation().i18n.language; | ||
|
||
const [quickStarts, quickStartsLoaded, quickStartsError] = useK8sWatchResource<QuickStart[]>({ | ||
groupVersionKind: getGroupVersionKindForModel(QuickStartModel), | ||
isList: true, | ||
}); | ||
|
||
const bestMatchQuickStarts = useMemo(() => { | ||
if (!quickStartsLoaded) { | ||
return []; | ||
} | ||
const groupedQuickStarts = groupQuickStartsByName(quickStarts); | ||
|
||
if (filterDisabledQuickStarts) { | ||
const disabledQuickStarts = getDisabledQuickStarts(); | ||
disabledQuickStarts.forEach((quickStartName) => delete groupedQuickStarts[quickStartName]); | ||
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. instead of delete and modifing the obj u can filter and get a new obj. from running on one obj u modify a different obj. better to work on groupedQuickStarts and filter on it |
||
} | ||
|
||
return Object.values(groupedQuickStarts).map((quickStartsByName) => | ||
getBestMatch(quickStartsByName, preferredLanguage), | ||
); | ||
}, [quickStarts, quickStartsLoaded, filterDisabledQuickStarts, preferredLanguage]); | ||
|
||
return [bestMatchQuickStarts, quickStartsLoaded, quickStartsError]; | ||
}; | ||
|
||
export default useQuickStarts; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const LOCALIZATION_NAME_LABEL = 'console.openshift.io/name'; | ||
export const LOCALIZATION_LANGUAGE_LABEL = 'console.openshift.io/lang'; | ||
export const LOCALIZATION_COUNTRY_LABEL = 'console.openshift.io/country'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
LOCALIZATION_COUNTRY_LABEL, | ||
LOCALIZATION_LANGUAGE_LABEL, | ||
LOCALIZATION_NAME_LABEL, | ||
} from '@kubevirt-utils/hooks/useQuickStarts/utils/constants'; | ||
import { QuickStart } from '@patternfly/quickstarts'; | ||
|
||
export const getQuickStartNameRef = (quickStart: QuickStart) => | ||
quickStart.metadata.labels?.[LOCALIZATION_NAME_LABEL] || | ||
quickStart.metadata.annotations?.[LOCALIZATION_NAME_LABEL] || | ||
quickStart.metadata.name; | ||
|
||
export const groupQuickStartsByName = (quickStarts: QuickStart[]) => { | ||
return quickStarts.reduce<Record<string, QuickStart[]>>((grouped, quickStart) => { | ||
const name = getQuickStartNameRef(quickStart); | ||
if (!grouped[name]) grouped[name] = []; | ||
grouped[name].push(quickStart); | ||
return grouped; | ||
}, {}); | ||
}; | ||
|
||
/** | ||
* Returns the QuickStart with the best localization match, for the given | ||
* preferred language and preferred country. It prefers a match in this order: | ||
* | ||
* 1. QuickStart language and country are equal to the preferred language and country. | ||
* This includes sample without language (fallbacks to en) and no country. | ||
* | ||
* 2. QuickStart language is equal to the preferred language. | ||
* 1. And the quick starts has no country defined. (eg, select en quick starts is used for en-CA and en-GB) | ||
* 2. Any country is defined. (eg, select en-CA quick starts is used for en-GB) | ||
* | ||
* 3. Fallback to an english quick starts | ||
* (QuickStart language is en OR quick starts language is not defined): | ||
* 1. Same country (use en-CA quick starts if preference is fr-CA) | ||
* 2. No country | ||
* 3. Any country (use en-CA quick starts if preference is en-US) | ||
* @param quickStarts QuickStart custom resources | ||
* @param language Language code | ||
*/ | ||
export const getBestMatch = (quickStarts: QuickStart[], language: string): null | QuickStart => { | ||
if (!quickStarts || !quickStarts.length) { | ||
return null; | ||
} | ||
const preferredLanguage = (language || 'en').split('-')[0].toLowerCase(); | ||
const preferredCountry = ((language || '').split('-')[1] || '').toUpperCase(); | ||
|
||
let sameLanguageWithoutCountry: QuickStart = null; | ||
let sameLanguageWithAnyCountry: QuickStart = null; | ||
let fallbackLanguageSameCountry: QuickStart = null; | ||
let fallbackLanguageNoCountry: QuickStart = null; | ||
let fallbackLanguageAnyCountry: QuickStart = null; | ||
|
||
for (const quickStart of quickStarts) { | ||
const quickStartLanguage = ( | ||
quickStart.metadata?.labels?.[LOCALIZATION_LANGUAGE_LABEL] || 'en' | ||
).toLowerCase(); | ||
const quickStartCountry = ( | ||
quickStart.metadata?.labels?.[LOCALIZATION_COUNTRY_LABEL] || '' | ||
).toUpperCase(); | ||
|
||
if (quickStartLanguage === preferredLanguage && quickStartCountry === preferredCountry) { | ||
return quickStart; | ||
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. here u are returning the first match, and all other checks u are returning the last match? 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. This code is also from console. I'll clean it up as well if we end up using it. |
||
} | ||
if (quickStartLanguage === preferredLanguage) { | ||
if (!quickStartCountry && !sameLanguageWithoutCountry) { | ||
sameLanguageWithoutCountry = quickStart; | ||
} else if (quickStartCountry && !sameLanguageWithAnyCountry) { | ||
sameLanguageWithAnyCountry = quickStart; | ||
} | ||
} | ||
if (quickStartLanguage === 'en') { | ||
if (quickStartCountry === preferredCountry && !fallbackLanguageSameCountry) { | ||
fallbackLanguageSameCountry = quickStart; | ||
} else if (!quickStartCountry && !fallbackLanguageNoCountry) { | ||
fallbackLanguageNoCountry = quickStart; | ||
} else if (!fallbackLanguageAnyCountry) { | ||
fallbackLanguageAnyCountry = quickStart; | ||
} | ||
} | ||
} | ||
Comment on lines
+65
to
+81
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. Please consider changing the mechanism to a more readable one, this if, if else if isn't something we usually do. |
||
return ( | ||
sameLanguageWithoutCountry || | ||
sameLanguageWithAnyCountry || | ||
fallbackLanguageSameCountry || | ||
fallbackLanguageNoCountry || | ||
fallbackLanguageAnyCountry | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.add-bootable-volume-link__inline-text { | ||
padding-left: 0; | ||
padding-right: 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { useInstanceTypeVMStore } from '@catalog/CreateFromInstanceTypes/state/useInstanceTypeVMStore'; | ||
import AddBootableVolumeModal from '@kubevirt-utils/components/AddBootableVolumeModal/AddBootableVolumeModal'; | ||
import { useModal } from '@kubevirt-utils/components/ModalProvider/ModalProvider'; | ||
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation'; | ||
import useCanCreateBootableVolume from '@kubevirt-utils/resources/bootableresources/hooks/useCanCreateBootableVolume'; | ||
import { Button, ButtonVariant } from '@patternfly/react-core'; | ||
|
||
import { getOSImagesNS } from '../../../../clusteroverview/OverviewTab/inventory-card/utils/utils'; | ||
|
||
import './AddBootableVolumeLink.scss'; | ||
|
||
type AddBootableVolumeLinkProps = { | ||
hidePopover?: () => void; | ||
loadError: Error; | ||
text?: string; | ||
}; | ||
|
||
const AddBootableVolumeLink: FC<AddBootableVolumeLinkProps> = ({ | ||
hidePopover, | ||
loadError, | ||
text, | ||
}) => { | ||
const { t } = useKubevirtTranslation(); | ||
const { createModal } = useModal(); | ||
const { onSelectCreatedVolume } = useInstanceTypeVMStore(); | ||
|
||
const sourceNamespace = getOSImagesNS(); | ||
const { canCreateDS, canCreatePVC } = useCanCreateBootableVolume(sourceNamespace); | ||
const canCreate = canCreateDS || canCreatePVC; | ||
|
||
return ( | ||
<Button | ||
onClick={() => { | ||
hidePopover?.(); | ||
createModal((props) => ( | ||
<AddBootableVolumeModal | ||
enforceNamespace={sourceNamespace} | ||
onCreateVolume={onSelectCreatedVolume} | ||
{...props} | ||
/> | ||
)); | ||
}} | ||
className="add-bootable-volume-link__inline-text" | ||
isDisabled={!!loadError || !canCreate} | ||
variant={ButtonVariant.link} | ||
> | ||
{text || t('Add Volume')} | ||
</Button> | ||
); | ||
}; | ||
|
||
export default AddBootableVolumeLink; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.bootable-volume-empty-state { | ||
&__icon-bar { | ||
margin-top: var(--pf-v5-global--spacer--xl); | ||
} | ||
|
||
&__icon-bar :nth-child(2) { | ||
margin-left: var(--pf-v5-global--spacer--2xl); | ||
margin-right: var(--pf-v5-global--spacer--2xl); | ||
} | ||
|
||
&__icon { | ||
height: 50px; | ||
} | ||
|
||
&__title { | ||
margin-bottom: var(--pf-v5-global--spacer--md); | ||
} | ||
} |
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.
its nicer to deconstruct on left side then the right when using hooks
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.
I'm not sure what you mean. Could you explain a bit more and explain why?
Also, the code in this file is from console. I copied it over in my attempt to get quick starts working. If we end up having to use it I'll clean it up.