import { Fragment, useEffect } from 'react'; import React, { useMemo, useState } from 'react'; import { isAudioFile, readDir, dir, playFile } from './files.mjs'; export function FilesTab() { const [path, setPath] = useState([]); useEffect(() => { let init = false; readDir('', { dir, recursive: true }) .then((children) => setPath([{ name: '~/music', children }])) .catch((err) => { console.log('error loadin files', err); }); return () => { init = true; }; }, []); const current = useMemo(() => path[path.length - 1], [path]); const subpath = useMemo( () => path .slice(1) .map((p) => p.name) .join('/'), [path], ); const folders = useMemo(() => current?.children.filter((e) => !!e.children), [current]); const files = useMemo(() => current?.children.filter((e) => !e.children && isAudioFile(e.name)), [current]); const select = (e) => setPath((p) => p.concat([e])); return (
{`samples('`} {path?.map((p, i) => { if (i < path.length - 1) { return ( setPath((p) => p.slice(0, i + 1))}> {p.name} / ); } else { return ( {p.name} ); } })} {`')`}
{!folders?.length && !files?.length && Nothing here} {folders?.map((e, i) => (
select(e)}> {e.name}
))} {files?.map((e, i) => (
playFile(`${subpath}/${e.name}`)} > {e.name}
))}
); }