Skip to content

Commit

Permalink
refactor(webserver): remove /resolve/meta api endpoint (#1816)
Browse files Browse the repository at this point in the history
* refactor(webserver): remove /resolve/meta api endpoint

* feat(ui): detect language by filename

* update

* move filename2prism to @/lib

* [autofix.ci] apply automated fixes

---------

Co-authored-by: liangfung <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent 2aa4520 commit f852da9
Show file tree
Hide file tree
Showing 16 changed files with 951 additions and 303 deletions.
12 changes: 7 additions & 5 deletions ee/tabby-db/src/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ impl DbConn {
}

pub async fn get_repository_by_name(&self, name: &str) -> Result<RepositoryDAO> {
let repository =
sqlx::query_as("SELECT id, name, git_url FROM repositories WHERE name = ?;")
.bind(name)
.fetch_one(&self.pool)
.await?;
let repository = sqlx::query_as!(
RepositoryDAO,
"SELECT id as 'id!: i64', name, git_url FROM repositories WHERE name = ?",
name
)
.fetch_one(&self.pool)
.await?;
Ok(repository)
}
}
Expand Down
11 changes: 3 additions & 8 deletions ee/tabby-ui/app/files/components/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTheme } from 'next-themes'

import { EXP_enable_code_browser_quick_action_bar } from '@/lib/experiment-flags'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { TFileMeta } from '@/lib/types'
import { TCodeTag } from '@/lib/types'
import CodeEditor, {
CodeMirrorEditorRef
} from '@/components/codemirror/codemirror'
Expand All @@ -17,17 +17,12 @@ import { ActionBarWidgetExtension } from './action-bar-widget/action-bar-widget-

interface CodeEditorViewProps {
value: string
meta: TFileMeta | undefined
language: string
}

const CodeEditorView: React.FC<CodeEditorViewProps> = ({
value,
meta,
language
}) => {
const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
const { theme } = useTheme()
const tags = meta?.tags
const tags: TCodeTag[] = []
const editorRef = React.useRef<CodeMirrorEditorRef>(null)
const isChatEnabled = useIsChatEnabled()

Expand Down
18 changes: 3 additions & 15 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use client'

import React, { PropsWithChildren } from 'react'
import filename2prism from 'filename2prism'
import { compact, findIndex, toNumber } from 'lodash-es'
import { ImperativePanelHandle } from 'react-resizable-panels'
import { toast } from 'sonner'
import { SWRResponse } from 'swr'
import useSWRImmutable from 'swr/immutable'

import filename2prism from '@/lib/filename2prism'
import useRouterStuff from '@/lib/hooks/use-router-stuff'
import fetcher from '@/lib/tabby/fetcher'
import type { ResolveEntriesResponse, TFile } from '@/lib/types'
Expand Down Expand Up @@ -254,14 +254,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
const fileBlob = rawFileResponse?.blob
const contentLength = rawFileResponse?.contentLength

// fetch active file meta
const { data: fileMeta } = useSWRImmutable(
isFileSelected
? `/repositories/${activeRepoName}/meta/${activeBasename}`
: null,
fetcher
)

// fetch active dir
const {
data: subTree,
Expand Down Expand Up @@ -398,11 +390,7 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
/>
)}
{showTextFileView && (
<TextFileView
blob={fileBlob}
meta={fileMeta}
contentLength={contentLength}
/>
<TextFileView blob={fileBlob} contentLength={contentLength} />
)}
{showRawFileView && (
<RawFileView
Expand Down Expand Up @@ -560,4 +548,4 @@ async function getFileViewType(

export type { TFileMap, TFileMapItem }

export { SourceCodeBrowserContext, SourceCodeBrowser, getFileViewType }
export { SourceCodeBrowserContext, SourceCodeBrowser }
9 changes: 3 additions & 6 deletions ee/tabby-ui/app/files/components/text-file-view.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { Suspense, useContext } from 'react'
import filename2prism from 'filename2prism'

import filename2prism from '@/lib/filename2prism'
import useRouterStuff from '@/lib/hooks/use-router-stuff'
import { TFileMeta } from '@/lib/types'
import { cn } from '@/lib/utils'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { ListSkeleton } from '@/components/skeleton'
Expand All @@ -15,14 +14,12 @@ const MarkdownView = React.lazy(() => import('./markdown-view'))

interface TextFileViewProps extends React.HTMLProps<HTMLDivElement> {
blob: Blob | undefined
meta: TFileMeta | undefined
contentLength?: number
}

export const TextFileView: React.FC<TextFileViewProps> = ({
className,
blob,
meta,
contentLength
}) => {
const { searchParams, updateSearchParams } = useRouterStuff()
Expand All @@ -32,7 +29,7 @@ export const TextFileView: React.FC<TextFileViewProps> = ({
const detectedLanguage = activePath
? filename2prism(activePath)[0]
: undefined
const language = (meta?.language ?? detectedLanguage) || ''
const language = detectedLanguage ?? 'plain'
const isMarkdown = !!value && language === 'markdown'
const isPlain = searchParams.get('plain')?.toString() === '1'
const showMarkdown = isMarkdown && !isPlain
Expand Down Expand Up @@ -86,7 +83,7 @@ export const TextFileView: React.FC<TextFileViewProps> = ({
{showMarkdown ? (
<MarkdownView value={value} />
) : (
<CodeEditorView value={value} language={language} meta={meta} />
<CodeEditorView value={value} language={language} />
)}
</Suspense>
</div>
Expand Down
40 changes: 40 additions & 0 deletions ee/tabby-ui/lib/filename2prism/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Fork from
// https://github.com/TomerAberbach/filename2prism/

import path from 'path'
import { has } from 'lodash-es'

import languages from './languages'

const filenames: Record<string, string[]> = {}
const extnames: Record<string, string[]> = {}

for (const [alias, associations] of Object.entries(languages)) {
for (const filename of associations.filenames) {
if (!has(filenames, filename)) {
filenames[filename] = []
}

filenames[filename].push(alias)
}

for (const extname of associations.extnames) {
if (!has(extnames, extname)) {
extnames[extname] = []
}

extnames[extname].push(alias)
}
}

const filename2prism: (filename: string) => Array<string> = filename => {
const result: string[] = []
return result
.concat(
filenames[path.basename(filename)],
extnames[path.extname(filename).substring(1)]
)
.filter(Boolean)
}

export default filename2prism
Loading

0 comments on commit f852da9

Please sign in to comment.