-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix[csv_table]: no reference exception #36
Conversation
WalkthroughThe changes introduce enhancements to the Changes
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## release/v0.3.0 #36 +/- ##
=================================================
Coverage ? 57.15%
=================================================
Files ? 37
Lines ? 1706
Branches ? 0
=================================================
Hits ? 975
Misses ? 731
Partials ? 0 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Outside diff range and nitpick comments (2)
frontend/src/components/ExtractReferenceDrawer.tsx (2)
Line range hint
5-5
: Remove unused import.The
useQuery
import is no longer needed since it's been replaced byuseProcessStepReferences
.-import { useQuery } from "@tanstack/react-query";
Also applies to: 9-9
I have enough information to generate the final response now.
Add array length validation to prevent data inconsistency
The review comment is valid. From the codebase analysis:
- The
Source
interface infrontend/src/interfaces/processSteps.ts
shows bothsources
andpage_numbers
are array properties- The current code only checks if index exists in
page_numbers
but doesn't validate array length consistency- There's no other validation of these arrays elsewhere in the codebase
- The test data shows that
page_numbers
can benull
in some cases (from backend tests)The suggested fix should be enhanced to handle null values:
filtered_output?.forEach((item: Source) => { const { sources, page_numbers } = item; if (!page_numbers) { console.warn(`page_numbers is null for source with name: ${item.name}`); return; } if (sources.length !== page_numbers.length) { console.warn(`Mismatched array lengths for ${item.name}: sources (${sources.length}) vs page_numbers (${page_numbers.length})`); } // Iterate through sources and page_numbers together for (let i = 0; i < sources.length; i++) { if (i < page_numbers.length) { filteredSourceDetails.push({ source: sources[i], page_number: page_numbers[i], }); } } });🔗 Analysis chain
Line range hint
36-46
: Verify array length consistency.The loop assumes
sources
andpage_numbers
arrays are properly paired, but only checks one direction. Consider adding validation to ensure both arrays have the same length.filtered_output?.forEach((item: Source) => { const { sources, page_numbers } = item; + + if (sources.length !== page_numbers.length) { + console.warn(`Mismatched array lengths: sources (${sources.length}) vs page_numbers (${page_numbers.length})`); + } // Iterate through sources and page_numbers together for (let i = 0; i < sources.length; i++) {
Let me gather more context about the Source type and any validation that might exist in the codebase.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for other usages of sources and page_numbers to understand if this mismatch is handled elsewhere rg -A 5 "sources.*page_numbers|page_numbers.*sources"Length of output: 1333
Script:
#!/bin/bash # Search for Source type definition and any validation ast-grep --pattern 'type Source = { $$$ }' # Also search for interface definition ast-grep --pattern 'interface Source { $$$ }' # Search for any validation related to these arrays rg -A 5 "sources.*length|page_numbers.*length"Length of output: 1119
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
- frontend/src/components/DataTable.tsx (3 hunks)
- frontend/src/components/ExtractReferenceDrawer.tsx (2 hunks)
- frontend/src/hooks/useProcessStepReferences.tsx (1 hunks)
🔇 Additional comments (4)
frontend/src/hooks/useProcessStepReferences.tsx (2)
1-2
: LGTM! Imports are clean and necessary.The imports are well-organized and properly typed, importing only what's needed for the hook's functionality.
2-2
: Verify the service implementation and return type.Let's ensure the service layer is properly implemented and returns the expected data structure.
Also applies to: 8-8
✅ Verification successful
Let me try different commands to verify the service implementation.
Let me check the implementation details of the service.
Let me check the interface definitions to complete the verification.
Service implementation and return type are properly defined
The verification shows that:
- The
GetProcessStepReferences
service is correctly implemented infrontend/src/services/processSteps.ts
- It returns a
Promise<ProcessStepOutputRef>
which is well-defined infrontend/src/interfaces/processSteps.ts
- The interface includes all necessary fields: id, process_id, project_id, asset_id, and output_reference
- The service is properly used across multiple components (DataTable, ExtractReferenceDrawer) and the hook
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check the implementation of GetProcessStepReferences service # Look for the service implementation echo "Checking GetProcessStepReferences implementation..." rg -t typescript -t javascript "export.*GetProcessStepReferences.*=" services/ # Look for any type definitions related to the service echo "Checking for related type definitions..." rg -t typescript "type.*ProcessStep.*Reference" --glob "!node_modules/"Length of output: 428
Script:
#!/bin/bash # Search for GetProcessStepReferences implementation echo "Searching for GetProcessStepReferences implementation..." rg "GetProcessStepReferences" --type-add 'ts:*.{ts,tsx}' -t ts # Search for type definitions echo "Searching for ProcessStep type definitions..." rg "ProcessStep.*Reference" --type-add 'ts:*.{ts,tsx}' -t ts # Look for the actual service file echo "Looking for processSteps service file..." fd "processSteps.ts" frontend/src/services/Length of output: 2168
Script:
#!/bin/bash # Get the implementation of GetProcessStepReferences echo "Getting service implementation..." cat frontend/src/services/processSteps.ts # Look for any interfaces or types used by the service echo "Looking for related interfaces..." rg "interface.*ProcessStep|type.*ProcessStep" --type-add 'ts:*.{ts,tsx}' -t tsLength of output: 1400
Script:
#!/bin/bash # Get the interface definitions echo "Getting interface definitions..." cat frontend/src/interfaces/processSteps.ts # Check usage of ProcessStepOutputRef in components echo "Checking ProcessStepOutputRef usage..." rg "ProcessStepOutputRef" --type-add 'ts:*.{ts,tsx}' -t ts -A 2Length of output: 1830
frontend/src/components/ExtractReferenceDrawer.tsx (1)
30-32
: LGTM! Good defensive programming.Adding the
item.page_numbers
check helps prevent issues with undefined page numbers in the PDF viewer.frontend/src/components/DataTable.tsx (1)
7-9
: LGTM! Imports are well-organized and necessary.The new imports support the enhanced reference handling functionality.
export const useProcessStepReferences = (process_step_id: string) => { | ||
return useQuery({ | ||
queryKey: ["processStepReferences", process_step_id], | ||
queryFn: async () => { | ||
return await GetProcessStepReferences(process_step_id); | ||
}, | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider enhancing the hook with better type safety and error handling.
The hook implementation is clean and follows React Query best practices. However, there are a few improvements that could make it more robust:
- Add explicit return type for better type safety
- Consider adding error handling for the service call
- Add caching configuration based on your data freshness requirements
Here's a suggested implementation:
+ type ProcessStepReference = {
+ // Add your reference type definition here
+ };
+ const QUERY_KEY = {
+ processStepReferences: (id: string) => ['processStepReferences', id] as const
+ };
- export const useProcessStepReferences = (process_step_id: string) => {
+ export const useProcessStepReferences = (process_step_id: string) => {
+ return useQuery<ProcessStepReference[], Error>({
- queryKey: ["processStepReferences", process_step_id],
+ queryKey: QUERY_KEY.processStepReferences(process_step_id),
queryFn: async () => {
- return await GetProcessStepReferences(process_step_id);
+ try {
+ return await GetProcessStepReferences(process_step_id);
+ } catch (error) {
+ throw new Error(`Failed to fetch process step references: ${error.message}`);
+ }
},
+ staleTime: 5 * 60 * 1000, // Consider adding appropriate stale time
});
};
Committable suggestion was skipped due to low confidence.
}); | ||
|
||
const output = data?.output_reference?.[index]?.[column_name] ?? null; | ||
const { data, error, isLoading } = useProcessStepReferences(process_step_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for query failures.
The error
state from useProcessStepReferences
is not being handled. Consider displaying an error message to users when the reference data fails to load.
const ExtractReferenceDrawer = ({...}: IProps) => {
const { data, error, isLoading } = useProcessStepReferences(process_step_id);
+
+ if (error) {
+ return (
+ <Drawer isOpen={isOpen} onClose={onCancel} title={column_name}>
+ <div className="text-red-500">Failed to load references. Please try again.</div>
+ </Drawer>
+ );
+ }
Committable suggestion was skipped due to low confidence.
handleRefClick({ | ||
id: props.row.___process_step_id, | ||
index: props.row.___extraction_index, | ||
filename: props.row.Filename, | ||
key: props.column.key, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider memoizing the click handler and handling loading state.
While the implementation is functionally correct, it could be optimized:
- Memoize the click handler to prevent unnecessary re-renders
- Disable the click action during loading state
Here's how you could improve it:
+ const handleIconClick = useCallback((row: Record<string, any>, columnKey: string) => (e: React.MouseEvent) => {
+ if (isLoading) return;
+ e.stopPropagation();
+ handleRefClick({
+ id: row.___process_step_id,
+ index: row.___extraction_index,
+ filename: row.Filename,
+ key: columnKey,
+ });
+ }, [isLoading, handleRefClick]);
// In the render function:
- onClick={(e) => {
- e.stopPropagation();
- handleRefClick({
- id: props.row.___process_step_id,
- index: props.row.___extraction_index,
- filename: props.row.Filename,
- key: props.column.key,
- });
- }}
+ onClick={handleIconClick(props.row, props.column.key)}
+ style={{ cursor: isLoading ? 'not-allowed' : 'pointer' }}
Committable suggestion was skipped due to low confidence.
const handleRefClick = async (props: SelectRowColumnType) => { | ||
const data = await GetProcessStepReferences(props.id); | ||
const filtered_output = data?.output_reference?.[props.index].filter( | ||
(item: Source) => item.name == props.key && item.page_numbers | ||
); | ||
|
||
// Verify valid reference exists | ||
if (filtered_output.length == 0) { | ||
toast.error("Couldn't find the reference for this"); | ||
} else { | ||
setSelectRowColumn(props); | ||
setDisplayDrawer(true); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling and loading state management.
The async function could be improved in several areas:
- Add loading state management during the async operation
- Handle potential API errors
- Use strict comparison
- Use JSDoc style comments for consistency
Here's the suggested implementation:
+ const [isLoading, setIsLoading] = useState<boolean>(false);
const handleRefClick = async (props: SelectRowColumnType) => {
+ setIsLoading(true);
try {
const data = await GetProcessStepReferences(props.id);
const filtered_output = data?.output_reference?.[props.index].filter(
(item: Source) => item.name === props.key && item.page_numbers
);
- // Verify valid reference exists
+ /** Verify valid reference exists */
- if (filtered_output.length == 0) {
+ if (filtered_output.length === 0) {
toast.error("Couldn't find the reference for this");
} else {
setSelectRowColumn(props);
setDisplayDrawer(true);
}
+ } catch (error) {
+ toast.error("Failed to fetch references");
+ console.error("Error fetching references:", error);
+ } finally {
+ setIsLoading(false);
}
};
Committable suggestion was skipped due to low confidence.
const handleRefClick = async (props: SelectRowColumnType) => { | ||
const data = await GetProcessStepReferences(props.id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Based on the gathered information, I can now provide the final response:
Test coverage needed for GetProcessStepReferences API integration
The GetProcessStepReferences
function is an API integration point that:
- Makes HTTP requests to fetch process step references
- Is used in multiple components (DataTable, ExtractReferenceDrawer, useProcessStepReferences hook)
- Handles error cases through a try-catch block
Since there are no test files found in the frontend directory and this function is a critical API integration point used across multiple components, tests should be added to ensure reliable behavior.
Key areas to test:
- Successful API response handling
- Error case handling
- Integration with React Query in the useProcessStepReferences hook
🔗 Analysis chain
Verify test coverage for GetProcessStepReferences.
Ensure that the GetProcessStepReferences function has proper test coverage, especially for error cases.
Let me try a different search strategy to verify the test coverage.
Let me try one more search to specifically look for test files in the repository.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for tests related to GetProcessStepReferences
rg -t ts -t tsx "GetProcessStepReferences.*test"
Length of output: 78
Script:
#!/bin/bash
# Search for GetProcessStepReferences implementation and tests
rg "GetProcessStepReferences" --type-add 'tsx:*.tsx' --type-add 'ts:*.ts' -t tsx -t ts -C 2
# Also search for test files containing "ProcessStep" as it might be part of a test suite
rg "ProcessStep.*test|test.*ProcessStep" --type-add 'tsx:*.tsx' --type-add 'ts:*.ts' -t tsx -t ts
Length of output: 2719
Script:
#!/bin/bash
# Search for test files in the frontend directory
fd "test\.(ts|tsx)$" frontend/src
# Look for the implementation of GetProcessStepReferences to understand its complexity
rg -A 10 "export const GetProcessStepReferences" frontend/src/services/processSteps.ts
Length of output: 458
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- frontend/src/components/DataTable.tsx (3 hunks)
- frontend/src/components/ExtractReferenceDrawer.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- frontend/src/components/ExtractReferenceDrawer.tsx
🔇 Additional comments (3)
frontend/src/components/DataTable.tsx (3)
7-9
: LGTM: New imports are properly organized and utilized.The added imports support the new reference handling functionality.
56-56
: LGTM: Loading state properly implemented.The loading state management follows the previous review suggestions.
112-119
: LGTM: Click handler implementation improved.The click handler now includes proper loading state feedback.
const handleRefClick = async (props: SelectRowColumnType) => { | ||
setIsLoading(true); | ||
try { | ||
const data = await GetProcessStepReferences(props.id); | ||
const filtered_output = data?.output_reference?.[props.index].filter( | ||
(item: Source) => item.name == props.key && item.page_numbers | ||
); | ||
// Verify valid reference exists | ||
if (filtered_output.length == 0) { | ||
toast.error("Couldn't find the reference for this"); | ||
} else { | ||
setSelectRowColumn(props); | ||
setDisplayDrawer(true); | ||
} | ||
} catch (error) { | ||
toast.error("Failed to fetch references"); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve error handling and use strict equality comparisons.
The function implementation could be enhanced:
- Use strict equality (
===
) for comparisons - Provide more specific error messages based on the error type
Apply these improvements:
const handleRefClick = async (props: SelectRowColumnType) => {
setIsLoading(true);
try {
const data = await GetProcessStepReferences(props.id);
const filtered_output = data?.output_reference?.[props.index].filter(
- (item: Source) => item.name == props.key && item.page_numbers
+ (item: Source) => item.name === props.key && item.page_numbers
);
// Verify valid reference exists
- if (filtered_output.length == 0) {
+ if (filtered_output.length === 0) {
toast.error("Couldn't find the reference for this");
} else {
setSelectRowColumn(props);
setDisplayDrawer(true);
}
} catch (error) {
- toast.error("Failed to fetch references");
+ const errorMessage = error instanceof Error
+ ? `Failed to fetch references: ${error.message}`
+ : "Failed to fetch references";
+ toast.error(errorMessage);
+ console.error("Reference fetch error:", error);
} finally {
setIsLoading(false);
}
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const handleRefClick = async (props: SelectRowColumnType) => { | |
setIsLoading(true); | |
try { | |
const data = await GetProcessStepReferences(props.id); | |
const filtered_output = data?.output_reference?.[props.index].filter( | |
(item: Source) => item.name == props.key && item.page_numbers | |
); | |
// Verify valid reference exists | |
if (filtered_output.length == 0) { | |
toast.error("Couldn't find the reference for this"); | |
} else { | |
setSelectRowColumn(props); | |
setDisplayDrawer(true); | |
} | |
} catch (error) { | |
toast.error("Failed to fetch references"); | |
} finally { | |
setIsLoading(false); | |
} | |
}; | |
const handleRefClick = async (props: SelectRowColumnType) => { | |
setIsLoading(true); | |
try { | |
const data = await GetProcessStepReferences(props.id); | |
const filtered_output = data?.output_reference?.[props.index].filter( | |
(item: Source) => item.name === props.key && item.page_numbers | |
); | |
// Verify valid reference exists | |
if (filtered_output.length === 0) { | |
toast.error("Couldn't find the reference for this"); | |
} else { | |
setSelectRowColumn(props); | |
setDisplayDrawer(true); | |
} | |
} catch (error) { | |
const errorMessage = error instanceof Error | |
? `Failed to fetch references: ${error.message}` | |
: "Failed to fetch references"; | |
toast.error(errorMessage); | |
console.error("Reference fetch error:", error); | |
} finally { | |
setIsLoading(false); | |
} | |
}; |
* refactor[summary]: clean up code to remove deprecated processes * fix leftovers * fix leftovers * fix leftovers * refactor: code improvements * remove extra print statement * refactor: cleanup some potential bug * fix: remove extra print statement * fix(csv): handle case if no reference is found * refactor[CSV_table]: error handling in csv
Summary by CodeRabbit
New Features
DataTable
component with improved click handling for reference icons, including error notifications.useProcessStepReferences
, for streamlined data fetching of process step references.Bug Fixes
Drawer
component for data fetching failures.Documentation