-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Add community modules #3947
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
fbea11c
Work in progress
akucharska db6439e
Merge branch 'main' into add-community-modules
akucharska e15471a
Fix refreshing with create opened
akucharska bfee381
Fix refreshing with create opened
akucharska f63fbb0
Show add community modules form
akucharska d1eb91a
Have version
akucharska d419b9d
prepare list of resourcesToAply
akucharska 2e18258
Partial commit without backend
akucharska 2f5e55f
Uplode yamls
akucharska 0e1a1e4
Merge branch 'main' into add-community-modules
akucharska 49049a2
Clean up
akucharska 5a4407d
Clean up
akucharska dccad49
Remove duplicated function
akucharska 2b7b101
Move getModulesAddData to separate file
akucharska 0a9f2ad
Show form id createType is right
akucharska e64de12
Move translations
akucharska ff3049d
Almost working
akucharska ec4e035
Fix delete
akucharska 655cc0c
Checkbox and clean up
akucharska f787f14
clean up
akucharska e5dfef8
clean up
akucharska dead59e
prettier
akucharska 429cf07
rename
akucharska aaa27cc
rename
akucharska 946a4c0
cleanup
akucharska 0b0a5d1
clean up
akucharska 8472ffc
remove file
akucharska dfe5231
Merge branch 'main' into add-community-modules
akucharska 6f1b976
cleanup after merge
akucharska 2af0277
Review changes
akucharska 30cb2f7
Fix delete
akucharska 7a37336
review changes
akucharska 38b68a9
Rename component, combine getInstalledModules
akucharska e180fb1
Fix deleting if no associatedResourceLeft
akucharska 367fde9
Fix install
akucharska 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 hidden or 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
339 changes: 339 additions & 0 deletions
339
src/components/KymaModules/CommunityModulesAddModule.tsx
This file contains hidden or 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,339 @@ | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { useNavigate } from 'react-router'; | ||
| import { useRecoilState } from 'recoil'; | ||
| import { useFeature } from 'hooks/useFeature'; | ||
| import { columnLayoutState } from 'state/columnLayoutAtom'; | ||
| import { ResourceForm } from 'shared/ResourceForm'; | ||
| import { MessageStrip } from '@ui5/webcomponents-react'; | ||
| import { Spinner } from 'shared/components/Spinner/Spinner'; | ||
| import { | ||
| fetchResourcesToApply, | ||
| getAvailableCommunityModules, | ||
| VersionInfo, | ||
| } from 'components/KymaModules/components/communityModulesHelpers'; | ||
| import { | ||
| getModuleName, | ||
| ModuleTemplateListType, | ||
| ModuleTemplateType, | ||
| } from 'components/KymaModules/support'; | ||
| import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
| import { UnsavedMessageBox } from 'shared/components/UnsavedMessageBox/UnsavedMessageBox'; | ||
| import { createPortal } from 'react-dom'; | ||
| import { SetterOrUpdater, useSetRecoilState } from 'recoil'; | ||
| import { isResourceEditedState } from 'state/resourceEditedAtom'; | ||
| import { useUploadResources } from 'resources/Namespaces/YamlUpload/useUploadResources'; | ||
| import { usePost } from 'shared/hooks/BackendAPI/usePost'; | ||
| import { CommunityModuleContext } from 'components/KymaModules/providers/CommunityModuleProvider'; | ||
| import CommunityModuleCard from 'components/KymaModules/components/CommunityModuleCard'; | ||
|
|
||
| import { useNotification } from 'shared/contexts/NotificationContext'; | ||
| import { ModuleTemplatesContext } from 'components/KymaModules/providers/ModuleTemplatesProvider'; | ||
|
|
||
| import './KymaModulesAddModule.scss'; | ||
| type VersionDisplayInfo = { | ||
| moduleTemplate: { | ||
| name: string; | ||
| namespace: string; | ||
| }; | ||
| version: string; | ||
| channel: string; | ||
| installed: boolean; | ||
| textToDisplay: string; | ||
| beta?: boolean; | ||
| icon?: { link: string; name: string }; | ||
| docsURL?: string; | ||
| }; | ||
| type ModuleDisplayInfo = { | ||
| name: string; | ||
| versions: VersionDisplayInfo[]; | ||
| }; | ||
|
|
||
| function onVersionChange( | ||
| moduleTemplates: ModuleTemplateListType, | ||
| moduleTemplatesToApply: Map<string, ModuleTemplateType>, | ||
| setModulesTemplatesToApply: SetterOrUpdater<Map<string, ModuleTemplateType>>, | ||
| setIsResourceEdited: SetterOrUpdater<any>, | ||
| ): any { | ||
| return (value: string, shouldRemove: boolean) => { | ||
| const newModulesTemplatesToApply = new Map(moduleTemplatesToApply); | ||
|
|
||
| const [name, namespace] = value.split('|'); | ||
| const newModuleTemplateToApply = moduleTemplates.items.find( | ||
| item => | ||
| item.metadata.namespace === namespace && item.metadata.name === name, | ||
| ); | ||
| if (!newModuleTemplateToApply) { | ||
| console.warn(`Can't find module template`); | ||
| return; | ||
| } | ||
|
|
||
| let moduleName = getModuleName(newModuleTemplateToApply); | ||
|
|
||
| if (shouldRemove) { | ||
| newModulesTemplatesToApply.delete(moduleName); | ||
| } else if (newModuleTemplateToApply) { | ||
| newModulesTemplatesToApply.set( | ||
| getModuleName(newModuleTemplateToApply), | ||
| newModuleTemplateToApply, | ||
| ); | ||
| } | ||
| if (newModulesTemplatesToApply.size === 0) { | ||
| setIsResourceEdited({ | ||
| isEdited: false, | ||
| }); | ||
| } else { | ||
| setIsResourceEdited({ | ||
| isEdited: true, | ||
| }); | ||
| } | ||
|
|
||
| setModulesTemplatesToApply(newModulesTemplatesToApply); | ||
| }; | ||
| } | ||
|
|
||
| function transformDataForDisplay( | ||
| availableCommunityModules: Map<string, VersionInfo[]>, | ||
| ): ModuleDisplayInfo[] { | ||
| return Array.from(availableCommunityModules, ([moduleName, versions]) => { | ||
| const formatDisplayText = (v: VersionInfo): string => { | ||
| const version = `${v.channel ? v.channel + ' ' : ''}(v${v.version})`; | ||
| return version; | ||
| }; | ||
|
|
||
| return { | ||
| name: moduleName, | ||
| versions: versions.map(v => ({ | ||
| moduleTemplate: { | ||
| name: v.moduleTemplateName, | ||
| namespace: v.moduleTemplateNamespace, | ||
| }, | ||
| version: v.version, | ||
| channel: v.channel ?? '', | ||
| installed: v.installed ?? false, | ||
| textToDisplay: formatDisplayText(v), | ||
| beta: v.beta, | ||
| icon: v.icon, | ||
| docsURL: v.docsURL, | ||
| })), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| export default function CommunityModulesAddModule(props: any) { | ||
| const { t } = useTranslation(); | ||
| const navigate = useNavigate(); | ||
| const { isEnabled: isCommunityModulesEnabled } = useFeature( | ||
| 'COMMUNITY_MODULES', | ||
| ); | ||
| const notification = useNotification(); | ||
| const post = usePost(); | ||
| const setIsResourceEdited = useSetRecoilState(isResourceEditedState); | ||
| const [resourcesToApply, setResourcesToApply] = useState<{ value: any }[]>( | ||
| [], | ||
| ); | ||
| const [layoutColumn, setLayoutColumn] = useRecoilState(columnLayoutState); | ||
|
|
||
| const uploadResources = useUploadResources( | ||
| resourcesToApply, | ||
| setResourcesToApply, | ||
| () => {}, | ||
| 'default', | ||
| ); | ||
| const [ | ||
| communityModulesTemplatesToApply, | ||
| setCommunityModulesTemplatesToApply, | ||
| ] = useState(new Map<string, ModuleTemplateType>()); | ||
|
|
||
| const { | ||
| moduleTemplatesLoading, | ||
| moduleReleaseMetasLoading, | ||
| moduleReleaseMetas, | ||
| } = useContext(ModuleTemplatesContext); | ||
| const { | ||
| installedCommunityModules, | ||
| notInstalledCommunityModuleTemplates, | ||
| installedCommunityModulesLoading: notInstalledCommunityModulesLoading, | ||
| } = useContext(CommunityModuleContext); | ||
|
|
||
| const availableCommunityModules = useMemo(() => { | ||
| if (!moduleReleaseMetasLoading && notInstalledCommunityModuleTemplates) { | ||
| return getAvailableCommunityModules( | ||
| notInstalledCommunityModuleTemplates, | ||
| {} as ModuleTemplateListType, | ||
| moduleReleaseMetas, | ||
| ); | ||
| } else { | ||
| return new Map(); | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [ | ||
| notInstalledCommunityModuleTemplates, | ||
| installedCommunityModules, | ||
| moduleReleaseMetas, | ||
| moduleReleaseMetasLoading, | ||
| ]); | ||
|
|
||
| useEffect(() => { | ||
| fetchResourcesToApply( | ||
| communityModulesTemplatesToApply, | ||
| setResourcesToApply, | ||
| post, | ||
| ); | ||
| }, [communityModulesTemplatesToApply]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
|
||
| const [columnsCount, setColumnsCount] = useState(2); | ||
| const [ | ||
| cardsContainerRef, | ||
| setCardsContainerRef, | ||
| ] = useState<HTMLDivElement | null>(null); | ||
|
|
||
| const calculateColumns = useCallback(() => { | ||
| if (cardsContainerRef?.clientWidth) { | ||
| const containerWidth = cardsContainerRef?.clientWidth; | ||
| const cardWidth = 350; | ||
| const gap = 16; | ||
| const colNumber = Math.max( | ||
| 1, | ||
| Math.floor((containerWidth + gap) / (cardWidth + gap)), | ||
| ); | ||
| return colNumber; | ||
| } | ||
| return 2; | ||
| }, [cardsContainerRef]); | ||
|
|
||
| useEffect(() => { | ||
| const resizeObserver = new ResizeObserver(() => { | ||
| setColumnsCount(calculateColumns()); | ||
| }); | ||
|
|
||
| if (cardsContainerRef) { | ||
| resizeObserver.observe(cardsContainerRef); | ||
| } | ||
|
|
||
| return () => { | ||
| if (cardsContainerRef) { | ||
| resizeObserver.unobserve(cardsContainerRef); | ||
| } | ||
| }; | ||
| }, [cardsContainerRef, calculateColumns]); | ||
| if (notInstalledCommunityModulesLoading || moduleTemplatesLoading) { | ||
| return ( | ||
| <div style={{ height: 'calc(100vh - 14rem)' }}> | ||
| <Spinner /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const communityModulesToDisplay = transformDataForDisplay( | ||
| availableCommunityModules, | ||
| ); | ||
|
|
||
| const isChecked = (name: string) => { | ||
| const sth = !!communityModulesTemplatesToApply.get(name); | ||
| return sth; | ||
| }; | ||
| const renderCards = () => { | ||
| const columns = Array.from({ length: columnsCount }, (): any => []); | ||
|
|
||
| communityModulesToDisplay?.forEach((module, i) => { | ||
| const card = ( | ||
| <CommunityModuleCard | ||
| module={module} | ||
| key={`${module.name}+${i}`} | ||
| isChecked={isChecked} | ||
| onChange={onVersionChange( | ||
| notInstalledCommunityModuleTemplates, | ||
| communityModulesTemplatesToApply, | ||
| setCommunityModulesTemplatesToApply, | ||
| setIsResourceEdited, | ||
| )} | ||
| selectedModules={communityModulesTemplatesToApply} | ||
| /> | ||
| ); | ||
| columns[i % columnsCount].push(card); | ||
| }); | ||
|
|
||
| return ( | ||
| <div | ||
| className="gridbox-addModule sap-margin-top-small" | ||
| ref={setCardsContainerRef} | ||
| > | ||
| {columns.map((column, columnIndex) => ( | ||
| <div | ||
| className={`gridbox-addModule-column column-${columnIndex}`} | ||
| key={columnIndex} | ||
| > | ||
| {column} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| const handleSubmit = (e: any) => { | ||
| e.preventDefault(); | ||
| try { | ||
| uploadResources(); | ||
|
|
||
| notification.notifySuccess({ | ||
| content: t('kyma-modules.messages.success', { | ||
| resourceType: 'Community Module', | ||
| }), | ||
| }); | ||
|
|
||
| setLayoutColumn({ | ||
| ...layoutColumn, | ||
| layout: 'OneColumn', | ||
| midColumn: null, | ||
| endColumn: null, | ||
| showCreate: null, | ||
| }); | ||
| navigate(window.location.pathname, { replace: true }); | ||
| } catch (e) { | ||
| console.error(e); | ||
| notification.notifyError({ | ||
| content: t('kyma-modules.messages.failure', { | ||
| resourceType: 'Community Module', | ||
| error: e instanceof Error && e?.message ? e.message : '', | ||
| }), | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| if (isCommunityModulesEnabled) { | ||
| return ( | ||
| <> | ||
| <ResourceForm | ||
| {...props} | ||
| disableDefaultFields | ||
| formElementRef={props.formElementRef} | ||
| onChange={props.onChange} | ||
| layoutNumber="startColumn" | ||
| resetLayout | ||
| afterCreatedCustomMessage={t('kyma-modules.messages.module-added')} | ||
| formWithoutPanel | ||
| className="add-modules-form" | ||
| onSubmit={handleSubmit} | ||
| > | ||
| <> | ||
| {communityModulesToDisplay?.length !== 0 ? ( | ||
| renderCards() | ||
| ) : ( | ||
| <MessageStrip | ||
| design="Critical" | ||
| hideCloseButton | ||
| className="sap-margin-top-small" | ||
| > | ||
| {t('extensibility.widgets.modules.no-community-modules')} | ||
| </MessageStrip> | ||
| )} | ||
| </> | ||
| </ResourceForm> | ||
|
|
||
| {createPortal(<UnsavedMessageBox />, document.body)} | ||
| </> | ||
| ); | ||
| } else { | ||
| return <></>; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.