2024-05-26 12:18:09 -07:00
|
|
|
import { type ClassValue, clsx } from "clsx"
|
2024-04-30 01:56:43 -04:00
|
|
|
// import { toast } from "sonner"
|
2024-05-26 12:18:09 -07:00
|
|
|
import { twMerge } from "tailwind-merge"
|
2024-09-05 13:30:24 +01:00
|
|
|
import fileExtToLang from "./file-extension-to-language.json"
|
2024-11-17 13:25:16 -05:00
|
|
|
import { TFile, TFolder } from "./types"
|
2024-05-12 22:06:11 -07:00
|
|
|
|
2024-04-06 19:03:04 -04:00
|
|
|
export function cn(...inputs: ClassValue[]) {
|
2024-05-26 12:18:09 -07:00
|
|
|
return twMerge(clsx(inputs))
|
2024-04-06 19:03:04 -04:00
|
|
|
}
|
2024-04-27 00:28:00 -04:00
|
|
|
|
|
|
|
export function processFileType(file: string) {
|
2024-09-05 13:30:24 +01:00
|
|
|
const extension = file.split(".").pop()
|
|
|
|
const fileExtToLangMap = fileExtToLang as Record<string, string>
|
|
|
|
if (extension && fileExtToLangMap[extension]) {
|
|
|
|
return fileExtToLangMap[extension]
|
|
|
|
}
|
2024-04-27 00:28:00 -04:00
|
|
|
|
2024-05-26 12:18:09 -07:00
|
|
|
return "plaintext"
|
2024-04-27 00:28:00 -04:00
|
|
|
}
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
export function validateName(
|
|
|
|
newName: string,
|
|
|
|
oldName: string,
|
|
|
|
type: "file" | "folder"
|
|
|
|
) {
|
2024-05-05 12:55:34 -07:00
|
|
|
if (newName === oldName || newName.length === 0) {
|
2024-05-26 12:18:09 -07:00
|
|
|
return { status: false, message: "" }
|
2024-05-05 12:55:34 -07:00
|
|
|
}
|
2024-04-29 00:50:25 -04:00
|
|
|
if (
|
|
|
|
newName.includes("/") ||
|
|
|
|
newName.includes("\\") ||
|
|
|
|
newName.includes(" ") ||
|
|
|
|
(type === "file" && !newName.includes(".")) ||
|
|
|
|
(type === "folder" && newName.includes("."))
|
|
|
|
) {
|
2024-05-26 12:18:09 -07:00
|
|
|
return { status: false, message: "Invalid file name." }
|
2024-04-29 00:50:25 -04:00
|
|
|
}
|
2024-05-26 12:18:09 -07:00
|
|
|
return { status: true, message: "" }
|
2024-04-29 00:50:25 -04:00
|
|
|
}
|
2024-05-11 18:03:42 -07:00
|
|
|
|
2024-09-05 13:30:24 +01:00
|
|
|
export function debounce<T extends (...args: any[]) => void>(
|
|
|
|
func: T,
|
|
|
|
wait: number
|
|
|
|
): T {
|
|
|
|
let timeout: NodeJS.Timeout | null = null
|
2024-06-28 02:39:03 -04:00
|
|
|
return function (...args: Parameters<T>) {
|
2024-09-05 13:30:24 +01:00
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
}
|
|
|
|
timeout = setTimeout(() => func(...args), wait)
|
|
|
|
} as T
|
|
|
|
}
|
2024-09-24 13:00:49 +01:00
|
|
|
|
|
|
|
// Deep merge utility function
|
|
|
|
export const deepMerge = (target: any, source: any) => {
|
|
|
|
const output = { ...target }
|
|
|
|
if (isObject(target) && isObject(source)) {
|
|
|
|
Object.keys(source).forEach((key) => {
|
|
|
|
if (isObject(source[key])) {
|
|
|
|
if (!(key in target)) {
|
|
|
|
Object.assign(output, { [key]: source[key] })
|
|
|
|
} else {
|
|
|
|
output[key] = deepMerge(target[key], source[key])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object.assign(output, { [key]: source[key] })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
const isObject = (item: any) => {
|
|
|
|
return item && typeof item === "object" && !Array.isArray(item)
|
|
|
|
}
|
2024-10-14 12:09:17 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|