Skip to content

Commit

Permalink
refactor(upgradeTableOfContent): migrate component to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Mousticke committed Jul 15, 2022
1 parent 473a011 commit 00f77f5
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions src/components/UpgradeTableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import styled from "styled-components"

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

interface Item {
id: string
title: string
}

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

const Aside = styled.aside`
padding: 0rem;
text-align: right;
Expand Down Expand Up @@ -57,14 +68,20 @@ const trimmedTitle = (title: string): string => {
return match ? title.replace(match[1], "").trim() : title
}

const TableOfContentsLink = ({ depth, item }) => {
const TableOfContentsLink = ({
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 @@ -78,26 +95,28 @@ const TableOfContentsLink = ({ depth, item }) => {
)
}

const ItemsList = ({ items, depth, maxDepth }) =>
depth <= maxDepth && !!items
? items.map((item, index) => (
const ItemsList = ({
items,
depth,
maxDepth,
}: {
items: Item[]
depth: number
maxDepth: number
}) =>
depth <= maxDepth && !!items ? (
<React.Fragment>
{items.map((item, index) => (
<ListItem key={index}>
<div>
<TableOfContentsLink depth={depth} item={item} />
</div>
</ListItem>
))
: null

interface Item {
items: Array<{ id: string; title: string }>
}

export interface IProps {
items: Array<Item>
maxDepth: number | null
className?: string
}
))}
</React.Fragment>
) : (
<></>
)

const UpgradeTableOfContents: React.FC<IProps> = ({
items,
Expand All @@ -109,7 +128,7 @@ const UpgradeTableOfContents: React.FC<IProps> = ({
}
// Exclude <h1> from TOC
if (items.length === 1) {
items = items[0].items
items = [items[0]]
}

return (
Expand Down

0 comments on commit 00f77f5

Please sign in to comment.