-
Notifications
You must be signed in to change notification settings - Fork 128
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
refactor: Js to Ts : src/simulator/src/hotkey_binder/view/panel.ui.js #429
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request involves the deletion of the Changes
Assessment against linked issues
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
src/simulator/src/hotkey_binder/view/panel.ui.ts (2)
44-57
: Improve type safety in DOM manipulation.Replace type assertions with proper type guards using
instanceof
.export const override = (combo: string): void => { const preferenceContainer = document.getElementById('preference') if (!preferenceContainer) return const children = preferenceContainer.children for (let x = 0; x < children.length; x++) { - const child = children[x] as HTMLElement - const valueElement = child.querySelector('.key-value') as HTMLElement + const child = children[x] + if (!(child instanceof HTMLElement)) continue + + const valueElement = child.querySelector('.key-value') + if (!(valueElement instanceof HTMLElement)) continue - if (valueElement && valueElement.innerText === combo) { + if (valueElement.innerText === combo) { valueElement.innerText = '' } } }
62-68
: Consider a more descriptive function name.The function name
closeEdit
could be more descriptive of its purpose, such ascloseHotkeyEditInterface
orhideKeyBindingEditor
.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/simulator/src/hotkey_binder/view/panel.ui.js
(0 hunks)src/simulator/src/hotkey_binder/view/panel.ui.ts
(1 hunks)
💤 Files with no reviewable changes (1)
- src/simulator/src/hotkey_binder/view/panel.ui.js
🔇 Additional comments (2)
src/simulator/src/hotkey_binder/view/panel.ui.ts (2)
1-79
: Verify TypeScript configuration and type coverage.Ensure proper TypeScript configuration for strict type checking.
✅ Verification successful
TypeScript configuration and type coverage is properly implemented ✓
The file has proper type safety with strict TypeScript configuration enabled, appropriate type annotations, and no unsafe 'any' types.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Check TypeScript configuration echo "Checking for tsconfig.json..." cat tsconfig.json # Check for any remaining 'any' types echo "Checking for 'any' types..." rg "any" --type ts # Check if all necessary types are imported echo "Checking for missing type imports..." ast-grep --pattern 'import type { $_ } from $_'Length of output: 3827
1-1
: Verify type definition for imported function.Ensure that
setUserKeys
has proper TypeScript type definitions in its source file.✅ Verification successful
JavaScript import is acceptable during TypeScript migration
The import of
setUserKeys
from a .js file is valid during the gradual migration to TypeScript. While the function lacks explicit type definitions, TypeScript can still import and use it. Consider convertingactions.js
to TypeScript in a future iteration to improve type safety.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Check if setUserKeys has type definitions ast-grep --pattern 'export const setUserKeys = $_: $_ => $_'Length of output: 62
Script:
#!/bin/bash # Try multiple patterns to find setUserKeys definition echo "=== Searching for setUserKeys export ===" ast-grep --pattern 'export $_ setUserKeys' echo -e "\n=== Searching for type declarations ===" ast-grep --pattern 'type $_setUserKeys$_ = $_' echo -e "\n=== Searching for implementation ===" rg "setUserKeys" -A 3 src/simulator/src/hotkey_binder/model/actions.tsLength of output: 528
Script:
#!/bin/bash # Find potential action files echo "=== Looking for action files ===" fd "actions" --type f echo -e "\n=== Searching for setUserKeys in all files ===" rg "setUserKeys" --type ts --type js -lLength of output: 428
Script:
#!/bin/bash echo "=== Checking actions.js implementation ===" cat src/simulator/src/hotkey_binder/model/actions.jsLength of output: 7326
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/simulator/src/hotkey_binder/view/panel.ui.ts (1)
44-57
: Improve type safety in DOM operations.The function uses type assertions and lacks proper null checks for querySelector operations.
export const override = (combo: string): void => { const preferenceContainer = document.getElementById('preference') if (!preferenceContainer) return - const children = preferenceContainer.children - for (let x = 0; x < children.length; x++) { - const child = children[x] as HTMLElement - const valueElement = child.querySelector('.key-value') as HTMLElement - - if (valueElement && valueElement.innerText === combo) { + Array.from(preferenceContainer.children).forEach(child => { + if (!(child instanceof HTMLElement)) return + + const valueElement = child.querySelector('.key-value') + if (valueElement instanceof HTMLElement && + valueElement.innerText === combo) { valueElement.innerText = '' } - } + }) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/simulator/src/hotkey_binder/view/panel.ui.ts
(1 hunks)
🔇 Additional comments (3)
src/simulator/src/hotkey_binder/view/panel.ui.ts (3)
7-38
: Previous review comment about localStorage API and error handling still applies.Reduce code duplication and improve type safety.
The function has duplicated code blocks for user and default modes. Consider refactoring to reduce duplication and improve type safety.
export const updateHTML = (mode: 'user' | 'default'): void => { const preferenceContainer = document.getElementById('preference') if (!preferenceContainer) return - if (mode === 'user') { - const userKeys = localStorage.get('userKeys') as Record<string, string> - const children = preferenceContainer.children - - for (let x = 0; x < children.length; x++) { - const child = children[x] as HTMLElement - const keyElement = child.querySelector('.key-name') as HTMLElement - const valueElement = child.querySelector('.key-value') as HTMLElement - - if (keyElement && valueElement) { - valueElement.innerText = userKeys[keyElement.innerText] || '' - } - } - } else if (mode === 'default') { - const defaultKeys = localStorage.get('defaultKeys') as Record<string, string> - const children = preferenceContainer.children - - for (let x = 0; x < children.length; x++) { - const child = children[x] as HTMLElement - const keyElement = child.querySelector('.key-name') as HTMLElement - const valueElement = child.querySelector('.key-value') as HTMLElement - - if (keyElement && valueElement) { - valueElement.innerText = defaultKeys[keyElement.innerText] || '' - } - } - } + const storageKey = `${mode}Keys` + let keys: Record<string, string> = {} + + try { + const storedData = localStorage.getItem(storageKey) + if (storedData) { + keys = JSON.parse(storedData) + } + } catch (error) { + console.error(`Failed to parse ${storageKey}:`, error) + return + } + + Array.from(preferenceContainer.children).forEach(child => { + if (!(child instanceof HTMLElement)) return + + const keyElement = child.querySelector('.key-name') + const valueElement = child.querySelector('.key-value') + + if (keyElement instanceof HTMLElement && + valueElement instanceof HTMLElement) { + valueElement.innerText = keys[keyElement.innerText] || '' + } + }) }
62-68
: Implementation looks good!The function has proper null checks and clear, focused functionality.
73-84
: Previous review comment about error handling has been addressed.Implement user feedback for error cases.
The TODO comment about adding user feedback should be addressed to improve user experience.
export const submit = async (): Promise<void> => { const editElement = document.getElementById('edit') if (editElement) editElement.style.display = 'none' try { await setUserKeys() updateHTML('user') } catch (error) { console.error('Failed to save user keys:', error) - // TODO: Add user feedback for failure case + const errorElement = document.getElementById('error-message') + if (errorElement instanceof HTMLElement) { + errorElement.textContent = 'Failed to save hotkey configuration' + errorElement.style.display = 'block' + setTimeout(() => { + errorElement.style.display = 'none' + }, 3000) + } } }
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/simulator/src/hotkey_binder/view/panel.ui.ts (3)
8-16
: Enhance error handling specificity.Consider differentiating between different types of errors (e.g., storage access vs JSON parsing) for better debugging and monitoring.
const getKeysFromStorage = (storageKey: string): Record<string, string> => { try { const storedData = localStorage.getItem(storageKey) return storedData ? JSON.parse(storedData) : {} - } catch (error) { - console.error(`Failed to parse ${storageKey} from localStorage:`, error) + } catch (error: unknown) { + if (error instanceof SyntaxError) { + console.error(`Invalid JSON in ${storageKey}:`, error) + } else { + console.error(`Failed to access ${storageKey} from localStorage:`, error) + } return {} } }
58-63
: Use type guard instead of type assertion.Replace the type assertion with a proper type guard for better type safety.
- Array.from(preferenceContainer.children).forEach((child) => { - const valueElement = (child as HTMLElement).querySelector('.key-value') - if (valueElement instanceof HTMLElement && valueElement.innerText === combo) { - valueElement.innerText = '' - } - }) + Array.from(preferenceContainer.children).forEach((child) => { + if (child instanceof HTMLElement) { + const valueElement = child.querySelector('.key-value') + if (valueElement instanceof HTMLElement && valueElement.innerText === combo) { + valueElement.innerText = '' + } + } + })
81-92
: Add cleanup for setTimeout to prevent potential memory leaks.The error message timeout should be cleared if the component is unmounted before the timeout completes.
const showErrorMessage = (message: string): void => { const errorMessageElement = document.getElementById('error-message') if (!errorMessageElement) return errorMessageElement.textContent = message errorMessageElement.style.display = 'block' - // Automatically hide error after 5 seconds - setTimeout(() => { + const timeoutId = setTimeout(() => { errorMessageElement.style.display = 'none' }, 5000) + + // Cleanup timeout if error element is removed + const observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + mutation.removedNodes.forEach((node) => { + if (node === errorMessageElement) { + clearTimeout(timeoutId) + observer.disconnect() + } + }) + }) + }) + observer.observe(errorMessageElement.parentElement || document.body, { childList: true }) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/simulator/src/hotkey_binder/view/panel.ui.ts
(1 hunks)
🔇 Additional comments (3)
src/simulator/src/hotkey_binder/view/panel.ui.ts (3)
23-30
: LGTM! Well-structured utility function.The function has proper type checking, safe DOM operations, and clear documentation.
36-48
: Great improvements in code organization and type safety!The refactored version effectively addresses the issues raised in previous reviews:
- Proper type safety with TypeScript
- Safe DOM traversal using Array.from
- Modular design with separate utility functions
97-108
: Excellent error handling implementation!The async implementation with proper error handling and user feedback addresses the concerns raised in previous reviews.
Fixes #414
cc @niladrix719
The netlify link generated by netlify bot wont work, since many files are inter-related with JS specifying the format as .js while calling different files within themselves, just checking one file will throw error since the new format is .ts !
Summary by CodeRabbit