diff --git a/.changeset/eleven-suns-nail.md b/.changeset/eleven-suns-nail.md new file mode 100644 index 00000000000..51971af7ddf --- /dev/null +++ b/.changeset/eleven-suns-nail.md @@ -0,0 +1,5 @@ +--- +"@wso2is/admin.logs.v1": patch +--- + +Fix bugs in the infinite scrolling mechanism in logs section diff --git a/features/admin.logs.v1/pages/diagnostic-logs-page.tsx b/features/admin.logs.v1/pages/diagnostic-logs-page.tsx index aec36be6cf6..1ef428b81a5 100644 --- a/features/admin.logs.v1/pages/diagnostic-logs-page.tsx +++ b/features/admin.logs.v1/pages/diagnostic-logs-page.tsx @@ -59,6 +59,8 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement ["data-componentid"]: componentId } = props; + const scrollRef: MutableRefObject = useRef(null); + const [ isPreviousEmpty, setIsPreviousEmpty ] = useState(false); const [ isNextEmpty, setIsNextEmpty ] = useState(false); const [ searchQuery, setSearchQuery ] = useState(""); @@ -78,9 +80,31 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement const [ timerRunning, setTimerRunning ] = useState(false); const [ diagnosticLogList, setDiagnosticLogList ] = useState([]); - const { t } = useTranslation(); const timeZone: string = "GMT+0000 UTC"; + const { t } = useTranslation(); + + const { error, list, loading, next, previous } = useFetch(requestPayload); + + useEffect(() => { + const current: number = getDateTimeWithOffset(timeZone); + const currentEndTime: string = current.toString(); + const currentStartTime: string = (current - 3600*1000*timeRange).toString(); + + setEndTime(currentEndTime); + setStartTime(currentStartTime); + setLastDiagnosticLogRequestTime(currentEndTime); + + // Fetch logs automatically during the first render. + setRequestPayload({ + endTime: currentEndTime, + filter: "", + limit: LogsConstants.LOG_FETCH_COUNT, + logType: TabIndex.DIAGNOSTIC_LOGS, + startTime: currentStartTime + }); + }, []); + useEffect(() => { // Display a message if the logs are not fetched within 15 seconds of the request. setTimeout(() => { @@ -107,33 +131,9 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement setShowRefreshButton(false); }, [ endTime, startTime, inputQuery, filterQuery ]); - useEffect(() => { - const current: number = getDateTimeWithOffset(timeZone); - const currentEndTime: string = current.toString(); - const currentStartTime: string = (current - 3600*1000*timeRange).toString(); - - setEndTime(currentEndTime); - setStartTime(currentStartTime); - setLastDiagnosticLogRequestTime(currentEndTime); - - // Fetch logs automatically during the first render. - setRequestPayload({ - endTime: currentEndTime, - filter: "", - limit: LogsConstants.LOG_FETCH_COUNT, - logType: TabIndex.DIAGNOSTIC_LOGS, - startTime: currentStartTime - }); - }, []); - - const scrollRef: MutableRefObject = useRef(null); - - const { error, list, loading, next, previous } = useFetch(requestPayload); - - useEffect (() => { if (!loading && list.length > 0) { - setDiagnosticLogList(list); + setDiagnosticLogList((previousLogs: InterfaceLogEntry[]) => [ ...previousLogs, ...list ]); } }, [ list, loading ]); @@ -144,24 +144,7 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement const element: any = e.target; setShowDelayMessage(false); - /** - * When the at the top of the log container - */ - if (element.scrollTop === 0) { - if (previous) { - setRequestPayload({ - filter: searchQuery, - limit: LogsConstants.LOG_FETCH_COUNT, - logType: TabIndex.DIAGNOSTIC_LOGS, - previousToken: previous - }); - setIsPreviousEmpty(false); - setIsNextEmpty(false); - } else { - setIsPreviousEmpty(true); - } - } /** * When at the bottom of the log container */ @@ -181,61 +164,6 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement } }; - const renderDiagnosticLogContent = () : ReactElement => { - - return ( -
-
- { advancedSearchFilter() } - setStartTime(value) } - setToTime={ (value: string): void => setEndTime(value) } - setTimeRange={ (value: number): void => setTimeRange(value) } - data-componentid={ componentId } - /> - { showRefreshButton - ? ( - fetchLatestLogs() } - data-componentid={ `${ componentId }-refresh-button` } - > - - { t("extensions:develop.monitor.filter.refreshButton.label") } - - ) : ( - handleSearch() } - data-componentid={ `${ componentId }-search-button` } - > - - { t("extensions:develop.monitor.filter.queryButton.label") } - - ) - } -
-
- <> -
- { renderRefreshTime() } - - { filterList && filterList.map( - (value: { key: string, value:string }, index: number) => - () - ) } - { resolveClearAllFilters() } - -
- { resolveDiagnosticLogs() } - -
-
- ); - }; - /** * Build filter query */ @@ -257,7 +185,9 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement * * @param query - search query with filters */ - const handleSearch = () => { + const handleSearch = (overrideQuery?: string) => { + setDiagnosticLogList([]); + let currentQuery: string = ""; if (inputQuery.length === 0) { @@ -268,7 +198,7 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement currentQuery = ` and ${currentQuery}`; } } - setSearchQuery(currentQuery); + setSearchQuery(overrideQuery === undefined ? currentQuery : overrideQuery); // If the custom time range is not defined, // start, end times needs to be updated to account for the time it takes to click the search button. @@ -286,7 +216,7 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement setRequestPayload({ endTime: currentEndTime, - filter: `${filterQuery}${currentQuery}`, + filter: `${filterQuery}${overrideQuery === undefined ? currentQuery : overrideQuery}`, limit: LogsConstants.LOG_FETCH_COUNT, logType: TabIndex.DIAGNOSTIC_LOGS, startTime: currentStartTime @@ -300,7 +230,7 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement setSearchQuery(""); setInputQuery(""); setShowRefreshButton(false); - + handleSearch(""); }; /** @@ -354,6 +284,60 @@ const DiagnosticLogsPage = (props: DiagnosticPagePropsInterface) : ReactElement setFilterList([]); }; + const renderDiagnosticLogContent = () : ReactElement => { + return ( +
+
+ { advancedSearchFilter() } + setStartTime(value) } + setToTime={ (value: string): void => setEndTime(value) } + setTimeRange={ (value: number): void => setTimeRange(value) } + data-componentid={ componentId } + /> + { showRefreshButton + ? ( + fetchLatestLogs() } + data-componentid={ `${ componentId }-refresh-button` } + > + + { t("extensions:develop.monitor.filter.refreshButton.label") } + + ) : ( + handleSearch() } + data-componentid={ `${ componentId }-search-button` } + > + + { t("extensions:develop.monitor.filter.queryButton.label") } + + ) + } +
+
+ <> +
+ { renderRefreshTime() } + + { filterList && filterList.map( + (value: { key: string, value:string }, index: number) => + () + ) } + { resolveClearAllFilters() } + +
+ { resolveDiagnosticLogs() } + +
+
+ ); + }; + /** * Returns search component */