Skip to content

Commit

Permalink
[Osquery] Fix Global Packs (#145820)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsonpl authored Nov 21, 2022
1 parent ad126a7 commit 80af40e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/osquery/common/schemas/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const convertShardsToArray = (
reduce(
shards,
(acc, value, key) => {
if (value) {
if (value != null) {
acc.push({
policy: {
key,
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/osquery/public/packs/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,19 @@ const PackFormComponent: React.FC<PackFormProps> = ({
<NameField euiFieldProps={euiFieldProps} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<EuiFlexItem>
<DescriptionField euiFieldProps={euiFieldProps} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<PackTypeSelectable packType={packType} setPackType={changePackType} />
</EuiFlexGroup>
<EuiSpacer size="m" />

{packType === 'policy' && (
<>
Expand All @@ -260,6 +263,7 @@ const PackFormComponent: React.FC<PackFormProps> = ({
<PolicyIdComboBoxField options={availableOptions} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<EuiFlexItem>
Expand All @@ -274,6 +278,7 @@ const PackFormComponent: React.FC<PackFormProps> = ({
</StyledEuiAccordion>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
</>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useEffect } from 'react';
import React, { useEffect, useMemo } from 'react';
import type { InternalFieldErrors } from 'react-hook-form';
import { useFieldArray, useForm, useFormContext } from 'react-hook-form';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
Expand Down Expand Up @@ -40,15 +40,26 @@ const PackShardsFieldComponent = ({ options }: PackShardsFieldProps) => {

const rootShards = watchRoot('shards');

const initialShardsArray = useMemo(() => {
const initialConvertedShards = convertShardsToArray(rootShards, agentPoliciesById);
if (!isEmpty(initialConvertedShards)) {
if (initialConvertedShards[initialConvertedShards.length - 1].policy.key) {
return [...initialConvertedShards, defaultShardData];
}

return initialConvertedShards;
}

return [defaultShardData];
}, [agentPoliciesById, rootShards]);

const { control, watch, getFieldState, formState, resetField, setValue } = useForm<{
shardsArray: ShardsArray;
}>({
mode: 'all',
shouldUnregister: true,
defaultValues: {
shardsArray: !isEmpty(convertShardsToArray(rootShards, agentPoliciesById))
? [...convertShardsToArray(rootShards, agentPoliciesById), defaultShardData]
: [defaultShardData],
shardsArray: initialShardsArray,
},
});
const { fields, remove, append } = useFieldArray({
Expand Down Expand Up @@ -108,6 +119,7 @@ const PackShardsFieldComponent = ({ options }: PackShardsFieldProps) => {
control={control}
options={options}
/>
<EuiSpacer size="xs" />
</div>
))}
</>
Expand Down

0 comments on commit 80af40e

Please sign in to comment.