From 0a7bd8e91f1916d8a6bdbfc259f2fda6554e090d Mon Sep 17 00:00:00 2001 From: juliopavila Date: Mon, 27 May 2024 16:07:18 -0300 Subject: [PATCH] feat: handle attemp for articles --- .../views/publication/ArticleView.tsx | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/app/src/components/views/publication/ArticleView.tsx b/packages/app/src/components/views/publication/ArticleView.tsx index 84480d84..7238fe32 100644 --- a/packages/app/src/components/views/publication/ArticleView.tsx +++ b/packages/app/src/components/views/publication/ArticleView.tsx @@ -1,6 +1,6 @@ -import { Chip, CircularProgress, Divider, Grid, Typography } from "@mui/material" +import { Box, Chip, CircularProgress, Divider, Grid, Typography } from "@mui/material" import moment from "moment" -import React, { useEffect, useMemo, useState } from "react" +import React, { useCallback, useEffect, useMemo, useState } from "react" import { Helmet } from "react-helmet" import { useParams } from "react-router-dom" import { useArticleContext } from "@/services/publications/contexts" @@ -16,8 +16,6 @@ import { addUrlToImageHashes } from "@/services/publications/utils/article-metho import { useIPFSContext } from "@/services/ipfs/context" interface ArticleViewProps {} -//Provisional solution to detect older articles and check the dif between markdown and html articles -// const VALIDATION_DATE = "2023-02-02T00:00:00Z" export const ArticleView: React.FC = () => { const { publicationSlug } = useParams<{ publicationSlug: string }>() const { articleId } = useParams<{ articleId: string }>() @@ -25,20 +23,31 @@ export const ArticleView: React.FC = () => { const { article, saveArticle, loading } = useArticleContext() const { data, executeQuery, imageSrc } = useArticle(articleId || "") const publication = usePublication(publicationSlug || "") + useDynamicFavIcon(publication?.imageSrc) - // const dateCreation = useMemo( - // () => article?.postedOn && new Date(parseInt(article.postedOn) * 1000), - // [article?.postedOn], - // ) const date = useMemo( () => article?.lastUpdated && new Date(parseInt(article.lastUpdated) * 1000), [article?.lastUpdated], ) - // const isAfterHtmlImplementation = useMemo(() => moment(dateCreation).isAfter(VALIDATION_DATE), [dateCreation]) + const isValidHash = useMemo(() => article && isIPFS.multihash(article.article), [article]) const [articleToShow, setArticleToShow] = useState("") + const [attempt, setAttempt] = useState(0) + const fetchArticleToShow = useCallback(() => { + if (article && !articleToShow) { + if (isValidHash) { + const decode = async () => { + const post = await decodeIpfsHash(article.article) + setArticleToShow(addUrlToImageHashes(post)) + } + decode() + } else { + setArticleToShow(addUrlToImageHashes(article.article)) + } + } + }, [article, articleToShow, decodeIpfsHash, isValidHash]) useEffect(() => { if (!article && articleId) { executeQuery() @@ -52,17 +61,15 @@ export const ArticleView: React.FC = () => { }, [data, article, saveArticle]) useEffect(() => { - if (article && !articleToShow && !isValidHash) { - return setArticleToShow(addUrlToImageHashes(article.article)) - } - if (article && !articleToShow && isValidHash) { - const decode = async () => { - const post = await decodeIpfsHash(article.article) - setArticleToShow(addUrlToImageHashes(post)) - } - decode() + if (article && !articleToShow && attempt < 5) { + const timer = setTimeout(() => { + fetchArticleToShow() + setAttempt(attempt + 1) + }, 2000) + + return () => clearTimeout(timer) } - }, [article, articleToShow, decodeIpfsHash, isValidHash]) + }, [article, articleToShow, attempt, fetchArticleToShow]) return ( = () => { )} + {!articleToShow && ( + + + Decoding article... + + )} {articleToShow && } - {/* {articleToShow} */}