77 */
88
99import { FormattedRelative , I18nProvider } from '@kbn/i18n-react' ;
10- import React , { PropsWithChildren , useCallback , useMemo , useState } from 'react' ;
10+ import React , { useMemo } from 'react' ;
1111
1212import {
1313 type TableListViewKibanaDependencies ,
1414 TableListViewKibanaProvider ,
15- type UserContentCommonSchema ,
1615} from '@kbn/content-management-table-list-view-table' ;
1716import { TableListView } from '@kbn/content-management-table-list-view' ;
18- import { ViewMode } from '@kbn/embeddable-plugin/public' ;
19- import { reportPerformanceMetricEvent } from '@kbn/ebt-tools' ;
20- import type { SavedObjectsFindOptionsReference } from '@kbn/core/public' ;
17+
2118import { toMountPoint , useExecutionContext } from '@kbn/kibana-react-plugin/public' ;
2219
23- import {
24- DASHBOARD_CONTENT_ID ,
25- SAVED_OBJECT_DELETE_TIME ,
26- SAVED_OBJECT_LOADED_TIME ,
27- } from '../dashboard_constants' ;
28- import {
29- dashboardListingTableStrings ,
30- dashboardListingErrorStrings ,
31- } from './_dashboard_listing_strings' ;
3220import { pluginServices } from '../services/plugin_services' ;
33- import { confirmCreateWithUnsaved } from './confirm_overlays' ;
34- import { DashboardItem } from '../../common/content_management' ;
35- import { DashboardUnsavedListing } from './dashboard_unsaved_listing' ;
36- import { DashboardApplicationService } from '../services/application/types' ;
37- import { DashboardListingEmptyPrompt } from './dashboard_listing_empty_prompt' ;
38-
39- // because the type of `application.capabilities.advancedSettings` is so generic, the provider
40- // requiring the `save` key to be part of it is causing type issues - so, creating a custom type
41- type TableListViewApplicationService = DashboardApplicationService & {
42- capabilities : { advancedSettings : { save : boolean } } ;
43- } ;
44-
45- const SAVED_OBJECTS_LIMIT_SETTING = 'savedObjects:listingLimit' ;
46- const SAVED_OBJECTS_PER_PAGE_SETTING = 'savedObjects:perPage' ;
47-
48- interface DashboardSavedObjectUserContent extends UserContentCommonSchema {
49- attributes : {
50- title : string ;
51- description ?: string ;
52- timeRestore : boolean ;
53- } ;
54- }
5521
56- const toTableListViewSavedObject = ( hit : DashboardItem ) : DashboardSavedObjectUserContent => {
57- const { title, description, timeRestore } = hit . attributes ;
58- return {
59- type : 'dashboard' ,
60- id : hit . id ,
61- updatedAt : hit . updatedAt ! ,
62- references : hit . references ,
63- attributes : {
64- title,
65- description,
66- timeRestore,
67- } ,
68- } ;
69- } ;
70-
71- export type DashboardListingProps = PropsWithChildren < {
72- initialFilter ?: string ;
73- useSessionStorageIntegration ?: boolean ;
74- goToDashboard : ( dashboardId ?: string , viewMode ?: ViewMode ) => void ;
75- getDashboardUrl : ( dashboardId : string , usesTimeRestore : boolean ) => string ;
76- } > ;
22+ import { DashboardUnsavedListing } from './dashboard_unsaved_listing' ;
23+ import { useDashboardListingTable } from './hooks/use_dashboard_listing_table' ;
24+ import {
25+ DashboardListingProps ,
26+ DashboardSavedObjectUserContent ,
27+ TableListViewApplicationService ,
28+ } from './types' ;
7729
7830export const DashboardListing = ( {
7931 children,
@@ -89,123 +41,22 @@ export const DashboardListing = ({
8941 http,
9042 chrome : { theme } ,
9143 savedObjectsTagging,
92- dashboardSessionStorage,
93- settings : { uiSettings } ,
94- notifications : { toasts } ,
44+
9545 coreContext : { executionContext } ,
96- dashboardCapabilities : { showWriteControls } ,
97- dashboardContentManagement : { findDashboards, deleteDashboards } ,
9846 } = pluginServices . getServices ( ) ;
9947
100- const [ unsavedDashboardIds , setUnsavedDashboardIds ] = useState < string [ ] > (
101- dashboardSessionStorage . getDashboardIdsWithUnsavedChanges ( )
102- ) ;
103-
10448 useExecutionContext ( executionContext , {
10549 type : 'application' ,
10650 page : 'list' ,
10751 } ) ;
10852
109- const listingLimit = uiSettings . get ( SAVED_OBJECTS_LIMIT_SETTING ) ;
110- const initialPageSize = uiSettings . get ( SAVED_OBJECTS_PER_PAGE_SETTING ) ;
111-
112- const createItem = useCallback ( ( ) => {
113- if ( useSessionStorageIntegration && dashboardSessionStorage . dashboardHasUnsavedEdits ( ) ) {
114- confirmCreateWithUnsaved ( ( ) => {
115- dashboardSessionStorage . clearState ( ) ;
116- goToDashboard ( ) ;
117- } , goToDashboard ) ;
118- return ;
119- }
120- goToDashboard ( ) ;
121- } , [ dashboardSessionStorage , goToDashboard , useSessionStorageIntegration ] ) ;
122-
123- const fetchItems = useCallback (
124- (
125- searchTerm : string ,
126- {
127- references,
128- referencesToExclude,
129- } : {
130- references ?: SavedObjectsFindOptionsReference [ ] ;
131- referencesToExclude ?: SavedObjectsFindOptionsReference [ ] ;
132- } = { }
133- ) => {
134- const searchStartTime = window . performance . now ( ) ;
135-
136- return findDashboards
137- . search ( {
138- search : searchTerm ,
139- size : listingLimit ,
140- hasReference : references ,
141- hasNoReference : referencesToExclude ,
142- } )
143- . then ( ( { total, hits } ) => {
144- const searchEndTime = window . performance . now ( ) ;
145- const searchDuration = searchEndTime - searchStartTime ;
146- reportPerformanceMetricEvent ( pluginServices . getServices ( ) . analytics , {
147- eventName : SAVED_OBJECT_LOADED_TIME ,
148- duration : searchDuration ,
149- meta : {
150- saved_object_type : DASHBOARD_CONTENT_ID ,
151- } ,
152- } ) ;
153- return {
154- total,
155- hits : hits . map ( toTableListViewSavedObject ) ,
156- } ;
157- } ) ;
158- } ,
159- [ findDashboards , listingLimit ]
160- ) ;
161-
162- const deleteItems = useCallback (
163- async ( dashboardsToDelete : Array < { id : string } > ) => {
164- try {
165- const deleteStartTime = window . performance . now ( ) ;
166-
167- await deleteDashboards (
168- dashboardsToDelete . map ( ( { id } ) => {
169- dashboardSessionStorage . clearState ( id ) ;
170- return id ;
171- } )
172- ) ;
173-
174- const deleteDuration = window . performance . now ( ) - deleteStartTime ;
175- reportPerformanceMetricEvent ( pluginServices . getServices ( ) . analytics , {
176- eventName : SAVED_OBJECT_DELETE_TIME ,
177- duration : deleteDuration ,
178- meta : {
179- saved_object_type : DASHBOARD_CONTENT_ID ,
180- total : dashboardsToDelete . length ,
181- } ,
182- } ) ;
183- } catch ( error ) {
184- toasts . addError ( error , {
185- title : dashboardListingErrorStrings . getErrorDeletingDashboardToast ( ) ,
186- } ) ;
187- }
188-
189- setUnsavedDashboardIds ( dashboardSessionStorage . getDashboardIdsWithUnsavedChanges ( ) ) ;
190- } ,
191- [ dashboardSessionStorage , deleteDashboards , toasts ]
192- ) ;
193-
194- const editItem = useCallback (
195- ( { id } : { id : string | undefined } ) => goToDashboard ( id , ViewMode . EDIT ) ,
196- [ goToDashboard ]
197- ) ;
198- const emptyPrompt = (
199- < DashboardListingEmptyPrompt
200- createItem = { createItem }
201- goToDashboard = { goToDashboard }
202- unsavedDashboardIds = { unsavedDashboardIds }
203- setUnsavedDashboardIds = { setUnsavedDashboardIds }
204- useSessionStorageIntegration = { useSessionStorageIntegration }
205- />
206- ) ;
207-
208- const { getEntityName, getTableListTitle, getEntityNamePlural } = dashboardListingTableStrings ;
53+ const { unsavedDashboardIds, refreshUnsavedDashboards, tableListViewTableProps } =
54+ useDashboardListingTable ( {
55+ goToDashboard,
56+ getDashboardUrl,
57+ useSessionStorageIntegration,
58+ initialFilter,
59+ } ) ;
20960
21061 const savedObjectsTaggingFakePlugin = useMemo ( ( ) => {
21162 return savedObjectsTagging . hasApi // TODO: clean up this logic once https://github.com/elastic/kibana/issues/140433 is resolved
@@ -231,32 +82,13 @@ export const DashboardListing = ({
23182 FormattedRelative,
23283 } }
23384 >
234- < TableListView < DashboardSavedObjectUserContent >
235- getDetailViewLink = { ( { id, attributes : { timeRestore } } ) =>
236- getDashboardUrl ( id , timeRestore )
237- }
238- deleteItems = { ! showWriteControls ? undefined : deleteItems }
239- createItem = { ! showWriteControls ? undefined : createItem }
240- editItem = { ! showWriteControls ? undefined : editItem }
241- entityNamePlural = { getEntityNamePlural ( ) }
242- title = { getTableListTitle ( ) }
243- headingId = "dashboardListingHeading"
244- initialPageSize = { initialPageSize }
245- initialFilter = { initialFilter }
246- entityName = { getEntityName ( ) }
247- listingLimit = { listingLimit }
248- emptyPrompt = { emptyPrompt }
249- findItems = { fetchItems }
250- id = "dashboard"
251- >
85+ < TableListView < DashboardSavedObjectUserContent > { ...tableListViewTableProps } >
25286 < >
25387 { children }
25488 < DashboardUnsavedListing
25589 goToDashboard = { goToDashboard }
25690 unsavedDashboardIds = { unsavedDashboardIds }
257- refreshUnsavedDashboards = { ( ) =>
258- setUnsavedDashboardIds ( dashboardSessionStorage . getDashboardIdsWithUnsavedChanges ( ) )
259- }
91+ refreshUnsavedDashboards = { refreshUnsavedDashboards }
26092 />
26193 </ >
26294 </ TableListView >
0 commit comments