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

chore: Object.groupBy polyfill #2287

Merged
merged 1 commit into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"brandi": "5.0.0",
"deepmerge": "4.3.1",
"js-yaml": "4.1.0",
"object.groupby": "1.0.2",
"pretty-bytes": "6.1.1",
"prismjs": "1.29.0",
"semver": "7.6.0",
Expand Down
5 changes: 5 additions & 0 deletions src/app/application/index.ts
@@ -1,4 +1,6 @@
import { i18nTComponent } from '@kong-ui-public/i18n'
// @ts-ignore TS comes with a Object.groupBy declaration but not a polyfill
import groupBy from 'object.groupby'

import AppView from './components/app-view/AppView.vue'
import DataCollection from './components/data-collection/DataCollection.vue'
Expand All @@ -25,6 +27,9 @@ if (!('structuredClone' in globalThis)) {
return JSON.parse(JSON.stringify(obj))
}
}
// temporary Object.groupBy polyfill
// TODO(jc): delete this once we get to 2026
groupBy.shim()

export type { DataSourceResponse, Source } from './services/data-source'
type Sources = ConstructorParameters<typeof DataSourcePool>[0]
Expand Down
186 changes: 92 additions & 94 deletions src/app/data-planes/components/RuleEntryList.vue
Expand Up @@ -4,116 +4,114 @@
multiple-open
>
<template
v-for="policies in [props.rules.reduce<Record<string, Rule[]>>((prev, item) => {
if(typeof prev[item.type] === 'undefined') {
prev[item.type] = []
}
prev[item.type].push(item)
return prev
}, {})]"
v-for="policies in [Object.groupBy(props.rules, (item, i) => item.type)]"
:key="policies"
>
<AccordionItem
<template
v-for="(items, type) in policies"
:key="type"
>
<template #accordion-header>
<h3 class="policy-type-heading">
<PolicyTypeTag :policy-type="type">
{{ type }}
</PolicyTypeTag>
</h3>
</template>

<template #accordion-content>
<template
v-for="hasMatchers in [items.some((item) => item.matchers.length > 0)]"
:key="hasMatchers"
>
<div class="policy-list">
<AppCollection
class="policy-type-table"
:class="{
'has-matchers': hasMatchers,
}"
:total="items.length"
:items="items"
:headers="[
...(hasMatchers ? [{ label: 'Matchers', key: 'matchers' }] : []),
{ label: 'Origin policies', key: 'origins' },
{ label: 'Conf', key: 'config' },
]"
>
<template
#matchers="{ row }"
<AccordionItem
v-if="items"
kleinfreund marked this conversation as resolved.
Show resolved Hide resolved
>
<template #accordion-header>
<h3 class="policy-type-heading">
<PolicyTypeTag :policy-type="type">
{{ type }}
</PolicyTypeTag>
</h3>
</template>

<template #accordion-content>
<template
v-for="hasMatchers in [items.some((item) => item.matchers.length > 0)]"
:key="hasMatchers"
>
<div class="policy-list">
<AppCollection
class="policy-type-table"
:class="{
'has-matchers': hasMatchers,
}"
:total="items.length"
:items="items"
:headers="[
...(hasMatchers ? [{ label: 'Matchers', key: 'matchers' }] : []),
{ label: 'Origin policies', key: 'origins' },
{ label: 'Conf', key: 'config' },
]"
>
<span
v-if="row.matchers.length > 0"
class="matcher"
<template
#matchers="{ row }"
>
<template
v-for="({ key, value, not }, matcherIndex) in row.matchers"
:key="matcherIndex"
<span
v-if="row.matchers.length > 0"
class="matcher"
>
<span
v-if="matcherIndex > 0"
class="matcher__and"
> and<br></span><span
v-if="not"
class="matcher__not"
>!</span><span class="matcher__term">{{ `${key}:${value}` }}</span>
<template
v-for="({ key, value, not }, matcherIndex) in row.matchers"
:key="matcherIndex"
>
<span
v-if="matcherIndex > 0"
class="matcher__and"
> and<br></span><span
v-if="not"
class="matcher__not"
>!</span><span class="matcher__term">{{ `${key}:${value}` }}</span>
</template>
</span>

<template v-else>
<i>{{ t('data-planes.routes.item.matches_everything') }}</i>
</template>
</span>

<template v-else>
<i>{{ t('data-planes.routes.item.matches_everything') }}</i>
</template>
</template>

<template #origins="{ row }">
<ul v-if="row.origins.length > 0">
<li
v-for="(origin, originIndex) in row.origins"
:key="`${type}-${originIndex}`"
>
<RouterLink
:to="{
name: 'policy-detail-view',
params: {
mesh: origin.mesh,
policyPath: props.policyTypesByName[origin.type]!.path,
policy: origin.name,
},
}"
<template #origins="{ row }">
<ul v-if="row.origins.length > 0">
<li
v-for="(origin, originIndex) in row.origins"
:key="`${type}-${originIndex}`"
>
{{ origin.name }}
</RouterLink>
</li>
</ul>

<template v-else>
{{ t('common.collection.none') }}
</template>
</template>

<template #config="{ row }">
<template v-if="row.config">
<CodeBlock
:code="toYaml(row.config)"
language="yaml"
:show-copy-button="false"
/>
<RouterLink
:to="{
name: 'policy-detail-view',
params: {
mesh: origin.mesh,
policyPath: props.policyTypesByName[origin.type]!.path,
policy: origin.name,
},
}"
>
{{ origin.name }}
</RouterLink>
</li>
</ul>

<template v-else>
{{ t('common.collection.none') }}
</template>
</template>

<template v-else>
{{ t('common.collection.none') }}
<template #config="{ row }">
<template v-if="row.config">
<CodeBlock
:code="toYaml(row.config)"
language="yaml"
:show-copy-button="false"
/>
</template>

<template v-else>
{{ t('common.collection.none') }}
</template>
</template>
</template>
</AppCollection>
</div>
</AppCollection>
</div>
</template>
</template>
</template>
</AccordionItem>
</AccordionItem>
</template>
</template>
</AccordionList>
</template>
Expand Down
12 changes: 2 additions & 10 deletions src/app/data-planes/components/StandardDataplanePolicies.vue
Expand Up @@ -54,15 +54,7 @@
{{ t('data-planes.routes.item.from_rules') }}
</h3>
<template
v-for="inbounds in [items.reduce<Record<number, Rule[]>>((prev, item) => {
if(typeof item.inbound?.port !== 'undefined') {
if (typeof prev[item.inbound.port] === 'undefined') {
prev[item.inbound.port] = []
}
prev[item.inbound.port].push(item)
}
return prev
}, {})]"
v-for="inbounds in [Object.groupBy(items, (item) => item.inbound!.port)]"
:key="inbounds"
>
<div
Expand All @@ -73,7 +65,7 @@

<RuleEntryList
class="mt-2"
:rules="rs"
:rules="rs!"
kleinfreund marked this conversation as resolved.
Show resolved Hide resolved
:policy-types-by-name="props.policyTypesByName"
:data-testid="`from-rule-list-${index}`"
/>
Expand Down