Skip to content

Commit

Permalink
fix: hide matched files/folders in sidebar (#155)
Browse files Browse the repository at this point in the history
* fix: hide matched files/folders in sidebar

* fix: show matched folder while access by url
  • Loading branch information
liuycy authored Mar 10, 2024
1 parent e52e8f4 commit 6f7056e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 56 deletions.
126 changes: 70 additions & 56 deletions src/components/FolderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
createEffect,
on,
} from "solid-js"
import { useFetch, useT } from "~/hooks"
import { useFetch, useT, useUtil } from "~/hooks"
import { getMainColor, password } from "~/store"
import { Obj } from "~/types"
import {
Expand All @@ -49,6 +49,7 @@ export interface FolderTreeProps {
autoOpen?: boolean
handle?: (handler: FolderTreeHandler) => void
showEmptyIcon?: boolean
showHiddenFolder?: boolean
}
interface FolderTreeContext extends Omit<FolderTreeProps, "handle"> {
value: Accessor<string>
Expand All @@ -69,6 +70,7 @@ export const FolderTree = (props: FolderTreeProps) => {
autoOpen: props.autoOpen ?? false,
forceRoot: props.forceRoot ?? false,
showEmptyIcon: props.showEmptyIcon ?? false,
showHiddenFolder: props.showHiddenFolder ?? true,
}}
>
<FolderTreeNode path="/" />
Expand All @@ -78,9 +80,16 @@ export const FolderTree = (props: FolderTreeProps) => {
}

const FolderTreeNode = (props: { path: string }) => {
const { isHidePath } = useUtil()
const [children, setChildren] = createSignal<Obj[]>()
const { value, onChange, forceRoot, autoOpen, showEmptyIcon } =
useContext(context)!
const {
value,
onChange,
forceRoot,
autoOpen,
showEmptyIcon,
showHiddenFolder,
} = useContext(context)!
const emptyIconVisible = () =>
Boolean(showEmptyIcon && children() !== undefined && !children()?.length)
const [loading, fetchDirs] = useFetch(() =>
Expand All @@ -103,71 +112,76 @@ const FolderTreeNode = (props: { path: string }) => {
}
const { isOpen, onToggle } = createDisclosure()
const active = () => value() === props.path
const isMatchedFolder = createMatcher(props.path)
const checkIfShouldOpen = async (pathname: string) => {
if (!autoOpen) return
if (createMatcher(props.path)(pathname)) {
if (isMatchedFolder(pathname)) {
if (!isOpen()) onToggle()
if (!isLoaded) load()
}
}
createEffect(on(value, checkIfShouldOpen))
const isHiddenFolder = () =>
isHidePath(props.path) && !isMatchedFolder(value())
return (
<Box>
<HStack spacing="$2">
<Show
when={!loading()}
fallback={<Spinner size="sm" color={getMainColor()} />}
>
<Show when={showHiddenFolder || !isHiddenFolder()}>
<Box>
<HStack spacing="$2">
<Show
when={!emptyIconVisible()}
fallback={<Icon color={getMainColor()} as={BiSolidFolderOpen} />}
when={!loading()}
fallback={<Spinner size="sm" color={getMainColor()} />}
>
<Icon
color={getMainColor()}
as={BiSolidRightArrow}
transform={isOpen() ? "rotate(90deg)" : "none"}
transition="transform 0.2s"
cursor="pointer"
onClick={() => {
onToggle()
if (isOpen()) {
load()
}
}}
/>
<Show
when={!emptyIconVisible()}
fallback={<Icon color={getMainColor()} as={BiSolidFolderOpen} />}
>
<Icon
color={getMainColor()}
as={BiSolidRightArrow}
transform={isOpen() ? "rotate(90deg)" : "none"}
transition="transform 0.2s"
cursor="pointer"
onClick={() => {
onToggle()
if (isOpen()) {
load()
}
}}
/>
</Show>
</Show>
<Text
css={{
// textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
// overflow="hidden"
fontSize="$md"
cursor="pointer"
px="$1"
rounded="$md"
bgColor={active() ? "$info8" : "transparent"}
_hover={{
bgColor: active() ? "$info8" : hoverColor(),
}}
onClick={() => {
onChange(props.path)
}}
>
{props.path === "/" ? "root" : pathBase(props.path)}
</Text>
</HStack>
<Show when={isOpen()}>
<VStack mt="$1" pl="$4" alignItems="start" spacing="$1">
<For each={children()}>
{(item) => (
<FolderTreeNode path={pathJoin(props.path, item.name)} />
)}
</For>
</VStack>
</Show>
<Text
css={{
// textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
// overflow="hidden"
fontSize="$md"
cursor="pointer"
px="$1"
rounded="$md"
bgColor={active() ? "$info8" : "transparent"}
_hover={{
bgColor: active() ? "$info8" : hoverColor(),
}}
onClick={() => {
onChange(props.path)
}}
>
{props.path === "/" ? "root" : pathBase(props.path)}
</Text>
</HStack>
<Show when={isOpen()}>
<VStack mt="$1" pl="$4" alignItems="start" spacing="$1">
<For each={children()}>
{(item) => (
<FolderTreeNode path={pathJoin(props.path, item.name)} />
)}
</For>
</VStack>
</Show>
</Box>
</Box>
</Show>
)
}

Expand Down
9 changes: 9 additions & 0 deletions src/hooks/useUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const useUtil = () => {
}
return false
},
isHidePath: (path: string) => {
const hideFiles = getHideFiles()
for (const reg of hideFiles) {
if (reg.test(path)) {
return true
}
}
return false
},
}
}

Expand Down
1 change: 1 addition & 0 deletions src/pages/home/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function SidebarPannel() {
<FolderTree
autoOpen
showEmptyIcon
showHiddenFolder={false}
onChange={(path) => to(path)}
handle={(handler) => setFolderTreeHandler(handler)}
/>
Expand Down

0 comments on commit 6f7056e

Please sign in to comment.