Files
grafana/packages/grafana-ui/src/components/TimePicker/TimeZonePicker/TimeZoneDescription.tsx
Zoltán Bedi e1c44d7a8a Chore: react hooks eslint fixes in grafana-ui (#28026)
* Fix some rule violation in grafan-ui

* Update eslint-plugin-react-hooks to latest

* Remove duplicate dependency

* Fix more files

* Props destruction
2020-10-21 09:06:41 +02:00

54 lines
1.2 KiB
TypeScript

import React, { PropsWithChildren, useMemo } from 'react';
import { css } from 'emotion';
import { GrafanaTheme, TimeZoneInfo } from '@grafana/data';
import { useTheme, stylesFactory } from '../../../themes';
interface Props {
info?: TimeZoneInfo;
}
export const TimeZoneDescription: React.FC<PropsWithChildren<Props>> = ({ info }) => {
const theme = useTheme();
const styles = getStyles(theme);
const description = useDescription(info);
if (!info) {
return null;
}
return <div className={styles.description}>{description}</div>;
};
const useDescription = (info?: TimeZoneInfo): string => {
return useMemo(() => {
const parts: string[] = [];
if (!info) {
return '';
}
if (info.countries.length > 0) {
const country = info.countries[0];
parts.push(country.name);
}
if (info.abbreviation) {
parts.push(info.abbreviation);
}
return parts.join(', ');
}, [info]);
};
const getStyles = stylesFactory((theme: GrafanaTheme) => {
return {
description: css`
font-weight: normal;
font-size: ${theme.typography.size.sm};
color: ${theme.colors.textWeak};
white-space: normal;
text-overflow: ellipsis;
`,
};
});