import { useWorkspace } from '../../../components/Workspace';
import { FileBrowserApp } from './FileBrowserApp';

const cwdToPath = (cwd: string) => (cwd === '~' ? '/' : cwd.slice(1));

export const FileBrowserPanelWrapper = () => {
  const { cwd, workspace } = useWorkspace();
  const basePath = cwdToPath(cwd);

  // Only the /files screen puts its folder in the URL. A dashboard can hold two file browsers, and one
  // shared `?path=` would move both — so the address bar belongs to the workspace that is guaranteed to
  // host exactly one. The test is the framework's parsed identity, not the base path: an unscoped panel
  // (cwd `~`) sits on dashboards too, so `basePath === '/'` would catch the wrong browsers.
  const urlPath = workspace?.kind === 'screen' && workspace.id === 'files';

  return <FileBrowserApp basePath={basePath} rootOverride={basePath !== '/' ? 'home' : undefined} urlPath={urlPath} />;
};
