diff --git a/src/renderer/src/components/common/AppErrorBoundary.tsx b/src/renderer/src/components/common/AppErrorBoundary.tsx index ec54d907a..44cedaf42 100644 --- a/src/renderer/src/components/common/AppErrorBoundary.tsx +++ b/src/renderer/src/components/common/AppErrorBoundary.tsx @@ -11,12 +11,41 @@ export interface AppErrorBoundaryProps extends PropsWithChildren { errorType: ErrorComponentType } -export const AppErrorBoundary: FC = ({ +export const AppErrorBoundary: FC< + Omit & { + errorType: ErrorComponentType[] | ErrorComponentType + } +> = ({ errorType, children }) => { + if (Array.isArray(errorType)) { + return ( + <> + {errorType.reduceRight( + (acc, type) => ( + + {acc} + + ), + children, + )} + + ) + } + + return ( + + {children} + + ) +} + +type ErrorFallbackProps = Parameters["0"] +export type AppErrorFallbackProps = ErrorFallbackProps & {} +const AppErrorBoundaryItem: FC = ({ errorType, children, }) => { - const fallbackRender: FallbackRender = useCallback( - (fallbackProps) => + const fallbackRender = useCallback( + (fallbackProps: ErrorFallbackProps) => createElement(getErrorFallback(errorType), fallbackProps), [errorType], ) diff --git a/src/renderer/src/components/common/NotFound.tsx b/src/renderer/src/components/common/NotFound.tsx index 29bbf8b82..873b5ea89 100644 --- a/src/renderer/src/components/common/NotFound.tsx +++ b/src/renderer/src/components/common/NotFound.tsx @@ -38,7 +38,7 @@ export const NotFound = () => { }, [location]) const navigate = useNavigate() return ( -
+
diff --git a/src/renderer/src/components/errors/FeedFoundCanBeFollowErrorFallback.tsx b/src/renderer/src/components/errors/FeedFoundCanBeFollowErrorFallback.tsx new file mode 100644 index 000000000..34fea6838 --- /dev/null +++ b/src/renderer/src/components/errors/FeedFoundCanBeFollowErrorFallback.tsx @@ -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 = ({ + resetError, + error, +}) => { + if (!(error instanceof FeedFoundCanBeFollowError)) { + throw error + } + const feed = error.detail + const { present } = useModalStack() + const navigate = useNavigate() + useResetErrorWhenRouteChange(resetError) + return ( +
+
+ + +
{feed.title}
+ +

{feed.description}

+ +
+ + We found this feed in our database, you can follow it or go back to + homepage. +
+
+ { + navigate("/") + setTimeout(() => { + resetError() + }, 100) + }} + > + Back + + + { + present({ + title: "Add Feed", + content: ({ dismiss }) => ( + { + dismiss() + + const { feedId, view } = getRouteParams() + + const entriesOptions = { + id: feedId, + view, + } + + entries.entries(entriesOptions).remove() + nextFrame(() => resetError()) + }} + /> + ), + }) + }} + variant="primary" + > + Follow + +
+
+
+ ) +} + +export class FeedFoundCanBeFollowError extends CustomSafeError { + constructor(public detail: FeedModel) { + super("Feed Found Can Be Follow") + } +} diff --git a/src/renderer/src/components/errors/FeedNotFound.tsx b/src/renderer/src/components/errors/FeedNotFound.tsx new file mode 100644 index 000000000..522cd7cf3 --- /dev/null +++ b/src/renderer/src/components/errors/FeedNotFound.tsx @@ -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 = ({ + resetError, + error, +}) => { + if (!(error instanceof FeedNotFound)) { + throw error + } + + useResetErrorWhenRouteChange(resetError) + const navigate = useNavigate() + return ( +
+
+
+ +
+

+ There is no feed with the given ID. Please check the URL and retry. +

+ +
+ { + navigate("/") + setTimeout(() => { + resetError() + }, 100) + }} + > + Back + +
+
+
+ ) +} + +export class FeedNotFound extends CustomSafeError { + constructor() { + super("Feed 404") + } +} diff --git a/src/renderer/src/components/errors/ModalError.tsx b/src/renderer/src/components/errors/ModalError.tsx index b67b37953..e92c2f523 100644 --- a/src/renderer/src/components/errors/ModalError.tsx +++ b/src/renderer/src/components/errors/ModalError.tsx @@ -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[0]> = ( - props, -) => { +export const ModalErrorFallback: FC = (props) => { const { message, stack } = parseError(props.error) const modal = useCurrentModal() return ( diff --git a/src/renderer/src/components/errors/PageError.tsx b/src/renderer/src/components/errors/PageError.tsx index 14d1cbc16..3d1b63c14 100644 --- a/src/renderer/src/components/errors/PageError.tsx +++ b/src/renderer/src/components/errors/PageError.tsx @@ -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[0]> = (props) => { +export const PageErrorFallback: FC = (props) => { const { message, stack } = parseError(props.error) return ( diff --git a/src/renderer/src/components/errors/helper.ts b/src/renderer/src/components/errors/helper.ts index 8bab2d33f..477c0f809 100644 --- a/src/renderer/src/components/errors/helper.ts +++ b/src/renderer/src/components/errors/helper.ts @@ -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]) +} diff --git a/src/renderer/src/components/errors/index.ts b/src/renderer/src/components/errors/index.ts index 3945d397b..9a6f828f4 100644 --- a/src/renderer/src/components/errors/index.ts +++ b/src/renderer/src/components/errors/index.ts @@ -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) => diff --git a/src/renderer/src/components/ui/sonner.tsx b/src/renderer/src/components/ui/sonner.tsx index e82438656..6aacd1ea3 100644 --- a/src/renderer/src/components/ui/sonner.tsx +++ b/src/renderer/src/components/ui/sonner.tsx @@ -1,10 +1,11 @@ +import { useDark } from "@renderer/hooks/common" import { Toaster as Sonner } from "sonner" type ToasterProps = React.ComponentProps const Toaster = ({ ...props }: ToasterProps) => ( + {virtuosoOptions.totalCount === 0 && + !entries.isLoading && + !entries.error && } + { + 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 diff --git a/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/index.tsx b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/index.tsx new file mode 100644 index 000000000..45c353c25 --- /dev/null +++ b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/index.tsx @@ -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}`) +} diff --git a/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx index c2daa6442..fe20facdb 100644 --- a/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx +++ b/src/renderer/src/pages/(main)/(layer)/feeds/[feedId]/layout.tsx @@ -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" diff --git a/src/renderer/src/pages/(main)/layout.tsx b/src/renderer/src/pages/(main)/layout.tsx index 0e8fcdf62..daa4ef434 100644 --- a/src/renderer/src/pages/(main)/layout.tsx +++ b/src/renderer/src/pages/(main)/layout.tsx @@ -82,7 +82,13 @@ export function Component() { // NOTE: tabIndex for main element can get by `document.activeElement` tabIndex={-1} > - +