Skip to content

Commit

Permalink
Fix[csv_table]: no reference exception (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ArslanSaleem authored and gventuri committed Oct 24, 2024
1 parent 60ce24b commit 44e1e1f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
29 changes: 27 additions & 2 deletions frontend/src/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import TextEditor from "@/components/editor/TextEditor";
import "react-data-grid/lib/styles.css";
import { FaInfoCircle } from "react-icons/fa";
import ExtractReferenceDrawer from "./ExtractReferenceDrawer";
import { GetProcessStepReferences } from "@/services/processSteps";
import { Source } from "@/interfaces/processSteps";
import toast from "react-hot-toast";

function calculateColumnWidth(
title: string,
Expand Down Expand Up @@ -50,6 +53,28 @@ const DataTable: React.FC<DataTableProps> = ({ data }) => {
useState<SelectRowColumnType | null>(null);
const [displayDrawer, setDisplayDrawer] = useState<boolean>(false);
const [hoveredCell, setHoveredCell] = useState<string | null>(null);
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
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 gridRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -84,14 +109,14 @@ const DataTable: React.FC<DataTableProps> = ({ data }) => {
size={14}
onClick={(e) => {
e.stopPropagation();
setSelectRowColumn({
handleRefClick({
id: props.row.___process_step_id,
index: props.row.___extraction_index,
filename: props.row.Filename,
key: props.column.key,
});
setDisplayDrawer(true);
}}
style={{ cursor: isLoading ? "not-allowed" : "pointer" }}
/>
</div>
)}
Expand Down
20 changes: 12 additions & 8 deletions frontend/src/components/ExtractReferenceDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useQuery } from "@tanstack/react-query";
import HighlightPdfViewer from "../ee/components/HighlightPdfViewer";
import { FlattenedSource, Source } from "@/interfaces/processSteps";
import { BASE_STORAGE_URL } from "@/constants";
import { useProcessStepReferences } from "@/hooks/useProcessStepReferences";

interface IProps {
process_step_id: string;
Expand All @@ -24,17 +25,20 @@ const ExtractReferenceDrawer = ({
index,
onCancel,
}: IProps) => {
const { data, error, isLoading } = useQuery({
queryKey: ["processStepReferences", process_step_id],
queryFn: async () => {
return await GetProcessStepReferences(process_step_id);
},
});
const { data, error, isLoading } = useProcessStepReferences(process_step_id);

const output = data?.output_reference?.[index]?.[column_name] ?? null;
if (error) {
return (
<Drawer isOpen={isOpen} onClose={onCancel} title={column_name}>
<div className="text-black">
Failed to load references. Please try again.
</div>
</Drawer>
);
}

const filtered_output = data?.output_reference?.[index].filter(
(item: Source) => item.name == column_name
(item: Source) => item.name == column_name && item.page_numbers
);

const filteredSourceDetails: FlattenedSource[] = [];
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/hooks/useProcessStepReferences.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { GetProcessStepReferences } from "@/services/processSteps";

export const useProcessStepReferences = (process_step_id: string) => {
return useQuery({
queryKey: ["processStepReferences", process_step_id],
queryFn: async () => {
return await GetProcessStepReferences(process_step_id);
},
});
};

0 comments on commit 44e1e1f

Please sign in to comment.