Skip to content

Commit ac758b4

Browse files
migrate file tree paths to rest apis
1 parent 3096efa commit ac758b4

File tree

14 files changed

+147
-41
lines changed

14 files changed

+147
-41
lines changed

packages/web/src/app/[domain]/browse/[...path]/components/pureTreePreviewPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use client';
22

33
import { useRef } from "react";
4-
import { FileTreeItem } from "@/features/fileTree/actions";
54
import { FileTreeItemComponent } from "@/features/fileTree/components/fileTreeItemComponent";
65
import { getBrowsePath } from "../../hooks/utils";
76
import { ScrollArea } from "@/components/ui/scroll-area";
87
import { useBrowseParams } from "../../hooks/useBrowseParams";
98
import { useDomain } from "@/hooks/useDomain";
9+
import { FileTreeItem } from "@/features/fileTree/types";
1010

1111
interface PureTreePreviewPanelProps {
1212
items: FileTreeItem[];

packages/web/src/app/[domain]/browse/[...path]/components/treePreviewPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Separator } from "@/components/ui/separator";
33
import { getRepoInfoByName } from "@/actions";
44
import { PathHeader } from "@/app/[domain]/components/pathHeader";
5-
import { getFolderContents } from "@/features/fileTree/actions";
5+
import { getFolderContents } from "@/features/fileTree/api";
66
import { isServiceError } from "@/lib/utils";
77
import { PureTreePreviewPanel } from "./pureTreePreviewPanel";
88

packages/web/src/app/[domain]/browse/components/fileSearchCommandDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { useState, useRef, useMemo, useEffect, useCallback } from "react";
55
import { useHotkeys } from "react-hotkeys-hook";
66
import { useQuery } from "@tanstack/react-query";
77
import { unwrapServiceError } from "@/lib/utils";
8-
import { FileTreeItem, getFiles } from "@/features/fileTree/actions";
98
import { Dialog, DialogContent, DialogDescription, DialogTitle } from "@/components/ui/dialog";
109
import { useBrowseNavigation } from "../hooks/useBrowseNavigation";
1110
import { useBrowseState } from "../hooks/useBrowseState";
1211
import { useBrowseParams } from "../hooks/useBrowseParams";
1312
import { FileTreeItemIcon } from "@/features/fileTree/components/fileTreeItemIcon";
1413
import { useLocalStorage } from "usehooks-ts";
1514
import { Skeleton } from "@/components/ui/skeleton";
15+
import { FileTreeItem } from "@/features/fileTree/types";
16+
import { getFiles } from "@/app/api/(client)/client";
1617

1718
const MAX_RESULTS = 100;
1819

packages/web/src/app/[domain]/search/components/codePreviewPanel/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SearchResultFile } from "@/features/search/types";
66
import { SymbolIcon } from "@radix-ui/react-icons";
77
import { SetStateAction, Dispatch, useMemo } from "react";
88
import { unwrapServiceError } from "@/lib/utils";
9-
import { fetchFileSource } from "@/app/api/(client)/client";
9+
import { getFileSource } from "@/app/api/(client)/client";
1010

