Skip to content

Conversation

@ankitpasayat
Copy link
Contributor

@ankitpasayat ankitpasayat commented Dec 6, 2025

Logging:

  • Add structured logger for browser extension (lib/logger.ts)
  • Add structured logger for web frontend (lib/logger.ts)
  • Replace console.log with proper logger calls across codebase

Code Quality:

  • Extract constants to dedicated file (lib/constants.ts)
  • Extract connector auth utilities to connector_auth.py
  • Improve route handlers with better error handling
  • Add frontend unit tests for components, hooks, and utilities

Files updated:

  • 50+ files with console.log ??? logger replacements
  • New logger implementations for extension and web
  • Frontend tests: Logo, Button, useMediaQuery, utils, pagination

Description

Motivation and Context

FIX #

Screenshots

API Changes

  • This PR includes API changes

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring
  • Documentation
  • Dependency/Build system
  • Breaking change
  • Other (specify):

Testing Performed

  • Tested locally
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR improves code quality and maintainability through comprehensive logging and refactoring changes. The main improvements include replacing console.log statements with structured loggers across 50+ files in both the browser extension and web frontend, extracting hardcoded constants to a dedicated constants.ts file, moving connector authentication utilities to a separate connector_auth.py module to avoid circular imports, enhancing error handling in backend route handlers with proper exception logging and more granular SQLAlchemy error handling, and adding frontend unit tests for components (Logo, Button), hooks (useMediaQuery), and utilities (auth-utils, pagination, utils).

