From d08861fb30bf34b05366ab182cbc3b8ae86e698e Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Thu, 13 Feb 2025 01:16:11 +0530 Subject: [PATCH] (fix/docs) revert effected portions --- frontend/src/settings/Analytics.tsx | 50 ++-- frontend/src/settings/Documents.tsx | 358 ++++++++++++++++++++++++---- 2 files changed, 344 insertions(+), 64 deletions(-) diff --git a/frontend/src/settings/Analytics.tsx b/frontend/src/settings/Analytics.tsx index d163066f..e748e943 100644 --- a/frontend/src/settings/Analytics.tsx +++ b/frontend/src/settings/Analytics.tsx @@ -121,12 +121,12 @@ export default function Analytics() { } }; - const fetchMessagesData = useCallback(async () => { - setLoadingWithMinDuration(setLoadingMessages, true); + const fetchMessagesData = async (chatbot_id?: string, filter?: string) => { + setLoadingMessages(true); try { const response = await userService.getMessageAnalytics({ - api_key_id: selectedChatbot?.id, - filter_option: messagesFilter.value, + api_key_id: chatbot_id, + filter_option: filter, }); if (!response.ok) { throw new Error('Failed to fetch analytics data'); @@ -139,14 +139,14 @@ export default function Analytics() { } finally { setLoadingWithMinDuration(setLoadingMessages, false); } - }, [selectedChatbot, messagesFilter, setLoadingWithMinDuration]); + }; - const fetchTokenData = useCallback(async () => { - setLoadingWithMinDuration(setLoadingTokens, true); + const fetchTokenData = async (chatbot_id?: string, filter?: string) => { + setLoadingTokens(true); try { const response = await userService.getTokenAnalytics({ - api_key_id: selectedChatbot?.id, - filter_option: tokenUsageFilter.value, + api_key_id: chatbot_id, + filter_option: filter, }); if (!response.ok) { throw new Error('Failed to fetch analytics data'); @@ -159,14 +159,14 @@ export default function Analytics() { } finally { setLoadingWithMinDuration(setLoadingTokens, false); } - }, [selectedChatbot, tokenUsageFilter, setLoadingWithMinDuration]); + }; - const fetchFeedbackData = useCallback(async () => { - setLoadingWithMinDuration(setLoadingFeedback, true); + const fetchFeedbackData = async (chatbot_id?: string, filter?: string) => { + setLoadingFeedback(true); try { const response = await userService.getFeedbackAnalytics({ - api_key_id: selectedChatbot?.id, - filter_option: feedbackFilter.value, + api_key_id: chatbot_id, + filter_option: filter, }); if (!response.ok) { throw new Error('Failed to fetch analytics data'); @@ -179,17 +179,29 @@ export default function Analytics() { } finally { setLoadingWithMinDuration(setLoadingFeedback, false); } - }, [selectedChatbot, feedbackFilter, setLoadingWithMinDuration]); + }; useEffect(() => { fetchChatbots(); }, []); useEffect(() => { - fetchMessagesData(); - fetchTokenData(); - fetchFeedbackData(); - }, [fetchMessagesData, fetchTokenData, fetchFeedbackData]); + const id = selectedChatbot?.id; + const filter = messagesFilter; + fetchMessagesData(id, filter?.value); + }, [selectedChatbot, messagesFilter]); + + useEffect(() => { + const id = selectedChatbot?.id; + const filter = tokenUsageFilter; + fetchTokenData(id, filter?.value); + }, [selectedChatbot, tokenUsageFilter]); + + useEffect(() => { + const id = selectedChatbot?.id; + const filter = feedbackFilter; + fetchFeedbackData(id, filter?.value); + }, [selectedChatbot, feedbackFilter]); return (
diff --git a/frontend/src/settings/Documents.tsx b/frontend/src/settings/Documents.tsx index 44a12104..99b40dc9 100644 --- a/frontend/src/settings/Documents.tsx +++ b/frontend/src/settings/Documents.tsx @@ -1,24 +1,33 @@ -import React, { useState, useEffect, useCallback } from 'react'; -import PropTypes from 'prop-types'; -import userService from '../api/services/userService'; -import SyncIcon from '../assets/sync.svg'; -import Trash from '../assets/trash.svg'; -import caretSort from '../assets/caret-sort.svg'; -import DropdownMenu from '../components/DropdownMenu'; -import SkeletonLoader from '../components/SkeletonLoader'; -import Input from '../components/Input'; -import Upload from '../upload/Upload'; -import Pagination from '../components/DocumentPagination'; +import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch } from 'react-redux'; -import { Doc, DocumentsProps, ActiveState } from '../models/misc'; -import { getDocs, getDocsWithPagination } from '../preferences/preferenceApi'; -import { setSourceDocs } from '../preferences/preferenceSlice'; -import { setPaginatedDocuments } from '../preferences/preferenceSlice'; -import { formatDate } from '../utils/dateTimeUtils'; -import ConfirmationModal from '../modals/ConfirmationModal'; -// Utility function to format numbers +import userService from '../api/services/userService'; +import ArrowLeft from '../assets/arrow-left.svg'; +import caretSort from '../assets/caret-sort.svg'; +import Edit from '../assets/edit.svg'; +import NoFilesDarkIcon from '../assets/no-files-dark.svg'; +import NoFilesIcon from '../assets/no-files.svg'; +import SyncIcon from '../assets/sync.svg'; +import Trash from '../assets/trash.svg'; +import Pagination from '../components/DocumentPagination'; +import DropdownMenu from '../components/DropdownMenu'; +import Input from '../components/Input'; +import SkeletonLoader from '../components/SkeletonLoader'; +import Spinner from '../components/Spinner'; +import { useDarkTheme } from '../hooks'; +import ChunkModal from '../modals/ChunkModal'; +import ConfirmationModal from '../modals/ConfirmationModal'; +import { ActiveState, Doc, DocumentsProps } from '../models/misc'; +import { getDocs, getDocsWithPagination } from '../preferences/preferenceApi'; +import { + setPaginatedDocuments, + setSourceDocs, +} from '../preferences/preferenceSlice'; +import Upload from '../upload/Upload'; +import { formatDate } from '../utils/dateTimeUtils'; +import { ChunkType } from './types'; + const formatTokens = (tokens: number): string => { const roundToTwoDecimals = (num: number): string => { return (Math.round((num + Number.EPSILON) * 100) / 100).toString(); @@ -35,19 +44,20 @@ const formatTokens = (tokens: number): string => { } }; -const Documents: React.FC = ({ +export default function Documents({ paginatedDocuments, handleDeleteDocument, -}) => { +}: DocumentsProps) { const { t } = useTranslation(); const dispatch = useDispatch(); + const [searchTerm, setSearchTerm] = useState(''); const [modalState, setModalState] = useState('INACTIVE'); const [isOnboarding, setIsOnboarding] = useState(false); const [loading, setLoading] = useState(false); const [sortField, setSortField] = useState<'date' | 'tokens'>('date'); const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc'); - + // Pagination const [currentPage, setCurrentPage] = useState(1); const [rowsPerPage, setRowsPerPage] = useState(10); const [totalPages, setTotalPages] = useState(1); @@ -58,6 +68,7 @@ const Documents: React.FC = ({ { label: t('settings.documents.syncFrequency.weekly'), value: 'weekly' }, { label: t('settings.documents.syncFrequency.monthly'), value: 'monthly' }, ]; + const [showDocumentChunks, setShowDocumentChunks] = useState(); const setLoadingWithMinDuration = useCallback((isLoading: boolean) => { if (isLoading) { @@ -77,7 +88,11 @@ const Documents: React.FC = ({ ) => { const page = pageNumber ?? currentPage; const rowsPerPg = rows ?? rowsPerPage; + + // If field is undefined, (Pagination or Search) use the current sortField const newSortField = field ?? sortField; + + // If field is undefined, (Pagination or Search) use the current sortOrder const newSortOrder = field === sortField ? sortOrder === 'asc' @@ -85,6 +100,7 @@ const Documents: React.FC = ({ : 'asc' : sortOrder; + // If field is defined, update the sortField and sortOrder if (field) { setSortField(newSortField); setSortOrder(newSortOrder); @@ -101,22 +117,13 @@ const Documents: React.FC = ({ .then((data) => { dispatch(setPaginatedDocuments(data ? data.docs : [])); setTotalPages(data ? data.totalPages : 0); - setLoadingWithMinDuration(false); }) - .catch((error) => { - console.error(error); + .catch((error) => console.error(error)) + .finally(() => { setLoadingWithMinDuration(false); }); }, - [ - currentPage, - rowsPerPage, - sortField, - sortOrder, - searchTerm, - dispatch, - setLoadingWithMinDuration, - ], + [currentPage, rowsPerPage, sortField, sortOrder, searchTerm], ); const handleManageSync = (doc: Doc, sync_frequency: string) => { @@ -140,10 +147,9 @@ const Documents: React.FC = ({ setPaginatedDocuments(paginatedData ? paginatedData.docs : []), ); setTotalPages(paginatedData ? paginatedData.totalPages : 0); - setLoadingWithMinDuration(false); }) - .catch((error) => { - console.error('Error in handleManageSync:', error); + .catch((error) => console.error('Error in handleManageSync:', error)) + .finally(() => { setLoadingWithMinDuration(false); }); }; @@ -172,7 +178,14 @@ const Documents: React.FC = ({ refreshDocs(undefined, 1, rowsPerPage); }, [searchTerm]); - return ( + return showDocumentChunks ? ( + { + setShowDocumentChunks(undefined); + }} + /> + ) : (
@@ -196,6 +209,7 @@ const Documents: React.FC = ({ setSearchTerm(e.target.value); setCurrentPage(1); }} + borderVariant="thin" />