import React, { useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; import { NavLink } from 'react-router-dom'; import { cleanDirectoryPath } from '@/helpers'; interface Props { withinFileEditor?: boolean; isNewFile?: boolean; } export default ({ 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); useEffect(() => { const parts = cleanDirectoryPath(window.location.hash).split('/'); if (withinFileEditor && !isNewFile) { setFile(parts.pop() || null); } }, [ withinFileEditor, isNewFile ]); const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/') .filter(directory => !!directory) .map((directory, index, dirs) => { if (!withinFileEditor && index === dirs.length - 1) { return { name: decodeURIComponent(directory) }; } return { name: decodeURIComponent(directory), path: `/${dirs.slice(0, index + 1).join('/')}` }; }); return (
/home/ container / { breadcrumbs().map((crumb, index) => ( crumb.path ? {crumb.name} / : {crumb.name} )) } {file && {decodeURIComponent(file)} }
); };