fleet/frontend/pages/labels/components/DynamicLabelForm/DynamicLabelForm.tsx
jacobshandling 7953fe2e2c
UI: Simplify New policy flow (#27173)
## For #26052 

- Remove add policy modal from flow
- Update "Schema" links
- Add "Examples" link


![add-policy](https://github.com/user-attachments/assets/69d60573-f6a6-45e8-92ff-4faa29f224a9)




- [x] Changes file added for user-visible changes in `changes/
- [x] A detailed QA plan exists on the associated ticket (if it isn't
there, work with the product group's QA engineer to add it)
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-03-18 12:56:05 -07:00

153 lines
4.1 KiB
TypeScript

import React, { useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { IAceEditor } from "react-ace/lib/types";
// @ts-ignore
import validateQuery from "components/forms/validators/validate_query";
import SQLEditor from "components/SQLEditor";
import Button from "components/buttons/Button";
import Icon from "components/Icon";
import LabelForm from "../LabelForm";
import { ILabelFormData } from "../LabelForm/LabelForm";
import PlatformField from "../PlatformField";
const baseClass = "dynamic-label-form";
const IMMUTABLE_QUERY_HELP_TEXT =
"Label queries are immutable. To change the query, delete this label and create a new one.";
export interface IDynamicLabelFormData {
name: string;
description: string;
query: string;
platform: string;
}
interface IDynamicLabelFormProps {
defaultName?: string;
defaultDescription?: string;
defaultQuery?: string;
defaultPlatform?: string;
showOpenSidebarButton?: boolean;
isEditing?: boolean;
onOpenSidebar?: () => void;
onOsqueryTableSelect?: (tableName: string) => void;
onSave: (formData: IDynamicLabelFormData) => void;
onCancel: () => void;
}
const DynamicLabelForm = ({
defaultName = "",
defaultDescription = "",
defaultQuery = "",
defaultPlatform = "",
isEditing = false,
showOpenSidebarButton = false,
onOpenSidebar,
onOsqueryTableSelect,
onSave,
onCancel,
}: IDynamicLabelFormProps) => {
const [query, setQuery] = useState(defaultQuery);
const [platform, setPlatform] = useState(defaultPlatform);
const [queryError, setQueryError] = useState<string | null>(null);
const debounceValidateSQL = useDebouncedCallback((queryString: string) => {
const { error } = validateQuery(queryString);
if (query === "" || error === "") {
setQueryError(null);
} else {
setQueryError(error);
}
}, 500);
const onQueryChange = (newQuery: string) => {
setQuery(newQuery);
debounceValidateSQL(newQuery);
};
const onSaveForm = (
labelFormData: ILabelFormData,
labelFormDataValid: boolean
) => {
const { error } = validateQuery(query);
if (error) {
setQueryError(error);
} else if (labelFormDataValid) {
// values from LabelForm component must be valid too
onSave({ ...labelFormData, query, platform });
}
};
const renderLabelComponent = (): JSX.Element | null => {
if (!showOpenSidebarButton) {
return null;
}
return (
<Button variant="text-icon" onClick={onOpenSidebar}>
Schema
<Icon name="info" size="small" />
</Button>
);
};
const onLoad = (editor: IAceEditor) => {
editor.setOptions({
enableLinking: true,
enableMultiselect: false, // Disables command + click creating multiple cursors
});
// @ts-expect-error
// the string "linkClick" is not officially in the lib but we need it
editor.on("linkClick", (data) => {
const { type, value } = data.token;
if (type === "osquery-token" && onOsqueryTableSelect) {
return onOsqueryTableSelect(value);
}
return false;
});
};
const onChangePlatform = (value: string) => {
setPlatform(value);
};
return (
<div className={baseClass}>
<LabelForm
defaultName={defaultName}
defaultDescription={defaultDescription}
onSave={onSaveForm}
onCancel={onCancel}
additionalFields={
<>
<SQLEditor
error={queryError}
name="query"
onChange={onQueryChange}
value={query}
label="Query"
labelActionComponent={renderLabelComponent()}
readOnly={isEditing}
onLoad={onLoad}
wrapperClassName={`${baseClass}__text-editor-wrapper form-field`}
helpText={isEditing ? IMMUTABLE_QUERY_HELP_TEXT : ""}
wrapEnabled
/>
<PlatformField
platform={platform}
isEditing={isEditing}
onChange={onChangePlatform}
/>
</>
}
/>
</div>
);
};
export default DynamicLabelForm;