⏱️ Estimated Review Time: 15-30 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/lib/logger.ts
2 surfsense_browser_extension/lib/logger.ts
3 surfsense_web/lib/constants.ts
4 surfsense_backend/app/utils/connector_auth.py
5 surfsense_backend/app/routes/airtable_add_connector_route.py
6 surfsense_backend/app/tasks/connector_indexers/airtable_indexer.py
7 surfsense_backend/app/routes/chats_routes.py
8 surfsense_backend/app/routes/podcasts_routes.py
9 surfsense_backend/app/tasks/document_processors/file_processors.py
10 surfsense_browser_extension/background/index.ts
11 surfsense_browser_extension/background/messages/savedata.ts
12 surfsense_browser_extension/background/messages/savesnapshot.ts
13 surfsense_browser_extension/routes/pages/HomePage.tsx
14 surfsense_browser_extension/utils/commons.ts
15 surfsense_web/app/(home)/login/GoogleLoginButton.tsx
16 surfsense_web/app/api/contact/route.ts
17 surfsense_web/app/api/convert-to-blocknote/route.ts
18 surfsense_web/app/api/convert-to-markdown/route.ts
19 surfsense_web/app/dashboard/[search_space_id]/connectors/(manage)/page.tsx
20 surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/page.tsx
21 surfsense_web/app/dashboard/[search_space_id]/connectors/add/airtable-connector/page.tsx
22 surfsense_web/app/dashboard/[search_space_id]/connectors/add/baidu-search-api/page.tsx
23 surfsense_web/app/dashboard/[search_space_id]/connectors/add/clickup-connector/page.tsx
24 surfsense_web/app/dashboard/[search_space_id]/connectors/add/confluence-connector/page.tsx
25 surfsense_web/app/dashboard/[search_space_id]/connectors/add/discord-connector/page.tsx
26 surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx
27 surfsense_web/app/dashboard/[search_space_id]/connectors/add/github-connector/page.tsx
28 surfsense_web/app/dashboard/[search_space_id]/connectors/add/google-calendar-connector/page.tsx
29 surfsense_web/app/dashboard/[search_space_id]/connectors/add/google-gmail-connector/page.tsx
30 surfsense_web/app/dashboard/[search_space_id]/connectors/add/jira-connector/page.tsx
31 surfsense_web/app/dashboard/[search_space_id]/connectors/add/linear-connector/page.tsx
32 surfsense_web/app/dashboard/[search_space_id]/connectors/add/linkup-api/page.tsx
33 surfsense_web/app/dashboard/[search_space_id]/connectors/add/luma-connector/page.tsx
34 surfsense_web/app/dashboard/[search_space_id]/connectors/add/notion-connector/page.tsx
35 surfsense_web/app/dashboard/[search_space_id]/connectors/add/searxng/page.tsx
36 surfsense_web/app/dashboard/[search_space_id]/connectors/add/serper-api/page.tsx
37 surfsense_web/app/dashboard/[search_space_id]/connectors/add/slack-connector/page.tsx
38 surfsense_web/app/dashboard/[search_space_id]/connectors/add/tavily-api/page.tsx
39 surfsense_web/app/dashboard/[search_space_id]/connectors/add/webcrawler-connector/page.tsx
40 surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/components/RowActions.tsx
41 surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx
42 surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx
43 surfsense_web/app/dashboard/[search_space_id]/logs/(manage)/page.tsx
44 surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx
45 surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx
46 surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx
47 surfsense_web/app/dashboard/[search_space_id]/team/page.tsx
48 surfsense_web/app/dashboard/page.tsx
49 surfsense_web/app/dashboard/searchspaces/page.tsx
50 surfsense_web/components/TokenHandler.tsx
51 surfsense_web/components/UserDropdown.tsx
52 surfsense_web/components/chat/ChatPanel/ChatPanelContainer.tsx
53 surfsense_web/components/chat/ChatPanel/PodcastPlayer/PodcastPlayer.tsx
54 surfsense_web/components/chat/CodeBlock.tsx
55 surfsense_web/components/contact/contact-form.tsx
56 surfsense_web/components/json-metadata-viewer.tsx
57 surfsense_web/components/onboard/setup-prompt-step.tsx
58 surfsense_web/components/settings/prompt-config-manager.tsx
59 surfsense_web/components/sidebar/AppSidebarProvider.tsx
60 surfsense_web/hooks/use-api-key.ts
61 surfsense_web/hooks/use-community-prompts.ts
62 surfsense_web/hooks/use-connector-edit-page.ts
63 surfsense_web/hooks/use-document-by-chunk.ts
64 surfsense_web/hooks/use-document-types.ts
65 surfsense_web/hooks/use-documents.ts
66 surfsense_web/hooks/use-github-stars.ts
67 surfsense_web/hooks/use-llm-configs.ts
68 surfsense_web/hooks/use-logs.ts
69 surfsense_web/hooks/use-rbac.ts
70 surfsense_web/hooks/use-search-source-connectors.ts
71 surfsense_web/hooks/use-search-space.ts
72 surfsense_web/hooks/use-search-spaces.ts
73 surfsense_web/hooks/use-user.ts
74 surfsense_web/lib/apis/auth-api.service.ts
75 surfsense_web/lib/apis/base-api.service.ts
76 surfsense_web/lib/apis/chats-api.service.ts
77 surfsense_web/lib/apis/podcasts-api.service.ts
78 surfsense_web/tests/components/Logo.test.tsx
79 surfsense_web/tests/components/ui/button.test.tsx
80 surfsense_web/tests/hooks/use-media-query.test.ts
81 surfsense_web/tests/lib/auth-utils.test.ts
82 surfsense_web/tests/lib/pagination.test.ts
83 surfsense_web/tests/lib/utils.test.ts

Need help? Join our Discord

Analyze latest changes

Logging:
- Add structured logger for browser extension (lib/logger.ts)
- Add structured logger for web frontend (lib/logger.ts)
- Replace console.log with proper logger calls across codebase

Code Quality:
- Extract constants to dedicated file (lib/constants.ts)
- Extract connector auth utilities to connector_auth.py
- Improve route handlers with better error handling
- Add frontend unit tests for components, hooks, and utilities

Files updated:
- 50+ files with console.log ??? logger replacements
- New logger implementations for extension and web
- Frontend tests: Logo, Button, useMediaQuery, utils, pagination
Copilot AI review requested due to automatic review settings December 6, 2025 16:53
@vercel
Copy link

