Extract basenameOfPath into shared utility in lib/utils.ts
Move the duplicated basenameOfPath function from ChatView.tsx and vscode-icons.ts into the shared lib/utils.ts module, and update both files to import from there.
This commit is contained in:
1 parent
b67f3e6c3d
commit
edf9d2cac3
3 files changed
+18
-22
No files matched your search
@@ -91,7 +91,7 @@ import { Separator } from "./ui/separator";
|
||||
import { Group, GroupSeparator } from "./ui/group";
|
||||
import { Menu, MenuItem, MenuPopup, MenuShortcut, MenuTrigger } from "./ui/menu";
|
||||
import { CursorIcon, Icon } from "./Icons";
|
||||
import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
|
||||
import { basenameOfPath, cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
|
||||
import { Badge } from "./ui/badge";
|
||||
import { Command, CommandItem, CommandList } from "./ui/command";
|
||||
|
||||
@@ -122,12 +122,6 @@ function workToneClass(tone: "thinking" | "tool" | "info" | "error"): string {
|
||||
return "text-muted-foreground/40";
|
||||
}
|
||||
|
||||
function basenameOfPath(pathValue: string): string {
|
||||
const slashIndex = pathValue.lastIndexOf("/");
|
||||
if (slashIndex === -1) return pathValue;
|
||||
return pathValue.slice(slashIndex + 1);
|
||||
}
|
||||
|
||||
type SessionContinuityState = "resumed" | "new" | "fallback_new";
|
||||
|
||||
interface EnsuredSessionInfo {
|
||||
@@ -483,11 +477,7 @@ export default function ChatView() {
|
||||
};
|
||||
}, [isPathTrigger, pathTriggerQuery]);
|
||||
const workspaceEntriesQuery = useQuery({
|
||||
queryKey: [
|
||||
"composer-workspace-entries",
|
||||
gitCwd,
|
||||
debouncedPathQuery,
|
||||
],
|
||||
queryKey: ["composer-workspace-entries", gitCwd, debouncedPathQuery],
|
||||
enabled: Boolean(api && gitCwd && isPathTrigger),
|
||||
staleTime: 15_000,
|
||||
placeholderData: (previous) => previous,
|
||||
@@ -1298,7 +1288,8 @@ export default function ChatView() {
|
||||
setComposerMenuIndex(index);
|
||||
}, []);
|
||||
const isComposerMenuLoading =
|
||||
composerTriggerKind === "path" && (workspaceEntriesQuery.isLoading || workspaceEntriesQuery.isFetching);
|
||||
composerTriggerKind === "path" &&
|
||||
(workspaceEntriesQuery.isLoading || workspaceEntriesQuery.isFetching);
|
||||
|
||||
const onPromptChange = useCallback((nextPrompt: string, nextCursor: number) => {
|
||||
setPrompt(nextPrompt);
|
||||
|
||||
@@ -12,3 +12,9 @@ export function isMacPlatform(platform: string): boolean {
|
||||
export function isWindowsPlatform(platform: string): boolean {
|
||||
return /^win(dows)?/i.test(platform);
|
||||
}
|
||||
|
||||
export function basenameOfPath(pathValue: string): string {
|
||||
const slashIndex = pathValue.lastIndexOf("/");
|
||||
if (slashIndex === -1) return pathValue;
|
||||
return pathValue.slice(slashIndex + 1);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { basenameOfPath } from "./lib/utils";
|
||||
import vscodeIconsManifest from "./vscode-icons-manifest.json";
|
||||
import languageAssociationsData from "./vscode-icons-language-associations.json";
|
||||
|
||||
@@ -61,12 +62,6 @@ function toLowercaseLookup(source: Record<string, string>): Record<string, strin
|
||||
return lookup;
|
||||
}
|
||||
|
||||
function basenameOfPath(pathValue: string): string {
|
||||
const slashIndex = pathValue.lastIndexOf("/");
|
||||
if (slashIndex === -1) return pathValue;
|
||||
return pathValue.slice(slashIndex + 1);
|
||||
}
|
||||
|
||||
function extensionCandidates(fileName: string): string[] {
|
||||
const candidates = new Set<string>();
|
||||
if (fileName.includes(".")) {
|
||||
@@ -83,7 +78,10 @@ function extensionCandidates(fileName: string): string[] {
|
||||
return [...candidates];
|
||||
}
|
||||
|
||||
function resolveLanguageFallbackDefinition(pathValue: string, theme: "light" | "dark"): string | null {
|
||||
function resolveLanguageFallbackDefinition(
|
||||
pathValue: string,
|
||||
theme: "light" | "dark",
|
||||
): string | null {
|
||||
const basename = basenameOfPath(pathValue).toLowerCase();
|
||||
const languageIds = theme === "light" ? lightLanguageIds : darkLanguageIds;
|
||||
|
||||
@@ -94,8 +92,9 @@ function resolveLanguageFallbackDefinition(pathValue: string, theme: "light" | "
|
||||
|
||||
for (const candidate of extensionCandidates(basename)) {
|
||||
const languageId =
|
||||
localLanguageIdByExtensionOverrides[candidate as keyof typeof localLanguageIdByExtensionOverrides] ??
|
||||
languageIdByExtension[candidate];
|
||||
localLanguageIdByExtensionOverrides[
|
||||
candidate as keyof typeof localLanguageIdByExtensionOverrides
|
||||
] ?? languageIdByExtension[candidate];
|
||||
if (!languageId) continue;
|
||||
return languageIds[languageId] ?? darkLanguageIds[languageId] ?? null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user