Skip to content

Commit 9c6568e

Browse files
committed
feat(app): add file icons in autocomplete and graph
1 parent c670982 commit 9c6568e

File tree

7 files changed

+70
-5
lines changed

7 files changed

+70
-5
lines changed

scripts/fetch-icons.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ async function fetchIcons(): Promise<void> {
2121
'[email protected]:PKief/vscode-material-icon-theme.git',
2222
vsCodeExtDirName,
2323
);
24-
await fse.copy(resolve(vsCodeExtDirPath, 'icons'), iconsDirPath);
2524
execSync('npm install', {
2625
cwd: vsCodeExtDirPath,
2726
stdio: 'inherit',
2827
});
28+
await fse.copy(resolve(vsCodeExtDirPath, 'icons'), iconsDirPath);
2929
execSync('npm run build', {
3030
cwd: vsCodeExtDirPath,
3131
stdio: 'inherit',

src/app/components/DepGraph.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { FC, useEffect, useRef, useState } from 'react';
33
import { Edge, Network, Node } from 'vis-network/standalone';
44
import Worker from 'worker-loader!../workers/graph.worker';
55
import { filenameFromPath } from '../utils/format';
6+
import { getIconUrlByName, getIconUrlForPath } from '../utils/icons';
67
import { DepGraph, Filters, ModuleDeps } from '../utils/types';
78

89
type Props = {
@@ -37,6 +38,7 @@ const DepGraph: FC<Props> = ({ moduleDeps, filters }) => {
3738
id: path,
3839
label: isLocal ? filenameFromPath(path) : path,
3940
title: path,
41+
image: isLocal ? getIconUrlForPath(path) : getIconUrlByName('npm'),
4042
}),
4143
);
4244

@@ -53,7 +55,8 @@ const DepGraph: FC<Props> = ({ moduleDeps, filters }) => {
5355
{ edges, nodes },
5456
{
5557
nodes: {
56-
shape: 'box',
58+
shape: 'image',
59+
image: getIconUrlByName('file'),
5760
},
5861
edges: {
5962
arrows: 'to',

src/app/components/SelectModules.tsx

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { TextField } from '@material-ui/core';
1+
import { Grid, Icon, TextField, Typography } from '@material-ui/core';
22
import {
33
Autocomplete,
44
AutocompleteRenderInputParams,
55
FilterOptionsState,
66
} from '@material-ui/lab';
77
import { matchSorter } from 'match-sorter';
88
import React, { ChangeEvent, FC } from 'react';
9+
import { dirnameFromPath, filenameFromPath } from '../utils/format';
10+
import { getIconUrlForPath } from '../utils/icons';
911
import { Module } from '../utils/types';
1012

1113
type Props = {
@@ -27,6 +29,26 @@ const SelectModules: FC<Props> = ({ modules, label, value, onChange }) => {
2729
const renderInput = (params: AutocompleteRenderInputParams) => (
2830
<TextField {...params} label={label} variant="outlined" />
2931
);
32+
const renderOption = (option: Module) => (
33+
<Grid container spacing={2} alignItems="center">
34+
<Grid item>
35+
<Icon fontSize="small" style={{ textAlign: 'center' }}>
36+
<img
37+
src={getIconUrlForPath(option.path)}
38+
style={{ display: 'flex', height: 'inherit', width: 'inherit' }}
39+
/>
40+
</Icon>
41+
</Grid>
42+
<Grid item>
43+
<Typography variant="subtitle1">
44+
{filenameFromPath(option.path)}
45+
</Typography>
46+
</Grid>
47+
<Grid item>
48+
<Typography variant="body2">{dirnameFromPath(option.path)}</Typography>
49+
</Grid>
50+
</Grid>
51+
);
3052
const handleChange = (_: ChangeEvent<{}>, value: Module[]) => {
3153
onChange?.(value);
3254
};
@@ -40,6 +62,7 @@ const SelectModules: FC<Props> = ({ modules, label, value, onChange }) => {
4062
value={value}
4163
getOptionLabel={getOptionLabel}
4264
renderInput={renderInput}
65+
renderOption={renderOption}
4366
filterOptions={filterOptions}
4467
onChange={handleChange}
4568
getOptionSelected={areModulesEqual}

src/app/generated/icon-map.json

+2
Original file line numberDiff line numberDiff line change
@@ -3793,7 +3793,9 @@
37933793
".nycrc": "istanbul",
37943794
".nycrc.json": "istanbul",
37953795
"tailwind.js": "tailwindcss",
3796+
"tailwind.ts": "tailwindcss",
37963797
"tailwind.config.js": "tailwindcss",
3798+
"tailwind.config.ts": "tailwindcss",
37973799
"buildkite.yml": "buildkite",
37983800
"buildkite.yaml": "buildkite",
37993801
"netlify.json": "netlify",

src/app/utils/format.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export function filenameFromPath(path: string): string {
44
}
55

66
export function dirnameFromPath(path: string): string {
7-
return path.replace(/\/[^/]$/, '/');
7+
return path.replace(/\/[^/]+$/, '/');
88
}

src/app/utils/icons.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import iconMap from '../generated/icon-map.json';
2+
import { filenameFromPath } from './format';
3+
4+
type IconDefinitionsKey = keyof typeof iconMap['iconDefinitions'];
5+
type FileNamesKey = keyof typeof iconMap['fileNames'];
6+
type FileExtensionsKey = keyof typeof iconMap['fileExtensions'];
7+
8+
const ICONS_URL =
9+
process.env.NODE_ENV === 'production'
10+
? 'assets/icons'
11+
: '../../../dist/app/assets/icons';
12+
13+
export function getIconUrlByName(iconName: IconDefinitionsKey): string {
14+
return `${ICONS_URL}/${iconName}.svg`;
15+
}
16+
17+
export function getIconUrlForPath(path: string): string {
18+
const fileName = filenameFromPath(path);
19+
const iconName = getIconNameForFileName(fileName) as IconDefinitionsKey;
20+
return getIconUrlByName(iconName);
21+
}
22+
23+
function getIconNameForFileName(fileName: string): string {
24+
return (
25+
iconMap.fileNames[fileName as FileNamesKey] ??
26+
iconMap.fileNames[fileName.toLowerCase() as FileNamesKey] ??
27+
iconMap.fileExtensions[getFileExtension(fileName)] ??
28+
(fileName.endsWith('.ts') ? 'typescript' : null) ??
29+
(fileName.endsWith('.js') ? 'javascript' : null) ??
30+
'file'
31+
);
32+
}
33+
34+
function getFileExtension(fileName: string): FileExtensionsKey {
35+
return fileName.substr(fileName.indexOf('.') + 1) as FileExtensionsKey;
36+
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"skipLibCheck": true,
88
"forceConsistentCasingInFileNames": true,
99
"jsx": "react",
10-
"sourceMap": true
10+
"sourceMap": true,
11+
"resolveJsonModule": true
1112
}
1213
}

0 commit comments

Comments
 (0)