feat(ui): add getIsVisible to metadata handlers

This commit is contained in:
psychedelicious 2024-05-07 17:44:50 +10:00 committed by Kent Keirsey
parent bfad814862
commit ccd399e277
2 changed files with 22 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import { MetadataItemView } from 'features/metadata/components/MetadataItemView'
import { useMetadataItem } from 'features/metadata/hooks/useMetadataItem';
import type { MetadataHandlers } from 'features/metadata/types';
import { MetadataParseFailedToken } from 'features/metadata/util/parsers';
import { isSymbol } from 'lodash-es';
type MetadataItemProps<T> = {
metadata: unknown;
@ -17,6 +18,10 @@ const _MetadataItem = typedMemo(<T,>({ metadata, handlers, direction = 'row' }:
return null;
}
if (handlers.getIsVisible && !isSymbol(value) && !handlers.getIsVisible(value)) {
return null;
}
return (
<MetadataItemView
label={label}

View File

@ -50,6 +50,14 @@ export type MetadataParseFunc<T = unknown> = (metadata: unknown) => Promise<T>;
*/
export type MetadataValidateFunc<T> = (value: T) => Promise<T>;
/**
* A function that determines whether a metadata item should be visible.
*
* @param value The value to check.
* @returns True if the item should be visible, false otherwise.
*/
export type MetadataGetIsVisibleFunc<T> = (value: T) => boolean;
export type MetadataHandlers<TValue = unknown, TItem = unknown> = {
/**
* Gets the label of the current metadata item as a string.
@ -111,6 +119,14 @@ export type MetadataHandlers<TValue = unknown, TItem = unknown> = {
* @returns The rendered item.
*/
renderItemValue?: MetadataRenderValueFunc<TItem>;
/**
* Checks if a parsed metadata value should be visible.
* If not provided, the item is always visible.
*
* @param value The value to check.
* @returns True if the item should be visible, false otherwise.
*/
getIsVisible?: MetadataGetIsVisibleFunc<TValue>;
};
// TODO(psyche): The types for item handlers should be able to be inferred from the type of the value:
@ -127,6 +143,7 @@ type BuildMetadataHandlersArg<TValue, TItem> = {
getLabel: MetadataGetLabelFunc;
renderValue?: MetadataRenderValueFunc<TValue>;
renderItemValue?: MetadataRenderValueFunc<TItem>;
getIsVisible?: MetadataGetIsVisibleFunc<TValue>;
};
export type BuildMetadataHandlers = <TValue, TItem>(