vercel bot commented Dec 6, 2025

@ankitpasayat is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the codebase to improve logging and code quality by introducing structured loggers, extracting constants and utilities, and adding comprehensive frontend unit tests.

Key changes:

  • Structured logging with environment-aware loggers for web and browser extension
  • Extracted connector authentication utilities to a dedicated module (connector_auth.py)
  • Extracted hardcoded connector IDs and configurations to a constants file
  • Added unit tests for frontend utilities, hooks, and UI components
  • Replaced 50+ console.log calls with proper logger usage
  • Improved error handling in backend routes with better exception catching and logging

Reviewed changes

Copilot reviewed 83 out of 83 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
surfsense_web/lib/logger.ts New structured logger for web frontend with environment-aware log levels
surfsense_browser_extension/lib/logger.ts New structured logger for browser extension with timestamp formatting
surfsense_web/lib/constants.ts Extracted connector IDs and configurations to centralized constants
surfsense_backend/app/utils/connector_auth.py Extracted Airtable auth token refresh logic from routes to utilities
surfsense_web/tests/* Added comprehensive unit tests for utilities, hooks, and components
surfsense_web/lib/apis/*.ts Replaced console.log with logger calls and fixed "frendly" typos
surfsense_web/hooks/*.ts Replaced console.error with logger.error across all hooks
surfsense_web/components/*.tsx Updated components to use structured logger
surfsense_web/app/dashboard/**/*.tsx Updated dashboard pages to use logger
surfsense_backend/app/routes/*.py Improved error handling with specific exception types and logging
surfsense_backend/app/tasks/document_processors/file_processors.py Improved error handling using OSError instead of generic Exception
surfsense_browser_extension/**/*.ts Updated extension code to use structured logger

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

},
{
id: CONNECTOR_IDS.YOUTUBE_VIDEO,
name: "YouTube Video",
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spelling: Line 27 uses "YouTube Video" (correct) but line 61 uses "Youtube Video" (incorrect capitalization). Should be "YouTube Video" for brand consistency.

Copilot uses AI. Check for mistakes.
Comment on lines 37 to 51
const copyToClipboard = useCallback(async () => {
if (!apiKey) return;

try {
if (navigator.clipboard && window.isSecureContext) {
// Use Clipboard API if available and in secure context
await navigator.clipboard.writeText(apiKey);
setCopied(true);
toast.success("API key copied to clipboard");
await navigator.clipboard.writeText(apiKey);
setCopied(true);
toast.success("API key copied to clipboard");

setTimeout(() => {
setCopied(false);
}, 2000);
} else {
// Fallback for non-secure contexts or browsers without clipboard API
fallbackCopyTextToClipboard(apiKey);
}
setTimeout(() => {
setCopied(false);
}, 2000);
} catch (err) {
console.error("Failed to copy:", err);
logger.error("Failed to copy:", err);
toast.error("Failed to copy API key");
}
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of the fallbackCopyTextToClipboard function eliminates backward compatibility for browsers that don't support the Clipboard API or non-secure contexts (non-HTTPS). While modern browsers support navigator.clipboard, the fallback using document.execCommand('copy') was useful for older browsers or non-HTTPS development environments. Consider keeping the fallback or documenting that the feature requires HTTPS and modern browser support.

Copilot uses AI. Check for mistakes.
} catch (err: any) {
setError(err.message || "Failed to fetch logs");
console.error("Error fetching logs:", err);
logger.error("Error fetching logs:", err);
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import: logger is used multiple times in this file (lines 115, 166, 198, 223, 244, 289) but not imported. Add import { logger } from "@/lib/logger"; at the top of the file.

Copilot uses AI. Check for mistakes.
} catch (err) {
setError(err instanceof Error ? err : new Error("An unknown error occurred"));
console.error("Error fetching document types:", err);
logger.error("Error fetching document types:", err);
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import: logger is used on line 59 but not imported. Add import { logger } from "@/lib/logger"; at the top of the file.

Copilot uses AI. Check for mistakes.
) from None
except Exception:
except Exception as e:
await session.rollback()
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The f-string interpolation is missing the 'f' prefix. Line 75 shows detail="Token refresh failed: {token_response.text}" but should be detail=f"Token refresh failed: {token_response.text}" to properly format the error message with the response text.

Copilot uses AI. Check for mistakes.

if token_response.status_code != 200:
raise HTTPException(
status_code=400, detail="Token refresh failed: {token_response.text}"
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The f-string interpolation is missing the 'f' prefix. Line 75 shows detail="Token refresh failed: {token_response.text}" but should be detail=f"Token refresh failed: {token_response.text}" to properly format the error message with the response text.

Suggested change
status_code=400, detail="Token refresh failed: {token_response.text}"
status_code=400, detail=f"Token refresh failed: {token_response.text}"

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +49
if (config.isDevelopment) {
console.info(formatMessage('info', message), ...args);
}
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistency between logger implementations: The browser extension's info method only logs in development mode (line 47-48), whereas the web application's info method always logs (surfsense_web/lib/logger.ts:48). This inconsistency could lead to confusion about when info-level logs appear. Consider aligning the behavior or documenting why they differ.

Suggested change
if (config.isDevelopment) {
console.info(formatMessage('info', message), ...args);
}
console.info(formatMessage('info', message), ...args);

Copilot uses AI. Check for mistakes.
} catch (err: any) {
setError(err.message || "Failed to fetch search spaces");
console.error("Error fetching search spaces:", err);
logger.error("Error fetching search spaces:", err);
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import: logger is used on line 42 but not imported. Add import { logger } from "@/lib/logger"; at the top of the file.

Copilot uses AI. Check for mistakes.

# Re-export for backward compatibility - this import is used by other modules
# that import refresh_airtable_token from this module
from app.utils.connector_auth import refresh_airtable_token as refresh_airtable_token # noqa: F401
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'refresh_airtable_token' is not used.

Copilot uses AI. Check for mistakes.
@ankitpasayat ankitpasayat mentioned this pull request Dec 6, 2025
16 tasks
Copy link

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by RecurseML

🔍 Review performed on 601489b..80bbcb7

✨ No bugs found, your code is sparkling clean

✅ Files analyzed, no issues (50)

surfsense_backend/app/routes/airtable_add_connector_route.py
surfsense_backend/app/routes/chats_routes.py
surfsense_backend/app/routes/podcasts_routes.py
surfsense_backend/app/tasks/connector_indexers/airtable_indexer.py
surfsense_backend/app/tasks/document_processors/file_processors.py
surfsense_backend/app/utils/connector_auth.py
surfsense_browser_extension/background/index.ts
surfsense_browser_extension/background/messages/savedata.ts
surfsense_browser_extension/background/messages/savesnapshot.ts
surfsense_browser_extension/lib/logger.ts
surfsense_browser_extension/routes/pages/HomePage.tsx
surfsense_browser_extension/utils/commons.ts
surfsense_web/app/(home)/login/GoogleLoginButton.tsx
surfsense_web/app/api/contact/route.ts
surfsense_web/app/api/convert-to-blocknote/route.ts
surfsense_web/app/api/convert-to-markdown/route.ts
surfsense_web/app/dashboard/[search_space_id]/connectors/(manage)/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/airtable-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/baidu-search-api/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/clickup-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/confluence-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/discord-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/github-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/google-calendar-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/google-gmail-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/jira-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/linear-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/linkup-api/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/luma-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/notion-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/searxng/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/serper-api/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/slack-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/tavily-api/page.tsx
surfsense_web/app/dashboard/[search_space_id]/connectors/add/webcrawler-connector/page.tsx
surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/components/RowActions.tsx
surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx
surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx
surfsense_web/app/dashboard/[search_space_id]/logs/(manage)/page.tsx
surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx
surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx
surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx
surfsense_web/app/dashboard/[search_space_id]/team/page.tsx
surfsense_web/app/dashboard/page.tsx
surfsense_web/app/dashboard/searchspaces/page.tsx
surfsense_web/components/TokenHandler.tsx
surfsense_web/components/UserDropdown.tsx
surfsense_web/components/chat/ChatPanel/ChatPanelContainer.tsx

⏭️ Files skipped (33)
  Locations  
surfsense_web/components/chat/ChatPanel/PodcastPlayer/PodcastPlayer.tsx
surfsense_web/components/chat/CodeBlock.tsx
surfsense_web/components/contact/contact-form.tsx
surfsense_web/components/json-metadata-viewer.tsx
surfsense_web/components/onboard/setup-prompt-step.tsx
surfsense_web/components/settings/prompt-config-manager.tsx
surfsense_web/components/sidebar/AppSidebarProvider.tsx
surfsense_web/hooks/use-api-key.ts
surfsense_web/hooks/use-community-prompts.ts
surfsense_web/hooks/use-connector-edit-page.ts
surfsense_web/hooks/use-document-by-chunk.ts
surfsense_web/hooks/use-document-types.ts
surfsense_web/hooks/use-documents.ts
surfsense_web/hooks/use-github-stars.ts
surfsense_web/hooks/use-llm-configs.ts
surfsense_web/hooks/use-logs.ts
surfsense_web/hooks/use-rbac.ts
surfsense_web/hooks/use-search-source-connectors.ts
surfsense_web/hooks/use-search-space.ts
surfsense_web/hooks/use-search-spaces.ts
surfsense_web/hooks/use-user.ts
surfsense_web/lib/apis/auth-api.service.ts
surfsense_web/lib/apis/base-api.service.ts
surfsense_web/lib/apis/chats-api.service.ts
surfsense_web/lib/apis/podcasts-api.service.ts
surfsense_web/lib/constants.ts
surfsense_web/lib/logger.ts
surfsense_web/tests/components/Logo.test.tsx
surfsense_web/tests/components/ui/button.test.tsx
surfsense_web/tests/hooks/use-media-query.test.ts
surfsense_web/tests/lib/auth-utils.test.ts
surfsense_web/tests/lib/pagination.test.ts
surfsense_web/tests/lib/utils.test.ts

@CREDO23
Copy link
Contributor

CREDO23 commented Dec 8, 2025

@ankitpasayat , can you please resolve copilot comments. ?

@unitagain
Copy link
Contributor

@unitagain, can you please resolve copilot comments. ?

Got it, I’ll need some time to resolve the copilot comments. : )

@unitagain
Copy link
Contributor

@ankitpasayat I've resolved all Copilot review comments. The fixes are in my fork:

https://github.com/unitagain/SurfSense-CN/tree/pr-537

To apply them, run:

git remote add unitagain [https://github.com/unitagain/SurfSense-CN.git](https://github.com/unitagain/SurfSense-CN.git)
git fetch unitagain pr-537
git cherry-pick 187700ce6
git push

Changes made:

Fixed YouTube capitalization for brand consistency
Restored clipboard fallback for backward compatibility
Added missing logger imports (use-logs.ts, use-document-types.ts, use-search-spaces.ts)
Fixed f-string interpolation in connector_auth.py
Aligned logger info behavior between web and browser extension

@CREDO23
Copy link
Contributor

CREDO23 commented Dec 8, 2025

My mistake — I tagged the wrong person earlier, which caused the confusion. Thanks @unitagain for the fixes. @ankitpasayat, feel free to pull them into your PR branch if helpful. Sorry for the mix-up!

@unitagain
Copy link
Contributor

@CREDO23 That’s totally fine ! Hope the fixes prove helpful, and I’m always happy to collaborate on items like this. Feel free to reach out whenever you need assistance with anything! : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants