@awsui/components-react
Version:
On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en
85 lines • 3.5 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { useEffect, useRef } from 'react';
import { warnOnce } from '@awsui/component-toolkit/internal';
import { isDevelopment } from '../internal/is-development';
/**
* Ponyfill for Array.prototype.findIndex.
*/
export function findIndex(array, condition) {
for (let i = 0; i < array.length; i++) {
if (condition(array[i])) {
return i;
}
}
return -1;
}
function makeMemoizedArray(prev, next, isEqual) {
for (let i = 0; i < Math.max(prev.length, next.length); i++) {
// The next array is shorter, but all the items match.
if (i === next.length) {
return prev.slice(0, i);
}
// The prev array is shorter, but all the items so far match.
if (i === prev.length) {
return [...prev.slice(0, i), ...next.slice(i)];
}
// The item is not equal. Partition at this point.
if (!isEqual(prev[i], next[i])) {
return [...prev.slice(0, i), next[i], ...makeMemoizedArray(prev.slice(i + 1), next.slice(i + 1), isEqual)];
}
}
// All the references match. Return the old array.
return prev;
}
export function useMemoizedArray(array, isEqual) {
const ref = useRef(array);
const updated = makeMemoizedArray(ref.current, array, isEqual);
useEffect(() => {
ref.current = updated;
}, [updated]);
return updated;
}
function hasDuplicateKeys(arr) {
const keys = arr.map(obj => obj.key);
const uniqueKeys = new Set(keys);
return uniqueKeys.size !== keys.length;
}
/**
* Compares the initial tags with the current tags passed to the tag editor
* and returns the differences, identifying which tags have been created or removed.
*
* This utility can be used to track tag changes and inform your tagging service about
* the removed and added tags.
*
* @param initialTags - The original tags fetched from the backend or tagging service.
* @param tags - The current tags provided to the tag editor, including any new or modified tags.
* @returns An object containing two arrays:
* - `created`: An record of tags that are new or updated (with modified values).
* Each tag is represented by its `key` and `value`.
* - `removed`: An array of tag keys that were present in the initial tags but marked for removal.
*
* Updated tags are treated as both `created` and `removed` tags.
*/
export function getTagsDiff(initialTags, tags) {
if (isDevelopment) {
if (initialTags.some(t => !t.existing)) {
warnOnce('getTagsDiff: existing property', 'all initial tags should have `existing` property set to `true`.');
}
if (hasDuplicateKeys(initialTags) || hasDuplicateKeys(tags)) {
warnOnce('getTagsDiff: duplicate keys', 'tags should not have duplicate keys.');
}
}
const updated = tags.filter(tag => initialTags.some(({ key, value }) => {
return !tag.markedForRemoval && tag.key === key && tag.existing && tag.value !== value;
}));
const created = [...tags.filter(tag => !tag.existing), ...updated]
.map(({ key, value }) => ({ key, value }))
.reduce((acc, tag) => {
acc[tag.key] = tag.value;
return acc;
}, {});
const removed = [...tags.filter(tag => tag.existing && tag.markedForRemoval), ...updated].map(t => t.key);
return { created, removed };
}
//# sourceMappingURL=utils.js.map