import React, { useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; import { NavLink, useLocation } from 'react-router-dom'; import { cleanDirectoryPath } from '@/helpers'; import tw from 'twin.macro'; interface Props { renderLeft?: JSX.Element; withinFileEditor?: boolean; isNewFile?: boolean; } export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => { const [ file, setFile ] = useState(null); const id = ServerContext.useStoreState(state => state.server.data!.id); const directory = ServerContext.useStoreState(state => state.files.directory); const { hash } = useLocation(); useEffect(() => { let pathHash = cleanDirectoryPath(hash); try { pathHash = decodeURI(pathHash); } catch (e) { console.warn('Error decoding URL parts in hash:', e); } 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); } }, [ withinFileEditor, isNewFile, hash ]); const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/') .filter(directory => !!directory) .map((directory, index, dirs) => { if (!withinFileEditor && index === dirs.length - 1) { return { name: directory }; } return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` }; }); return (
{renderLeft ||
} /home/ container / { breadcrumbs().map((crumb, index) => ( crumb.path ? {decodeURIComponent(crumb.name)} / : {decodeURIComponent(crumb.name)} )) } {file && {file} }
); };