Skip to content

Commit

Permalink
fix(ui): display an error view when redirected to a repo without acce…
Browse files Browse the repository at this point in the history
…ss permissions (#3168)
  • Loading branch information
liangfung authored Sep 19, 2024
1 parent 58befa5 commit f6df3fd
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -136,6 +136,8 @@ type SourceCodeBrowserContextValue = {
isPathInitialized: boolean
activeEntryInfo: ReturnType<typeof resolveRepositoryInfoFromPath>
prevActivePath: React.MutableRefObject<string | undefined>
error: Error | undefined
setError: (e: Error | undefined) => void
}

const SourceCodeBrowserContext =
Expand All @@ -147,7 +149,8 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
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<string | undefined>()
const activeEntryInfo = React.useMemo(() => {
Expand All @@ -165,6 +168,7 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
const [pendingEvent, setPendingEvent] = React.useState<
QuickActionEventPayload | undefined
>()
const [error, setError] = useState<Error | undefined>()
const prevActivePath = React.useRef<string | undefined>()

const updateActivePath: SourceCodeBrowserContextValue['updateActivePath'] =
Expand Down Expand Up @@ -289,7 +293,11 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
if (!isPathInitialized) {
setIsPathInitialized(true)
}
}, [pathname])

if (error) {
setError(undefined)
}
}, [pathname, redirectGitUrl])

return (
<SourceCodeBrowserContext.Provider
Expand All @@ -316,7 +324,9 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
activeRepoRef,
isPathInitialized,
activeEntryInfo,
prevActivePath
prevActivePath,
error,
setError
}}
>
{children}
Expand Down Expand Up @@ -348,7 +358,9 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
activeEntryInfo,
prevActivePath,
updateFileMap,
setExpandedKeys
setExpandedKeys,
error,
setError
} = React.useContext(SourceCodeBrowserContext)

const { searchParams } = useRouterStuff()
Expand Down Expand Up @@ -461,9 +473,9 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
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
Expand Down Expand Up @@ -517,6 +529,9 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
)
initializing.current = false
return
} else {
// target repository not found
setError(new Error(CodeBrowserError.REPOSITORY_NOT_FOUND))
}
}

Expand Down Expand Up @@ -655,7 +670,7 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
) : showErrorView ? (
<ErrorView
className={`rounded-lg border p-4`}
error={entriesError || rawFileError}
error={viewAffectingError}
/>
) : (
<>
Expand Down

0 comments on commit f6df3fd

Please sign in to comment.