Skip to content

Commit

Permalink
feat(Next > Walk): Add list of root local filesystem with traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Jul 24, 2022
1 parent 02483a6 commit a91fba4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
15 changes: 9 additions & 6 deletions next/pages/admin/walk.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'

import GenericList from '../../src/components/GenericList'
import ListFile from '../../src/components/Walk/ListFile'

import { isImage, organizeByMedia } from '../../src/utils/walk'
import { isImage, parseHash, organizeByMedia } from '../../src/utils/walk'

function WalkPage() {
const { asPath } = useRouter()
const [data, setData] = useState(null)
const [isLoading, setLoading] = useState(false)
const pathQs = parseHash('path', asPath)

useEffect(() => {
setLoading(true)
fetch('/api/admin/filesystems')
.then((res) => res.json())
.then((fetchedData) => {
setData(fetchedData)
fetch(`/api/admin/filesystems?path=${pathQs ?? '/'}`)
.then((response) => response.json())
.then((result) => {
setData(result)
setLoading(false)
})
}, [])
}, [asPath])

if (isLoading) return <p>Loading...</p>
if (!data) return <p>No filesystem data</p>
Expand Down
21 changes: 17 additions & 4 deletions next/src/utils/__tests__/walk.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import util, { isImage } from '../walk'

const {
import {
isImage,
parseHash,
addParentDirectoryNav,
associateMedia,
generateImageFilenames,
isAnyImageOrVideo,
mergeMedia,
getJpgLike,
} = util
} from '../walk'

describe('Walk - util', () => {
describe('isImage', () => {
Expand All @@ -24,6 +24,19 @@ describe('Walk - util', () => {
})
})

describe('parseHash', () => {
test('find 1', () => {
const path = 'dotca'
const received = parseHash('path', `http://localhost#path=${path}`)
expect(received).toBe(path)
})

test('find 0', () => {
const received = parseHash('path', 'http://localhost')
expect(received).toBeNull()
})
})

describe('addParentDirectoryNav', () => {
const file = {
filename: '..',
Expand Down
3 changes: 2 additions & 1 deletion next/src/utils/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function isImage(file) {

export function parseHash(find, from) {
if (!find || !from) return ''
return RegExp(`[#&]${find}(=([^&#]*)|&|#|$)`).exec(from)[2]
const found = RegExp(`[#&]${find}(=([^&#]*)|&|#|$)`).exec(from)
return found?.[2] ?? null
}

export function addParentDirectoryNav(itemFiles, path) {
Expand Down

0 comments on commit a91fba4

Please sign in to comment.