feat: sort files in explorer
This commit is contained in:
parent
fa5d1e9a57
commit
6e14f676cf
@ -11,7 +11,7 @@ import {
|
|||||||
import SidebarFile from "./file"
|
import SidebarFile from "./file"
|
||||||
import SidebarFolder from "./folder"
|
import SidebarFolder from "./folder"
|
||||||
import { Sandbox, TFile, TFolder, TTab } from "@/lib/types"
|
import { Sandbox, TFile, TFolder, TTab } from "@/lib/types"
|
||||||
import { useEffect, useRef, useState } from "react"
|
import { useEffect, useMemo, useRef, useState } from "react"
|
||||||
import New from "./new"
|
import New from "./new"
|
||||||
import { Socket } from "socket.io-client"
|
import { Socket } from "socket.io-client"
|
||||||
import { Switch } from "@/components/ui/switch"
|
import { Switch } from "@/components/ui/switch"
|
||||||
@ -22,6 +22,7 @@ import {
|
|||||||
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
|
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
|
||||||
import Button from "@/components/ui/customButton"
|
import Button from "@/components/ui/customButton"
|
||||||
import { Skeleton } from "@/components/ui/skeleton"
|
import { Skeleton } from "@/components/ui/skeleton"
|
||||||
|
import { sortFileExplorer } from "@/lib/utils"
|
||||||
|
|
||||||
export default function Sidebar({
|
export default function Sidebar({
|
||||||
sandboxData,
|
sandboxData,
|
||||||
@ -55,7 +56,9 @@ export default function Sidebar({
|
|||||||
|
|
||||||
const [creatingNew, setCreatingNew] = useState<"file" | "folder" | null>(null)
|
const [creatingNew, setCreatingNew] = useState<"file" | "folder" | null>(null)
|
||||||
const [movingId, setMovingId] = useState("")
|
const [movingId, setMovingId] = useState("")
|
||||||
console.log(files)
|
const sortedFiles = useMemo(() => {
|
||||||
|
return sortFileExplorer(files)
|
||||||
|
}, [files])
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = ref.current
|
const el = ref.current
|
||||||
|
|
||||||
@ -136,7 +139,7 @@ export default function Sidebar({
|
|||||||
isDraggedOver ? "bg-secondary/50" : ""
|
isDraggedOver ? "bg-secondary/50" : ""
|
||||||
} rounded-sm w-full mt-1 flex flex-col`}
|
} rounded-sm w-full mt-1 flex flex-col`}
|
||||||
> */}
|
> */}
|
||||||
{files.length === 0 ? (
|
{sortedFiles.length === 0 ? (
|
||||||
<div className="w-full flex flex-col justify-center">
|
<div className="w-full flex flex-col justify-center">
|
||||||
{new Array(6).fill(0).map((_, i) => (
|
{new Array(6).fill(0).map((_, i) => (
|
||||||
<Skeleton key={i} className="h-[1.625rem] mb-0.5 rounded-sm" />
|
<Skeleton key={i} className="h-[1.625rem] mb-0.5 rounded-sm" />
|
||||||
@ -144,7 +147,7 @@ export default function Sidebar({
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{files.map((child) =>
|
{sortedFiles.map((child) =>
|
||||||
child.type === "file" ? (
|
child.type === "file" ? (
|
||||||
<SidebarFile
|
<SidebarFile
|
||||||
key={child.id}
|
key={child.id}
|
||||||
|
@ -98,3 +98,28 @@ export const deepMerge = (target: any, source: any) => {
|
|||||||
const isObject = (item: any) => {
|
const isObject = (item: any) => {
|
||||||
return item && typeof item === "object" && !Array.isArray(item)
|
return item && typeof item === "object" && !Array.isArray(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sortFileExplorer(
|
||||||
|
items: (TFile | TFolder)[]
|
||||||
|
): (TFile | TFolder)[] {
|
||||||
|
return items
|
||||||
|
.sort((a, b) => {
|
||||||
|
// First, sort by type (folders before files)
|
||||||
|
if (a.type !== b.type) {
|
||||||
|
return a.type === "folder" ? -1 : 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then, sort alphabetically by name
|
||||||
|
return a.name.localeCompare(b.name, undefined, { sensitivity: "base" })
|
||||||
|
})
|
||||||
|
.map((item) => {
|
||||||
|
// If it's a folder, recursively sort its children
|
||||||
|
if (item.type === "folder") {
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
children: sortFileExplorer(item.children),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user