Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed Nov 14, 2024
1 parent 5c4d12a commit 2b79631
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
"start:main": "docker compose pull grafana-main && docker compose --profile main up",
"stop": "docker compose down",
"test": "jest --watch --onlyChanged",
"test:ci": "jest --maxWorkers 4 --coverage",
"test:e2e": "npx playwright test",
"test:e2e:dev": "npx playwright test --ui",
"test:e2e:docker": "docker compose --profile e2e up --exit-code-from test",
"test:ci": "jest --maxWorkers 4 --coverage",
"upgrade": "npm upgrade --save"
},
"version": "4.9.0"
Expand Down
4 changes: 2 additions & 2 deletions src/components/FormPanel/FormPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ describe('Panel', () => {
initial: {
method: RequestMethod.NONE,
code: `
const section = context.panel.sectionsUtils.get('section12'); context.grafana.notifySuccess(['Sections',section.id]);
const section = context.panel.sectionsUtils.get('section12'); context.grafana.notifySuccess(['Sections', section]);
`,
},
layout: {
Expand All @@ -1005,7 +1005,7 @@ describe('Panel', () => {

expect(appEventsMock.publish).toHaveBeenCalledWith({
type: AppEvents.alertSuccess.name,
payload: ['Sections', null],
payload: ['Sections', undefined],
});
});
});
Expand Down
15 changes: 6 additions & 9 deletions src/hooks/useFormLayout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventBusSrv, SelectableValue } from '@grafana/data';
import { useCallback, useEffect, useRef, useState } from 'react';

