-
Notifications
You must be signed in to change notification settings - Fork 37
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
PAC-1569-added codes for Packs API fetch #2484
Conversation
@nage1234 I put the PR in draft to prevent accidental merge 😄 |
…ogy card as per mockup
plugins/packs-integrations.js
Outdated
return doc.frontMatter.type === "integration"; | ||
function getReadMeMap(packValues) { | ||
const map = new Map(); | ||
packValues.forEach((packValue) => { |
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.
reduce can be used here.
side note: you should use map.set instead of assigning attributes on this map
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.
generateCustomData
plugins/packs-integrations.js
Outdated
}); | ||
return packsData; | ||
//console.log("roots length= ", roots.length); |
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.
code cleanup
package.json
Outdated
@@ -45,7 +46,7 @@ | |||
"@fortawesome/free-solid-svg-icons": "^6.4.0", | |||
"@fortawesome/react-fontawesome": "^0.2.0", | |||
"@mdx-js/react": "^3.0.0", | |||
"antd": "^5.6.2", | |||
"antd": "^4.22.6", |
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.
this has a high chance of breaking things
setSelectedVersion(version); | ||
} | ||
|
||
function getOptions() { |
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.
renderVersionOptions
})} | ||
</Tabs> | ||
); | ||
} else if (Object.keys(packData.packReadme).length) { |
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 think you can simplify this if else logic
} else if (md) { | ||
return md; | ||
} else { | ||
return renderEmptyContent(); |
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 mostly in favor of creating a function for reusability.
if it just wraps some content and is not called more than once, then you can safely inline that content in the return
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.
else you have to track what a function truly returns
); | ||
} | ||
function getProviders() { | ||
if(packData.provider.includes("all")) { |
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.
if (
isn't there a linter here?
src/components/PacksReadme/index.ts
Outdated
import PacksReadme from "./PacksReadme"; | ||
|
||
export default PacksReadme; |
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 don't understand why this file was created
{isError ?(<IconMapper type={type} />) : | ||
(<Image | ||
preview={false} | ||
height={52} | ||
src={logoUrl} | ||
alt={`${title} logo`} | ||
onError={handleImageError} | ||
/>) | ||
} |
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.
linting
|
||
if (category1 < category2) { | ||
return -1; | ||
const filteredTechCards = useMemo(() => { |
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.
this entire block is very hard to read.
My suggestion is to create a list of conditions that each card would have to accommodate.
const conditions = [];
// ....
if (selectedFilters.provider) {
conditions.push(card => card.cloudTypes.includes(selectedFilters.provider))
}
// ...
// apply the filter only once
cards.filter(card => {
return conditions.every(condition => condition(card))
})
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.
at the end you can use the fuse search to narrow things down even further and then group them by categories
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 if your approach here to have the data already grouped by categories is the best.
Flat structures are easier to manipulate.
plugins/packs-integrations.js
Outdated
versions: getAggregatedVersions(packContent.tags, packValues, packName, layer) | ||
} | ||
}; | ||
return packValueMap; |
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.
why not just return the object?
plugins/packs-integrations.js
Outdated
const packValueMap = { | ||
fields: { |
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.
why nest all of the properties under one parent key?
plugins/packs-integrations.js
Outdated
counter += 1; | ||
if (counter % 10 === 0) { | ||
await setTimeout(2000); | ||
} |
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.
do you need this?
@@ -54,10 +55,15 @@ | |||
"docusaurus-plugin-sass": "^0.2.5", | |||
"docusaurus-theme-openapi-docs": "^3.0.0-beta.3", | |||
"fuse.js": "^6.6.2", | |||
"markdown-to-jsx": "^7.0.0", |
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.
is this used?
plugins/packs-integrations.js
Outdated
const layer = packMDValue.spec.layer === "addon" ? packMDValue.spec.addonType : packTypeNames[packMDValue.spec.layer]; | ||
const packValues = packContent.packValues; | ||
return { | ||
fields: { |
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.
do we need this wraping object?
plugins/packs-integrations.js
Outdated
} | ||
|
||
function getAggregatedVersions(tags, packValues, packName, layer) { | ||
const _sortedVersions = sortVersions(tags); |
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.
what does _
mean?
background-color: #1c202b; | ||
} | ||
padding: 10px; | ||
.packdescfirstcol { |
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.
while some classes are camel case this one is hard to read.
please make it consistent.
}); | ||
} | ||
|
||
function renderTabs() { |
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.
a lot of duplicated code that is hard to read.
side note: refactoring if else if
statements in ternary operations makes it even worse.
Take a step back. What are you trying to render here?
How do you simplify it, but how I make it scalable?
You have 3 cases:
tabs, readme only, author-provided content only.
I think you boxed yourself in with thinking only at these cases.
Lets think about the happy path. You'd render tabs if the API returned readme file and the author has provided content.
const tabs = [
readme && {label: "Readme", key: "readme", content: <Markdown children={readme} /> },
md && {label: "Additional Details" // .... etc }
].filter(Boolean)
From here you have edge cases. Only one or no tabs might render:
Single tab would not make sense so add a check for it
if (tabs.length === 1) {
return <><h2>{tabs[0].label}</h2>{tabs[0].content}</>
}
no tabs... no problem
if (tabs.length === 0) {
return <div className={styles.emptyContent}> // etc
}
Why?
Well it's scalable. If in the future you add another tab this will still render perfectly.
|
||
export default function PacksReadme() { | ||
const [selectedVersion, setSelectedVersion] = useState<string>(""); | ||
const [md, setMd] = useState<ReactElement<any, any> | null>(null); |
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.
what does md mean?
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.
please use a better name for this
plugins/packs-integrations.js
Outdated
let promises = new Array(); | ||
for (let i = 0; i < packDataArr.length; i++) { | ||
const packData = packDataArr[i]; | ||
if (packData.spec.registries.length && isSelectedRegistry(packData.spec.registries, mappedRepos)) { | ||
packMDMap[packData.spec.name] = packData; | ||
const cloudType = packData.spec.cloudTypes.includes("all") ? "aws" : packData.spec.cloudTypes[0]; | ||
promises.push(`${packUrl}${packData.spec.name}/registries/${packData.spec.registries[0].uid}?cloudType=${cloudType}&layer=${packData.spec.layer}`); | ||
} | ||
} |
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.
refactor using map
<Select | ||
className={filterStyles.selectbox} | ||
mode="multiple" | ||
allowClear | ||
placeholder="Search" | ||
onChange={(item) => setSelectedSearchFilters({ category: item as string[] })} | ||
> | ||
{categories.map((category) => { | ||
return ( | ||
<Select.Option key={category}> | ||
{packTypeNames[category as keyof typeof packTypeNames]} | ||
</Select.Option> | ||
); | ||
})} | ||
</Select> |
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.
create a component for this and reuse it bellow as well. remove unnecessary code duplication
const additionalFilters = Array.isArray(_values) ? _values.reduce((acc, _value) => { | ||
acc.push({ | ||
name: _value, | ||
values: true | ||
}); | ||
return acc; | ||
}, [] as SearchFilter[]) : []; | ||
_selectedFilters = _selectedFilters.concat(additionalFilters); |
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 don't understand why this has to be so different then the normal data structure.
Please try to use typescript to have a consistent data structure / interface for all filters
const handleClick = () => { | ||
history.push({ | ||
pathname: `/integrations/packs/${name}`, | ||
}); | ||
}; |
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.
so basically a Link
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.
what you created here makes the component less accessible.
plugins/packs-integrations.js
Outdated
const urls = packDataArr.map((packData) => { | ||
packMDMap[packData.spec.name] = packData; | ||
const cloudType = packData.spec.cloudTypes.includes("all") ? "aws" : packData.spec.cloudTypes[0]; | ||
return (`${packUrl}${packData.spec.name}/registries/${packData.spec.registries[0].uid}?cloudType=${cloudType}&layer=${packData.spec.layer}`); | ||
}) |
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.
this map should return functions that return promises directly.
we thunk the promises to avoid triggering them.
} | ||
}; | ||
importComponent(); | ||
}, []); | ||
|
||
const packData = useMemo(() => { | ||
const { packs, repositories } = usePluginData("plugin-packs-integrations") as PacksIntegrationsPluginData; | ||
const _packData = packs.filter((pack) => pack.fields.name === packName)[0]; | ||
const _packData = packs.find((pack) => pack.name === packName); |
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.
const pack =
key: string, | ||
name: string | ||
} | ||
export default function FilterSelect({ selectMode, options, setSelectedSearchFilters }: FilterSelectProps) { |
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.
value
and onChange
as props
src/services/api/index.js
Outdated
return Promise.allSettled( | ||
urls.map(url => limit(() => (api[method](url, payload)).catch(e => e))) | ||
); | ||
function callRateLimitAPI(url, method, payload) { |
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.
callRateLimitAPI(delayedApiCall) {
return limit(delayedApiCall)
}
plugins/packs-integrations.js
Outdated
const tempPackArr = packDataArr.concat(response[0]?.value.data.items); | ||
if (response[0]?.value.data.listmeta.continue) { | ||
return fetchPackListItems("?limit=100&continue=" + response[0]?.value.data.listmeta.continue, tempPackArr, counter); | ||
const response = await callRateLimitAPI("/v1/packs/search" + queryParams, 'post', payload); |
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.
await callRateLimitAPI(() => api.post(`/v1/packs/search${queryParams}, payload));
plugins/packs-integrations.js
Outdated
}) | ||
const results = await callRateLimitAPI(urls, 'get', {}); | ||
const url = `${packUrl}${packData.spec.name}/registries/${packData.spec.registries[0].uid}?cloudType=${cloudType}&layer=${packData.spec.layer}`; | ||
return callRateLimitAPI(url, 'get', {}); |
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.
callRateLimitAPI(() => api.get(url));
return ( | ||
<div className={filterStyles.wrapper}> | ||
<Select | ||
className={filterStyles.selectBox} | ||
mode={selectMode as "tags" | "multiple" | undefined} | ||
allowClear={true} | ||
placeholder="Search" | ||
onChange={(item: any) => setSelectedSearchFilters(item)} | ||
onChange={(item: any) => onChange(item)} |
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.
onChange={onChange}
@@ -3,27 +3,24 @@ import { Select } from "antd"; | |||
import filterStyles from "./CategorySelector.module.scss"; | |||
interface FilterSelectProps { | |||
selectMode?: string; |
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.
selectMode?: SelectProps["mode"]
import type {SelectProps} from "antd"
<div className={styles.filterItems}> | ||
<CustomLabel label="Type" /> | ||
<FilterSelect | ||
selectMode="multiple" |
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.
value={selectedFilters.type}
Describe the Change
Still lots of changes to be added in this PR. please DO NOT MERGE.. API error handling has to be done, so dont merge this..
Changed Pages
💻 Add Preview URL for Page
Jira Tickets
🎫 PAC-1569
Backports
Can this PR be backported?
No