Skip to content

Commit

Permalink
feat(Next > Walk): Add folder traversal with .. link
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Sep 6, 2023
1 parent 8b5bbdf commit 9e92867
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
32 changes: 20 additions & 12 deletions next/pages/admin/walk.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { List, ListDivider } from '@mui/joy'
import { List, ListDivider, ListItem } from '@mui/joy'
import { useRouter } from 'next/router'
import { Fragment, useEffect, useState } from 'react'

import ListFile from '../../src/components/Walk/ListFile'
import type { Filesystem, FilesystemBody } from '../../src/lib/filesystems'
import { isImage, organizeByMedia, parseHash } from '../../src/utils/walk'
import {
addParentDirectoryNav,
isImage,
organizeByMedia,
parseHash,
} from '../../src/utils/walk'

type ItemFile = Partial<Filesystem> & {
id: Filesystem['id'];
Expand Down Expand Up @@ -34,18 +39,21 @@ function WalkPage() {
if (!data) return <p>No filesystem data</p>

const itemImages = data.files.filter((file) => isImage(file))
// eslint-disable-next-line no-console
console.log('itemImages', itemImages)
const hasImages = !isLoading && itemImages.length > 0
const fsItems = addParentDirectoryNav(organizeByMedia(data.files), pathQs)

return (
<List>
{organizeByMedia(data.files).map((item, i) => (
<Fragment key={item.id}>
{i > 0 && <ListDivider />}
<ListFile item={item} />
</Fragment>
))}
</List>
<>
<List>
{fsItems.map((item, i) => (
<Fragment key={item.id}>
{i > 0 && <ListDivider />}
<ListFile item={item} />
</Fragment>
))}
</List>
{hasImages && (<div>TODO display OrganizeThumbs</div>)}
</>
)
}

Expand Down
14 changes: 11 additions & 3 deletions next/pages/api/admin/filesystems/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ describe('Filesystem API', () => {
})
})

function matchFile(expect: jest.Expect, file: Filesystem) {
function matchPFile(expect: jest.Expect, file: Filesystem) {
expect(file.filename).toEqual('P1160066.JPG')
expect(file.ext).toEqual('JPG')
expect(file.mediumType).toEqual('image')
expect(file.name).toEqual('P1160066')
}

function matchJFile(expect: jest.Expect, file: Filesystem) {
expect(file.filename).toEqual('jay.js')
expect(file.ext).toEqual('js')
expect(file.mediumType).toEqual('application')
expect(file.name).toEqual('jay')
}

test('* GET fixtures has test files', async () => {
await testApiHandler({
handler,
Expand All @@ -42,7 +49,8 @@ describe('Filesystem API', () => {

expect(response.status).toBe(200)

matchFile(expect, result.files[2])
matchPFile(expect, result.files[1])
matchJFile(expect, result.files[0])
expect(result.files.length).toEqual(3)
},
params: { path: 'test/fixtures/walkable' },
Expand All @@ -58,7 +66,7 @@ describe('Filesystem API', () => {

expect(response.status).toBe(200)

matchFile(expect, result.files[0])
matchPFile(expect, result.files[0])
expect(result.files.length).toEqual(1)
},
params: { path: 'test/fixtures/walk%20able' },
Expand Down
3 changes: 2 additions & 1 deletion next/src/components/Walk/ListFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Link from '../Link'

function ListFile({ item: file }: { item: ItemFile }) {
if (file.mediumType === 'folder') {
const href = file.path ? `#path=${file.path}` : ''
return (
<ListItem>
<Link href={`#path=${file.path}`}>{file.label}</Link>
<Link href={href}>{file.label}</Link>
</ListItem>
)
}
Expand Down
15 changes: 14 additions & 1 deletion next/src/components/Walk/__tests__/ListFile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('<ListFile />', () => {
const { getByText } = render(<ListFile item={{ ...mockItemFile, label }} />)
expect(getByText(label)).toBeInTheDocument()
})
test('should render a folder', () => {
test('should render a folder with path', () => {
const path = 'testPath'
const label = 'Link text'
const { getByText } = render(<ListFile
Expand All @@ -27,4 +27,17 @@ describe('<ListFile />', () => {
/>)
expect(getByText(label).closest('a')?.href).toBe(`http://localhost/#path=${path}`)
})
test('should render a folder with path', () => {
const path = ''
const label = 'Link text'
const { getByText } = render(<ListFile
item={{
...mockItemFile,
mediumType: 'folder',
path,
label,
}}
/>)
expect(getByText(label).closest('a')?.href).toBe('http://localhost/')
})
})
10 changes: 1 addition & 9 deletions next/src/lib/filesystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ async function get(
ext: fileExt,
name: fileName,
}
}).sort((a, b) => {
if (a.name < b.name) {
return -1
}
if (a.name > b.name) {
return 1
}
return 0
})
}).sort((a, b) => a.name.localeCompare(b.name))

const body = { files: webPaths, destinationPath }
if (returnEnvelope) {
Expand Down
2 changes: 1 addition & 1 deletion next/src/utils/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function parseHash(find: 'path', from: string) {
return found?.[2] ?? null
}

export function addParentDirectoryNav(itemFiles: ItemFile[], path: string) {
export function addParentDirectoryNav(itemFiles: ItemFile[], path: string | null) {
const file: ItemFile = {
path: 'harddrive',
filename: '..',
Expand Down

0 comments on commit 9e92867

Please sign in to comment.