-
-
Notifications
You must be signed in to change notification settings - Fork 10
Updated Project view to highlight active project #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,16 +1,63 @@ | ||||||||
(function () { | ||||||||
const vscode = acquireVsCodeApi(); | ||||||||
const tree = document.querySelector('#project-tree'); | ||||||||
let prevData = []; | ||||||||
|
||||||||
window.addEventListener('message', event => { | ||||||||
const message = event.data; // The JSON data our extension sent | ||||||||
tree.data = message; | ||||||||
// Keep a deep copy to restore open states when needed | ||||||||
try { | ||||||||
prevData = JSON.parse(JSON.stringify(message)); | ||||||||
} catch { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The empty catch block silently ignores errors without logging or handling them. Consider logging the error or providing a more specific fallback behavior to aid in debugging.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
prevData = message; | ||||||||
} | ||||||||
}); | ||||||||
|
||||||||
function findProjectItemByName(data, name) { | ||||||||
if (!Array.isArray(data)) { | ||||||||
return undefined; | ||||||||
} | ||||||||
return data.find(item => item && item.value && item.value.project === name); | ||||||||
} | ||||||||
|
||||||||
function getActiveProjectName(data) { | ||||||||
if (!Array.isArray(data)) { | ||||||||
return undefined; | ||||||||
} | ||||||||
const active = data.find(item => item && item.selected); | ||||||||
return active && active.value ? active.value.project : undefined; | ||||||||
} | ||||||||
|
||||||||
function revertOpenStateIfNeeded(selectedProject) { | ||||||||
const activeProject = getActiveProjectName(prevData); | ||||||||
if (activeProject === selectedProject) { | ||||||||
return false; | ||||||||
} | ||||||||
const currItem = findProjectItemByName(tree.data, selectedProject); | ||||||||
const prevItem = findProjectItemByName(prevData, selectedProject); | ||||||||
if (currItem && prevItem && typeof prevItem.open === 'boolean') { | ||||||||
currItem.open = prevItem.open; | ||||||||
tree.data = [...tree.data]; | ||||||||
} | ||||||||
return true; | ||||||||
} | ||||||||
|
||||||||
tree.addEventListener('vsc-select', (event) => { | ||||||||
vscode.postMessage({ command: event.detail.value.cmd ? event.detail.value.cmd : "setActive", value: event.detail.value, treeData: tree.data }); | ||||||||
const val = event.detail.value; | ||||||||
const isProjectClick = val && val.project && !val.build && !val.runner && !val.test && !val.cmd; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The boolean logic for detecting project clicks is complex and fragile. Consider extracting this into a named function like 'isProjectSelection(val)' to improve readability and maintainability. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
if (isProjectClick) { | ||||||||
const selectedProject = val.project; | ||||||||
if (revertOpenStateIfNeeded(selectedProject)) { | ||||||||
vscode.postMessage({ command: "setActive", value: val, treeData: tree.data }); | ||||||||
return; | ||||||||
} | ||||||||
} | ||||||||
vscode.postMessage({ command: val && val.cmd ? val.cmd : "setActive", value: val, treeData: tree.data }); | ||||||||
}); | ||||||||
|
||||||||
// No additional toggle listeners needed; we already revert open-state on project click when switching active. | ||||||||
|
||||||||
tree.addEventListener("vsc-run-action", (event) => { | ||||||||
vscode.postMessage({ command: event.detail.actionId, value: event.detail.value, treeData: tree.data }); | ||||||||
}); | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using JSON.parse(JSON.stringify()) for deep cloning is inefficient and can fail with circular references or non-serializable data. Consider using a proper deep clone utility or structured cloning if available.
Copilot uses AI. Check for mistakes.