-
Notifications
You must be signed in to change notification settings - Fork 20
Chore : Refactor Header.tsx #390
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
Open
Grafikart
wants to merge
1
commit into
v3-master
Choose a base branch
from
ref/header-refactor
base: v3-master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,19 +1,9 @@ | ||
| import { useState, useEffect } from 'react'; | ||
| import { HeaderType } from './HeaderType'; | ||
| import { isValidElement, useMemo } from 'react'; | ||
| import { HeaderQuickAccessItem, HeaderType } from './HeaderType'; | ||
| import { DEFAULT_HEADER } from './default-header'; | ||
| import ConvertContent from '../../utils/convertContent'; | ||
| import Button from '@codegouvfr/react-dsfr/Button'; | ||
| import { fr } from '@codegouvfr/react-dsfr'; | ||
| import Display from '@codegouvfr/react-dsfr/Display/Display'; | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const iconTheme = fr.cx('fr-icon-theme-fill'); | ||
|
|
||
| function getAuthLabel(isAuthenticated: boolean): string { | ||
| if (isAuthenticated) { | ||
| return 'Me déconnecter'; | ||
| } | ||
| return 'Me connecter'; | ||
| } | ||
|
|
||
| export type HeaderProps = { | ||
| header?: HeaderType; | ||
|
|
@@ -22,39 +12,24 @@ export type HeaderProps = { | |
| }; | ||
|
|
||
| export function Header(props: HeaderProps) { | ||
| const { header, handleOidcAuth, isAuthenticated = false } = props; | ||
|
|
||
| const [brandTop, setBrandTop] = useState(DEFAULT_HEADER.brandTop); | ||
| const [homeLinkProps, setHomeLinkProps] = useState( | ||
| DEFAULT_HEADER.homeLinkProps | ||
| ); | ||
| const [serviceTitle, setServiceTitle] = useState(DEFAULT_HEADER.serviceTitle); | ||
| const [operatorLogo, setOperatorLogo] = useState(DEFAULT_HEADER.operatorLogo); | ||
| const [quickAccessItems, setQuickAccessItems] = useState<Array<any>>([]); | ||
|
|
||
| useEffect(() => { | ||
| if (!header) { | ||
| return; | ||
| } | ||
| setBrandTop(header.brandTop || DEFAULT_HEADER.brandTop); | ||
| setHomeLinkProps(header.homeLinkProps || DEFAULT_HEADER.homeLinkProps); | ||
| setServiceTitle(header.serviceTitle || DEFAULT_HEADER.serviceTitle); | ||
| setOperatorLogo(header.operatorLogo || DEFAULT_HEADER.operatorLogo); | ||
| }, [header]); | ||
|
|
||
| useEffect(() => { | ||
| const others = header?.quickAccessItems || []; | ||
| setQuickAccessItems([ | ||
| ...others, | ||
| const { header, handleOidcAuth, isAuthenticated } = props; | ||
| const brandTop = header?.brandTop ?? DEFAULT_HEADER.brandTop; | ||
| const homeLinkProps = header?.homeLinkProps ?? DEFAULT_HEADER.homeLinkProps; | ||
| const serviceTitle = header?.serviceTitle ?? DEFAULT_HEADER.serviceTitle; | ||
| const operatorLogo = header?.operatorLogo ?? DEFAULT_HEADER.operatorLogo; | ||
| const quickAccessItems = useMemo( | ||
| () => [ | ||
| ...(header?.quickAccessItems ?? []), | ||
| { | ||
| iconId: 'fr-icon-lock-line', | ||
| buttonProps: { | ||
| onClick: handleOidcAuth, | ||
| }, | ||
| text: getAuthLabel(isAuthenticated), | ||
| }, | ||
| ]); | ||
| }, [isAuthenticated, handleOidcAuth, header]); | ||
| text: isAuthenticated ? 'Me déconnecter' : 'Me connecter', | ||
| } satisfies HeaderQuickAccessItem, | ||
| ], | ||
| [isAuthenticated, handleOidcAuth, header] | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
|
|
@@ -113,35 +88,9 @@ export function Header(props: HeaderProps) { | |
| Paramètres d'affichage | ||
| </button> | ||
| </li> | ||
| {quickAccessItems && | ||
| quickAccessItems.map((quickAccessItem, index) => { | ||
| return ( | ||
| <li key={index}> | ||
| {quickAccessItem.buttonProps ? ( | ||
| <Button | ||
| iconId={quickAccessItem.iconId} | ||
| onClick={quickAccessItem.buttonProps.onClick} | ||
| > | ||
| {quickAccessItem.text} | ||
| </Button> | ||
| ) : ( | ||
| <Button | ||
| linkProps={{ | ||
| target: quickAccessItem.linkProps?.target, | ||
| href: quickAccessItem.linkProps?.href, | ||
| title: | ||
| quickAccessItem.linkProps?.target === | ||
| '_blank' | ||
| ? `${quickAccessItem.text} - ouvre une nouvelle fenêtre` | ||
| : quickAccessItem.text, | ||
| }} | ||
| > | ||
| {quickAccessItem.text} | ||
| </Button> | ||
| )} | ||
| </li> | ||
| ); | ||
| })} | ||
| {quickAccessItems.map((item, index) => ( | ||
| <QuickAccessLi key={index} item={item} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use index as key could cause trouble, use an id |
||
| ))} | ||
| </ul> | ||
| </div> | ||
| </div> | ||
|
|
@@ -172,34 +121,9 @@ export function Header(props: HeaderProps) { | |
| Paramètres d'affichage | ||
| </button> | ||
| </li> | ||
| {quickAccessItems && | ||
| quickAccessItems.map((quickAccessItem, index) => { | ||
| return ( | ||
| <li key={index}> | ||
| {quickAccessItem.buttonProps ? ( | ||
| <Button | ||
| iconId={quickAccessItem.iconId} | ||
| onClick={quickAccessItem.buttonProps.onClick} | ||
| > | ||
| {quickAccessItem.text} | ||
| </Button> | ||
| ) : ( | ||
| <Button | ||
| linkProps={{ | ||
| target: quickAccessItem.linkProps?.target, | ||
| href: quickAccessItem.linkProps?.href, | ||
| title: | ||
| quickAccessItem.linkProps?.target === '_blank' | ||
| ? `${quickAccessItem.text} - ouvre une nouvelle fenêtre` | ||
| : quickAccessItem.text, | ||
| }} | ||
| > | ||
| {quickAccessItem.text} | ||
| </Button> | ||
| )} | ||
| </li> | ||
| ); | ||
| })} | ||
| {quickAccessItems.map((item, index) => ( | ||
| <QuickAccessLi key={index} item={item} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
| ))} | ||
| </ul> | ||
| </div> | ||
| </div> | ||
|
|
@@ -209,3 +133,57 @@ export function Header(props: HeaderProps) { | |
| </> | ||
| ); | ||
| } | ||
|
|
||
| type QuickAccessLiProps = { | ||
| item: HeaderQuickAccessItem; | ||
| }; | ||
|
|
||
| function QuickAccessLi({ item }: QuickAccessLiProps) { | ||
| if (!item) { | ||
| return null; | ||
| } | ||
|
|
||
| if (typeof item === 'object' && 'buttonProps' in item && item.buttonProps) { | ||
| return ( | ||
| <li> | ||
| <Button iconId={item.iconId} onClick={item.buttonProps.onClick}> | ||
| {item.text} | ||
| </Button> | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| if (typeof item === 'object' && 'linkProps' in item) { | ||
| return ( | ||
| <li> | ||
| <Button | ||
| linkProps={{ | ||
| target: item.linkProps.target, | ||
| href: item.linkProps.href, | ||
| title: | ||
| item.linkProps.target === '_blank' | ||
| ? `${item.text} - ouvre une nouvelle fenêtre` | ||
| : `${item.text}`, | ||
| }} | ||
| > | ||
| {item.text} | ||
| </Button> | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| // Item shape is unexpected at this point, try to find the best fallback to avoid errors | ||
| if (isValidElement(item)) { | ||
| return <li>{item}</li>; | ||
| } | ||
|
|
||
| if (typeof item === 'object' && 'text' in item) { | ||
| return ( | ||
| <li> | ||
| <Button>{`${item.text}`}</Button> | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import { HeaderProps } from '@codegouvfr/react-dsfr/Header'; | ||
| import type { ItemOf } from '../../type.utils'; | ||
|
|
||
| export type HeaderType = HeaderProps & {}; | ||
|
|
||
| export type HeaderQuickAccessItem = ItemOf< | ||
| Required<HeaderType>['quickAccessItems'] | ||
| >; |
This file contains hidden or 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,5 @@ | ||
| type Defined<T> = T extends undefined | infer V ? V : T; | ||
|
|
||
| export type ItemOf<ArrayType> = Defined<ArrayType> extends (infer ElementType)[] | ||
| ? ElementType | ||
| : never; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it will clearer to extract this into function that will take header and return brandTop, homeLinkProps ...