Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryBuilder: Add support to disable operations #135

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/VisualQueryBuilder/components/OperationEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface Props<T extends VisualQuery> {
queryModeller: VisualQueryModeller;
onChange: (index: number, update: QueryBuilderOperation) => void;
onRemove: (index: number) => void;
onToggle: (index: number) => void;
onRunQuery: () => void;
flash?: boolean;
highlight?: boolean;
Expand All @@ -31,6 +32,7 @@ export function OperationEditor<T extends VisualQuery>({
operation,
index,
onRemove,
onToggle,
onChange,
onRunQuery,
queryModeller,
Expand Down Expand Up @@ -78,6 +80,7 @@ export function OperationEditor<T extends VisualQuery>({
definition={def}
onChange={onChange}
onRemove={onRemove}
onToggle={onToggle}
queryModeller={queryModeller}
query={query}
timeRange={timeRange}
Expand Down
12 changes: 11 additions & 1 deletion src/VisualQueryBuilder/components/OperationEditorBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Props = {
query: VisualQuery;
onChange: (index: number, update: QueryBuilderOperation) => void;
onRemove: (index: number) => void;
onToggle: (index: number) => void;
onRunQuery: () => void;
datasource: DataSourceApi;
flash?: boolean;
Expand All @@ -42,6 +43,7 @@ export function OperationEditorBody({
queryModeller,
onChange,
onRemove,
onToggle,
operation,
definition,
query,
Expand Down Expand Up @@ -84,7 +86,11 @@ export function OperationEditorBody({

return (
<div
className={cx(styles.card, (shouldFlash || highlight) && styles.cardHighlight, isConflicting && styles.cardError)}
className={cx(styles.card, {
[styles.cardHighlight]: shouldFlash || highlight,
[styles.cardError]: isConflicting,
[styles.disabled]: operation.disabled,
})}
ref={provided.innerRef}
{...provided.draggableProps}
data-testid={`operations.${index}.wrapper`}
Expand All @@ -96,6 +102,7 @@ export function OperationEditorBody({
index={index}
onChange={onChange}
onRemove={onRemove}
onToggle={onToggle}
queryModeller={queryModeller}
/>
<div className={styles.body}>
Expand Down Expand Up @@ -175,6 +182,9 @@ const getStyles = (theme: GrafanaTheme2, isConflicting: boolean) => {
transition: 'all 0.5s ease-in 0s',
height: isConflicting ? 'auto' : '100%',
}),
disabled: css({
opacity: 0.5,
}),
cardError: css({
boxShadow: `0px 0px 4px 0px ${theme.colors.warning.main}`,
border: `1px solid ${theme.colors.warning.main}`,
Expand Down
14 changes: 13 additions & 1 deletion src/VisualQueryBuilder/components/OperationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ interface Props {
dragHandleProps?: DraggableProvided['dragHandleProps'];
onChange: (index: number, update: QueryBuilderOperation) => void;
onRemove: (index: number) => void;
onToggle: (index: number) => void;
}

interface State {
isOpen?: boolean;
disabled?: boolean;
alternatives?: Array<SelectableValue<QueryBuilderOperationDefinition>>;
}

export const OperationHeader = React.memo<Props>(
({ operation, definition, index, onChange, onRemove, queryModeller, dragHandleProps }) => {
({ operation, definition, index, onChange, onRemove, onToggle, queryModeller, dragHandleProps }) => {
const styles = useStyles2(getStyles);
const [state, setState] = useState<State>({});

Expand Down Expand Up @@ -57,6 +59,16 @@ export const OperationHeader = React.memo<Props>(
title="Click to view alternative operations"
/>
<OperationInfoButton definition={definition} operation={operation} innerQueryPlaceholder={queryModeller.innerQueryPlaceholder} />
{definition.toggleable && (
<Button
icon={operation.disabled ? "eye-slash" : "eye"}
size="sm"
onClick={() => onToggle(index)}
fill="text"
variant="secondary"
title="Disable operation"
/>
)}
<Button
icon="times"
size="sm"
Expand Down
5 changes: 5 additions & 0 deletions src/VisualQueryBuilder/components/OperationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export function OperationList<T extends VisualQuery>({
onChange({ ...query, operations: updatedList });
};

const onToggle = (index: number) => {
onOperationChange(index, { ...operations[index], disabled: !operations[index].disabled, });
};

const addOptions: CascaderOption[] = queryModeller.getCategories().map((category) => {
return {
value: category,
Expand Down Expand Up @@ -107,6 +111,7 @@ export function OperationList<T extends VisualQuery>({
datasource={datasource}
onChange={onOperationChange}
onRemove={onRemove}
onToggle={onToggle}
onRunQuery={onRunQuery}
flash={opsToHighlight[index]}
highlight={highlightedOp === op}
Expand Down
2 changes: 2 additions & 0 deletions src/VisualQueryBuilder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface QueryBuilderLabelFilter {
export interface QueryBuilderOperation {
id: string;
params: QueryBuilderOperationParamValue[];
disabled?: boolean;
}

export interface QueryBuilderOperationDefinition<T = any> extends RegistryItem {
Expand All @@ -27,6 +28,7 @@ export interface QueryBuilderOperationDefinition<T = any> extends RegistryItem {
paramChangedHandler?: QueryBuilderOnParamChangedHandler;
explainHandler?: QueryBuilderExplainOperationHandler;
changeTypeHandler?: (op: QueryBuilderOperation, newDef: QueryBuilderOperationDefinition<T>) => QueryBuilderOperation;
toggleable?: boolean;
}

type QueryBuilderAddOperationHandler<T> = (
Expand Down
Loading