Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ts migrate upgrade table contents #7085

Merged
merged 6 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/TableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ const ItemsList: React.FC<IPropsItemsList> = ({
if (depth > maxDepth || !items) {
return null
}

return (
<>
{items.map((item, index) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { motion } from "framer-motion"
import { Link } from "gatsby"
import styled from "styled-components"

import type { Item as ItemTableOfContents } from "./TableOfContents"

const customIdRegEx = /^.+(\s*\{#([A-Za-z0-9\-_]+?)\}\s*)$/

const Aside = styled.aside`
Expand Down Expand Up @@ -40,12 +42,10 @@ const StyledTableOfContentsLink = styled(Link)`
margin-bottom: 0.5rem !important;
`

const slugify = (s) =>
const slugify = (s: string): string =>
encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, "-"))

const getCustomId = (title) => {
// match .md headings
// ex. `## Why stake with a pool? {#why-stake-with-a-pool}`
const getCustomId = (title: string): string => {
const match = customIdRegEx.exec(title)
if (match) {
return match[2].toLowerCase()
Expand All @@ -54,19 +54,32 @@ const getCustomId = (title) => {
return slugify(title)
}

const trimmedTitle = (title) => {
const trimmedTitle = (title: string): string => {
const match = customIdRegEx.exec(title)
return match ? title.replace(match[1], "").trim() : title
}

const TableOfContentsLink = ({ depth, item }) => {
export interface Item extends ItemTableOfContents {}

interface IPropsTableOfContentsLink {
depth: number
item: Item
}

const TableOfContentsLink: React.FC<IPropsTableOfContentsLink> = ({
depth,
item,
}: {
depth: number
item: Item
}) => {
const url = `#${getCustomId(item.title)}`
let isActive = false
if (typeof window !== `undefined`) {
isActive = window.location.hash === url
}
const isNested = depth === 2
let classes = ""
const isNested: boolean = depth === 2
let classes: string = ""
if (isActive) {
classes += " active"
}
Expand All @@ -80,30 +93,61 @@ const TableOfContentsLink = ({ depth, item }) => {
)
}

const ItemsList = ({ items, depth, maxDepth }) =>
depth <= maxDepth && !!items
? items.map((item, index) => (
interface IPropsItemsList {
items: Array<Item>
depth: number
maxDepth: number
}

const ItemsList: React.FC<IPropsItemsList> = ({
items,
depth,
maxDepth,
}: {
items: Array<Item>
depth: number
maxDepth: number
}) => {
if (depth > maxDepth || !items) {
return null
}

return (
<>
{items.map((item, index) => (
<ListItem key={index}>
<div>
<TableOfContentsLink depth={depth} item={item} />
</div>
</ListItem>
))
: null
))}
</>
)
}

export interface IProps {
items: Array<Item>
maxDepth?: number
className?: string
}

const UpgradeTableOfContents = ({ items, maxDepth, className }) => {
const UpgradeTableOfContents: React.FC<IProps> = ({
items,
maxDepth = 1,
className,
}) => {
if (!items) {
return null
}
// Exclude <h1> from TOC
if (items.length === 1) {
items = items[0].items
items = [items[0]]
}

return (
<Aside className={className}>
<OuterList>
<ItemsList items={items} depth={0} maxDepth={maxDepth ? maxDepth : 1} />
<ItemsList items={items} depth={0} maxDepth={maxDepth} />
</OuterList>
</Aside>
)
Expand Down
2 changes: 1 addition & 1 deletion src/templates/staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ const StakingPage = ({
{mdx.frontmatter.sidebar && tocItems && (
<UpgradeTableOfContents
items={tocItems}
maxDepth={mdx.frontmatter.sidebarDepth}
maxDepth={mdx.frontmatter.sidebarDepth!}
/>
)}
</InfoColumn>
Expand Down
14 changes: 6 additions & 8 deletions src/templates/upgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import PageMetadata from "../components/PageMetadata"
import Pill from "../components/Pill"
import RandomAppList from "../components/RandomAppList"
import Roadmap from "../components/Roadmap"
import UpgradeTableOfContents from "../components/UpgradeTableOfContents"
import UpgradeTableOfContents, {
Item as ItemTableOfContents,
} from "../components/UpgradeTableOfContents"
import Translation from "../components/Translation"
import TranslationsInProgress from "../components/TranslationsInProgress"
import SectionNav from "../components/SectionNav"
Expand All @@ -44,11 +46,7 @@ import MergeInfographic from "../components/MergeInfographic"
import FeedbackCard from "../components/FeedbackCard"

import { getLocaleTimestamp } from "../utils/time"
import {
isLangRightToLeft,
translateMessageId,
TranslationKey,
} from "../utils/translations"
import { isLangRightToLeft } from "../utils/translations"
import { getSummaryPoints } from "../utils/getSummaryPoints"
import { Lang } from "../utils/languages"
import { Context } from "../types"
Expand Down Expand Up @@ -345,7 +343,7 @@ const UpgradePage = ({
throw new Error("Required `title` property missing for upgrade template")

const isRightToLeft = isLangRightToLeft(mdx.frontmatter.lang as Lang)
const tocItems = mdx.tableOfContents?.items
const tocItems = mdx.tableOfContents?.items as Array<ItemTableOfContents>

// FIXME: remove this any, currently not sure how to fix the ts error
const parent: any = mdx.parent
Expand Down Expand Up @@ -394,7 +392,7 @@ const UpgradePage = ({
{mdx.frontmatter.sidebar && tocItems && (
<UpgradeTableOfContents
items={tocItems}
maxDepth={mdx.frontmatter.sidebarDepth}
maxDepth={mdx.frontmatter.sidebarDepth!}
/>
)}
</InfoColumn>
Expand Down
8 changes: 4 additions & 4 deletions src/templates/use-cases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import PageMetadata from "../components/PageMetadata"
import Pill from "../components/Pill"
import RandomAppList from "../components/RandomAppList"
import Roadmap from "../components/Roadmap"
import UpgradeTableOfContents from "../components/UpgradeTableOfContents"
import TableOfContents, {
import UpgradeTableOfContents, {
Item as ItemTableOfContents,
} from "../components/TableOfContents"
} from "../components/UpgradeTableOfContents"
import TableOfContents from "../components/TableOfContents"
import TranslationsInProgress from "../components/TranslationsInProgress"
import Translation from "../components/Translation"
import SectionNav from "../components/SectionNav"
Expand Down Expand Up @@ -421,7 +421,7 @@ const UseCasePage = ({
{mdx.frontmatter.sidebar && tocItems && (
<UpgradeTableOfContents
items={tocItems}
maxDepth={mdx.frontmatter.sidebarDepth}
maxDepth={mdx.frontmatter.sidebarDepth!}
/>
)}
</InfoColumn>
Expand Down