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

[Embeddables Rebuild] Move titles API #179202

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import { EuiMarkdownEditor, EuiMarkdownFormat } from '@elastic/eui';
import { css } from '@emotion/react';
import {
initializeReactEmbeddableTitles,
ReactEmbeddableFactory,
registerReactEmbeddableFactory,
} from '@kbn/embeddable-plugin/public';
import { i18n } from '@kbn/i18n';
import { useInheritedViewMode, useStateFromPublishingSubject } from '@kbn/presentation-publishing';
import {
initializeTitles,
useInheritedViewMode,
useStateFromPublishingSubject,
} from '@kbn/presentation-publishing';
import { euiThemeVars } from '@kbn/ui-theme';
import React from 'react';
import { BehaviorSubject } from 'rxjs';
Expand All @@ -40,7 +43,7 @@ const markdownEmbeddableFactory: ReactEmbeddableFactory<
/**
* initialize state (source of truth)
*/
const { titlesApi, titleComparators, serializeTitles } = initializeReactEmbeddableTitles(state);
const { titlesApi, titleComparators, serializeTitles } = initializeTitles(state);
const content$ = new BehaviorSubject(state.content);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* Side Public License, v 1.
*/

import {
DefaultEmbeddableApi,
SerializedReactEmbeddableTitles,
} from '@kbn/embeddable-plugin/public';
import { DefaultEmbeddableApi } from '@kbn/embeddable-plugin/public';
import { SerializedTitles } from '@kbn/presentation-publishing';

