Skip to content

Commit c3fb402

Browse files
committed
fix: Buggy root directory Modal
1 parent f62b38d commit c3fb402

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

src/lib/components/git/selectRootModal.svelte

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
];
3838
let currentPath: string = './';
3939
let currentDir: Directory;
40-
export let expanded = writable(['lib-0', 'tree-0']);
40+
export let expanded = writable([]);
41+
let initialized = false;
42+
let initialPath: string = './';
43+
let pathNotFound = false;
4144
4245
onMount(async () => {
4346
try {
@@ -61,13 +64,13 @@
6164
}));
6265
currentDir = directories[0];
6366
isLoading = false;
67+
$expanded = Array.from(new Set([...$expanded, './']));
6468
} catch {
6569
return;
6670
}
6771
});
6872
69-
async function fetchContents(e: CustomEvent) {
70-
const path = e.detail.fullPath as string;
73+
async function loadPath(path: string) {
7174
currentPath = path;
7275
7376
const pathSegments = path.split('/').filter((segment) => segment !== '.' && segment !== '');
@@ -99,6 +102,7 @@
99102
const contentDirectories = content.contents.filter((e) => e.isDirectory);
100103
101104
if (contentDirectories.length === 0) {
105+
$expanded = Array.from(new Set([...$expanded, path]));
102106
return;
103107
}
104108
@@ -133,7 +137,7 @@
133137
});
134138
}
135139
directories = [...directories];
136-
$expanded = [...$expanded, path];
140+
$expanded = Array.from(new Set([...$expanded, path]));
137141
} catch (error) {
138142
console.error(error);
139143
} finally {
@@ -142,6 +146,60 @@
142146
}
143147
}
144148
149+
async function fetchContents(e: CustomEvent) {
150+
const path = e.detail.fullPath as string;
151+
await loadPath(path);
152+
}
153+
154+
function normalizePath(path: string): string {
155+
if (!path || path === '') return './';
156+
if (path === './') return './';
157+
if (path.startsWith('./')) return path.replace(/\/$/, '');
158+
if (path.startsWith('/')) return '.' + path.replace(/\/$/, '');
159+
return './' + path.replace(/\/$/, '');
160+
}
161+
162+
async function expandToPath(path: string) {
163+
pathNotFound = false;
164+
const normalized = normalizePath(path);
165+
const segments = normalized.split('/').filter((s) => s !== '.' && s !== '');
166+
let cumulative = './';
167+
$expanded = Array.from(new Set([...$expanded, './']));
168+
for (const segment of segments) {
169+
cumulative = cumulative === './' ? `./${segment}` : `${cumulative}/${segment}`;
170+
const parentSegments = cumulative.split('/').filter((s) => s !== '.' && s !== '');
171+
let cursor = directories[0];
172+
for (const s of parentSegments.slice(0, -1)) {
173+
const next = cursor.children?.find((d) => d.title === s);
174+
if (!next) {
175+
pathNotFound = true;
176+
return;
177+
}
178+
cursor = next;
179+
}
180+
await loadPath(cursor.fullPath ?? './');
181+
const exists = cursor.children?.some((d) => d.title === segment);
182+
if (!exists) {
183+
pathNotFound = true;
184+
return;
185+
}
186+
$expanded = Array.from(new Set([...$expanded, cumulative]));
187+
}
188+
currentPath = normalized;
189+
}
190+
191+
$: if (show && !initialized && !isLoading) {
192+
initialized = true;
193+
initialPath = normalizePath(rootDir ?? './');
194+
currentPath = initialPath;
195+
expandToPath(initialPath);
196+
}
197+
198+
$: if (!show && initialized) {
199+
initialized = false;
200+
pathNotFound = false;
201+
}
202+
145203
function handleSubmit() {
146204
rootDir = currentPath;
147205
show = false;
@@ -156,6 +214,6 @@
156214

157215
<svelte:fragment slot="footer">
158216
<Button secondary on:click={() => (show = false)}>Cancel</Button>
159-
<Button submit disabled={isLoading}>Save</Button>
217+
<Button submit disabled={isLoading || currentPath === initialPath}>Save</Button>
160218
</svelte:fragment>
161219
</Modal>

0 commit comments

Comments
 (0)