Skip to content

feat: add file extension based icons #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion packages/react/src/core/FileTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@
className?: string;
}

const extensionsToIcons = new Map([
['ts', 'i-ph-file-ts-duotone'],

['cjs', 'i-ph-file-js-duotone'],
['mjs', 'i-ph-file-js-duotone'],
['js', 'i-ph-file-js-duotone'],

['html', 'i-ph-file-html-duotone'],

['css', 'i-ph-file-css-duotone'],

['md', 'i-ph-file-md-duotone'],

['gif', 'i-ph-file-image-duotone'],
['jpg', 'i-ph-file-image-duotone'],
['jpeg', 'i-ph-file-image-duotone'],
['png', 'i-ph-file-image-duotone'],
]);

export function FileTree({
files,
onFileSelect,
Expand Down Expand Up @@ -183,7 +202,9 @@
}

function File({ file: { depth, name }, onClick, selected }: FileProps) {
const extension = getFileExtension(name);
const fileIcon = extensionsToIcons.get(extension) || 'i-ph-file-duotone';
return (

Check failure on line 207 in packages/react/src/core/FileTree.tsx

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 18)

Expected newline before return statement

Check failure on line 207 in packages/react/src/core/FileTree.tsx

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 20)

Expected newline before return statement

Check failure on line 207 in packages/react/src/core/FileTree.tsx

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 22)

Expected newline before return statement

Check failure on line 207 in packages/react/src/core/FileTree.tsx

View workflow job for this annotation

GitHub Actions / Test (macos-latest, 20)

Expected newline before return statement

Check failure on line 207 in packages/react/src/core/FileTree.tsx

View workflow job for this annotation

GitHub Actions / Test (windows-latest, 20.13.1)

Expected newline before return statement
<NodeButton
className={classNames('group transition-theme', {
'bg-tk-elements-fileTree-file-backgroundColor hover:bg-tk-elements-fileTree-file-backgroundColorHover text-tk-elements-fileTree-file-textColor hover:text-tk-elements-fileTree-file-textColorHover':
Expand All @@ -192,7 +213,7 @@
selected,
})}
depth={depth}
iconClasses={classNames('i-ph-file-duotone', {
iconClasses={classNames(fileIcon, {
'text-tk-elements-fileTree-file-iconColor group-hover:text-tk-elements-fileTree-file-iconColorHover': !selected,
'text-tk-elements-fileTree-file-iconColorSelected': selected,
})}
Expand Down Expand Up @@ -358,3 +379,13 @@

return 0;
}

function getFileExtension(filename: string) {
const parts = filename.split('.');

parts.shift();

const extension = parts.at(-1) || '';

return extension;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading