mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
fix: optimise pdf viewer rerendering
This commit is contained in:
6
package-lock.json
generated
6
package-lock.json
generated
@ -16479,6 +16479,11 @@
|
|||||||
"url": "https://opencollective.com/unified"
|
"url": "https://opencollective.com/unified"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/remeda": {
|
||||||
|
"version": "1.27.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/remeda/-/remeda-1.27.1.tgz",
|
||||||
|
"integrity": "sha512-va05uuDBz/E55O9wmpDdbVlwdWbHGJJy3oC0EAHSFn74MpWF3S81NVJDz/FW05bc/UDg769t1u6YPhBK/gmvLw=="
|
||||||
|
},
|
||||||
"node_modules/repeat-string": {
|
"node_modules/repeat-string": {
|
||||||
"version": "1.6.1",
|
"version": "1.6.1",
|
||||||
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
||||||
@ -19812,6 +19817,7 @@
|
|||||||
"next-auth": "4.22.3",
|
"next-auth": "4.22.3",
|
||||||
"pdf-lib": "^1.17.1",
|
"pdf-lib": "^1.17.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
"remeda": "^1.27.1",
|
||||||
"stripe": "^12.7.0",
|
"stripe": "^12.7.0",
|
||||||
"ts-pattern": "^5.0.5",
|
"ts-pattern": "^5.0.5",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { createContext, useContext, useEffect, useState } from 'react';
|
import { createContext, useContext, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
|
import { equals } from 'remeda';
|
||||||
|
|
||||||
import { getLimits } from '../client';
|
import { getLimits } from '../client';
|
||||||
import { FREE_PLAN_LIMITS } from '../constants';
|
import { FREE_PLAN_LIMITS } from '../constants';
|
||||||
@ -31,15 +33,26 @@ export const LimitsProvider = ({ initialValue, children }: LimitsProviderProps)
|
|||||||
remaining: FREE_PLAN_LIMITS,
|
remaining: FREE_PLAN_LIMITS,
|
||||||
};
|
};
|
||||||
|
|
||||||
const [limits, setLimits] = useState(() => initialValue ?? defaultValue);
|
const $limits = useRef(initialValue ?? defaultValue);
|
||||||
|
const [limits, setLimits] = useState(() => $limits.current);
|
||||||
|
|
||||||
|
const refreshLimits = async () => {
|
||||||
|
const newLimits = await getLimits();
|
||||||
|
|
||||||
|
if (equals(newLimits, $limits.current)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setLimits(newLimits);
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void getLimits().then((limits) => setLimits(limits));
|
void refreshLimits();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onFocus = () => {
|
const onFocus = () => {
|
||||||
void getLimits().then((limits) => setLimits(limits));
|
void refreshLimits();
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('focus', onFocus);
|
window.addEventListener('focus', onFocus);
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
"next-auth": "4.22.3",
|
"next-auth": "4.22.3",
|
||||||
"pdf-lib": "^1.17.1",
|
"pdf-lib": "^1.17.1",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
"remeda": "^1.27.1",
|
||||||
"stripe": "^12.7.0",
|
"stripe": "^12.7.0",
|
||||||
"ts-pattern": "^5.0.5",
|
"ts-pattern": "^5.0.5",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
import { Loader } from 'lucide-react';
|
import { Loader } from 'lucide-react';
|
||||||
import { PDFDocumentProxy } from 'pdfjs-dist';
|
import { PDFDocumentProxy } from 'pdfjs-dist';
|
||||||
@ -66,6 +66,11 @@ export const PDFViewer = ({
|
|||||||
const [numPages, setNumPages] = useState(0);
|
const [numPages, setNumPages] = useState(0);
|
||||||
const [pdfError, setPdfError] = useState(false);
|
const [pdfError, setPdfError] = useState(false);
|
||||||
|
|
||||||
|
const memoizedData = useMemo(
|
||||||
|
() => ({ type: documentData.type, data: documentData.data }),
|
||||||
|
[documentData.data, documentData.type],
|
||||||
|
);
|
||||||
|
|
||||||
const isLoading = isDocumentBytesLoading || !documentBytes;
|
const isLoading = isDocumentBytesLoading || !documentBytes;
|
||||||
|
|
||||||
const onDocumentLoaded = (doc: LoadedPDFDocument) => {
|
const onDocumentLoaded = (doc: LoadedPDFDocument) => {
|
||||||
@ -134,7 +139,7 @@ export const PDFViewer = ({
|
|||||||
try {
|
try {
|
||||||
setIsDocumentBytesLoading(true);
|
setIsDocumentBytesLoading(true);
|
||||||
|
|
||||||
const bytes = await getFile(documentData);
|
const bytes = await getFile(memoizedData);
|
||||||
|
|
||||||
setDocumentBytes(bytes);
|
setDocumentBytes(bytes);
|
||||||
|
|
||||||
@ -151,7 +156,7 @@ export const PDFViewer = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
void fetchDocumentBytes();
|
void fetchDocumentBytes();
|
||||||
}, [documentData, toast]);
|
}, [memoizedData, toast]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={$el} className={cn('overflow-hidden', className)} {...props}>
|
<div ref={$el} className={cn('overflow-hidden', className)} {...props}>
|
||||||
|
|||||||
Reference in New Issue
Block a user