@@ -11,12 +11,41 @@ export interface AppErrorBoundaryProps extends PropsWithChildren {
|
||||
errorType: ErrorComponentType
|
||||
}
|
||||
|
||||
export const AppErrorBoundary: FC<AppErrorBoundaryProps> = ({
|
||||
export const AppErrorBoundary: FC<
|
||||
Omit<AppErrorBoundaryProps, "errorType"> & {
|
||||
errorType: ErrorComponentType[] | ErrorComponentType
|
||||
}
|
||||
> = ({ errorType, children }) => {
|
||||
if (Array.isArray(errorType)) {
|
||||
return (
|
||||
<>
|
||||
{errorType.reduceRight(
|
||||
(acc, type) => (
|
||||
<AppErrorBoundaryItem key={type} errorType={type}>
|
||||
{acc}
|
||||
</AppErrorBoundaryItem>
|
||||
),
|
||||
children,
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<AppErrorBoundaryItem errorType={errorType}>
|
||||
{children}
|
||||
</AppErrorBoundaryItem>
|
||||
)
|
||||
}
|
||||
|
||||
type ErrorFallbackProps = Parameters<FallbackRender>["0"]
|
||||
export type AppErrorFallbackProps = ErrorFallbackProps & {}
|
||||
const AppErrorBoundaryItem: FC<AppErrorBoundaryProps> = ({
|
||||
errorType,
|
||||
children,
|
||||
}) => {
|
||||
const fallbackRender: FallbackRender = useCallback(
|
||||
(fallbackProps) =>
|
||||
const fallbackRender = useCallback(
|
||||
(fallbackProps: ErrorFallbackProps) =>
|
||||
createElement(getErrorFallback(errorType), fallbackProps),
|
||||
[errorType],
|
||||
)
|
||||
|
||||
@@ -38,7 +38,7 @@ export const NotFound = () => {
|
||||
}, [location])
|
||||
const navigate = useNavigate()
|
||||
return (
|
||||
<div className="prose center m-auto size-full flex-col">
|
||||
<div className="prose center m-auto size-full flex-col dark:prose-invert">
|
||||
<main className="flex grow flex-col items-center justify-center">
|
||||
<div className="center mb-8 flex">
|
||||
<Logo className="size-20" />
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { getSidebarActiveView } from "@renderer/atoms/sidebar"
|
||||
import { getRouteParams } from "@renderer/hooks/biz/useRouteParams"
|
||||
import { nextFrame } from "@renderer/lib/dom"
|
||||
import type { FeedModel } from "@renderer/models"
|
||||
import { FeedForm } from "@renderer/modules/discover/feed-form"
|
||||
import { entries } from "@renderer/queries/entries"
|
||||
import type { FC } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
|
||||
import type { AppErrorFallbackProps } from "../common/AppErrorBoundary"
|
||||
import { FeedIcon } from "../feed-icon"
|
||||
import { StyledButton } from "../ui/button"
|
||||
import { useModalStack } from "../ui/modal"
|
||||
import { CustomSafeError, useResetErrorWhenRouteChange } from "./helper"
|
||||
|
||||
export const FeedFoundCanBeFollowErrorFallback: FC<AppErrorFallbackProps> = ({
|
||||
resetError,
|
||||
error,
|
||||
}) => {
|
||||
if (!(error instanceof FeedFoundCanBeFollowError)) {
|
||||
throw error
|
||||
}
|
||||
const feed = error.detail
|
||||
const { present } = useModalStack()
|
||||
const navigate = useNavigate()
|
||||
useResetErrorWhenRouteChange(resetError)
|
||||
return (
|
||||
<div className="flex w-full flex-col items-center justify-center rounded-md bg-theme-modal-background-opaque p-2">
|
||||
<div className="center m-auto flex max-w-prose flex-col gap-4 text-center">
|
||||
<FeedIcon feed={feed} size={60} />
|
||||
|
||||
<div className="text-lg font-bold">{feed.title}</div>
|
||||
|
||||
<p className="my-3">{feed.description}</p>
|
||||
|
||||
<div className="center gap-1 opacity-80">
|
||||
<i className="i-mgc-information-cute-re" />
|
||||
We found this feed in our database, you can follow it or go back to
|
||||
homepage.
|
||||
</div>
|
||||
<div className="center mt-12 gap-4">
|
||||
<StyledButton
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
navigate("/")
|
||||
setTimeout(() => {
|
||||
resetError()
|
||||
}, 100)
|
||||
}}
|
||||
>
|
||||
Back
|
||||
</StyledButton>
|
||||
|
||||
<StyledButton
|
||||
onClick={() => {
|
||||
present({
|
||||
title: "Add Feed",
|
||||
content: ({ dismiss }) => (
|
||||
<FeedForm
|
||||
asWidget
|
||||
url={feed.url}
|
||||
defaultValues={{
|
||||
view: getSidebarActiveView().toString(),
|
||||
}}
|
||||
onSuccess={() => {
|
||||
dismiss()
|
||||
|
||||
const { feedId, view } = getRouteParams()
|
||||
|
||||
const entriesOptions = {
|
||||
id: feedId,
|
||||
view,
|
||||
}
|
||||
|
||||
entries.entries(entriesOptions).remove()
|
||||
nextFrame(() => resetError())
|
||||
}}
|
||||
/>
|
||||
),
|
||||
})
|
||||
}}
|
||||
variant="primary"
|
||||
>
|
||||
Follow
|
||||
</StyledButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export class FeedFoundCanBeFollowError extends CustomSafeError {
|
||||
constructor(public detail: FeedModel) {
|
||||
super("Feed Found Can Be Follow")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { FC } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
|
||||
import type { AppErrorFallbackProps } from "../common/AppErrorBoundary"
|
||||
import { Logo } from "../icons/logo"
|
||||
import { StyledButton } from "../ui/button"
|
||||
import { CustomSafeError, useResetErrorWhenRouteChange } from "./helper"
|
||||
|
||||
export const FeedNotFoundErrorFallback: FC<AppErrorFallbackProps> = ({
|
||||
resetError,
|
||||
error,
|
||||
}) => {
|
||||
if (!(error instanceof FeedNotFound)) {
|
||||
throw error
|
||||
}
|
||||
|
||||
useResetErrorWhenRouteChange(resetError)
|
||||
const navigate = useNavigate()
|
||||
return (
|
||||
<div className="flex w-full flex-col items-center justify-center rounded-md bg-theme-modal-background-opaque p-2">
|
||||
<div className="center m-auto flex max-w-prose flex-col gap-4 text-center">
|
||||
<div className="center mb-8 flex">
|
||||
<Logo className="size-20" />
|
||||
</div>
|
||||
<p className="font-semibold">
|
||||
There is no feed with the given ID. Please check the URL and retry.
|
||||
</p>
|
||||
|
||||
<div className="center mt-12 gap-4">
|
||||
<StyledButton
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
navigate("/")
|
||||
setTimeout(() => {
|
||||
resetError()
|
||||
}, 100)
|
||||
}}
|
||||
>
|
||||
Back
|
||||
</StyledButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export class FeedNotFound extends CustomSafeError {
|
||||
constructor() {
|
||||
super("Feed 404")
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
import { attachOpenInEditor } from "@renderer/lib/dev"
|
||||
import type { FallbackRender } from "@sentry/react"
|
||||
import type { FC } from "react"
|
||||
|
||||
import type { AppErrorFallbackProps } from "../common/AppErrorBoundary"
|
||||
import { FallbackIssue } from "../common/ErrorElement"
|
||||
import { m } from "../common/Motion"
|
||||
import { StyledButton } from "../ui/button"
|
||||
import { useCurrentModal } from "../ui/modal"
|
||||
import { parseError } from "./helper"
|
||||
|
||||
export const ModalErrorFallback: FC<Parameters<FallbackRender>[0]> = (
|
||||
props,
|
||||
) => {
|
||||
export const ModalErrorFallback: FC<AppErrorFallbackProps> = (props) => {
|
||||
const { message, stack } = parseError(props.error)
|
||||
const modal = useCurrentModal()
|
||||
return (
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { attachOpenInEditor } from "@renderer/lib/dev"
|
||||
import type { FallbackRender } from "@sentry/react"
|
||||
import type { FC } from "react"
|
||||
|
||||
import type { AppErrorFallbackProps } from "../common/AppErrorBoundary"
|
||||
import { FallbackIssue } from "../common/ErrorElement"
|
||||
import { StyledButton } from "../ui/button"
|
||||
import { parseError } from "./helper"
|
||||
|
||||
export const PageErrorFallback: FC<Parameters<FallbackRender>[0]> = (props) => {
|
||||
export const PageErrorFallback: FC<AppErrorFallbackProps> = (props) => {
|
||||
const { message, stack } = parseError(props.error)
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
export const parseError = (error: unknown): { message?: string, stack?: string } => {
|
||||
import { useEffect, useRef } from "react"
|
||||
import { useLocation } from "react-router-dom"
|
||||
|
||||
export const parseError = (
|
||||
error: unknown,
|
||||
): { message?: string, stack?: string } => {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
message: error.message,
|
||||
@@ -11,3 +16,22 @@ export const parseError = (error: unknown): { message?: string, stack?: string }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomSafeError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
export const useResetErrorWhenRouteChange = (resetError: () => void) => {
|
||||
const location = useLocation()
|
||||
const currentPathnameRef = useRef(location.pathname)
|
||||
const onceRef = useRef(false)
|
||||
useEffect(() => {
|
||||
if (onceRef.current) { return }
|
||||
if (currentPathnameRef.current !== location.pathname) {
|
||||
resetError()
|
||||
onceRef.current = true
|
||||
}
|
||||
}, [location.pathname])
|
||||
}
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
import { FeedFoundCanBeFollowErrorFallback } from "./FeedFoundCanBeFollowErrorFallback"
|
||||
import { FeedNotFoundErrorFallback } from "./FeedNotFound"
|
||||
import { ModalErrorFallback } from "./ModalError"
|
||||
import { PageErrorFallback } from "./PageError"
|
||||
|
||||
export enum ErrorComponentType {
|
||||
Modal = "Modal",
|
||||
Page = "Page",
|
||||
|
||||
// Feed
|
||||
FeedFoundCanBeFollow = "FeedFoundCanBeFollow",
|
||||
FeedNotFound = "FeedNotFound",
|
||||
}
|
||||
|
||||
export const ErrorFallbackMap = {
|
||||
[ErrorComponentType.Modal]: ModalErrorFallback,
|
||||
[ErrorComponentType.Page]: PageErrorFallback,
|
||||
[ErrorComponentType.FeedFoundCanBeFollow]: FeedFoundCanBeFollowErrorFallback,
|
||||
[ErrorComponentType.FeedNotFound]: FeedNotFoundErrorFallback,
|
||||
}
|
||||
|
||||
export const getErrorFallback = (type: ErrorComponentType) =>
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useDark } from "@renderer/hooks/common"
|
||||
import { Toaster as Sonner } from "sonner"
|
||||
|
||||
type ToasterProps = React.ComponentProps<typeof Sonner>
|
||||
|
||||
const Toaster = ({ ...props }: ToasterProps) => (
|
||||
<Sonner
|
||||
theme="system"
|
||||
theme={useDark() ? "dark" : "light"}
|
||||
className="toaster group"
|
||||
toastOptions={{
|
||||
classNames: {
|
||||
|
||||
@@ -2,6 +2,8 @@ import { env } from "@env"
|
||||
import type { BrowserOptions } from "@sentry/react"
|
||||
import { FetchError } from "ofetch"
|
||||
|
||||
import { CustomSafeError } from "./components/errors/helper"
|
||||
|
||||
export const SentryConfig: BrowserOptions = {
|
||||
// Performance Monitoring
|
||||
tracesSampleRate: 1, // Capture 100% of the transactions
|
||||
@@ -24,6 +26,10 @@ export const SentryConfig: BrowserOptions = {
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (error instanceof CustomSafeError) {
|
||||
return null
|
||||
}
|
||||
if (error instanceof FetchError) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
} from "@renderer/atoms/settings/general"
|
||||
import { useWhoami } from "@renderer/atoms/user"
|
||||
import { m } from "@renderer/components/common/Motion"
|
||||
import { FeedFoundCanBeFollowError } from "@renderer/components/errors/FeedFoundCanBeFollowErrorFallback"
|
||||
import { FeedNotFound } from "@renderer/components/errors/FeedNotFound"
|
||||
import { AutoResizeHeight } from "@renderer/components/ui/auto-resize-height"
|
||||
import { ActionButton } from "@renderer/components/ui/button"
|
||||
import { DividerVertical } from "@renderer/components/ui/divider"
|
||||
@@ -17,11 +19,14 @@ import {
|
||||
} from "@renderer/constants"
|
||||
import { shortcuts } from "@renderer/constants/shortcuts"
|
||||
import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry"
|
||||
import { useRouteParms } from "@renderer/hooks/biz/useRouteParams"
|
||||
import {
|
||||
useRouteParamsSelector,
|
||||
useRouteParms,
|
||||
} from "@renderer/hooks/biz/useRouteParams"
|
||||
import { useIsOnline } from "@renderer/hooks/common/useIsOnline"
|
||||
import { cn, getOS, isBizId } from "@renderer/lib/utils"
|
||||
import { EntryHeader } from "@renderer/modules/entry-content/header"
|
||||
import { useRefreshFeedMutation } from "@renderer/queries/feed"
|
||||
import { useFeed, useRefreshFeedMutation } from "@renderer/queries/feed"
|
||||
import { entryActions, useEntry } from "@renderer/store/entry"
|
||||
import { useFeedById, useFeedHeaderTitle } from "@renderer/store/feed"
|
||||
import type { FC } from "react"
|
||||
@@ -127,6 +132,7 @@ export function EntryColumn() {
|
||||
|
||||
const navigate = useNavigateEntry()
|
||||
const isRefreshing = entries.isFetching && !entries.isFetchingNextPage
|
||||
|
||||
return (
|
||||
<div
|
||||
className="relative flex h-full flex-1 flex-col @container"
|
||||
@@ -136,6 +142,10 @@ export function EntryColumn() {
|
||||
})}
|
||||
data-total-count={virtuosoOptions.totalCount}
|
||||
>
|
||||
{virtuosoOptions.totalCount === 0 &&
|
||||
!entries.isLoading &&
|
||||
!entries.error && <AddFeedHelper />}
|
||||
|
||||
<ListHeader
|
||||
refetch={entries.refetch}
|
||||
isRefreshing={isRefreshing}
|
||||
@@ -195,6 +205,21 @@ export function EntryColumn() {
|
||||
)
|
||||
}
|
||||
|
||||
const AddFeedHelper = () => {
|
||||
const feedId = useRouteParamsSelector((s) => s.feedId)
|
||||
const feedQuery = useFeed({ id: feedId })
|
||||
|
||||
if (feedQuery.error && feedQuery.error.statusCode === 404) {
|
||||
throw new FeedNotFound()
|
||||
}
|
||||
|
||||
if (!feedQuery.data) {
|
||||
return null
|
||||
}
|
||||
|
||||
throw new FeedFoundCanBeFollowError(feedQuery.data.feed)
|
||||
}
|
||||
|
||||
const ListHeader: FC<{
|
||||
totalCount: number
|
||||
refetch: () => void
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ROUTE_ENTRY_PENDING } from "@renderer/constants"
|
||||
import { redirect } from "react-router-dom"
|
||||
|
||||
export const loader = ({
|
||||
request,
|
||||
}: {
|
||||
request: Request
|
||||
params: { feedId: string }
|
||||
}) => {
|
||||
const url = new URL(request.url)
|
||||
return redirect(`${url.pathname}/${ROUTE_ENTRY_PENDING}`)
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import { getUISettings, setUISetting } from "@renderer/atoms/settings/ui"
|
||||
import { PanelSplitter } from "@renderer/components/ui/divider"
|
||||
import { views } from "@renderer/constants"
|
||||
import { useRouteParms } from "@renderer/hooks/biz/useRouteParams"
|
||||
import {
|
||||
useRouteParms,
|
||||
} from "@renderer/hooks/biz/useRouteParams"
|
||||
import { cn, isSafari } from "@renderer/lib/utils"
|
||||
import { EntryColumn } from "@renderer/modules/entry-column"
|
||||
import { useMemo, useRef } from "react"
|
||||
|
||||
@@ -82,7 +82,13 @@ export function Component() {
|
||||
// NOTE: tabIndex for main element can get by `document.activeElement`
|
||||
tabIndex={-1}
|
||||
>
|
||||
<AppErrorBoundary errorType={ErrorComponentType.Page}>
|
||||
<AppErrorBoundary
|
||||
errorType={[
|
||||
ErrorComponentType.Page,
|
||||
ErrorComponentType.FeedFoundCanBeFollow,
|
||||
ErrorComponentType.FeedNotFound,
|
||||
]}
|
||||
>
|
||||
<Outlet />
|
||||
</AppErrorBoundary>
|
||||
</main>
|
||||
|
||||
Reference in New Issue
Block a user