/* * 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 React, { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import type { FileJSON } from '@kbn/files-plugin/common'; import type { FilesClientResponses } from '@kbn/files-plugin/public'; import { EuiPageTemplate, EuiInMemoryTable, EuiInMemoryTableProps, EuiButton, EuiIcon, EuiButtonIcon, EuiLink, } from '@elastic/eui'; import { CoreStart } from '@kbn/core/public'; import { MyFilePicker } from './file_picker'; import type { MyImageMetadata } from '../../common'; import type { FileClients } from '../types'; import { DetailsFlyout } from './details_flyout'; import { ConfirmButtonIcon } from './confirm_button'; import { Modal } from './modal'; interface FilesExampleAppDeps { files: FileClients; notifications: CoreStart['notifications']; } type ListResponse = FilesClientResponses['list']; export const FilesExampleApp = ({ files, notifications }: FilesExampleAppDeps) => { const { data, isLoading, error, refetch } = useQuery( ['files'], () => files.example.list(), { refetchOnWindowFocus: false } ); const [showUploadModal, setShowUploadModal] = useState(false); const [showFilePickerModal, setShowFilePickerModal] = useState(false); const [isDeletingFile, setIsDeletingFile] = useState(false); const [selectedItem, setSelectedItem] = useState>(); const renderToolsRight = () => { return [ setShowFilePickerModal(true)} isDisabled={isLoading || isDeletingFile} iconType="eye" > Select a file , setShowUploadModal(true)} isDisabled={isLoading || isDeletingFile} iconType="exportAction" > Upload image , ]; }; const items = [...(data?.files ?? [])].reverse(); const columns: EuiInMemoryTableProps>['columns'] = [ { field: 'name', name: 'Name', render: (name, item) => ( setSelectedItem(item)}> {name} ), }, { field: 'status', name: 'Status', render: (status: FileJSON['status']) => status === 'READY' ? ( ) : status === 'AWAITING_UPLOAD' ? ( ) : ( ), }, { name: 'Actions', actions: [ { name: 'View', description: 'View file', isPrimary: true, render: (item) => ( setSelectedItem(item)} /> ), }, { name: 'Delete', description: 'Delete this file', render: (item) => ( { try { setIsDeletingFile(true); await files.example.delete({ id: item.id }); await refetch(); } finally { setIsDeletingFile(false); } }} /> ), }, ], }, ]; return ( <> {selectedItem && ( setSelectedItem(undefined)} /> )} {showUploadModal && ( setShowUploadModal(false)} onUploaded={() => { notifications.toasts.addSuccess('Uploaded file!'); refetch(); setShowUploadModal(false); }} /> )} {showFilePickerModal && ( setShowFilePickerModal(false)} onUpload={() => { notifications.toasts.addSuccess({ title: 'Uploaded files', }); refetch(); }} onDone={(ids) => { notifications.toasts.addSuccess({ title: 'Selected files!', text: 'IDS:' + JSON.stringify(ids, null, 2), }); setShowFilePickerModal(false); }} /> )} ); };