misc_pterodactyl-panel/resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx

83 lines
3 KiB
TypeScript
Raw Normal View History

2019-10-26 20:16:27 +00:00
import React, { useEffect, useState } from 'react';
import { ServerContext } from '@/state/server';
import { NavLink, useLocation } from 'react-router-dom';
import { cleanDirectoryPath } from '@/helpers';
2020-07-05 00:57:24 +00:00
import tw from 'twin.macro';
2019-10-26 20:16:27 +00:00
2019-12-22 00:38:40 +00:00
interface Props {
renderLeft?: JSX.Element;
2019-12-22 00:38:40 +00:00
withinFileEditor?: boolean;
isNewFile?: boolean;
}
export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => {
2019-10-26 20:16:27 +00:00
const [ file, setFile ] = useState<string | null>(null);
const id = ServerContext.useStoreState(state => state.server.data!.id);
const directory = ServerContext.useStoreState(state => state.files.directory);
const { hash } = useLocation();
2019-10-26 20:16:27 +00:00
useEffect(() => {
let pathHash = cleanDirectoryPath(hash);
try {
pathHash = decodeURI(pathHash);
} catch (e) {
console.warn('Error decoding URL parts in hash:', e);
}
2019-10-26 20:16:27 +00:00
2019-12-22 00:38:40 +00:00
if (withinFileEditor && !isNewFile) {
let name = pathHash.split('/').pop() || null;
if (name) {
try {
name = decodeURIComponent(name);
} catch (e) {
console.warn('Error decoding filename:', e);
}
}
setFile(name);
2019-10-26 20:16:27 +00:00
}
}, [ withinFileEditor, isNewFile, hash ]);
2019-10-26 20:16:27 +00:00
const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/')
.filter(directory => !!directory)
.map((directory, index, dirs) => {
if (!withinFileEditor && index === dirs.length - 1) {
return { name: directory };
2019-10-26 20:16:27 +00:00
}
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
2019-10-26 20:16:27 +00:00
});
return (
<div css={tw`flex flex-grow-0 items-center text-sm text-neutral-500 overflow-x-hidden`}>
{renderLeft || <div css={tw`w-12`}/>}
2020-07-05 00:57:24 +00:00
/<span css={tw`px-1 text-neutral-300`}>home</span>/
2019-10-26 20:16:27 +00:00
<NavLink
to={`/server/${id}/files`}
2020-07-05 00:57:24 +00:00
css={tw`px-1 text-neutral-200 no-underline hover:text-neutral-100`}
2019-10-26 20:16:27 +00:00
>
container
</NavLink>/
{
breadcrumbs().map((crumb, index) => (
crumb.path ?
<React.Fragment key={index}>
<NavLink
to={`/server/${id}/files#${crumb.path}`}
2020-07-05 00:57:24 +00:00
css={tw`px-1 text-neutral-200 no-underline hover:text-neutral-100`}
2019-10-26 20:16:27 +00:00
>
{decodeURIComponent(crumb.name)}
2019-10-26 20:16:27 +00:00
</NavLink>/
</React.Fragment>
:
<span key={index} css={tw`px-1 text-neutral-300`}>{decodeURIComponent(crumb.name)}</span>
2019-10-26 20:16:27 +00:00
))
}
{file &&
<React.Fragment>
<span css={tw`px-1 text-neutral-300`}>{file}</span>
2019-10-26 20:16:27 +00:00
</React.Fragment>
}
</div>
);
};