From 633e0a69b2d1d7d6fc13d24e1fca715faec9e601 Mon Sep 17 00:00:00 2001 From: liangfung <1098486429@qq.com> Date: Thu, 19 Sep 2024 22:16:42 +0800 Subject: [PATCH] fix(ui): display an error view when redirected to a repo without access permissions --- .../files/components/source-code-browser.tsx | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/ee/tabby-ui/app/files/components/source-code-browser.tsx b/ee/tabby-ui/app/files/components/source-code-browser.tsx index 21562b20eb8..bfaaa89963e 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -1,6 +1,6 @@ 'use client' -import React, { PropsWithChildren } from 'react' +import React, { PropsWithChildren, useState } from 'react' import { usePathname } from 'next/navigation' import { createRequest } from '@urql/core' import { compact, isEmpty, isNil, toNumber } from 'lodash-es' @@ -136,6 +136,8 @@ type SourceCodeBrowserContextValue = { isPathInitialized: boolean activeEntryInfo: ReturnType prevActivePath: React.MutableRefObject + error: Error | undefined + setError: (e: Error | undefined) => void } const SourceCodeBrowserContext = @@ -147,7 +149,8 @@ const SourceCodeBrowserContextProvider: React.FC = ({ children }) => { const pathname = usePathname() - const { updateUrlComponents } = useRouterStuff() + const { updateUrlComponents, searchParams } = useRouterStuff() + const redirectGitUrl = searchParams.get('redirect_git_url')?.toString() const [isPathInitialized, setIsPathInitialized] = React.useState(false) const [activePath, setActivePath] = React.useState() const activeEntryInfo = React.useMemo(() => { @@ -165,6 +168,7 @@ const SourceCodeBrowserContextProvider: React.FC = ({ const [pendingEvent, setPendingEvent] = React.useState< QuickActionEventPayload | undefined >() + const [error, setError] = useState() const prevActivePath = React.useRef() const updateActivePath: SourceCodeBrowserContextValue['updateActivePath'] = @@ -289,7 +293,11 @@ const SourceCodeBrowserContextProvider: React.FC = ({ if (!isPathInitialized) { setIsPathInitialized(true) } - }, [pathname]) + + if (error) { + setError(undefined) + } + }, [pathname, redirectGitUrl]) return ( = ({ activeRepoRef, isPathInitialized, activeEntryInfo, - prevActivePath + prevActivePath, + error, + setError }} > {children} @@ -348,7 +358,9 @@ const SourceCodeBrowserRenderer: React.FC = ({ activeEntryInfo, prevActivePath, updateFileMap, - setExpandedKeys + setExpandedKeys, + error, + setError } = React.useContext(SourceCodeBrowserContext) const { searchParams } = useRouterStuff() @@ -461,9 +473,9 @@ const SourceCodeBrowserRenderer: React.FC = ({ const fileBlob = rawFileResponse?.blob const contentLength = rawFileResponse?.contentLength const fileDisplayType = rawFileResponse?.fileDisplayType - const error = rawFileError || entriesError + const viewAffectingError = error || rawFileError || entriesError - const showErrorView = !!error + const showErrorView = !!viewAffectingError const isTreeMode = activeEntryInfo?.viewMode === 'tree' || !activeEntryInfo?.viewMode @@ -517,6 +529,9 @@ const SourceCodeBrowserRenderer: React.FC = ({ ) initializing.current = false return + } else { + // target repository not found + setError(new Error(CodeBrowserError.REPOSITORY_NOT_FOUND)) } } @@ -655,7 +670,7 @@ const SourceCodeBrowserRenderer: React.FC = ({ ) : showErrorView ? ( ) : ( <>