Skip to content

Commit 12716f7

Browse files
duranbAaronPlave
authored andcommitted
update actions list when selected file updates
refactor for consistency
1 parent bf7df4b commit 12716f7

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

src/routes/workspaces/[workspaceId]/+page.svelte

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -106,62 +106,23 @@
106106
}
107107
108108
$: if (!isWorkspaceLoading && selectedFilePath !== activeFilePath) {
109-
console.log(`reactive nav! active: ${activeFilePath}, selected: ${selectedFilePath}`);
110109
// the UI's selected file doesn't match our actively loaded file, try to navigate to selected
111110
maybeNavigate(selectedFilePath);
112111
}
113112
114-
async function maybeNavigate(nextPath: string | null) {
115-
// don't navigate if the selected path is a text file and not a folder or binary
116-
// treat `null` as a navigable path so we can intentionally unload the editor file rather than skipping
117-
const isNavigableFileOrNull = (nextPath && isNavigableFile(workspaceTreeMap[nextPath]?.type)) || nextPath === null;
118-
if (!isNavigableFileOrNull) {
119-
// wait a tick then revert selected UI to the existing active path
120-
await tick();
121-
selectedFilePath = activeFilePath;
122-
return;
123-
}
124-
125-
const didNavigate = await goToSequence(nextPath);
126-
if (!didNavigate) {
127-
// user decided not to navigate away due to unsaved changes, set selected UI back to active file
128-
selectedFilePath = activeFilePath;
129-
return;
130-
}
131-
// successfully navigated, update activeFilePath & get the file contents
132-
activeFilePath = nextPath;
133-
if (activeFilePath) {
134-
const { filename } = separateFilenameFromPath(activeFilePath);
135-
getSelectedFileContent(activeFilePath);
136-
availableActionsForActiveFile = getAvailableActionsForNodes(allActionsForWorkspace, [
137-
workspaceTreeMap[activeFilePath],
138-
]);
139-
140-
if (filename) {
141-
selectedFileName = filename;
142-
selectedFileType = workspaceTreeMap[activeFilePath]?.type ?? null;
143-
} else {
144-
selectedFileName = undefined;
145-
selectedFileType = null;
146-
}
147-
} else {
148-
// navigated to a null/empty file, reset the editor contents
149-
initialSelectedFileContent = '';
150-
updatedSelectedFileContent = initialSelectedFileContent;
151-
selectedFileName = undefined;
152-
selectedFileType = null;
153-
}
154-
}
155-
156113
$: if (initialWorkspace || $workspace) {
157114
const ws: Workspace = $workspace ?? (initialWorkspace as Workspace);
158115
159116
hasEditWorkspacePermission = featurePermissions.workspace.canUpdate(user, ws);
160117
hasEditWorkspaceCollaboratorsPermission = featurePermissions.workspaceCollaborators.canCreate(user, ws);
161-
if (activeFilePath) {
118+
if (activeFilePath && workspaceTreeMap[activeFilePath]) {
162119
hasEditFilePermission = featurePermissions.workspace.canUpdate(user, ws, workspaceTreeMap[activeFilePath]);
120+
availableActionsForActiveFile = getAvailableActionsForNodes(allActionsForWorkspace, [
121+
workspaceTreeMap[activeFilePath],
122+
]);
163123
} else {
164124
hasEditFilePermission = featurePermissions.workspace.canUpdate(user, ws);
125+
availableActionsForActiveFile = [];
165126
}
166127
}
167128
@@ -216,6 +177,45 @@
216177
}
217178
}
218179
180+
async function maybeNavigate(nextPath: string | null) {
181+
// don't navigate if the selected path is a text file and not a folder or binary
182+
// treat `null` as a navigable path so we can intentionally unload the editor file rather than skipping
183+
const isNavigableFileOrNull = (nextPath && isNavigableFile(workspaceTreeMap[nextPath]?.type)) || nextPath === null;
184+
if (!isNavigableFileOrNull) {
185+
// wait a tick then revert selected UI to the existing active path
186+
await tick();
187+
selectedFilePath = activeFilePath;
188+
return;
189+
}
190+
191+
const didNavigate = await goToSequence(nextPath);
192+
if (!didNavigate) {
193+
// user decided not to navigate away due to unsaved changes, set selected UI back to active file
194+
selectedFilePath = activeFilePath;
195+
return;
196+
}
197+
// successfully navigated, update activeFilePath & get the file contents
198+
activeFilePath = nextPath;
199+
if (activeFilePath) {
200+
const { filename } = separateFilenameFromPath(activeFilePath);
201+
getSelectedFileContent(activeFilePath);
202+
203+
if (filename) {
204+
selectedFileName = filename;
205+
selectedFileType = workspaceTreeMap[activeFilePath]?.type ?? null;
206+
} else {
207+
selectedFileName = undefined;
208+
selectedFileType = null;
209+
}
210+
} else {
211+
// navigated to a null/empty file, reset the editor contents
212+
initialSelectedFileContent = '';
213+
updatedSelectedFileContent = initialSelectedFileContent;
214+
selectedFileName = undefined;
215+
selectedFileType = null;
216+
}
217+
}
218+
219219
function resetRefreshInterval() {
220220
if (refreshInterval !== null) {
221221
clearInterval(refreshInterval);

0 commit comments

Comments
 (0)