-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from 10up/refactor/migrate-post-author-compon…
…ents-to-ts Migrate post author components to ts
- Loading branch information
Showing
5 changed files
with
148 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createContext, useContext } from '@wordpress/element'; | ||
|
||
export type Author = { | ||
avatar_urls: Record<string, string>; | ||
description: string; | ||
email: string; | ||
first_name: string; | ||
id: number; | ||
last_name: string; | ||
link: string; | ||
name: string; | ||
nickname: string; | ||
registered_date: string; | ||
slug: string; | ||
url: string; | ||
}; | ||
|
||
export const AuthorContext = createContext<Author>({ | ||
avatar_urls: {}, | ||
description: '', | ||
email: '', | ||
first_name: '', | ||
id: 0, | ||
last_name: '', | ||
link: '', | ||
name: '', | ||
nickname: '', | ||
registered_date: '', | ||
slug: '', | ||
url: '', | ||
}); | ||
|
||
export const useAuthor = () => { | ||
return useContext(AuthorContext); | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
Check failure on line 2 in components/author/index.tsx GitHub Actions / Cypress
|
||
import { useAuthor } from './context'; | ||
|
||
interface NameProps { | ||
tagName?: keyof JSX.IntrinsicElements; | ||
[key: string]: any; | ||
} | ||
|
||
export const Name: React.FC<NameProps> = (props) => { | ||
const { tagName: TagName = 'span', ...rest } = props; | ||
const { name, link } = useAuthor(); | ||
|
||
const wrapperProps = { ...rest }; | ||
|
||
if (TagName === 'a' && link) { | ||
wrapperProps.href = link; | ||
} | ||
|
||
return <TagName {...wrapperProps}>{name}</TagName>; | ||
}; | ||
|
||
interface FirstNameProps { | ||
tagName?: keyof JSX.IntrinsicElements; | ||
[key: string]: any; | ||
} | ||
|
||
export const FirstName: React.FC<FirstNameProps> = (props) => { | ||
const { tagName: TagName = 'span', ...rest } = props; | ||
const { first_name: firstName } = useAuthor(); | ||
|
||
return <TagName {...rest}>{firstName}</TagName>; | ||
}; | ||
|
||
interface LastNameProps { | ||
tagName?: keyof JSX.IntrinsicElements; | ||
[key: string]: any; | ||
} | ||
|
||
export const LastName: React.FC<LastNameProps> = (props) => { | ||
const { tagName: TagName = 'span', ...rest } = props; | ||
const { last_name: lastName } = useAuthor(); | ||
|
||
return <TagName {...rest}>{lastName}</TagName>; | ||
}; | ||
|
||
function useDefaultAvatar() { | ||
const { avatarURL: defaultAvatarUrl } = useSelect((select) => { | ||
const { getSettings } = select(blockEditorStore); | ||
const { __experimentalDiscussionSettings } = getSettings(); | ||
return __experimentalDiscussionSettings; | ||
}, []); | ||
return defaultAvatarUrl; | ||
} | ||
|
||
interface AvatarProps { | ||
[key: string]: any; | ||
} | ||
|
||
export const Avatar: React.FC<AvatarProps> = (props) => { | ||
const { ...rest } = props; | ||
const authorDetails = useAuthor(); | ||
|
||
const avatarUrls = authorDetails?.avatar_urls ? Object.values(authorDetails.avatar_urls) : null; | ||
const defaultAvatar = useDefaultAvatar(); | ||
|
||
const avatarSourceUrl = avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar; | ||
|
||
return <img src={avatarSourceUrl} {...rest} />; | ||
}; | ||
|
||
interface BioProps { | ||
tagName?: keyof JSX.IntrinsicElements; | ||
[key: string]: any; | ||
} | ||
|
||
export const Bio: React.FC<BioProps> = (props) => { | ||
const { tagName: TagName = 'p', ...rest } = props; | ||
const { description } = useAuthor(); | ||
|
||
return <TagName {...rest}>{description}</TagName>; | ||
}; | ||
|
||
interface EmailProps { | ||
[key: string]: any; | ||
} | ||
|
||
export const Email: React.FC<EmailProps> = (props) => { | ||
const { ...rest } = props; | ||
const { email } = useAuthor(); | ||
|
||
return ( | ||
<a href={`mailto:${email}`} {...rest}> | ||
{email} | ||
</a> | ||
); | ||
}; |
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