2022-11-25 20:25:03 +00:00
|
|
|
import { Fragment, useEffect, useState } from 'react';
|
2023-01-12 18:22:24 +00:00
|
|
|
import { NavLink, useParams } from 'react-router-dom';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
2019-10-26 20:16:27 +00:00
|
|
|
|
2023-01-12 18:22:24 +00:00
|
|
|
import { encodePathSegments } from '@/helpers';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
|
2019-12-22 00:38:40 +00:00
|
|
|
interface Props {
|
2020-12-09 04:34:31 +00:00
|
|
|
renderLeft?: JSX.Element;
|
2019-12-22 00:38:40 +00:00
|
|
|
withinFileEditor?: boolean;
|
|
|
|
isNewFile?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-12-09 04:34:31 +00:00
|
|
|
export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => {
|
2022-11-25 20:25:03 +00:00
|
|
|
const id = ServerContext.useStoreState(state => state.server.data!.id);
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2023-01-12 18:22:24 +00:00
|
|
|
|
|
|
|
const params = useParams<'*'>();
|
|
|
|
|
|
|
|
const [file, setFile] = useState<string>();
|
2019-10-26 20:16:27 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-12 18:22:24 +00:00
|
|
|
if (!withinFileEditor || isNewFile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withinFileEditor && params['*'] !== undefined && !isNewFile) {
|
|
|
|
setFile(decodeURIComponent(params['*']).split('/').pop());
|
|
|
|
}
|
|
|
|
}, [withinFileEditor, isNewFile]);
|
2019-10-26 20:16:27 +00:00
|
|
|
|
2023-01-12 18:22:24 +00:00
|
|
|
const breadcrumbs = (): { name: string; path?: string }[] => {
|
|
|
|
if (directory === '.') {
|
|
|
|
return [];
|
2019-10-26 20:16:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 18:22:24 +00:00
|
|
|
return directory
|
2022-06-26 19:13:52 +00:00
|
|
|
.split('/')
|
2022-11-25 20:25:03 +00:00
|
|
|
.filter(directory => !!directory)
|
2022-06-26 19:13:52 +00:00
|
|
|
.map((directory, index, dirs) => {
|
|
|
|
if (!withinFileEditor && index === dirs.length - 1) {
|
|
|
|
return { name: directory };
|
|
|
|
}
|
2019-10-26 20:16:27 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
|
|
|
|
});
|
2023-01-12 18:22:24 +00:00
|
|
|
};
|
2019-10-26 20:16:27 +00:00
|
|
|
|
|
|
|
return (
|
2020-12-04 04:10:41 +00:00
|
|
|
<div css={tw`flex flex-grow-0 items-center text-sm text-neutral-500 overflow-x-hidden`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{renderLeft || <div css={tw`w-12`} />}/<span css={tw`px-1 text-neutral-300`}>home</span>/
|
|
|
|
<NavLink to={`/server/${id}/files`} css={tw`px-1 text-neutral-200 no-underline hover:text-neutral-100`}>
|
2019-10-26 20:16:27 +00:00
|
|
|
container
|
2022-06-26 19:13:52 +00:00
|
|
|
</NavLink>
|
|
|
|
/
|
|
|
|
{breadcrumbs().map((crumb, index) =>
|
|
|
|
crumb.path ? (
|
2022-11-25 20:25:03 +00:00
|
|
|
<Fragment key={index}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<NavLink
|
|
|
|
to={`/server/${id}/files#${encodePathSegments(crumb.path)}`}
|
|
|
|
css={tw`px-1 text-neutral-200 no-underline hover:text-neutral-100`}
|
2023-01-12 18:22:24 +00:00
|
|
|
end
|
2022-06-26 19:13:52 +00:00
|
|
|
>
|
|
|
|
{crumb.name}
|
|
|
|
</NavLink>
|
|
|
|
/
|
2022-11-25 20:25:03 +00:00
|
|
|
</Fragment>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
|
|
|
<span key={index} css={tw`px-1 text-neutral-300`}>
|
|
|
|
{crumb.name}
|
|
|
|
</span>
|
2022-11-25 20:25:03 +00:00
|
|
|
),
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
|
|
|
{file && (
|
2022-11-25 20:25:03 +00:00
|
|
|
<Fragment>
|
2022-06-26 19:13:52 +00:00
|
|
|
<span css={tw`px-1 text-neutral-300`}>{file}</span>
|
2022-11-25 20:25:03 +00:00
|
|
|
</Fragment>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2019-10-26 20:16:27 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|