export type MarkdownEditorSerializedState = SerializedReactEmbeddableTitles & {
export type MarkdownEditorSerializedState = SerializedTitles & {
content: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import {
DATA_VIEW_SAVED_OBJECT_TYPE,
} from '@kbn/data-views-plugin/public';
import {
initializeReactEmbeddableTitles,
ReactEmbeddableFactory,
registerReactEmbeddableFactory,
} from '@kbn/embeddable-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { i18n } from '@kbn/i18n';
import { useBatchedPublishingSubjects } from '@kbn/presentation-publishing';
import { initializeTitles, useBatchedPublishingSubjects } from '@kbn/presentation-publishing';
import { LazyDataViewPicker, withSuspense } from '@kbn/presentation-util-plugin/public';
import { euiThemeVars } from '@kbn/ui-theme';
import {
Expand Down Expand Up @@ -83,8 +82,7 @@ export const registerFieldListFactory = (
},
buildEmbeddable: async (initialState, buildApi) => {
const subscriptions = new Subscription();
const { titlesApi, titleComparators, serializeTitles } =
initializeReactEmbeddableTitles(initialState);
const { titlesApi, titleComparators, serializeTitles } = initializeTitles(initialState);

const allDataViews = await dataViews.getIdsWithTitle();
const selectedDataViewId$ = new BehaviorSubject<string | undefined>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* Side Public License, v 1.
*/

import {
DefaultEmbeddableApi,
SerializedReactEmbeddableTitles,
} from '@kbn/embeddable-plugin/public';
import { DefaultEmbeddableApi } from '@kbn/embeddable-plugin/public';
import { SerializedTitles } from '@kbn/presentation-publishing';

export type FieldListSerializedStateState = SerializedReactEmbeddableTitles & {
export type FieldListSerializedStateState = SerializedTitles & {
dataViewId?: string;
selectedFieldNames?: string[];
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export type { ComparatorFunction, ComparatorDefinition, StateComparators } from './types';
export { getInitialValuesFromComparators, runComparators } from './state_comparators';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { StateComparators } from './types';

const defaultComparator = <T>(a: T, b: T) => a === b;

export const getInitialValuesFromComparators = <StateType extends object = object>(
comparators: StateComparators<StateType>,
comparatorKeys: Array<keyof StateType>
) => {
const initialValues: Partial<StateType> = {};
for (const key of comparatorKeys) {
const comparatorSubject = comparators[key][0]; // 0th element of tuple is the subject
initialValues[key] = comparatorSubject?.value;
}
return initialValues;
};

export const runComparators = <StateType extends object = object>(
comparators: StateComparators<StateType>,
comparatorKeys: Array<keyof StateType>,
lastSavedState: StateType | undefined,
latestState: Partial<StateType>
) => {
if (!lastSavedState) {
// if we have no last saved state, everything is considered a change
return latestState;
}
const latestChanges: Partial<StateType> = {};
for (const key of comparatorKeys) {
const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator
const comparator = customComparator ?? defaultComparator;
if (!comparator(lastSavedState?.[key], latestState[key], lastSavedState, latestState)) {
latestChanges[key] = latestState[key];
}
}
return Object.keys(latestChanges).length > 0 ? latestChanges : undefined;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { PublishingSubject } from '../publishing_subject';

export type ComparatorFunction<StateType, KeyType extends keyof StateType> = (
last: StateType[KeyType] | undefined,
current: StateType[KeyType] | undefined,
lastState?: Partial<StateType>,
currentState?: Partial<StateType>
) => boolean;

export type ComparatorDefinition<StateType, KeyType extends keyof StateType> = [
PublishingSubject<StateType[KeyType]>,
(value: StateType[KeyType]) => void,
ComparatorFunction<StateType, KeyType>?
];

export type StateComparators<StateType> = {
[KeyType in keyof StateType]: ComparatorDefinition<StateType, KeyType>;
};
81 changes: 43 additions & 38 deletions packages/presentation/presentation_publishing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ export interface EmbeddableApiContext {
embeddable: unknown;
}

export {
getInitialValuesFromComparators,
runComparators,
type ComparatorDefinition,
type ComparatorFunction,
type StateComparators,
} from './comparators';
export {
apiCanAccessViewMode,
getInheritedViewMode,
getViewModeSubject,
useInheritedViewMode,
type CanAccessViewMode,
} from './interfaces/can_access_view_mode';
export {
apiPublishesPhaseEvents,
type PublishesPhaseEvents,
type PhaseEvent,
type PhaseEventType,
} from './interfaces/publishes_phase_events';
export { apiHasDisableTriggers, type HasDisableTriggers } from './interfaces/has_disable_triggers';
export { hasEditCapabilities, type HasEditCapabilities } from './interfaces/has_edit_capabilities';
export { apiHasParentApi, type HasParentApi } from './interfaces/has_parent_api';
export {
apiHasSupportedTriggers,
type HasSupportedTriggers,
} from './interfaces/has_supported_triggers';
export {
apiHasType,
apiIsOfType,
type HasType,
type HasTypeDisplayName,
} from './interfaces/has_type';
export { apiHasUniqueId, type HasUniqueId } from './interfaces/has_uuid';
export {
apiPublishesBlockingError,
useBlockingError,
Expand All @@ -56,57 +63,55 @@ export {
type PublishesDisabledActionIds,
} from './interfaces/publishes_disabled_action_ids';
export {
apiPublishesUnifiedSearch,
apiPublishesPhaseEvents,
type PhaseEvent,
type PhaseEventType,
type PublishesPhaseEvents,
} from './interfaces/publishes_phase_events';
export {
apiPublishesSavedObjectId,
useSavedObjectId,
type PublishesSavedObjectId,
} from './interfaces/publishes_saved_object_id';
export {
apiPublishesPartialUnifiedSearch,
apiPublishesUnifiedSearch,
apiPublishesWritableUnifiedSearch,
type PublishesUnifiedSearch,
type PublishesWritableUnifiedSearch,
} from './interfaces/publishes_unified_search';
export {
apiPublishesUnsavedChanges,
useUnsavedChanges,
type PublishesUnsavedChanges,
} from './interfaces/publishes_unsaved_changes';
export {
apiPublishesViewMode,
apiPublishesWritableViewMode,
useViewMode,
type PublishesViewMode,
type PublishesWritableViewMode,
type ViewMode,
} from './interfaces/publishes_view_mode';
export {
apiPublishesPanelDescription,
apiPublishesWritablePanelDescription,
useDefaultPanelDescription,
usePanelDescription,
type PublishesPanelDescription,
type PublishesWritablePanelDescription,
} from './interfaces/publishes_panel_description';
} from './interfaces/titles/publishes_panel_description';
export {
getPanelTitle,
apiPublishesPanelTitle,
apiPublishesWritablePanelTitle,
useDefaultPanelTitle,
useHidePanelTitle,
usePanelTitle,
getPanelTitle,
type PublishesPanelTitle,
type PublishesWritablePanelTitle,
} from './interfaces/publishes_panel_title';
export {
apiPublishesSavedObjectId,
useSavedObjectId,
type PublishesSavedObjectId,
} from './interfaces/publishes_saved_object_id';
export { apiHasUniqueId, type HasUniqueId } from './interfaces/has_uuid';
export { apiHasDisableTriggers, type HasDisableTriggers } from './interfaces/has_disable_triggers';
export {
apiHasSupportedTriggers,
type HasSupportedTriggers,
} from './interfaces/has_supported_triggers';
export {
apiPublishesViewMode,
apiPublishesWritableViewMode,
useViewMode,
type PublishesViewMode,
type PublishesWritableViewMode,
type ViewMode,
} from './interfaces/publishes_view_mode';
export {
type PublishesUnsavedChanges,
apiPublishesUnsavedChanges,
useUnsavedChanges,
} from './interfaces/publishes_unsaved_changes';
} from './interfaces/titles/publishes_panel_title';
export { initializeTitles, type SerializedTitles } from './interfaces/titles/titles_api';
export {
useBatchedPublishingSubjects,
useStateFromPublishingSubject,
usePublishingSubject,
useStateFromPublishingSubject,
type PublishingSubject,
} from './publishing_subject';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { PublishingSubject, useStateFromPublishingSubject } from '../publishing_subject';
import { PublishingSubject, useStateFromPublishingSubject } from '../../publishing_subject';

export interface PublishesPanelDescription {
panelDescription: PublishingSubject<string | undefined>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { PublishingSubject, useStateFromPublishingSubject } from '../publishing_subject';
import { PublishingSubject } from '../../publishing_subject';

export interface PublishesPanelTitle {
panelTitle: PublishingSubject<string | undefined>;
Expand Down Expand Up @@ -44,24 +44,3 @@ export const apiPublishesWritablePanelTitle = (
typeof (unknownApi as PublishesWritablePanelTitle).setHidePanelTitle === 'function'
);
};

/**
* A hook that gets this API's panel title as a reactive variable which will cause re-renders on change.
*/
export const usePanelTitle = (api: Partial<PublishesPanelTitle> | undefined) => {
const title = useStateFromPublishingSubject(api?.panelTitle);
const defaultTitle = useStateFromPublishingSubject(api?.defaultPanelTitle);
return title || defaultTitle;
};

/**
* A hook that gets this API's hide panel title setting as a reactive variable which will cause re-renders on change.
*/
export const useHidePanelTitle = (api: Partial<PublishesPanelTitle> | undefined) =>
useStateFromPublishingSubject(api?.hidePanelTitle);

/**
* A hook that gets this API's default title as a reactive variable which will cause re-renders on change.
*/
export const useDefaultPanelTitle = (api: Partial<PublishesPanelTitle> | undefined) =>
useStateFromPublishingSubject(api?.defaultPanelTitle);
Loading