Skip to content

Commit 52c1a6e

Browse files
authored
feat(entities-shared): add a prop to persist format preference (#2131)
1 parent de15ff1 commit 52c1a6e

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

packages/entities/entities-shared/docs/config-card-display.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ The base property configuration when format is structured.
3838

3939
Format to be displayed in the Config Card. Can be `structured`, `json`, or `yaml`.
4040

41+
#### `formatPreferenceKey`
42+
43+
- type: `String`
44+
- required: `false`
45+
- default: `''`
46+
47+
The localStorage key to use while persisting the format preference. If omitted, the format will not be persisted.
48+
4149
#### `propListTypes`
4250

4351
- type: `Array as PropType<String[]>`

packages/entities/entities-shared/sandbox/pages/EntityBaseConfigCardPage.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ const konnectConfig = ref<KonnectBaseEntityConfig>({
8282
// Set the root `.env.development.local` variable to a control plane your PAT can access
8383
controlPlaneId,
8484
entityId,
85+
formatPreferenceKey: 'konnect-entities-base-config-card-format-sandbox',
8586
})
8687
const kongManagerConfig = ref<KongManagerBaseEntityConfig>({
8788
app: 'kongManager',
8889
workspace: 'default',
8990
apiBaseUrl: '/kong-manager', // For local dev server proxy
9091
entityId,
92+
formatPreferenceKey: 'kong-manager-entities-base-config-card-format-sandbox',
9193
})
9294
9395
const configSchema = ref<ConfigurationSchema>({

packages/entities/entities-shared/src/components/entity-base-config-card/EntityBaseConfigCard.vue

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
{{ label }}
2828
</KLabel>
2929
<KSelect
30+
v-model="configFormat"
3031
data-testid="select-config-format"
3132
:items="configFormatItems"
3233
@change="handleChange"
@@ -103,7 +104,7 @@
103104

104105
<script setup lang="ts">
105106
import type { PropType } from 'vue'
106-
import { computed, ref, onBeforeMount, watch } from 'vue'
107+
import { computed, ref, onBeforeMount, watch, onMounted } from 'vue'
107108
import type { AxiosError } from 'axios'
108109
import type {
109110
KonnectBaseEntityConfig,
@@ -267,12 +268,36 @@ if (props.config.app === 'konnect') {
267268
})
268269
}
269270
270-
const configFormat = ref<Format>('structured')
271+
const DEFAULT_FORMAT = configFormatItems[0].value as Format
272+
273+
const configFormat = ref<Format>(DEFAULT_FORMAT)
271274
272275
const handleChange = (payload: any): void => {
273276
configFormat.value = payload?.value
274277
}
275278
279+
const persistFormat = (localStorageKey: string, format: Format): void => {
280+
localStorage.setItem(localStorageKey, format)
281+
}
282+
283+
watch(configFormat, (format: Format) => {
284+
if (props.config.formatPreferenceKey) {
285+
persistFormat(props.config.formatPreferenceKey, format)
286+
}
287+
})
288+
289+
onMounted(() => {
290+
if (props.config.formatPreferenceKey) {
291+
const storedFormat = localStorage.getItem(props.config.formatPreferenceKey)
292+
if (storedFormat && configFormatItems.some(item => item.value === storedFormat)) {
293+
configFormat.value = storedFormat as Format
294+
} else {
295+
configFormat.value = DEFAULT_FORMAT
296+
}
297+
persistFormat(props.config.formatPreferenceKey, configFormat.value)
298+
}
299+
})
300+
276301
/**
277302
* default ordering for these common fields:
278303
* - name, id, enabled (if exists), updated_at, created_at, tags

packages/entities/entities-shared/src/types/entity-base-config-card.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export const SupportedEntityTypesArray = Object.values(SupportedEntityType)
3131
export interface BaseEntityConfig {
3232
/** the ID of the entity */
3333
entityId: string
34+
35+
/**
36+
* The localStorage key to use while persisting the config format preference.
37+
*
38+
* If omitted, the preference will not be persisted.
39+
*/
40+
formatPreferenceKey?: string
3441
}
3542

3643
/** Konnect base form config */

0 commit comments

Comments
 (0)