Skip to content

Commit

Permalink
I might have to rewrite the entiore document and audio preview funcit…
Browse files Browse the repository at this point in the history
…onality
  • Loading branch information
aamirazad committed Jul 25, 2024
1 parent 46b8e50 commit 2178d07
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 68 deletions.
14 changes: 0 additions & 14 deletions src/app/@modal/(.)paperless/document/[id]/page.tsx

This file was deleted.

16 changes: 1 addition & 15 deletions src/components/audio-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ async function fetchWhishperRecording(searchId: string, whishperURL: string) {
}
}

async function downloadWhishperRecording(url: string, name: string) {
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
const recordingUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.download = name;
link.href = recordingUrl;
link.click();
} else {
console.error("Failed to download");
return null;
}
}

async function deleteWhishperRecording(url: string) {
const response = await fetch(url, {
method: "DELETE",
Expand Down Expand Up @@ -263,6 +248,7 @@ function AudioInfo({ id }: AudioInfoProps) {
"An error occurred while deleting the recording.",
});
}
router.back();
}}
>
Continue
Expand Down
50 changes: 11 additions & 39 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ const queryClient = new QueryClient();

async function getPaperlessDocument(
documentId: number,
userData: UsersTableType,
): Promise<string | null> {
const userData = await getUserData();
if (!userData) {
console.error("Error getting user data");
return null;
}
try {
const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`;
const response = await fetch(url, {
Expand Down Expand Up @@ -135,44 +131,18 @@ async function deleteDocument(documentId: number) {
function DocumentViewer(props: { id: number }) {
const router = useRouter();

const [pdfUrl, setPdfUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const fetchDataCalledRef = useRef(false);

useEffect(() => {
if (!fetchDataCalledRef.current) {
const fetchData = async () => {
setLoading(true);

try {
const objectUrl = await getPaperlessDocument(props.id);
if (objectUrl) {
setPdfUrl(objectUrl);
} else {
setPdfUrl(null);
}
} catch (error) {
console.error("An error occurred:", error);
setPdfUrl(null);
} finally {
setLoading(false);
}
};

fetchData().catch((error) => {
console.error("An error occurred:", error);
});

fetchDataCalledRef.current = true; // Mark as fetched
}
}, [props.id]); // Include props.id in the dependency array if refetch is needed on id change

const { data: userData, isLoading: isUserDataLoading } = useQuery({
queryKey: ["userData"],
queryFn: fetchUserData,
});

if (loading ?? isUserDataLoading) {
const { data: pdfUrl, isLoading: isPdfUrlLoading } = useQuery({
queryKey: ["pdfUrl", props.id, userData], // Include id and paperlessURL in the query key
queryFn: () => getPaperlessDocument(props.id, userData!),
enabled: !!userData,
});

if (isPdfUrlLoading ?? isUserDataLoading) {
return <SkeletonLoader />;
}

Expand Down Expand Up @@ -224,6 +194,7 @@ function DocumentViewer(props: { id: number }) {
Download
<Download />
</a>
<div id="dialog-container" />
<OpenExternalLink
className={buttonVariants({ variant: "default" })}
href={`${userData.paperlessURL}/documents/${props.id}/details/`}
Expand Down Expand Up @@ -256,9 +227,10 @@ function DocumentViewer(props: { id: number }) {
} else {
toast("Error deleting pdf", {
description:
"An error occurred while deleting the recording.",
"An error occurred while deleting the pdf.",
});
}
router.back();
}}
>
Continue
Expand Down

0 comments on commit 2178d07

Please sign in to comment.