Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

fix: Cannot view PDF (#11706)

René Wangd99d3694eeb3
diff --git a/src/app/[variants]/(main)/resource/(home)/index.tsx b/src/app/[variants]/(main)/resource/(home)/index.tsx
index 8789517d52f..ee1f7599ecd 100644
--- a/src/app/[variants]/(main)/resource/(home)/index.tsx
+++ b/src/app/[variants]/(main)/resource/(home)/index.tsx
@@ -1,28 +1,20 @@
'use client';
-import { memo, useEffect, useLayoutEffect } from 'react';
+import { memo, useLayoutEffect } from 'react';
import { useLocation, useSearchParams } from 'react-router-dom';
import ResourceManager from '@/features/ResourceManager';
-import { documentSelectors, useFileStore } from '@/store/file';
import { FilesTabs } from '@/types/files';
+import { useInitFileCheck } from '../features/hooks/useInitFileCheck';
import { useResourceManagerStore } from '../features/store';
const ResourceHomePage = memo(() => {
const [searchParams] = useSearchParams();
const location = useLocation();
- const [setMode, setCurrentViewItemId, setCategory, setLibraryId] = useResourceManagerStore(
- (s) => [s.setMode, s.setCurrentViewItemId, s.setCategory, s.setLibraryId],
- );
+ const [setCategory, setLibraryId] = useResourceManagerStore((s) => [s.setCategory, s.setLibraryId]);
const categoryParam = (searchParams.get('category') as FilesTabs) || FilesTabs.All;
- const fileId = searchParams.get('file');
-
- // Fetch file or document details to determine correct mode
- const useFetchKnowledgeItem = useFileStore((s) => s.useFetchKnowledgeItem);
- const { data: fileData } = useFetchKnowledgeItem(fileId || undefined);
- const documentData = useFileStore(documentSelectors.getDocumentById(fileId || undefined));
// Clear libraryId when on home route using useLayoutEffect
// useLayoutEffect runs synchronously before browser paint, ensuring state is cleared
@@ -31,7 +23,8 @@ const ResourceHomePage = memo(() => {
// When location changes to /resource, clear libraryId
// Don't clear when location is /library/* (even if this component is still mounted)
useLayoutEffect(() => {
- const isOnHomeRoute = location.pathname === '/resource' || !location.pathname.includes('/library/');
+ const isOnHomeRoute =
+ location.pathname === '/resource' || !location.pathname.includes('/library/');
if (isOnHomeRoute) {
setLibraryId(undefined);
}
@@ -40,52 +33,15 @@ const ResourceHomePage = memo(() => {
// Sync category from URL using useLayoutEffect
// IMPORTANT: Only sync if we're actually on the home route (not transitioning to library)
useLayoutEffect(() => {
- const isOnHomeRoute = location.pathname === '/resource' || !location.pathname.includes('/library/');
+ const isOnHomeRoute =
+ location.pathname === '/resource' || !location.pathname.includes('/library/');
if (isOnHomeRoute) {
setCategory(categoryParam);
}
}, [categoryParam, setCategory, location.pathname]);
// Sync file view mode from URL
- useEffect(() => {
- if (fileId) {
- setCurrentViewItemId(fileId);
-
- // Only determine mode when we have file data loaded
- // This prevents incorrect mode being set while data is loading
- if (fileData || documentData) {
- // Check if it's a PDF file - check both file data and document data
- const isPDF =
- fileData?.fileType?.toLowerCase() === 'pdf' ||
- fileData?.fileType?.toLowerCase() === 'application/pdf' ||
- fileData?.name?.toLowerCase().endsWith('.pdf') ||
- documentData?.fileType?.toLowerCase() === 'pdf' ||
- documentData?.fileType?.toLowerCase() === 'application/pdf' ||
- documentData?.filename?.toLowerCase().endsWith('.pdf');
-
- // Check if it's a page/document
- const isPage =
- !isPDF &&
- (fileData?.sourceType === 'document' ||
- fileData?.fileType === 'custom/document' ||
- !!documentData);
-
- // Determine mode based on file type
- if (isPDF) {
- // PDF files should always use editor mode for PDF viewer
- setMode('editor');
- } else if (isPage) {
- setMode('page');
- } else {
- setMode('editor');
- }
- }
- } else {
- // Reset to explorer mode when no file is selected
- setMode('explorer');
- setCurrentViewItemId(undefined);
- }
- }, [fileId, fileData, documentData, setCurrentViewItemId, setMode]);
+ useInitFileCheck();
return <ResourceManager />;
});
diff --git a/src/app/[variants]/(main)/resource/features/hooks/useInitFileCheck.ts b/src/app/[variants]/(main)/resource/features/hooks/useInitFileCheck.ts
new file mode 100644
index 00000000000..c155e1b890d
--- /dev/null
+++ b/src/app/[variants]/(main)/resource/features/hooks/useInitFileCheck.ts
@@ -0,0 +1,61 @@
+'use client';
+
+import { useEffect } from 'react';
+import { useSearchParams } from 'react-router-dom';
+
+import { documentSelectors, useFileStore } from '@/store/file';
+
+import { useResourceManagerStore } from '../store';
+
+/**
+ * Used for initial loading only, handle URL like:
+ *
+ * /resource?file=xxxxxx
+ */
+export const useInitFileCheck = () => {
+ const [searchParams] = useSearchParams();
+ const [setMode, setCurrentViewItemId] = useResourceManagerStore((s) => [
+ s.setMode,
+ s.setCurrentViewItemId,
+ ]);
+
+ const fileId = searchParams.get('file');
+
+ const useFetchKnowledgeItem = useFileStore((s) => s.useFetchKnowledgeItem);
+ const { data: fileData } = useFetchKnowledgeItem(fileId || undefined);
+ const documentData = useFileStore(documentSelectors.getDocumentById(fileId || undefined));
+
+ useEffect(() => {
+ if (fileId) {
+ setCurrentViewItemId(fileId);
+
+ if (fileData || documentData) {
+ const isPDF =
+ fileData?.fileType?.toLowerCase() === 'pdf' ||
+ fileData?.fileType?.toLowerCase() === 'application/pdf' ||
+ fileData?.name?.toLowerCase().endsWith('.pdf') ||
+ documentData?.fileType?.toLowerCase() === 'pdf' ||
+ documentData?.fileType?.toLowerCase() === 'application/pdf' ||
+ documentData?.filename?.toLowerCase().endsWith('.pdf') ||
+ documentData?.source?.toLowerCase().endsWith('.pdf');
+
+ const isPage =
+ !isPDF &&
+ (fileData?.sourceType === 'document' ||
+ fileData?.fileType === 'custom/document' ||
+ !!documentData);
+
+ if (isPDF) {
+ setMode('editor');
+ } else if (isPage) {
+ setMode('page');
+ } else {
+ setMode('editor');
+ }
+ }
+ } else {
+ setMode('explorer');
+ setCurrentViewItemId(undefined);
+ }
+ }, [fileId, fileData, documentData]);
+};
diff --git a/src/app/[variants]/(main)/resource/library/index.tsx b/src/app/[variants]/(main)/resource/library/index.tsx
index cb0e4aa0e48..7a176289f25 100644
--- a/src/app/[variants]/(main)/resource/library/index.tsx
+++ b/src/app/[variants]/(main)/resource/library/index.tsx
@@ -1,32 +1,20 @@
'use client';
-import { memo, useEffect, useLayoutEffect } from 'react';
-import { useLocation, useParams, useSearchParams } from 'react-router-dom';
+import { memo, useLayoutEffect } from 'react';
+import { useLocation, useParams } from 'react-router-dom';
import Container from '@/app/[variants]/(main)/resource/library/features/Container';
import NProgress from '@/components/NProgress';
import ResourceManager from '@/features/ResourceManager';
-import { documentSelectors, useFileStore } from '@/store/file';
+import { useInitFileCheck } from '../features/hooks/useInitFileCheck';
import { useKnowledgeBaseItem } from '../features/hooks/useKnowledgeItem';
import { useResourceManagerStore } from '../features/store';
const MainContent = memo(() => {
const { id: knowledgeBaseId } = useParams<{ id: string }>();
- const [searchParams] = useSearchParams();
const location = useLocation();
- const [setMode, setCurrentViewItemId, setLibraryId] = useResourceManagerStore((s) => [
- s.setMode,
- s.setCurrentViewItemId,
- s.setLibraryId,
- ]);
-
- const fileId = searchParams.get('file');
-
- // Fetch file or document details to determine correct mode
- const useFetchKnowledgeItem = useFileStore((s) => s.useFetchKnowledgeItem);
- const { data: fileData } = useFetchKnowledgeItem(fileId || undefined);
- const documentData = useFileStore(documentSelectors.getDocumentById(fileId || undefined));
+ const setLibraryId = useResourceManagerStore((s) => s.setLibraryId);
// Load knowledge base data
useKnowledgeBaseItem(knowledgeBaseId || '');
@@ -43,45 +31,7 @@ const MainContent = memo(() => {
}, [knowledgeBaseId, setLibraryId, location.pathname]);
// Sync file view mode from URL
- useEffect(() => {
- if (fileId) {
- setCurrentViewItemId(fileId);
-
- // Only determine mode when we have file data loaded
- // This prevents incorrect mode being set while data is loading
- if (fileData || documentData) {
- // Check if it's a PDF file - check both file data and document data
- const isPDF =
- fileData?.fileType?.toLowerCase() === 'pdf' ||
- fileData?.fileType?.toLowerCase() === 'application/pdf' ||
- fileData?.name?.toLowerCase().endsWith('.pdf') ||
- documentData?.fileType?.toLowerCase() === 'pdf' ||
- documentData?.fileType?.toLowerCase() === 'application/pdf' ||
- documentData?.filename?.toLowerCase().endsWith('.pdf');
-
- // Check if it's a page/document
- const isPage =
- !isPDF &&
- (fileData?.sourceType === 'document' ||
- fileData?.fileType === 'custom/document' ||
- !!documentData);
-
- // Determine mode based on file type
- if (isPDF) {
- // PDF files should always use editor mode for PDF viewer
- setMode('editor');
- } else if (isPage) {
- setMode('page');
- } else {
- setMode('editor');
- }
- }
- } else {
- // Reset to explorer mode when no file is selected
- setMode('explorer');
- setCurrentViewItemId(undefined);
- }
- }, [fileId, fileData, documentData, setCurrentViewItemId, setMode]);
+ useInitFileCheck();
return <ResourceManager />;
});
diff --git a/src/server/routers/lambda/file.ts b/src/server/routers/lambda/file.ts
index 529c50ae992..e378f3aee2e 100644
--- a/src/server/routers/lambda/file.ts
+++ b/src/server/routers/lambda/file.ts
@@ -224,6 +224,8 @@ export const fileRouter = router({
limit: limit + 1,
});
+ console.log('getKnow', knowledgeItems);
+
// Check if there are more items
const hasMore = knowledgeItems.length > limit;