2021-12-01 23:45:55 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Chip from '../../../ui/chip';
|
|
|
|
import Box from '../../../ui/box';
|
|
|
|
import Typography from '../../../ui/typography';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-06-02 11:38:39 +02:00
|
|
|
TEXT_ALIGN,
|
2023-02-02 21:15:26 +01:00
|
|
|
BackgroundColor,
|
|
|
|
TextColor,
|
2021-12-01 23:45:55 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
2022-06-02 11:38:39 +02:00
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2023-02-06 11:05:57 +01:00
|
|
|
import { SNAPS_METADATA } from '../../../../../shared/constants/snaps';
|
2021-12-01 23:45:55 +01:00
|
|
|
|
2022-05-25 18:19:02 +02:00
|
|
|
const snapIdPrefixes = ['npm:', 'local:'];
|
|
|
|
|
2022-06-02 11:38:39 +02:00
|
|
|
const SnapsAuthorshipPill = ({ snapId, version, className }) => {
|
2023-02-06 11:05:57 +01:00
|
|
|
// @todo Use getSnapPrefix from snaps-monorepo when possible
|
2022-08-03 18:02:44 +02:00
|
|
|
// We're using optional chaining with snapId, because with the current implementation
|
|
|
|
// of snap update in the snap controller, we do not have reference to snapId when an
|
|
|
|
// update request is rejected because the reference comes from the request itself and not subject metadata
|
|
|
|
// like it is done with snap install
|
|
|
|
const snapPrefix = snapIdPrefixes.find((prefix) =>
|
|
|
|
snapId?.startsWith(prefix),
|
|
|
|
);
|
|
|
|
const packageName = snapId?.replace(snapPrefix, '');
|
2022-05-25 18:19:02 +02:00
|
|
|
const isNPM = snapPrefix === 'npm:';
|
|
|
|
const url = isNPM
|
|
|
|
? `https://www.npmjs.com/package/${packageName}`
|
|
|
|
: packageName;
|
|
|
|
const icon = isNPM ? 'fab fa-npm fa-lg' : 'fas fa-code';
|
2022-06-02 11:38:39 +02:00
|
|
|
const t = useI18nContext();
|
2023-02-06 11:05:57 +01:00
|
|
|
|
|
|
|
const friendlyName = SNAPS_METADATA[snapId]?.name ?? packageName;
|
|
|
|
|
2021-12-01 23:45:55 +01:00
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href={url}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className={classnames(className, `snaps-authorship-pill`)}
|
|
|
|
>
|
|
|
|
<Chip
|
|
|
|
leftIcon={
|
|
|
|
<Box paddingLeft={2}>
|
2022-05-25 18:19:02 +02:00
|
|
|
<i className={`${icon} snaps-authorship-icon`} />
|
2021-12-01 23:45:55 +01:00
|
|
|
</Box>
|
|
|
|
}
|
2022-06-02 11:38:39 +02:00
|
|
|
rightIcon={
|
|
|
|
version && (
|
|
|
|
<Box
|
|
|
|
className="snaps-authorship-version"
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.primaryDefault}
|
2022-06-02 11:38:39 +02:00
|
|
|
paddingLeft={2}
|
|
|
|
paddingRight={2}
|
|
|
|
>
|
|
|
|
<Typography
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.primaryInverse}
|
|
|
|
variant={TypographyVariant.H7}
|
2022-06-02 11:38:39 +02:00
|
|
|
align={TEXT_ALIGN.CENTER}
|
2022-08-10 21:42:42 +02:00
|
|
|
as="span"
|
2022-06-02 11:38:39 +02:00
|
|
|
className="version"
|
|
|
|
>
|
|
|
|
{t('shorthandVersion', [version])}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
)
|
|
|
|
}
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.backgroundDefault}
|
2021-12-01 23:45:55 +01:00
|
|
|
>
|
|
|
|
<Typography
|
|
|
|
className="chip__label"
|
2023-02-02 21:15:26 +01:00
|
|
|
variant={TypographyVariant.H7}
|
2022-08-10 21:42:42 +02:00
|
|
|
as="span"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textAlternative}
|
2023-02-06 11:05:57 +01:00
|
|
|
title={friendlyName}
|
2021-12-01 23:45:55 +01:00
|
|
|
>
|
2023-02-06 11:05:57 +01:00
|
|
|
{friendlyName}
|
2021-12-01 23:45:55 +01:00
|
|
|
</Typography>
|
|
|
|
</Chip>
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SnapsAuthorshipPill.propTypes = {
|
|
|
|
/**
|
2022-05-25 18:19:02 +02:00
|
|
|
* The id of the snap
|
2021-12-01 23:45:55 +01:00
|
|
|
*/
|
2022-05-25 18:19:02 +02:00
|
|
|
snapId: PropTypes.string,
|
2022-06-02 11:38:39 +02:00
|
|
|
/**
|
|
|
|
* The version of the snap
|
|
|
|
*/
|
|
|
|
version: PropTypes.string,
|
2021-12-01 23:45:55 +01:00
|
|
|
/**
|
|
|
|
* The className of the SnapsAuthorshipPill
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SnapsAuthorshipPill;
|