From 75ab1b1a51e548b86c4e4235b61de54f48516bd0 Mon Sep 17 00:00:00 2001 From: Danil Kostromin Date: Mon, 15 Jan 2024 06:12:47 +0200 Subject: [PATCH] feat(import): add live import logs via polling for changes Changes: - Poll import logs every 5 seconds for updates - Fix broken link to live import logs Signed-off-by: Danil Kostromin --- .../import-log.component.tsx | 61 +++++++++++++------ ...n-form-result-list.component.spec.tsx.snap | 42 +++++++------ .../import-run-form-result-list.component.tsx | 11 +--- 3 files changed, 68 insertions(+), 46 deletions(-) diff --git a/libs/bublik/features/run-import/src/lib/import-events-table/import-log.component.tsx b/libs/bublik/features/run-import/src/lib/import-events-table/import-log.component.tsx index ce70e13f..a4ed3124 100644 --- a/libs/bublik/features/run-import/src/lib/import-events-table/import-log.component.tsx +++ b/libs/bublik/features/run-import/src/lib/import-events-table/import-log.component.tsx @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* SPDX-FileCopyrightText: 2021-2023 OKTET Labs Ltd. */ -import { useGetImportLogQuery, usePrefetch } from '@/services/bublik-api'; +import { ComponentProps, ElementRef, useEffect, useRef } from 'react'; + +import { useGetImportLogQuery } from '@/services/bublik-api'; import { Dialog, DialogClose, @@ -13,60 +15,81 @@ import { import { ImportJsonLog } from '@/shared/types'; import { useCopyToClipboard } from '@/shared/hooks'; -export interface JsonLogContainerProsp { +export interface JsonLogContainerProps { taskId: string; + enablePolling?: boolean; } -export const JsonLogContainer = ({ taskId }: JsonLogContainerProsp) => { - const prefetchLog = usePrefetch('getImportLog'); - +export const JsonLogContainer = ({ + taskId, + enablePolling +}: JsonLogContainerProps) => { return ( - prefetchLog(taskId)}> + - + ); }; export interface ImportLogTableContainerProps { - taskId: string; + taskId: ComponentProps['taskId']; + enablePolling: ComponentProps['enablePolling']; } export const ImportLogTableContainer = ( props: ImportLogTableContainerProps ) => { const { taskId } = props; + const { data, isLoading, isFetching, error } = useGetImportLogQuery(taskId, { + pollingInterval: props.enablePolling ? 5000 : 0 + }); - const { data, isLoading, error } = useGetImportLogQuery(taskId); + const scrollRef = useRef>(null); const [, copy] = useCopyToClipboard({ onSuccess: () => toast.success('Copied to clipboard') }); - if (error) { - return
Error..
; - } + useEffect(() => { + const el = scrollRef.current; + if (!el) return; + if (!props.enablePolling) return; + + setTimeout( + () => el.scrollTo({ top: el.scrollHeight, behavior: 'smooth' }), + 0 + ); + }, [props.enablePolling, isFetching]); + + if (error) return
Error..
; - if (isLoading) { - return ; - } + if (isLoading) return ; - if (!data) { - return
Empty
; - } + if (!data) return
Empty
; return ( <>

Logs

+ {props.enablePolling ? ( + + ) : null}
-
+
diff --git a/libs/bublik/features/run-import/src/lib/import-run-form/__snapshots__/import-run-form-result-list.component.spec.tsx.snap b/libs/bublik/features/run-import/src/lib/import-run-form/__snapshots__/import-run-form-result-list.component.spec.tsx.snap index c0355c46..bd2dd0d1 100644 --- a/libs/bublik/features/run-import/src/lib/import-run-form/__snapshots__/import-run-form-result-list.component.spec.tsx.snap +++ b/libs/bublik/features/run-import/src/lib/import-run-form/__snapshots__/import-run-form-result-list.component.spec.tsx.snap @@ -36,11 +36,13 @@ exports[`ImportRunFormResultList > should render successfully 1`] = `