[FEATURE] Add annotation support#120
Conversation
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
| const errors: string[] = []; | ||
|
|
||
| /** Annotation names must be unique */ | ||
| const annotationNames = annotationSpecs.map((annotationSpec) => annotationSpec.display.name); | ||
| const uniqueAnnotationNames = new Set(annotationNames); | ||
| if (annotationNames.length !== uniqueAnnotationNames.size) { | ||
| errors.push('Annotation names must be unique'); | ||
| } |
There was a problem hiding this comment.
The error message Annotation names must be unique is vague.
The captured error should clarify what exact annotations are redundant.
You can either .join(',') the redundant or push individual errors into the array.
It should be feasible to collect the duplicates by O(N)
| import AddIcon from 'mdi-material-ui/Plus'; | ||
| import { Action } from '@perses-dev/core'; |
There was a problem hiding this comment.
Action is available in Perses/Shared/Components
Please avoid using Core
import { Action } from '@perses-dev/components';| import { ValidationProvider, AnnotationEditorForm } from '@perses-dev/plugin-system'; | ||
| import { useDiscardChangesConfirmationDialog } from '../../context'; | ||
|
|
||
| function getValidation(annotationSpecs: AnnotationSpec[]): { isValid: boolean; errors: string[] } { |
There was a problem hiding this comment.
The function name is vague. getValidation sounds like a function which returns a validation function.
Here the function simply validates the annotationSpecs. So, it could be validateAnnotationSpecs
| const changeAnnotationOrder = (index: number, direction: 'up' | 'down'): void => { | ||
| setAnnotationSpecs((draft) => { | ||
| if (direction === 'up') { | ||
| const prevElement = draft[index - 1]; | ||
| const currentElement = draft[index]; | ||
| if (index === 0 || !prevElement || !currentElement) { | ||
| return; | ||
| } | ||
| draft[index - 1] = currentElement; | ||
| draft[index] = prevElement; | ||
| } else { | ||
| const nextElement = draft[index + 1]; | ||
| const currentElement = draft[index]; | ||
| if (index === draft.length - 1 || !nextElement || !currentElement) { | ||
| return; | ||
| } | ||
| draft[index + 1] = currentElement; | ||
| draft[index] = nextElement; | ||
| } | ||
| }); | ||
| }; |
There was a problem hiding this comment.
The code could be shortened and optimized by generating the target indexes dynamically.
Also, the code should probably annotationEditIdx
const changeAnnotationOrder = (index: number, direction: 'up' | 'down'): void => {
const step = direction === 'up' ? -1 : 1;
setAnnotationSpecs((draft) => {
const current = draft[index];
const adjacent = draft[index + step];
if (!current || !adjacent) {
return;
}
draft[index + step] = current;
draft[index] = adjacent;
});
setAnnotationEditIdx(index + step);
};| export function AnnotationHydrationWrapper({ children }: AnnotationHydrationWrapperProps): ReactNode { | ||
| const annotationSpecs = useAnnotationSpecs(); | ||
| const { setAnnotationState } = useAnnotationActions(); | ||
| const annotations: Array<UseQueryResult<AnnotationData[]>> = useAnnotations(annotationSpecs); | ||
|
|
||
| useEffect(() => { | ||
| for (const [index, definition] of annotationSpecs.entries()) { | ||
| const query = annotations[index] ?? null; | ||
| if (query) { | ||
| setAnnotationState(definition.display.name, { | ||
| data: query.data ?? null, | ||
| isPending: query.isLoading, | ||
| error: (query?.error as Error) ?? null, | ||
| }); | ||
| } | ||
| } | ||
| }, [annotationSpecs, annotations, setAnnotationState]); | ||
|
|
||
| return <>{children}</>; | ||
| } |
| // Copyright 2025 The Perses Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); |
There was a problem hiding this comment.
Should the file be removed?
Description
Adding Annotation support with Provider, Editor, ...
PR related to:
Screenshots
Checklist
[<catalog_entry>] <commit message>naming convention using one of thefollowing
catalog_entryvalues:FEATURE,ENHANCEMENT,BUGFIX,BREAKINGCHANGE,DOC,IGNORE.UI Changes
See e2e docs for more details. Common issues include: