Skip to content

Commit d2a710e

Browse files
committed
Fix path navigation in file browser
1 parent a86166c commit d2a710e

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/common/utils/stringUtils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,23 @@ export function toKebabCase(str: string) {
77
.replace(/^-+|-+$/g, "") // trim leading/trailing -
88
.toLowerCase();
99
}
10+
11+
export function trimStart(input: string, trim: string): string {
12+
if (!trim) return input;
13+
14+
while (input.startsWith(trim)) {
15+
input = input.slice(trim.length);
16+
}
17+
18+
return input;
19+
}
20+
21+
export function trimEnd(input: string, trim: string): string {
22+
if (!trim) return input;
23+
24+
while (input.endsWith(trim)) {
25+
input = input.slice(0, -trim.length);
26+
}
27+
28+
return input;
29+
}

src/hooks/useLocalFSWithHistory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import useLocalFS, {
44
FSObject,
55
isFSDirectory,
66
} from "../stores/localFS";
7+
import { trimEnd } from "../common/utils/stringUtils";
78

89
function useLocalFSWithHistory(currentPath: string) {
910
const history = useRef<string[]>([currentPath]);
@@ -14,7 +15,7 @@ function useLocalFSWithHistory(currentPath: string) {
1415
);
1516

1617
function getDirOrDefault(path: string, defaultValue = "/home/user") {
17-
let dir = fs.getDirectory(path);
18+
let dir = fs.getDirectory(trimEnd(path, fs.separator));
1819
if (!dir) {
1920
// TODO: Some kind of user notification
2021
console.warn("Invalid path");

src/stores/localFS.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,29 @@ export function isFSFile(fsObject: FSObject): fsObject is FSFile {
9797

9898
export interface LocalFSState {
9999
root: FSObject;
100+
separator: string;
100101
getDirectory: (path: string) => FSDirectory | null;
101102
createDirectory: (
102103
name: string,
103-
parentDirectory: FSDirectory
104+
parentDirectory: FSDirectory,
104105
) => FSDirectory | null;
105106
createFile: (
106107
name: string,
107108
parentDirectory: FSDirectory,
108-
contents?: string
109+
contents?: string,
109110
) => FSFile | null;
110111
favorites: Array<string>;
111112
validateFSObjectName: (name: string) => string | null;
112113
fsObjectNameIsAvailable: (name: string, directory: FSDirectory) => boolean;
113114
create: (
114115
type: FSObjectType,
115116
name: string,
116-
parentDirectory: FSDirectory
117+
parentDirectory: FSDirectory,
117118
) => FSObject | null;
118119
rename: (
119120
parentDirectory: FSDirectory,
120121
fsObject: FSObject,
121-
newName: string
122+
newName: string,
122123
) => void;
123124
delete: (parentDirectory: FSDirectory, fsObject: FSObject) => void;
124125
getParentDirectory: (path: string) => FSDirectory | null;
@@ -149,7 +150,7 @@ export const useLocalFS = create<LocalFSState>()(
149150

150151
const createDirectory: LocalFSState["createDirectory"] = (
151152
name,
152-
parentDirectory
153+
parentDirectory,
153154
) => {
154155
if (parentDirectory.contents[name]) return null;
155156

@@ -173,7 +174,7 @@ export const useLocalFS = create<LocalFSState>()(
173174
const createFile: LocalFSState["createFile"] = (
174175
name,
175176
parentDirectory,
176-
contents
177+
contents,
177178
) => {
178179
if (parentDirectory.contents[name]) {
179180
return null;
@@ -198,7 +199,7 @@ export const useLocalFS = create<LocalFSState>()(
198199
};
199200

200201
const validateFSObjectName: LocalFSState["validateFSObjectName"] = (
201-
name
202+
name,
202203
) => {
203204
if (!name) {
204205
return "A value is required";
@@ -221,7 +222,7 @@ export const useLocalFS = create<LocalFSState>()(
221222

222223
const fsObjectNameIsAvailable: LocalFSState["fsObjectNameIsAvailable"] = (
223224
name,
224-
directory
225+
directory,
225226
) => {
226227
return !directory.contents[name];
227228
};
@@ -262,7 +263,7 @@ export const useLocalFS = create<LocalFSState>()(
262263

263264
const deleteFSObject: LocalFSState["delete"] = (
264265
parentDirectory,
265-
fsObject
266+
fsObject,
266267
) => {
267268
delete parentDirectory.contents[fsObject.name];
268269
get().removeFromFavorites(fsObject.path);
@@ -272,7 +273,7 @@ export const useLocalFS = create<LocalFSState>()(
272273
const rename: LocalFSState["rename"] = (
273274
parentDirectory,
274275
fsObject,
275-
newName
276+
newName,
276277
) => {
277278
const oldName = fsObject.name;
278279
const oldPath = fsObject.path;
@@ -326,7 +327,7 @@ export const useLocalFS = create<LocalFSState>()(
326327
set({ favorites });
327328
};
328329
const removeFromFavorites: LocalFSState["removeFromFavorites"] = (
329-
path
330+
path,
330331
) => {
331332
const oldFavorites = get().favorites;
332333
const favorites = [...oldFavorites];
@@ -338,6 +339,7 @@ export const useLocalFS = create<LocalFSState>()(
338339
};
339340
return {
340341
root: rootDir,
342+
separator: pathSeparator,
341343
favorites: [
342344
userDir.path,
343345
documentsDir.path,
@@ -362,8 +364,13 @@ export const useLocalFS = create<LocalFSState>()(
362364
},
363365
{
364366
name: "local-fs",
365-
}
366-
)
367+
partialize: (state) => {
368+
const updated = { ...state } as Partial<LocalFSState>;
369+
delete updated.separator;
370+
return updated;
371+
},
372+
},
373+
),
367374
);
368375

369376
export default useLocalFS;

src/stores/systemSettingsStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const defaultTheme: Theme = {
2121
warningColor: "#ebcb8b",
2222
successColor: "#a3be8c",
2323
background: "https://regolith-linux.org/images/releases/nord-dark.png",
24-
opacity: 0.5,
25-
blur: 5,
24+
opacity: 0.1,
25+
blur: 3,
2626
};
2727

2828
export interface SystemSettingState {

0 commit comments

Comments
 (0)