import { FormElement, LayoutSection, LocalFormElement } from '../types';
import { FormElement, LayoutSection, LayoutSectionWithElements, LocalFormElement } from '../types';
import {
convertElementsToPayload,
isElementConflict,
Expand Down Expand Up @@ -323,17 +323,14 @@ export const useFormLayout = ({
* Get Section
*/
const getSection = useCallback(
(id: string) => {
(id: string): LayoutSectionWithElements | undefined => {
const sections = sectionsRef.current;
const section = sections.find((section) => section.id === id);

if (!section) {
return {
id: null,
name: null,
elements: [],
};
return;
}

const currentElements = elementsRef.current;
const sectionElements = currentElements.filter((curElement) => curElement.section === section.name);
return {
Expand All @@ -347,10 +344,10 @@ export const useFormLayout = ({
/**
* Get Section
*/
const getAllSections = useCallback(() => {
const getAllSections = useCallback((): LayoutSectionWithElements[] => {
const sections = sectionsRef.current;

return sections.map((section) => {
return sections.map((section): LayoutSectionWithElements => {
const currentElements = elementsRef.current;
const sectionElements = currentElements.filter((curElement) => curElement.section === section.name);
return {
Expand Down
8 changes: 7 additions & 1 deletion src/types/layout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LayoutOrientation, LayoutVariant, SectionVariant } from '../constants';
import { LayoutOrientation, LayoutVariant, SectionVariant } from '@/constants';
import { LocalFormElement } from '@/types/form-element';

/**
* Layout Section
Expand Down Expand Up @@ -26,6 +27,11 @@ export interface LayoutSection {
expanded?: boolean;
}

/**
* Layout Section With Elements
*/
export type LayoutSectionWithElements = LayoutSection & { elements: LocalFormElement[] };

/**
* Layout Options
*/
Expand Down
103 changes: 42 additions & 61 deletions src/utils/code-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@ import { BackendSrv, FetchResponse, LocationService, TemplateSrv, toDataQueryRes
import { CodeEditorSuggestionItemKind } from '@grafana/ui';
import { CodeParameterItem, CodeParametersBuilder } from '@volkovlabs/components';

import { FormElement, LayoutSection, LocalFormElement, PanelOptions } from '../types';
import { FormElement, LayoutSection, LayoutSectionWithElements, LocalFormElement, PanelOptions } from '@/types';

import { fileToBase64 } from './request';

/**
* Sections Utils
*/
const sectionsUtils = {
detail: 'Section Helpers',
items: {
add: new CodeParameterItem<({ name, id, elements }: { name: string; id: string; elements: string[] }) => void>(
'Add new Section',
CodeEditorSuggestionItemKind.Method
),
update: new CodeParameterItem<(sections: LayoutSection[]) => void>(
'Change sections',
CodeEditorSuggestionItemKind.Method
),
remove: new CodeParameterItem<(id: string) => void>('Remove section', CodeEditorSuggestionItemKind.Method),
assign: new CodeParameterItem<(id: string, elements: string[]) => void>(
'Assign elements to section',
CodeEditorSuggestionItemKind.Method
),
unassign: new CodeParameterItem<(elements: string[]) => void>(
'Unassign elements from section',
CodeEditorSuggestionItemKind.Method
),
get: new CodeParameterItem<(id: string) => LayoutSectionWithElements | undefined>(
'Get section',
CodeEditorSuggestionItemKind.Method
),
getAll: new CodeParameterItem<() => LayoutSectionWithElements[]>(
'Get all sections',
CodeEditorSuggestionItemKind.Method
),
toggle: new CodeParameterItem<(id: string) => void>('Toggle section', CodeEditorSuggestionItemKind.Method),
collapse: new CodeParameterItem<(id: string) => void>('Collapse section', CodeEditorSuggestionItemKind.Method),
expand: new CodeParameterItem<(id: string) => void>('Expand section', CodeEditorSuggestionItemKind.Method),
expandedState: new CodeParameterItem<Record<string, boolean>>('Sections Expanded State'),
},
};

/**
* Request Code Parameters
*/
Expand Down Expand Up @@ -52,36 +91,7 @@ export const requestCodeParameters = new CodeParametersBuilder({
response: new CodeParameterItem<FetchResponse | Response | DataQueryResponse | null | undefined>(
'Response object.'
),
sectionsUtils: {
detail: 'Section Helpers',
items: {
add: new CodeParameterItem<
({ name, id, elements }: { name: string; id: string; elements: string[] }) => void
>('Add new Section', CodeEditorSuggestionItemKind.Method),
update: new CodeParameterItem<(sections: LayoutSection[]) => void>(
'Change sections',
CodeEditorSuggestionItemKind.Method
),
remove: new CodeParameterItem<(id: string) => void>('Remove section', CodeEditorSuggestionItemKind.Method),
assign: new CodeParameterItem<(id: string, elements: string[]) => void>(
'Assign elements to section',
CodeEditorSuggestionItemKind.Method
),
unassign: new CodeParameterItem<(elements: string[]) => void>(
'Unassign elements from section',
CodeEditorSuggestionItemKind.Method
),
get: new CodeParameterItem<(id: string) => void>('Get section', CodeEditorSuggestionItemKind.Method),
getAll: new CodeParameterItem<() => void>('Get all sections', CodeEditorSuggestionItemKind.Method),
toggle: new CodeParameterItem<(id: string) => void>('Toggle section', CodeEditorSuggestionItemKind.Method),
collapse: new CodeParameterItem<(id: string) => void>(
'Collapse section',
CodeEditorSuggestionItemKind.Method
),
expand: new CodeParameterItem<(id: string) => void>('Expand section', CodeEditorSuggestionItemKind.Method),
expandedState: new CodeParameterItem<Record<string, boolean>>('Sections Expanded State'),
},
},
sectionsUtils,
},
},
grafana: {
Expand Down Expand Up @@ -176,36 +186,7 @@ export const elementValueChangedCodeParameters = new CodeParametersBuilder({
'Disable save default button.',
CodeEditorSuggestionItemKind.Method
),
sectionsUtils: {
detail: 'Section Helpers',
items: {
add: new CodeParameterItem<
({ name, id, elements }: { name: string; id: string; elements: string[] }) => void
>('Add new Section', CodeEditorSuggestionItemKind.Method),
update: new CodeParameterItem<(sections: LayoutSection[]) => void>(
'Change sections',
CodeEditorSuggestionItemKind.Method
),
remove: new CodeParameterItem<(id: string) => void>('Remove section', CodeEditorSuggestionItemKind.Method),
assign: new CodeParameterItem<(id: string, elements: string[]) => void>(
'Assign elements to section',
CodeEditorSuggestionItemKind.Method
),
unassign: new CodeParameterItem<(elements: string[]) => void>(
'Unassign elements from section',
CodeEditorSuggestionItemKind.Method
),
get: new CodeParameterItem<(id: string) => void>('Get section', CodeEditorSuggestionItemKind.Method),
getAll: new CodeParameterItem<() => void>('Get all sections', CodeEditorSuggestionItemKind.Method),
toggle: new CodeParameterItem<(id: string) => void>('Toggle section', CodeEditorSuggestionItemKind.Method),
collapse: new CodeParameterItem<(id: string) => void>(
'Collapse section',
CodeEditorSuggestionItemKind.Method
),
expand: new CodeParameterItem<(id: string) => void>('Expand section', CodeEditorSuggestionItemKind.Method),
expandedState: new CodeParameterItem<Record<string, boolean>>('Sections Expanded State'),
},
},
sectionsUtils,
},
},
grafana: {
Expand Down

0 comments on commit 2b79631

Please sign in to comment.