-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
238 additions
and
115 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import isNetworkError from 'is-network-error'; | ||
|
||
import { YacdBackendUnauthorizedError, YacdFetchNetworkError } from '$src/misc/errors'; | ||
import { FetchCtx } from '$src/types'; | ||
|
||
export function req(url: string, init: RequestInit) { | ||
if (import.meta.env.DEV) { | ||
return import('./mock').then((mod) => mod.mock(url, init)); | ||
} | ||
return fetch(url, init); | ||
} | ||
|
||
export function handleFetchError(err: unknown, ctx: FetchCtx) { | ||
if (isNetworkError(err)) throw new YacdFetchNetworkError('', ctx); | ||
throw err; | ||
} | ||
|
||
export function validateFetchResponse(res: Response, ctx: FetchCtx) { | ||
if (res.status === 401) throw new YacdBackendUnauthorizedError('', ctx); | ||
return res; | ||
} |
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 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
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 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
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,59 @@ | ||
import React, { useCallback } from 'react'; | ||
import type { FallbackProps } from 'react-error-boundary'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { FetchCtx } from '$src/types'; | ||
|
||
import Button from '../Button'; | ||
import { Sep } from '../shared/Basic'; | ||
import { ErrorFallbackLayout } from './ErrorFallbackLayout'; | ||
|
||
function useStuff(resetErrorBoundary: FallbackProps['resetErrorBoundary']) { | ||
const { t } = useTranslation(); | ||
const navigate = useNavigate(); | ||
const onClick = useCallback( | ||
(e: React.MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
resetErrorBoundary(); | ||
navigate('/backend'); | ||
}, | ||
[navigate, resetErrorBoundary], | ||
); | ||
return { t, onClick }; | ||
} | ||
|
||
export function FetchNetworkErrorFallback(props: { | ||
ctx: FetchCtx; | ||
resetErrorBoundary: FallbackProps['resetErrorBoundary']; | ||
}) { | ||
const { resetErrorBoundary, ctx } = props; | ||
const { t, onClick } = useStuff(resetErrorBoundary); | ||
return ( | ||
<ErrorFallbackLayout> | ||
<p>Failed to connect to the backend {ctx.apiConfig.baseURL}</p> | ||
<Sep /> | ||
<Button onClick={onClick}>{t('switch_backend')}</Button> | ||
</ErrorFallbackLayout> | ||
); | ||
} | ||
|
||
export function BackendUnauthorizedErrorFallback(props: { | ||
ctx: FetchCtx; | ||
resetErrorBoundary: FallbackProps['resetErrorBoundary']; | ||
}) { | ||
const { resetErrorBoundary, ctx } = props; | ||
const { t, onClick } = useStuff(resetErrorBoundary); | ||
return ( | ||
<ErrorFallbackLayout> | ||
<p>Unauthorized to connect to the backend {ctx.apiConfig.baseURL}</p> | ||
{ | ||
ctx.apiConfig.secret ? | ||
<p>You might using a wrong secret</p> | ||
: <p>You probably need to provide a secret</p> | ||
} | ||
<Sep /> | ||
<Button onClick={onClick}>{t('switch_backend')}</Button> | ||
</ErrorFallbackLayout> | ||
); | ||
} |
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,14 @@ | ||
.link { | ||
display: inline-flex; | ||
align-items: center; | ||
|
||
color: var(--color-text-secondary); | ||
&:hover, | ||
&:active { | ||
color: #387cec; | ||
} | ||
|
||
svg { | ||
margin-right: 5px; | ||
} | ||
} |
Oops, something went wrong.