1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Chip } from '@material-ui/core';
7 import { connect } from 'react-redux';
8 import { RootState } from '~/store/store';
9 import * as CopyToClipboard from 'react-copy-to-clipboard';
10 import { getVocabulary } from '~/store/vocabulary/vocabulary-selectors';
11 import { Dispatch } from 'redux';
12 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
13 import { getTagValueLabel, getTagKeyLabel, Vocabulary } from '~/models/vocabulary';
15 interface PropertyChipComponentDataProps {
19 vocabulary: Vocabulary;
22 interface PropertyChipComponentActionProps {
23 onDelete?: () => void;
24 onCopy: (message: string) => void;
27 type PropertyChipComponentProps = PropertyChipComponentActionProps & PropertyChipComponentDataProps;
29 const mapStateToProps = ({ properties }: RootState) => ({
30 vocabulary: getVocabulary(properties),
33 const mapDispatchToProps = (dispatch: Dispatch) => ({
34 onCopy: (message: string) => dispatch(snackbarActions.OPEN_SNACKBAR({
37 kind: SnackbarKind.SUCCESS
41 // Renders a Chip with copyable-on-click tag:value data based on the vocabulary
42 export const PropertyChipComponent = connect(mapStateToProps, mapDispatchToProps)(
43 ({ propKey, propValue, vocabulary, className, onCopy, onDelete }: PropertyChipComponentProps) => {
44 const label = `${getTagKeyLabel(propKey, vocabulary)}: ${getTagValueLabel(propKey, propValue, vocabulary)}`;
46 <CopyToClipboard key={propKey} text={label} onCopy={() => onCopy("Copied to clipboard")}>
47 <Chip onDelete={onDelete} key={propKey}
48 className={className} label={label} />