chore: status vs state

This commit is contained in:
EnixCoda 2021-04-18 13:26:55 +08:00
parent e9f7ee2f91
commit 69614104ec
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 19 additions and 19 deletions

View file

@ -10,13 +10,13 @@ const className = 'clippy-wrapper'
export const ClippyClassName = className
export function Clippy({ codeSnippetElement }: Props) {
const [status, setStatus] = React.useState<'normal' | 'success' | 'fail'>('normal')
const [state, setState] = React.useState<'normal' | 'success' | 'fail'>('normal')
React.useEffect(() => {
const timer = window.setTimeout(() => {
setStatus('normal')
setState('normal')
}, 1000)
return () => window.clearTimeout(timer)
}, [status])
}, [state])
// Temporary fix:
// React moved root node of event delegation since v17
@ -26,7 +26,7 @@ export function Clippy({ codeSnippetElement }: Props) {
const element = elementRef.current
if (element) {
function onClippyClick() {
setStatus(copyElementContent(codeSnippetElement) ? 'success' : 'fail')
setState(copyElementContent(codeSnippetElement) ? 'success' : 'fail')
}
element.addEventListener('click', onClippyClick)
return () => element.removeEventListener('click', onClippyClick)
@ -36,7 +36,7 @@ export function Clippy({ codeSnippetElement }: Props) {
return (
<div className={className}>
<button className="clippy" ref={elementRef}>
<i className={cx('icon', status)} />
<i className={cx('icon', state)} />
</button>
</div>
)

View file

@ -25,7 +25,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
const {
metaData,
treeData,
status,
state,
defer,
error,
shouldShow,
@ -59,7 +59,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
React.useEffect(
function attachKeyDown() {
if (status === 'disabled' || !configContext.value.shortcut) return
if (state === 'disabled' || !configContext.value.shortcut) return
function onKeyDown(e: KeyboardEvent) {
const keys = keyHelper.parseEvent(e)
@ -70,13 +70,13 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
window.addEventListener('keydown', onKeyDown)
return () => window.removeEventListener('keydown', onKeyDown)
},
[toggleShowSideBar, status === 'disabled', configContext.value.shortcut],
[toggleShowSideBar, state === 'disabled', configContext.value.shortcut],
)
const intelligentToggle = configContext.value.intelligentToggle
// Hide sidebar when error due to auth but token is set #128
const hideSidebarOnInvalidToken: boolean =
intelligentToggle === null && Boolean(status === 'error-due-to-auth' && accessToken)
intelligentToggle === null && Boolean(state === 'error-due-to-auth' && accessToken)
React.useEffect(() => {
if (hideSidebarOnInvalidToken) {
props.setShouldShow(false)
@ -121,7 +121,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
</div>
<div className={'gitako-side-bar-content'}>
{(() => {
switch (status) {
switch (state) {
case 'loading-meta':
return <LoadingIndicator text={'Fetching repo meta...'} />
case 'loading-tree':
@ -164,7 +164,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
RawGitako.defaultProps = {
shouldShow: false,
showSettings: false,
status: 'loading-meta',
state: 'loading-meta',
}
export const SideBar = connect(SideBarCore)(RawGitako)

View file

@ -19,7 +19,7 @@ export type ConnectorState = {
metaData?: MetaData
// file tree data
treeData?: TreeNode
status: 'loading-meta' | 'loading-tree' | 'idle' | 'error-due-to-auth' | 'disabled'
state: 'loading-meta' | 'loading-tree' | 'idle' | 'error-due-to-auth' | 'disabled'
logoContainerElement: Element | null
defer?: boolean
} & {
@ -37,10 +37,10 @@ export const init: BoundMethodCreator = dispatch => async () => {
const leave = await promiseQueue.enter()
try {
dispatch.set({ status: 'loading-meta' })
dispatch.set({ state: 'loading-meta' })
const metaData = platform.resolveMeta()
if (!metaData) {
dispatch.set({ status: 'disabled' })
dispatch.set({ state: 'disabled' })
return
}
const { userName, repoName, branchName } = metaData
@ -113,9 +113,9 @@ export const init: BoundMethodCreator = dispatch => async () => {
}
}
dispatch.set({ status: 'loading-tree' })
dispatch.set({ state: 'loading-tree' })
const { root: treeData, defer } = await getTreeData
dispatch.set({ status: 'idle', treeData, defer })
dispatch.set({ state: 'idle', treeData, defer })
} catch (err) {
dispatch.call(handleError, err)
}
@ -133,13 +133,13 @@ export const handleError: BoundMethodCreator<[Error]> = dispatch => async err =>
err.message === errors.BAD_CREDENTIALS ||
err.message === errors.API_RATE_LIMIT
) {
dispatch.set({ status: 'error-due-to-auth' })
dispatch.set({ state: 'error-due-to-auth' })
} else if (err.message === errors.CONNECTION_BLOCKED) {
const { props } = dispatch.get()
if (props.configContext.value.accessToken) {
dispatch.call(setError, `Cannot connect to ${platformName}.`)
} else {
dispatch.set({ status: 'error-due-to-auth' })
dispatch.set({ state: 'error-due-to-auth' })
}
} else if (err.message === errors.SERVER_FAULT) {
dispatch.call(setError, `${platformName} server went down.`)

View file

@ -8,7 +8,7 @@ export type Config = {
compressSingletonFolder: boolean
copyFileButton: boolean
copySnippetButton: boolean
intelligentToggle: boolean | null // `null` stands for intelligent, boolean for sidebar open status
intelligentToggle: boolean | null // `null` stands for intelligent, boolean for sidebar open state
icons: 'rich' | 'dim' | 'native'
toggleButtonVerticalDistance: number
toggleButtonContent: 'logo' | 'octoface'