Skip to content

Commit

Permalink
fix(docz-theme-default): useMemo with conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 8, 2019
1 parent 754c8cb commit 3b653e5
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions core/docz-theme-default/src/components/ui/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ export const LinkStyled = styled.a<any>`
${get('styles.link')};
`

const getSeparator = (separator: string, href?: string) => {
if (typeof window === 'undefined') return null
return [
location.pathname
.split(separator)
.slice(0, -1)
.join(separator)
.slice(1),
(href || '').replace(/^(?:\.\/)+/gi, ''),
].join('/')
}

type LinkProps = React.AnchorHTMLAttributes<any>
export const Link: SFC<LinkProps> = ({ href, ...props }) => {
const { separator, linkComponent: Link } = useConfig()
const docs = useDocs()
const toCheck =
typeof window === 'undefined'
? null
: useMemo(
() =>
[
location.pathname
.split(separator)
.slice(0, -1)
.join(separator)
.slice(1),
(href || '').replace(/^(?:\.\/)+/gi, ''),
].join('/'),
[separator]
)
const toCheck = useMemo(() => getSeparator(separator, href), [
separator,
href,
])

const matched = docs && docs.find(doc => doc.filepath === toCheck)
const nHref = matched ? matched.route : href
Expand Down

0 comments on commit 3b653e5

Please sign in to comment.