-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platform): add descriptions and titles to each route
- Loading branch information
1 parent
74ca335
commit 067fec6
Showing
16 changed files
with
391 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright 2022 ByteDance and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { compact } from 'lodash' | ||
import type { FC } from 'react' | ||
import { Helmet as ReactHelmet } from 'react-helmet' | ||
|
||
import Logo from './logo.png' | ||
|
||
type Props = { | ||
title?: string | ||
description?: string | ||
keywords?: string[] | ||
} | ||
|
||
const DEFAULT_TITLE = 'Perfsee' | ||
const DEFAULT_DESCRIPTION = 'Perfsee is set of tools for measuring and debugging performance of frontend applications' | ||
const DEFAULT_KEYWORDS = ['perfsee', 'performance', 'frontend', 'webpack', 'lighthouse'] | ||
|
||
export const Helmet: FC<Props> = (props) => { | ||
const { title = DEFAULT_TITLE, description = DEFAULT_DESCRIPTION, keywords = DEFAULT_KEYWORDS } = props | ||
|
||
const keywordsString = compact(keywords).join(', ') | ||
|
||
return ( | ||
<ReactHelmet defaultTitle={DEFAULT_TITLE}> | ||
{title && <title>{title}</title>} | ||
{description && <meta name="description" content={description} />} | ||
{keywordsString && <meta name="keywords" content={keywordsString} />} | ||
|
||
<meta property="og:title" content={title} /> | ||
<meta property="og:type" content="website" /> | ||
<meta property="og:description" content={description} /> | ||
|
||
<meta property="og:image" content={Logo} /> | ||
<meta property="og:image:type" content="image/png" /> | ||
<meta property="og:image:width" content="960" /> | ||
<meta property="og:image:height" content="960" /> | ||
<meta property="og:image:alt" content="Perfsee Logo" /> | ||
|
||
<meta name="twitter:card" content="summary_large_image" /> | ||
<meta name="twitter:title" content={title} /> | ||
<meta name="twitter:description" content={description} /> | ||
<meta name="twitter:image" content={Logo} /> | ||
</ReactHelmet> | ||
) | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2022 ByteDance and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { useMemo } from 'react' | ||
import { Route as ReactRoute } from 'react-router' | ||
import type { RouteProps as ReactRouteProps } from 'react-router' | ||
|
||
import { titleFactory as titleFactories } from '@perfsee/shared/routes' | ||
|
||
import { Helmet } from '../helmet' | ||
|
||
interface RouteProps extends ReactRouteProps { | ||
title?: (data: any) => string | ||
// transferred by Switch component | ||
computedMatch?: any | ||
} | ||
|
||
export const Route = (props: RouteProps) => { | ||
const { title, path, computedMatch, ...routeProps } = props | ||
|
||
const titleFactory = useMemo(() => { | ||
if (!title) { | ||
if (typeof path === 'string') { | ||
return titleFactories[path] as (data: any) => string | undefined | ||
} | ||
return undefined | ||
} else { | ||
return title | ||
} | ||
}, [path, title]) | ||
|
||
const titleString = useMemo(() => { | ||
if (!titleFactory) { | ||
return null | ||
} | ||
|
||
return titleFactory(computedMatch?.params ?? {}) | ||
}, [computedMatch?.params, titleFactory]) | ||
|
||
if (titleString) { | ||
return ( | ||
<> | ||
<Helmet title={titleString} /> | ||
<ReactRoute path={path} {...routeProps} /> | ||
</> | ||
) | ||
} | ||
|
||
return <ReactRoute path={path} {...routeProps} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.