feat(TextObjectPreview): cache file's fetched content

This commit is contained in:
Aminejv 2021-11-29 08:40:21 +01:00
parent d65162d916
commit b3a79ba686
3 changed files with 22 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import * as Strings from "~/common/strings";
import { css } from "@emotion/react"; import { css } from "@emotion/react";
import { Markdown } from "~/components/system/components/Markdown"; import { Markdown } from "~/components/system/components/Markdown";
import { H1, H2, H3, H4, P1, UL, OL, LI, A } from "~/components/system/components/Typography"; import { H1, H2, H3, H4, P1, UL, OL, LI, A } from "~/components/system/components/Typography";
import { useCache, useIsomorphicLayoutEffect } from "~/common/hooks";
const STYLES_ASSET = (theme) => css` const STYLES_ASSET = (theme) => css`
padding: 120px calc(32px + 16px + 8px); padding: 120px calc(32px + 16px + 8px);
@ -133,12 +134,18 @@ const STYLES_INTENT = (theme) => css`
); );
`; `;
export default function MarkdownFrame({ url, date }) { export default function MarkdownFrame({ cid, url, date }) {
const [content, setContent] = React.useState(""); const [cache, setCache] = useCache();
const cachedContent = cache[cid] || "";
const [content, setContent] = React.useState(cachedContent);
useIsomorphicLayoutEffect(() => {
if (cachedContent) return;
React.useEffect(() => {
fetch(url).then(async (res) => { fetch(url).then(async (res) => {
const content = await res.text(); const content = await res.text();
setCache(content);
setContent(content); setContent(content);
}); });
}, []); }, []);

View File

@ -5,7 +5,7 @@ import * as Styles from "~/common/styles";
import * as Utilities from "~/common/utilities"; import * as Utilities from "~/common/utilities";
import { P3 } from "~/components/system"; import { P3 } from "~/components/system";
import { useIsomorphicLayoutEffect } from "~/common/hooks"; import { useCache, useIsomorphicLayoutEffect } from "~/common/hooks";
import { css } from "@emotion/react"; import { css } from "@emotion/react";
import FilePlaceholder from "~/components/core/ObjectPreview/placeholders/File"; import FilePlaceholder from "~/components/core/ObjectPreview/placeholders/File";
@ -29,12 +29,21 @@ const STYLES_TEXT_PREVIEW = (theme) =>
}); });
export default function TextObjectPreview({ url, file, ...props }) { export default function TextObjectPreview({ url, file, ...props }) {
const [{ content, error }, setState] = React.useState({ content: "", error: undefined }); const [cache, setCache] = useCache();
const cachedContent = cache[file.cid] || "";
const [{ content, error }, setState] = React.useState({
content: cachedContent,
error: undefined,
});
useIsomorphicLayoutEffect(() => { useIsomorphicLayoutEffect(() => {
if (cachedContent) return;
fetch(url) fetch(url)
.then(async (res) => { .then(async (res) => {
const content = await res.text(); const content = await res.text();
setCache({ key: file.cid, value: content });
setState({ content }); setState({ content });
}) })
.catch((e) => { .catch((e) => {

View File

@ -198,7 +198,7 @@ export default class SlateMediaObject extends React.Component {
} }
if (Validations.isMarkdown(file.filename, type)) { if (Validations.isMarkdown(file.filename, type)) {
return <MarkdownFrame date={file.createdAt} url={url} />; return <MarkdownFrame date={file.createdAt} cid={file?.cid} url={url} />;
} }
if (Validations.isPreviewableImage(type)) { if (Validations.isPreviewableImage(type)) {