2022-09-23 14:44:24 +02:00
|
|
|
import { css } from '@emotion/css';
|
2022-09-14 14:04:36 +02:00
|
|
|
import React from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-09-23 14:44:24 +02:00
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
|
|
|
|
import { Icon, Stack, Tooltip, useStyles2 } from '@grafana/ui';
|
2022-06-03 15:09:27 +02:00
|
|
|
import { FolderPicker, Props as FolderPickerProps } from 'app/core/components/Select/FolderPicker';
|
2022-06-20 14:48:57 +02:00
|
|
|
import { PermissionLevelString } from 'app/types';
|
2021-04-14 15:57:36 +03:00
|
|
|
|
2022-09-23 14:44:24 +02:00
|
|
|
import { FolderWarning, CustomAdd } from '../../../../../core/components/Select/FolderPicker';
|
|
|
|
|
|
2021-04-14 15:57:36 +03:00
|
|
|
export interface Folder {
|
|
|
|
|
title: string;
|
|
|
|
|
id: number;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-02 17:06:07 +02:00
|
|
|
export interface RuleFolderPickerProps extends Omit<FolderPickerProps, 'initialTitle' | 'initialFolderId'> {
|
2021-04-14 15:57:36 +03:00
|
|
|
value?: Folder;
|
2022-09-23 14:44:24 +02:00
|
|
|
dissalowSlashes: boolean;
|
2021-04-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
2022-09-23 14:44:24 +02:00
|
|
|
const SlashesWarning = () => {
|
|
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
|
const onClick = () => window.open('https://github.com/grafana/grafana/issues/42947', '_blank');
|
|
|
|
|
return (
|
|
|
|
|
<Stack gap={0.5}>
|
|
|
|
|
<div className={styles.slashNotAllowed}>Folders with '/' character are not allowed.</div>
|
|
|
|
|
<Tooltip placement="top" content={'Link to the Github issue'} theme="info">
|
|
|
|
|
<Icon name="info-circle" size="xs" className={styles.infoIcon} onClick={onClick} />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const containsSlashes = (str: string): boolean => str.indexOf('/') !== -1;
|
|
|
|
|
|
2022-09-14 14:04:36 +02:00
|
|
|
export function RuleFolderPicker(props: RuleFolderPickerProps) {
|
|
|
|
|
const { value } = props;
|
2022-09-23 14:44:24 +02:00
|
|
|
const warningCondition = (folderName: string) => containsSlashes(folderName);
|
|
|
|
|
|
|
|
|
|
const folderWarning: FolderWarning = {
|
|
|
|
|
warningCondition: warningCondition,
|
|
|
|
|
warningComponent: SlashesWarning,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const customAdd: CustomAdd = {
|
|
|
|
|
disallowValues: true,
|
|
|
|
|
isAllowedValue: (value) => !containsSlashes(value),
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-02 17:06:07 +02:00
|
|
|
return (
|
|
|
|
|
<FolderPicker
|
|
|
|
|
showRoot={false}
|
|
|
|
|
allowEmpty={true}
|
|
|
|
|
initialTitle={value?.title}
|
|
|
|
|
initialFolderId={value?.id}
|
|
|
|
|
accessControlMetadata
|
|
|
|
|
{...props}
|
|
|
|
|
permissionLevel={PermissionLevelString.View}
|
2022-09-23 14:44:24 +02:00
|
|
|
customAdd={customAdd}
|
|
|
|
|
folderWarning={folderWarning}
|
2022-06-02 17:06:07 +02:00
|
|
|
/>
|
|
|
|
|
);
|
2022-09-14 14:04:36 +02:00
|
|
|
}
|
2022-09-23 14:44:24 +02:00
|
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme2) => ({
|
|
|
|
|
slashNotAllowed: css`
|
|
|
|
|
color: ${theme.colors.warning.main};
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
margin-bottom: 2px;
|
|
|
|
|
`,
|
|
|
|
|
infoIcon: css`
|
|
|
|
|
color: ${theme.colors.warning.main};
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
margin-bottom: 2px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
`,
|
|
|
|
|
});
|