Skip to content

refactor: ts integration in restrictedElementDiv #471

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions src/simulator/src/restrictedElementDiv.js

This file was deleted.

60 changes: 60 additions & 0 deletions src/simulator/src/restrictedElementDiv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Scope } from './types/restrictedElementDiv.types'

const globalScope: Scope = {
restrictedCircuitElementsUsed: []
};
const restrictedElements: string[] = [];

export function updateRestrictedElementsList(): void {
if (globalScope.restrictedCircuitElementsUsed.length === 0) {
const restrictedElementsDiv = document.getElementById('restrictedElementsDiv--list');
if (restrictedElementsDiv) {
restrictedElementsDiv.innerHTML = 'None';
}
return;
}
const { restrictedCircuitElementsUsed } = globalScope;
const restrictedStr = restrictedCircuitElementsUsed.join(', ');
const restrictedElementsDiv = document.getElementById('restrictedElementsDiv--list');
if (restrictedElementsDiv) {
restrictedElementsDiv.innerHTML = restrictedStr;
} else {
console.error('Element restrictedElementsDiv--list not found');
}
}

export function updateRestrictedElementsInScope(scope: Scope = globalScope): void {
if (restrictedElements.length === 0) return;

const restrictedElementsUsed: string[] = [];
restrictedElements.forEach((element: string) => {
if (scope[element] && scope[element].length > 0) {
restrictedElementsUsed.push(element);
}
});

scope.restrictedCircuitElementsUsed = restrictedElementsUsed;
updateRestrictedElementsList();
}
Comment on lines +26 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve type safety for scope indexing operation.

The code accesses scope[element] as an index, but the TypeScript interface for Scope might not explicitly support this operation, which could lead to type errors.

import { Scope } from './types/restrictedElementDiv.types'

+// Type guard to check if an element exists in scope with valid length
+function isValidElement(scope: Scope, element: string): boolean {
+    return element in scope && 
+           Array.isArray(scope[element as keyof Scope]) && 
+           (scope[element as keyof Scope] as unknown as any[]).length > 0;
+}

export function updateRestrictedElementsInScope(scope: Scope = globalScope): void {
    if (restrictedElements.length === 0) return;

    const restrictedElementsUsed: string[] = [];
    restrictedElements.forEach((element: string) => {
-        if (scope[element] && scope[element].length > 0) {
+        if (isValidElement(scope, element)) {
            restrictedElementsUsed.push(element);
        }
    });

    scope.restrictedCircuitElementsUsed = restrictedElementsUsed;
    updateRestrictedElementsList();
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function updateRestrictedElementsInScope(scope: Scope = globalScope): void {
if (restrictedElements.length === 0) return;
const restrictedElementsUsed: string[] = [];
restrictedElements.forEach((element: string) => {
if (scope[element] && scope[element].length > 0) {
restrictedElementsUsed.push(element);
}
});
scope.restrictedCircuitElementsUsed = restrictedElementsUsed;
updateRestrictedElementsList();
}
import { Scope } from './types/restrictedElementDiv.types'
// Type guard to check if an element exists in scope with valid length
function isValidElement(scope: Scope, element: string): boolean {
return element in scope &&
Array.isArray(scope[element as keyof Scope]) &&
(scope[element as keyof Scope] as unknown as any[]).length > 0;
}
export function updateRestrictedElementsInScope(scope: Scope = globalScope): void {
if (restrictedElements.length === 0) return;
const restrictedElementsUsed: string[] = [];
restrictedElements.forEach((element: string) => {
if (isValidElement(scope, element)) {
restrictedElementsUsed.push(element);
}
});
scope.restrictedCircuitElementsUsed = restrictedElementsUsed;
updateRestrictedElementsList();
}


const RESTRICTED_MESSAGE = 'The element has been restricted by mentor. Usage might lead to deduction in marks';
export function showRestricted(): void {
const restrictedDiv = document.getElementById('restrictedDiv');
const helpDiv = document.getElementById('Help');
if (!restrictedDiv) {
console.error('Element restrictedDiv not found');
return;
}
restrictedDiv.classList.remove('display--none');
restrictedDiv.innerHTML = RESTRICTED_MESSAGE;
if (helpDiv) {
helpDiv.classList.remove('show');
}
}

export function hideRestricted(): void {
const restrictedDiv = document.getElementById('restrictedDiv');
if (restrictedDiv) {
restrictedDiv.classList.add('display--none');
}
}
4 changes: 4 additions & 0 deletions src/simulator/src/types/restrictedElementDiv.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Scope {
[key: string]: string[];
restrictedCircuitElementsUsed: string[];
}
Loading