* fix(file-explorer): sort numbered file names naturally The File Explorer compared names with bare localeCompare, so numbered files listed 100, 200 before 99. Hoist the numeric collator Source Control file rows already use (#10850) into src/shared and apply it to the local and runtime directory listings, the name-filtered view, and Source Control directory nodes, which were inconsistent with the file rows one line below (#11426). * fix(file-explorer): natural sort on SSH funnels, relay, and pickers Adversarial-review round 1 rework: - Both readDir funnels short-circuited to the SSH filesystem provider before the patched sort, so SSH workspaces kept lexicographic order; re-sort locally after the provider returns (the remote relay may be an older build), and fix the relay's own comparator for relay-native consumers. - sortDirEntries (shared, unit-tested) owns the directories-first + natural-order listing contract used by every funnel. - compareFileNames breaks numeric-collation ties ('2' vs '02') by code units so sibling order stays total instead of readdir order, and pins the collator locale to 'en' so every host produces one order. - The SSH folder browser and runtime server dir picker now match the Explorer they browse into. - Ordering pinned by tests at the relay, source-control tree, and shared helper. * fix(mobile): natural sort in the mobile file explorer Mobile re-sorted host readDir results with bare localeCompare, undoing the host funnel's natural order (round-2 review). Reuse the shared comparator and pin the order in the mobile suite. * fix(file-explorer): natural sort at the renderer choke point and remaining ties Round-3 review: the remote-runtime RPC and paired-web routes return the host's order verbatim, so re-sort in readFileExplorerDirectory where every desktop route converges; pin the SSH funnel with a handler-level test; and route Source Control path compares through compareFileNames so numeric-collation ties share one total order with the Explorer. * docs(file-name-sort): state the real perf baseline in the hoist comment * refactor(source-control): drop the dead collator export; pin the test oracle locale * fix(file-listings): cover remaining natural-sort surfaces
154 lines
4.1 KiB
TypeScript
154 lines
4.1 KiB
TypeScript
// Pure tree projection for the mobile file explorer. Mobile mirrors desktop
|
|
// browse semantics by flattening cached files.readDir results as folders open.
|
|
import { compareFileNames } from '../../../src/shared/file-name-sort'
|
|
|
|
export type MobileDirEntry = {
|
|
name: string
|
|
isDirectory: boolean
|
|
isSymlink?: boolean
|
|
}
|
|
|
|
export type MobileFileKind = 'text' | 'binary'
|
|
|
|
export type TreeNode = {
|
|
id: string
|
|
name: string
|
|
relativePath: string
|
|
depth: number
|
|
kind: 'directory' | MobileFileKind
|
|
isSymlink?: boolean
|
|
}
|
|
|
|
export type DirectoryState = {
|
|
entries: MobileDirEntry[]
|
|
loading?: boolean
|
|
error?: string
|
|
}
|
|
|
|
export type DirectoryCache = Record<string, DirectoryState | undefined>
|
|
|
|
export type InlineStatusNode = {
|
|
id: string
|
|
relativePath: string
|
|
depth: number
|
|
kind: 'loading' | 'error'
|
|
message?: string
|
|
}
|
|
|
|
export type FileExplorerRow = TreeNode | InlineStatusNode
|
|
|
|
const DESKTOP_EXCLUDED_NAMES = new Set(['.git', 'node_modules'])
|
|
const BINARY_EXTENSIONS = new Set([
|
|
'.avif',
|
|
'.bmp',
|
|
'.gif',
|
|
'.heic',
|
|
'.ico',
|
|
'.jpeg',
|
|
'.jpg',
|
|
'.mov',
|
|
'.mp3',
|
|
'.mp4',
|
|
'.pdf',
|
|
'.png',
|
|
'.webp',
|
|
'.zip'
|
|
])
|
|
|
|
export function flattenDirectoryCache(
|
|
cache: DirectoryCache,
|
|
expanded: ReadonlySet<string>
|
|
): FileExplorerRow[] {
|
|
const rows: FileExplorerRow[] = []
|
|
visitDirectory('', 0, cache, expanded, rows)
|
|
return rows
|
|
}
|
|
|
|
function visitDirectory(
|
|
relativePath: string,
|
|
depth: number,
|
|
cache: DirectoryCache,
|
|
expanded: ReadonlySet<string>,
|
|
rows: FileExplorerRow[]
|
|
): void {
|
|
const state = getDirectoryCacheState(cache, relativePath)
|
|
const entries = state?.entries ?? []
|
|
const visibleEntries = entries
|
|
.filter(shouldIncludeMobileFileExplorerEntry)
|
|
.sort(compareDirectoryEntries)
|
|
|
|
for (const entry of visibleEntries) {
|
|
const childPath = joinRelativePath(relativePath, entry.name)
|
|
rows.push(toTreeNode(entry, childPath, depth))
|
|
if (entry.isDirectory && expanded.has(childPath)) {
|
|
const childState = getDirectoryCacheState(cache, childPath)
|
|
if (childState?.loading) {
|
|
rows.push({
|
|
id: `loading:${childPath}`,
|
|
relativePath: childPath,
|
|
depth: depth + 1,
|
|
kind: 'loading'
|
|
})
|
|
} else if (childState?.error) {
|
|
rows.push({
|
|
id: `error:${childPath}`,
|
|
relativePath: childPath,
|
|
depth: depth + 1,
|
|
kind: 'error',
|
|
message: childState.error
|
|
})
|
|
} else {
|
|
visitDirectory(childPath, depth + 1, cache, expanded, rows)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function toTreeNode(entry: MobileDirEntry, relativePath: string, depth: number): TreeNode {
|
|
return {
|
|
id: `${entry.isDirectory ? 'dir' : 'file'}:${relativePath}`,
|
|
name: entry.name,
|
|
relativePath,
|
|
depth,
|
|
kind: entry.isDirectory ? 'directory' : getMobileFileKind(relativePath),
|
|
isSymlink: entry.isSymlink
|
|
}
|
|
}
|
|
|
|
function compareDirectoryEntries(a: MobileDirEntry, b: MobileDirEntry): number {
|
|
if (a.isDirectory !== b.isDirectory) {
|
|
return a.isDirectory ? -1 : 1
|
|
}
|
|
return compareFileNames(a.name, b.name)
|
|
}
|
|
|
|
export function shouldIncludeMobileFileExplorerEntry(entry: MobileDirEntry): boolean {
|
|
return !DESKTOP_EXCLUDED_NAMES.has(entry.name)
|
|
}
|
|
|
|
export function getDirectoryCacheState(
|
|
cache: DirectoryCache,
|
|
relativePath: string
|
|
): DirectoryState | undefined {
|
|
// Why: repository paths are arbitrary object keys; inherited keys like
|
|
// "constructor" must not masquerade as loaded directory state.
|
|
return Object.prototype.hasOwnProperty.call(cache, relativePath) ? cache[relativePath] : undefined
|
|
}
|
|
|
|
export function joinRelativePath(parentPath: string, name: string): string {
|
|
return parentPath ? `${parentPath}/${name}` : name
|
|
}
|
|
|
|
export function getMobileFileKind(relativePath: string): MobileFileKind {
|
|
const basename = relativePath.split('/').pop() ?? relativePath
|
|
const dotIndex = basename.lastIndexOf('.')
|
|
if (dotIndex <= 0) {
|
|
return 'text'
|
|
}
|
|
return BINARY_EXTENSIONS.has(basename.slice(dotIndex).toLowerCase()) ? 'binary' : 'text'
|
|
}
|
|
|
|
export function isMarkdownPath(relativePath: string): boolean {
|
|
return /\.(md|mdx|markdown)$/i.test(relativePath)
|
|
}
|