mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Refactor FolderPicker to functional componment and add dissalowSlashes as an optional prop * Update DetailsStep component to use FolderPicker dissalowing slashes in folders * Adds icon that links to the Github issue when showing slashes not allowed warning * Add test for slashed folders warning * Fix merge conflicts with folder creation fix PR * Move slashes filter to useRuleFolderFilter hook, and make folder warning an optional generic prop * Apply PR review suggestions
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { css } from '@emotion/css';
|
|
import React from 'react';
|
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
|
import { Icon, Stack, Tooltip, useStyles2 } from '@grafana/ui';
|
|
import { FolderPicker, Props as FolderPickerProps } from 'app/core/components/Select/FolderPicker';
|
|
import { PermissionLevelString } from 'app/types';
|
|
|
|
import { FolderWarning, CustomAdd } from '../../../../../core/components/Select/FolderPicker';
|
|
|
|
export interface Folder {
|
|
title: string;
|
|
id: number;
|
|
}
|
|
|
|
export interface RuleFolderPickerProps extends Omit<FolderPickerProps, 'initialTitle' | 'initialFolderId'> {
|
|
value?: Folder;
|
|
dissalowSlashes: boolean;
|
|
}
|
|
|
|
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;
|
|
|
|
export function RuleFolderPicker(props: RuleFolderPickerProps) {
|
|
const { value } = props;
|
|
const warningCondition = (folderName: string) => containsSlashes(folderName);
|
|
|
|
const folderWarning: FolderWarning = {
|
|
warningCondition: warningCondition,
|
|
warningComponent: SlashesWarning,
|
|
};
|
|
|
|
const customAdd: CustomAdd = {
|
|
disallowValues: true,
|
|
isAllowedValue: (value) => !containsSlashes(value),
|
|
};
|
|
|
|
return (
|
|
<FolderPicker
|
|
showRoot={false}
|
|
allowEmpty={true}
|
|
initialTitle={value?.title}
|
|
initialFolderId={value?.id}
|
|
accessControlMetadata
|
|
{...props}
|
|
permissionLevel={PermissionLevelString.View}
|
|
customAdd={customAdd}
|
|
folderWarning={folderWarning}
|
|
/>
|
|
);
|
|
}
|
|
|
|
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;
|
|
`,
|
|
});
|