From 8dec148ca2bd273f257a794fa4daa3af8624f866 Mon Sep 17 00:00:00 2001 From: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Date: Thu, 2 Dec 2021 01:26:55 +0800 Subject: [PATCH] refresh edited insights panel on change (#10180) * refresh edited insights panel on change * move api simpleHash to shared utils getSimpleHash * Simply reactivity fix for time-series * Remove unused import Co-authored-by: rijkvanzanten --- api/src/utils/get-default-index-name.ts | 5 ++--- app/src/panels/time-series/time-series.vue | 14 ++++++-------- packages/shared/src/utils/get-simple-hash.test.ts | 11 +++++++++++ .../shared}/src/utils/get-simple-hash.ts | 2 +- packages/shared/src/utils/index.ts | 1 + 5 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 packages/shared/src/utils/get-simple-hash.test.ts rename {api => packages/shared}/src/utils/get-simple-hash.ts (86%) diff --git a/api/src/utils/get-default-index-name.ts b/api/src/utils/get-default-index-name.ts index b9a70fc9cf..c1ad083a3c 100644 --- a/api/src/utils/get-default-index-name.ts +++ b/api/src/utils/get-default-index-name.ts @@ -1,5 +1,4 @@ -import { simpleHash } from './get-simple-hash'; - +import { getSimpleHash } from '@directus/shared/utils'; /** * Generate an index name for a given collection + fields combination. * @@ -20,7 +19,7 @@ export function getDefaultIndexName( if (indexName.length <= 60) return indexName; - const suffix = `__${simpleHash(indexName)}_${type}`; + const suffix = `__${getSimpleHash(indexName)}_${type}`; const prefix = indexName.substring(0, 60 - suffix.length); return `${prefix}${suffix}`; diff --git a/app/src/panels/time-series/time-series.vue b/app/src/panels/time-series/time-series.vue index 9a5276ba08..b8f3aa5e2b 100644 --- a/app/src/panels/time-series/time-series.vue +++ b/app/src/panels/time-series/time-series.vue @@ -8,7 +8,7 @@ import api from '@/api'; import ApexCharts from 'apexcharts'; import { adjustDate } from '@/utils/adjust-date'; import { useI18n } from 'vue-i18n'; -import { isEqual, isNil } from 'lodash'; +import { isNil } from 'lodash'; import { useFieldsStore } from '@/stores'; import { Filter } from '@directus/shared/types'; import { abbreviateNumber } from '@/utils/abbreviate-number'; @@ -111,13 +111,11 @@ export default defineComponent({ }); watch( - [() => props, () => props.showHeader, () => props.height], - (newVal, oldVal) => { - if (isEqual(newVal, oldVal) === false) { - fetchData(); - chart.value?.destroy(); - setupChart(); - } + () => props, + () => { + fetchData(); + chart.value?.destroy(); + setupChart(); }, { deep: true } ); diff --git a/packages/shared/src/utils/get-simple-hash.test.ts b/packages/shared/src/utils/get-simple-hash.test.ts new file mode 100644 index 0000000000..82ffdd4627 --- /dev/null +++ b/packages/shared/src/utils/get-simple-hash.test.ts @@ -0,0 +1,11 @@ +import { getSimpleHash } from './get-simple-hash'; + +describe('getSimpleHash', () => { + it('returns "364492" for string "test"', () => { + expect(getSimpleHash('test')).toBe('364492'); + }); + + it('returns "28cb67ba" for stringified object "{ key: \'value\' }"', () => { + expect(getSimpleHash(JSON.stringify({ key: 'value' }))).toBe('28cb67ba'); + }); +}); diff --git a/api/src/utils/get-simple-hash.ts b/packages/shared/src/utils/get-simple-hash.ts similarity index 86% rename from api/src/utils/get-simple-hash.ts rename to packages/shared/src/utils/get-simple-hash.ts index 0e90ead32f..be4f1a7082 100644 --- a/api/src/utils/get-simple-hash.ts +++ b/packages/shared/src/utils/get-simple-hash.ts @@ -2,7 +2,7 @@ * Generate a simple short hash for a given string * This is not cryptographically secure in any way, and has a high chance of collision */ -export function simpleHash(str: string) { +export function getSimpleHash(str: string) { let hash = 0; for (let i = 0; i < str.length; hash &= hash) { diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 4741ac430a..beff0b1119 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -6,6 +6,7 @@ export * from './get-collection-type'; export * from './get-fields-from-template'; export * from './get-filter-operators-for-type'; export * from './get-relation-type'; +export * from './get-simple-hash'; export * from './is-dynamic-variable'; export * from './is-extension'; export * from './merge-filters';