1111
interface CodePreviewPanelProps {
1212
previewedFile: SearchResultFile;
@@ -31,7 +31,7 @@ export const CodePreviewPanel = ({
3131
const { data: file, isLoading, isPending, isError } = useQuery({
3232
queryKey: ["source", previewedFile, branch],
3333
queryFn: () => unwrapServiceError(
34-
fetchFileSource({
34+
getFileSource({
3535
fileName: previewedFile.fileName.text,
3636
repository: previewedFile.repository,
3737
branch,

packages/web/src/app/api/(client)/client.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ import {
99
SearchRequest,
1010
SearchResponse,
1111
} from "@/features/search/types";
12-
import { FindRelatedSymbolsRequest, FindRelatedSymbolsResponse } from "@/features/codeNav/types";
12+
import {
13+
FindRelatedSymbolsRequest,
14+
FindRelatedSymbolsResponse,
15+
} from "@/features/codeNav/types";
16+
import {
17+
GetFilesRequest,
18+
GetFilesResponse,
19+
GetTreeRequest,
20+
GetTreeResponse,
21+
} from "@/features/fileTree/types";
1322

1423
export const search = async (body: SearchRequest): Promise<SearchResponse | ServiceError> => {
1524
const result = await fetch("/api/search", {
@@ -27,7 +36,7 @@ export const search = async (body: SearchRequest): Promise<SearchResponse | Serv
2736
return result as SearchResponse | ServiceError;
2837
}
2938

30-
export const fetchFileSource = async (body: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => {
39+
export const getFileSource = async (body: FileSourceRequest): Promise<FileSourceResponse | ServiceError> => {
3140
const result = await fetch("/api/source", {
3241
method: "POST",
3342
headers: {
@@ -74,4 +83,20 @@ export const findSearchBasedSymbolDefinitions = async (body: FindRelatedSymbolsR
7483
body: JSON.stringify(body),
7584
}).then(response => response.json());
7685
return result as FindRelatedSymbolsResponse | ServiceError;
86+
}
87+
88+
export const getTree = async (body: GetTreeRequest): Promise<GetTreeResponse | ServiceError> => {
89+
const result = await fetch("/api/tree", {
90+
method: "POST",
91+
body: JSON.stringify(body),
92+
}).then(response => response.json());
93+
return result as GetTreeResponse | ServiceError;
94+
}
95+
96+
export const getFiles = async (body: GetFilesRequest): Promise<GetFilesResponse | ServiceError> => {
97+
const result = await fetch("/api/files", {
98+
method: "POST",
99+
body: JSON.stringify(body),
100+
}).then(response => response.json());
101+
return result as GetFilesResponse | ServiceError;
77102
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use server';
2+
3+
import { getFiles } from "@/features/fileTree/api";
4+
import { getFilesRequestSchema } from "@/features/fileTree/types";
5+
import { schemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
6+
import { isServiceError } from "@/lib/utils";
7+
import { NextRequest } from "next/server";
8+
9+
export const POST = async (request: NextRequest) => {
10+
const body = await request.json();
11+
const parsed = await getFilesRequestSchema.safeParseAsync(body);
12+
if (!parsed.success) {
13+
return serviceErrorResponse(schemaValidationError(parsed.error));
14+
}
15+
16+
const response = await getFiles(parsed.data);
17+
if (isServiceError(response)) {
18+
return serviceErrorResponse(response);
19+
}
20+
21+
return Response.json(response);
22+
}
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use server';
2+
3+
import { getTree } from "@/features/fileTree/api";
4+
import { getTreeRequestSchema } from "@/features/fileTree/types";
5+
import { schemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
6+
import { isServiceError } from "@/lib/utils";
7+
import { NextRequest } from "next/server";
8+
9+
export const POST = async (request: NextRequest) => {
10+
const body = await request.json();
11+
const parsed = await getTreeRequestSchema.safeParseAsync(body);
12+
if (!parsed.success) {
13+
return serviceErrorResponse(schemaValidationError(parsed.error));
14+
}
15+
16+
const response = await getTree(parsed.data);
17+
if (isServiceError(response)) {
18+
return serviceErrorResponse(response);
19+
}
20+
21+
return Response.json(response);
22+
}
23+

packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { fetchFileSource } from "@/app/api/(client)/client";
3+
import { getFileSource } from "@/app/api/(client)/client";
44
import { VscodeFileIcon } from "@/app/components/vscodeFileIcon";
55
import { ScrollArea } from "@/components/ui/scroll-area";
66
import { Skeleton } from "@/components/ui/skeleton";
@@ -99,11 +99,11 @@ export const ReferencedSourcesListView = ({
9999
const fileSourceQueries = useQueries({
100100
queries: referencedFileSources.map((file) => ({
101101
queryKey: ['fileSource', file.path, file.repo, file.revision, domain],
102-
queryFn: () => unwrapServiceError(fetchFileSource({
102+
queryFn: () => unwrapServiceError(getFileSource({
103103
fileName: file.path,
104104
repository: file.repo,
105105
branch: file.revision,
106-
}, domain)),
106+
})),
107107
staleTime: Infinity,
108108
})),
109109
});

packages/web/src/features/fileTree/actions.ts renamed to packages/web/src/features/fileTree/api.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use server';
1+
import 'server-only';
22

33
import { sew } from '@/actions';
44
import { env } from '@sourcebot/shared';
@@ -8,19 +8,10 @@ import { Repo } from '@sourcebot/db';
88
import { createLogger } from '@sourcebot/shared';
99
import path from 'path';
1010
import { simpleGit } from 'simple-git';
11+
import { FileTreeItem, FileTreeNode } from './types';
1112

1213
const logger = createLogger('file-tree');
1314

14-
export type FileTreeItem = {
15-
type: string;
16-
path: string;
17-
name: string;
18-
}
19-
20-
export type FileTreeNode = FileTreeItem & {
21-
children: FileTreeNode[];
22-
}
23-
2415
/**
2516
* Returns the tree of files (blobs) and directories (trees) for a given repository,
2617
* at a given revision.
@@ -218,7 +209,7 @@ const buildFileTree = (flatList: { type: string, path: string }[]): FileTreeNode
218209
const part = parts[i];
219210
const isLeaf = i === parts.length - 1;
220211
const nodeType = isLeaf ? item.type : 'tree';
221-
let next = current.children.find(child => child.name === part && child.type === nodeType);
212+
let next = current.children.find((child: FileTreeNode) => child.name === part && child.type === nodeType);
222213

223214
if (!next) {
224215
next = {
@@ -240,7 +231,7 @@ const buildFileTree = (flatList: { type: string, path: string }[]): FileTreeNode
240231

241232
const sortedChildren = node.children
242233
.map(sortTree)
243-
.sort((a, b) => {
234+
.sort((a: FileTreeNode, b: FileTreeNode) => {
244235
if (a.type !== b.type) {
245236
return a.type === 'tree' ? -1 : 1;
246237
}

packages/web/src/features/fileTree/components/fileTreeItemComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { FileTreeItem } from "../actions";
3+
import { FileTreeItem } from "../api";
44
import { useEffect, useRef } from "react";
55
import clsx from "clsx";
66
import scrollIntoView from 'scroll-into-view-if-needed';

0 commit comments

Comments
 (0)