From ac959933bb431d8cd7f6b0134d095b18746b6289 Mon Sep 17 00:00:00 2001 From: developomp Date: Sun, 17 Apr 2022 22:24:12 +0900 Subject: [PATCH] simplified `PostCard` logic and fixed bug - simplified locale-specific url generation logic for `PostCard` - fixed bug where navigation fails on series contents --- src/components/PostCard.tsx | 31 ++++++++++++++----------- src/pages/Home/Home.tsx | 3 +-- src/pages/Page/Page.tsx | 2 +- src/pages/Page/SeriesControlButtons.tsx | 22 ++++++++++++------ src/pages/Page/helper.ts | 8 +++---- src/pages/Search/Search.tsx | 2 +- 6 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/components/PostCard.tsx b/src/components/PostCard.tsx index 7c44596..37e0e8c 100644 --- a/src/components/PostCard.tsx +++ b/src/components/PostCard.tsx @@ -1,3 +1,4 @@ +import { useContext } from "react" import styled from "styled-components" import { Link } from "react-router-dom" @@ -15,6 +16,7 @@ import TagList from "./TagList" import MainContent from "./MainContent" import theming from "../styles/theming" +import { globalContext } from "../globalContext" const PostCardContainer = styled(Link)` display: block; @@ -66,7 +68,7 @@ const StyledTitle = styled.h1` const StyledMetaContainer = styled.small`` interface PostCardData extends PostData { - url: string + content_id: string } interface Props { @@ -75,40 +77,43 @@ interface Props { const PostCard = (props: Props) => { const { postData } = props + const { content_id, wordCount, date, readTime, title, tags } = postData + + const { globalState } = useContext(globalContext) return ( - + - {postData.title || "No title"} + {title || "No title"} {/* show "(series)" for urls that matches regex "/series/" */} - {/\/series\/[^/]*$/.test(postData.url) && " (series)"} + {/\/series\/[^/]*$/.test(content_id) && " (series)"}
- {postData.tags && - postData.tags.map((tag) => { - return + {tags && + tags.map((tag) => { + return })}
    - {postData.date || "Unknown date"} + {date || "Unknown date"}          - {postData.readTime - ? postData.readTime + " read" - : "unknown read time"} + {readTime ? readTime + " read" : "unknown read time"}          - {typeof postData.wordCount === "number" - ? postData.wordCount + " words" + {typeof wordCount === "number" + ? wordCount + " words" : "unknown length"}
diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 9490884..3abbf7a 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -56,8 +56,7 @@ const Home = () => { / - url: `/${locale}${content_id.replace(/(.kr)$/g, "")}`, + content_id: content_id, ...map.posts[content_id], }} /> diff --git a/src/pages/Page/Page.tsx b/src/pages/Page/Page.tsx index 5cf7eb6..e3ec576 100644 --- a/src/pages/Page/Page.tsx +++ b/src/pages/Page/Page.tsx @@ -203,7 +203,7 @@ export default function Page() { diff --git a/src/pages/Page/SeriesControlButtons.tsx b/src/pages/Page/SeriesControlButtons.tsx index 321a701..ace2825 100644 --- a/src/pages/Page/SeriesControlButtons.tsx +++ b/src/pages/Page/SeriesControlButtons.tsx @@ -1,3 +1,4 @@ +import { useContext } from "react" import styled, { css } from "styled-components" import { Link } from "react-router-dom" @@ -10,6 +11,8 @@ import { import theming from "../../styles/theming" +import { globalContext } from "../../globalContext" + const Container = styled.div` display: flex; justify-content: space-between; @@ -54,15 +57,20 @@ const DisabledButton = styled.div` } ` -const SeriesControlButtons = (props: { +interface Props { seriesHome: string prevURL?: string nextURL?: string -}) => { +} + +function SeriesControlButtons({ prevURL, seriesHome, nextURL }: Props) { + const { globalState } = useContext(globalContext) + const { locale } = globalState + return ( - {props.prevURL ? ( - + {prevURL ? ( + @@ -73,14 +81,14 @@ const SeriesControlButtons = (props: { )} - + - {props.nextURL ? ( - + {nextURL ? ( + diff --git a/src/pages/Page/helper.ts b/src/pages/Page/helper.ts index 5a4cb24..8641cbf 100644 --- a/src/pages/Page/helper.ts +++ b/src/pages/Page/helper.ts @@ -79,6 +79,7 @@ export function checkURLValidity( : content_id + ".kr" // add .kr suffix switch (pageType) { + case PageType.SERIES: case PageType.POST: { if (map.posts[content_id]) return URLValidity.VALID @@ -88,20 +89,19 @@ export function checkURLValidity( break } - case PageType.SERIES_HOME: - case PageType.SERIES: { + case PageType.SERIES_HOME: { const series_keys = Object.keys(map.series) if ( series_keys.some((seriesHomeURL) => - content_id.startsWith(seriesHomeURL) + seriesHomeURL.startsWith(content_id) ) ) return URLValidity.VALID if ( series_keys.some((seriesHomeURL) => - alt_content_id.startsWith(seriesHomeURL) + seriesHomeURL.startsWith(alt_content_id) ) ) return URLValidity.VALID_BUT_IN_OTHER_LOCALE diff --git a/src/pages/Search/Search.tsx b/src/pages/Search/Search.tsx index 8b6ba4d..698a8b6 100644 --- a/src/pages/Search/Search.tsx +++ b/src/pages/Search/Search.tsx @@ -201,7 +201,7 @@ const Search = () => {