-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add dependencies Add packages: isomorphic-dompurify hoist-non-react-statics @types/hoist-non-react-statics react-recompose @types/react-recompose react-helmet @types/react-helmet * feat: add wrapper for React Context * feat: add context for language selection * feat: add Query type * feat: add context for router * feat: add HOC for context * feat: add HOC for language selection * feat: add HOC for router * feat: add HTML sanitizer * feat: add DocMeta component
- Loading branch information
1 parent
5de8873
commit fc99858
Showing
12 changed files
with
738 additions
and
17 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
import React from 'react'; | ||
import {Helmet} from 'react-helmet'; | ||
import {compose} from 'react-recompose'; | ||
|
||
import {sanitizeHtml} from '../../utils/sanitize'; | ||
import withRouter, {WithRouterProps} from '../../hoc/withRouter'; | ||
|
||
export interface DocMetaProps { | ||
title?: string; | ||
defaultTitle?: string; | ||
titleTemplate?: string; | ||
metadata?: Record<string, string>[]; | ||
} | ||
|
||
type DocMetaInnerProps = DocMetaProps & WithRouterProps; | ||
|
||
const sanitizeObject = (target: Record<string, string>) => { | ||
const clear = Object.entries(target).map( | ||
([name, content]) => [sanitizeHtml(name), sanitizeHtml(content)] as [string, string], | ||
); | ||
|
||
return Object.fromEntries(clear); | ||
}; | ||
|
||
const DocMeta: React.FC<DocMetaInnerProps> = (props) => { | ||
const title = sanitizeHtml(props.title); | ||
const titleTemplate = sanitizeHtml(props.titleTemplate); | ||
const defaultTitle = sanitizeHtml(props.defaultTitle); | ||
|
||
const {metadata = []} = props; | ||
|
||
return ( | ||
<Helmet title={title} defaultTitle={defaultTitle} titleTemplate={titleTemplate}> | ||
{metadata.map((element, index) => { | ||
/* list is immutable */ | ||
return <meta key={index} {...sanitizeObject(element)} />; | ||
})} | ||
</Helmet> | ||
); | ||
}; | ||
|
||
export default compose<DocMetaInnerProps, DocMetaProps>(withRouter)(DocMeta); |
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,2 @@ | ||
export {default as DocMeta} from './DocMeta'; | ||
export * from './DocMeta'; |
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,8 @@ | ||
import namedContext from './_namedContext'; | ||
|
||
export enum Lang { | ||
Ru = 'ru', | ||
En = 'en', | ||
} | ||
|
||
export default namedContext<Lang>('Lang', (process.env.DEFAULT_LANG as Lang) || Lang.En); |
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,15 @@ | ||
import type {Query} from '../models'; | ||
|
||
export interface RouterData { | ||
page: string; | ||
pathname: string; | ||
query: Query; | ||
hash?: string; | ||
as: string; | ||
hostname: string; | ||
serverRouter?: RouterData; | ||
} | ||
|
||
import namedContext from './_namedContext'; | ||
|
||
export default namedContext<RouterData>('Request', {} as RouterData); |
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,9 @@ | ||
import React from 'react'; | ||
|
||
export default function <T>(name: string, defaults: T): React.Context<T> { | ||
const Context = React.createContext(defaults); | ||
|
||
Context.displayName = name; | ||
|
||
return Context; | ||
} |
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,30 @@ | ||
import React from 'react'; | ||
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export default function <P extends Record<string, any>>( | ||
prop: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Context: React.Context<any> & {name?: string}, | ||
) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return function <T extends Record<string, any>>(Component: React.ComponentType<any>) { | ||
const contextName = Context.displayName || Context.name || 'Context'; | ||
const componentName = Component.displayName || Component.name || 'Component'; | ||
|
||
class WithContextProp extends React.Component<Omit<T, keyof P>> { | ||
static displayName = `with${contextName}(${componentName})`; | ||
|
||
static contextType = Context; | ||
|
||
render() { | ||
return <Component {...this.props} {...{[prop]: this.context}} />; | ||
} | ||
} | ||
|
||
// Copies non-react specific statics from a child component to a parent component | ||
hoistNonReactStatics(WithContextProp, Component); | ||
|
||
return WithContextProp; | ||
}; | ||
} |
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,9 @@ | ||
import LangContext, {Lang} from '../contexts/LangContext'; | ||
|
||
import withPropContext from './_withContext'; | ||
|
||
export interface WithLangProps { | ||
lang: Lang; | ||
} | ||
|
||
export default withPropContext<WithLangProps>('lang', LangContext); |
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,9 @@ | ||
import RouterContext, {RouterData} from '../contexts/RouterContext'; | ||
|
||
import withPropContext from './_withContext'; | ||
|
||
export interface WithRouterProps { | ||
router: RouterData; | ||
} | ||
|
||
export default withPropContext<WithRouterProps>('router', RouterContext); |
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,10 @@ | ||
import {sanitize} from 'isomorphic-dompurify'; | ||
|
||
const sanitizeStripOptions = { | ||
ALLOWED_TAGS: [], | ||
ALLOWED_ATTR: [], | ||
}; | ||
|
||
export function sanitizeHtml(html?: string) { | ||
return html && sanitize(html, sanitizeStripOptions); | ||
} |