Skip to content

Commit 8dfc265

Browse files
authored
nest type declaration files under source file in code browser (#2405)
1 parent 139dbf6 commit 8dfc265

4 files changed

Lines changed: 56 additions & 28 deletions

File tree

components/Package/CodeBrowser/CodeBrowserFileTree.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ type Props = {
99
activeFile: string | null;
1010
onSelectFile: (filePath: string) => void;
1111
depth?: number;
12+
isNested?: boolean;
1213
};
1314

14-
export default function CodeBrowserFileTree({ tree, activeFile, onSelectFile, depth = 0 }: Props) {
15+
export default function CodeBrowserFileTree({
16+
tree,
17+
activeFile,
18+
onSelectFile,
19+
depth = 0,
20+
isNested = false,
21+
}: Props) {
1522
const directories = Object.values(tree.directories).sort((a, b) => a.name.localeCompare(b.name));
1623
const files = [...tree.files].sort((a, b) => a.name.localeCompare(b.name));
1724

@@ -39,17 +46,22 @@ export default function CodeBrowserFileTree({ tree, activeFile, onSelectFile, de
3946
depth={depth}
4047
onPress={() => onSelectFile(file.path)}
4148
isActive={file.path === activeFile}
49+
isNested={isNested}
4250
/>
43-
{file.nestedFiles?.map(nestedFile => (
44-
<CodeBrowserFileRow
45-
isNested
46-
key={nestedFile.path}
47-
label={nestedFile.name}
51+
{file.nestedFiles && (
52+
<CodeBrowserFileTree
53+
tree={{
54+
name: '',
55+
path: file.path,
56+
directories: {},
57+
files: file.nestedFiles,
58+
}}
59+
activeFile={activeFile}
60+
onSelectFile={onSelectFile}
4861
depth={depth + 1}
49-
onPress={() => onSelectFile(nestedFile.path)}
50-
isActive={nestedFile.path === activeFile}
62+
isNested
5163
/>
52-
))}
64+
)}
5365
</View>
5466
))}
5567
</>

components/Package/CodeBrowser/DisplayModeButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Pressable, View } from 'react-native';
22

33
import { useLayout } from '~/common/styleguide';
4-
import { MinimizeIcon } from '~/components/Icons';
4+
import { MaximizeIcon, MinimizeIcon } from '~/components/Icons';
55
import InputKeyHint from '~/components/InputKeyHint';
66
import Tooltip from '~/components/Tooltip';
77
import tw from '~/util/tailwind';
@@ -14,7 +14,7 @@ type Props = {
1414
export default function DisplayModeButton({ isBrowserMaximized, toggleMaximized }: Props) {
1515
const { isSmallScreen } = useLayout();
1616

17-
const Icon = isBrowserMaximized ? MinimizeIcon : MinimizeIcon;
17+
const Icon = isBrowserMaximized ? MinimizeIcon : MaximizeIcon;
1818

1919
return (
2020
<Tooltip

components/Package/CodeBrowser/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { type LibraryType, type UnpkgMeta } from '~/types';
99
import {
1010
buildCodeBrowserFileTree,
1111
getCodeBrowserFilePath,
12-
getCodeBrowserNestedFileParentPath,
12+
getCodeBrowserNestedFileParentPaths,
1313
} from '~/util/codeBrowser';
1414
import { TimeRange } from '~/util/datetime';
1515
import { formatBytes } from '~/util/formatBytes';
@@ -67,9 +67,7 @@ export default function CodeBrowser({
6767
for (const file of files) {
6868
filesByPath.set(file.path, file);
6969

70-
const nestedFileParentPath = getCodeBrowserNestedFileParentPath(file.path);
71-
72-
if (nestedFileParentPath) {
70+
getCodeBrowserNestedFileParentPaths(file.path).forEach(nestedFileParentPath => {
7371
relatedPaths.set(
7472
file.path,
7573
(relatedPaths.get(file.path) ?? new Set()).add(nestedFileParentPath)
@@ -78,7 +76,7 @@ export default function CodeBrowser({
7876
nestedFileParentPath,
7977
(relatedPaths.get(nestedFileParentPath) ?? new Set()).add(file.path)
8078
);
81-
}
79+
});
8280

8381
const relativePath = getCodeBrowserFilePath(file.path, data?.prefix).toLowerCase();
8482

util/codeBrowser.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ const SOURCE_MAP_PARENT_EXTENSIONS = new Set([
104104
'tsx',
105105
]);
106106

107+
const DECLARATION_FILE_PARENT_EXTENSIONS = new Map([
108+
['.d.cts', ['cjs']],
109+
['.d.mts', ['mjs']],
110+
['.d.ts', ['js', 'mjs', 'cjs']],
111+
]);
112+
107113
const HASH_FILE_EXTENSIONS = new Set(['md5', 'sha1', 'sha3', 'sha256', 'sha512']);
108114

109115
export function getFileWarning(fileName?: string) {
@@ -118,10 +124,26 @@ export function getCodeBrowserFilePath(path: string, prefix?: string) {
118124
}
119125

120126
export function getCodeBrowserNestedFileParentPath(path: string) {
127+
return getCodeBrowserNestedFileParentPaths(path)[0] ?? null;
128+
}
129+
130+
export function getCodeBrowserNestedFileParentPaths(path: string) {
131+
const lowerCasedPath = path.toLowerCase();
132+
133+
for (const [declarationFileSuffix, parentExtensions] of DECLARATION_FILE_PARENT_EXTENSIONS) {
134+
if (!lowerCasedPath.endsWith(declarationFileSuffix)) {
135+
continue;
136+
}
137+
138+
const declarationFileBasePath = path.slice(0, -declarationFileSuffix.length);
139+
140+
return parentExtensions.map(parentExtension => `${declarationFileBasePath}.${parentExtension}`);
141+
}
142+
121143
const nestedFileExtension = path.split('.').pop()?.toLowerCase();
122144

123145
if (!nestedFileExtension) {
124-
return null;
146+
return [];
125147
}
126148

127149
const nestedFileParentPath = path.slice(0, -(nestedFileExtension.length + 1));
@@ -130,17 +152,17 @@ export function getCodeBrowserNestedFileParentPath(path: string) {
130152
const sourceMapParentExtension = nestedFileParentPath.split('.').pop()?.toLowerCase();
131153

132154
if (!sourceMapParentExtension || !SOURCE_MAP_PARENT_EXTENSIONS.has(sourceMapParentExtension)) {
133-
return null;
155+
return [];
134156
}
135157

136-
return nestedFileParentPath;
158+
return [nestedFileParentPath];
137159
}
138160

139161
if (HASH_FILE_EXTENSIONS.has(nestedFileExtension)) {
140-
return nestedFileParentPath;
162+
return [nestedFileParentPath];
141163
}
142164

143-
return null;
165+
return [];
144166
}
145167

146168
export function buildCodeBrowserFileTree(
@@ -197,13 +219,9 @@ function nestCodeBrowserSidecarFiles(directory: CodeBrowserTreeDirectory) {
197219
const nestedFilePaths = new Set<string>();
198220

199221
directory.files.forEach(file => {
200-
const nestedFileParentPath = getCodeBrowserNestedFileParentPath(file.path);
201-
202-
if (!nestedFileParentPath) {
203-
return;
204-
}
205-
206-
const nestedFileParent = filesByPath.get(nestedFileParentPath);
222+
const nestedFileParent = getCodeBrowserNestedFileParentPaths(file.path)
223+
.map(nestedFileParentPath => filesByPath.get(nestedFileParentPath))
224+
.find(Boolean);
207225

208226
if (!nestedFileParent) {
209227
return;

0 commit comments

Comments
 (0)