Skip to content

Clean up notification-types.ts#3799

Open
flomillot wants to merge 4 commits intomainfrom
clean-notif-types
Open

Clean up notification-types.ts#3799
flomillot wants to merge 4 commits intomainfrom
clean-notif-types

Conversation

@flomillot
Copy link
Contributor

@flomillot flomillot commented Mar 11, 2026

No description provided.

Signed-off-by: Florent MILLOT <florent.millot_externe@rte-france.com>
@flomillot flomillot requested review from Meklo and sBouzols March 11, 2026 13:42
…uit-analysis-loader.

Signed-off-by: Florent MILLOT <75525996+flomillot@users.noreply.github.com>
@sonarqubecloud
Copy link

@coderabbitai
Copy link

coderabbitai bot commented Mar 20, 2026

📝 Walkthrough

Walkthrough

This pull request systematically refactors event parsing and notification handling across multiple components and type definitions. The generic type used in parseEventData calls is changed from StudyUpdateEventData to CommonStudyEventData, guard clauses are added to validate parsed data before processing, and the notification types file is consolidated by removing numerous specific event and header interfaces.

Changes

Cohort / File(s) Summary
Event parsing in hooks and utilities
src/components/dialogs/parameters/use-parameters-notification.ts, src/hooks/use-optional-loading-parameters.ts, src/hooks/use-study-path.ts
Updated parseEventData generic type from StudyUpdateEventData to CommonStudyEventData and added guard clauses to validate parsed data is truthy before type-checking and processing downstream notifications.
Event handling in graph menus
src/components/graph/menus/dynamic-simulation/event-modification-scenario-editor.tsx, src/components/graph/menus/network-modifications/network-modification-node-editor.tsx
Changed parseEventData type parameter to CommonStudyEventData and added early return guards when parsing fails, preventing subsequent notification type-checks and side effects.
Root network notification listeners
src/components/graph/menus/root-network/use-root-network-notifications.ts
Replaced direct JSON.parse(event.data) with parseEventData<CommonStudyEventData>(event) and added guards (eventData && ...) so type-specific predicate checks only run when parsing succeeds.
Component event handlers
src/components/grid-layout/cards/diagrams/singleLineDiagram/hooks/use-one-bus-shortcircuit-analysis-loader.tsx, src/components/network/network-map-panel.tsx
Updated event parsing type to CommonStudyEventData and restructured boolean conditions to ensure both notification type-checks are gated by eventData being truthy; network-map-panel also extended imports to include parseEventData and CommonStudyEventData.
Type definitions consolidation
src/types/notification-types.ts
Removed exported constants (MODIFYING_NODES_NOTIFICATION_TYPES, MODIFYING_NODE_NOTIFICATION_TYPES, COMPUTATION_NOTIFICATION_TYPES), multiple specific event header and data interfaces, and union types (ComputationEventData, StudyUpdateEventData). Refactored remaining event data interfaces to extend CommonStudyEventData with payload: string | undefined and updated all type-guard function signatures to accept CommonStudyEventData instead of unknown.
🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 2.63% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author. Add a description explaining the rationale behind the cleanup, such as why these types/constants were removed and how the refactoring impacts the codebase.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: a cleanup of the notification-types.ts file involving removal of unused exports and restructuring of type definitions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a 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/types/notification-types.ts (1)

731-735: Type guard does not narrow the type.

The function isSpreadsheetNodeAliasesUpdatedNotification returns notif is CommonStudyEventData, which is the same as the input type. This makes the type guard ineffective for narrowing purposes. Consider either:

  1. Creating a specific SpreadsheetNodeAliasesUpdatedEventData interface with appropriate headers, or
  2. Removing the type predicate if narrowing isn't needed.
Suggested fix
-export function isSpreadsheetNodeAliasesUpdatedNotification(
-    notif: CommonStudyEventData
-): notif is CommonStudyEventData {
+export function isSpreadsheetNodeAliasesUpdatedNotification(notif: CommonStudyEventData): boolean {
     return notif.headers?.updateType === NotificationType.SPREADSHEET_NODE_ALIASES_UPDATED;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/types/notification-types.ts` around lines 731 - 735, The type guard is
ineffective because isSpreadsheetNodeAliasesUpdatedNotification currently claims
the input is the same type (notif is CommonStudyEventData); update it to narrow
to a new specific type (e.g., SpreadsheetNodeAliasesUpdatedEventData) that
extends CommonStudyEventData and declares the expected headers/updateType shape,
then change the function signature to return notif is
SpreadsheetNodeAliasesUpdatedEventData and keep the runtime check
notif.headers?.updateType === NotificationType.SPREADSHEET_NODE_ALIASES_UPDATED;
alternatively, if no narrower type is needed, remove the type predicate and make
the function return boolean instead, updating all call sites accordingly
(referencing isSpreadsheetNodeAliasesUpdatedNotification, CommonStudyEventData,
and NotificationType.SPREADSHEET_NODE_ALIASES_UPDATED).
src/components/graph/menus/root-network/use-root-network-notifications.ts (1)

109-117: Consider consolidating notification listeners.

Three separate useNotificationsListener calls are registered for the same NotificationsUrlKeys.STUDY channel. While this works, it could be simplified into a single listener that handles all three notification types. This is a minor optimization that could be addressed in a follow-up.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/graph/menus/root-network/use-root-network-notifications.ts`
around lines 109 - 117, Consolidate the three useNotificationsListener calls for
NotificationsUrlKeys.STUDY into a single call: create one listener callback
(e.g., a wrapper function) that inspects the incoming notification payload
(type/event field) and delegates to rootNetworksUpdatedNotification,
rootNetworksUpdateFailedNotification, or rootNetworkDeletionStartedNotification
as appropriate, then pass that wrapper as listenerCallbackMessage to
useNotificationsListener(NotificationsUrlKeys.STUDY); this keeps the same
behavior but registers only one listener.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/types/notification-types.ts`:
- Around line 686-688: Rename the predicate function
isNodSubTreeCreatedNotification to isNodeSubTreeCreatedNotification in the
declaration inside notification-types (update the export name) and update all
imports/usages to the new name (e.g., inside useRootNetworkSearchNotifications
and any other modules importing it); ensure TypeScript types and any type-guard
signatures remain identical so only the identifier changes.
- Around line 384-387: The code in use-get-study-impacts.ts parses
eventData.payload with JSON.parse without guarding for undefined; update the
logic (where JSON.parse(eventData.payload) is called) to first check that
eventData.payload is a non-empty string (or truthy) and only then attempt
JSON.parse, and wrap the parse in a try-catch similar to
use-workspace-notifications.ts so undefined or invalid JSON does not throw —
reference the CommonStudyEventData type, the eventData variable, and the
JSON.parse call in use-get-study-impacts.ts to locate and fix the issue.

---

Nitpick comments:
In `@src/components/graph/menus/root-network/use-root-network-notifications.ts`:
- Around line 109-117: Consolidate the three useNotificationsListener calls for
NotificationsUrlKeys.STUDY into a single call: create one listener callback
(e.g., a wrapper function) that inspects the incoming notification payload
(type/event field) and delegates to rootNetworksUpdatedNotification,
rootNetworksUpdateFailedNotification, or rootNetworkDeletionStartedNotification
as appropriate, then pass that wrapper as listenerCallbackMessage to
useNotificationsListener(NotificationsUrlKeys.STUDY); this keeps the same
behavior but registers only one listener.

In `@src/types/notification-types.ts`:
- Around line 731-735: The type guard is ineffective because
isSpreadsheetNodeAliasesUpdatedNotification currently claims the input is the
same type (notif is CommonStudyEventData); update it to narrow to a new specific
type (e.g., SpreadsheetNodeAliasesUpdatedEventData) that extends
CommonStudyEventData and declares the expected headers/updateType shape, then
change the function signature to return notif is
SpreadsheetNodeAliasesUpdatedEventData and keep the runtime check
notif.headers?.updateType === NotificationType.SPREADSHEET_NODE_ALIASES_UPDATED;
alternatively, if no narrower type is needed, remove the type predicate and make
the function return boolean instead, updating all call sites accordingly
(referencing isSpreadsheetNodeAliasesUpdatedNotification, CommonStudyEventData,
and NotificationType.SPREADSHEET_NODE_ALIASES_UPDATED).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 905ccd1e-82e6-4884-9e84-a5a32eb552bf

📥 Commits

Reviewing files that changed from the base of the PR and between 94eed1c and d2a039e.

📒 Files selected for processing (9)
  • src/components/dialogs/parameters/use-parameters-notification.ts
  • src/components/graph/menus/dynamic-simulation/event-modification-scenario-editor.tsx
  • src/components/graph/menus/network-modifications/network-modification-node-editor.tsx
  • src/components/graph/menus/root-network/use-root-network-notifications.ts
  • src/components/grid-layout/cards/diagrams/singleLineDiagram/hooks/use-one-bus-shortcircuit-analysis-loader.tsx
  • src/components/network/network-map-panel.tsx
  • src/hooks/use-optional-loading-parameters.ts
  • src/hooks/use-study-path.ts
  • src/types/notification-types.ts

Comment on lines +384 to 387
export interface CommonStudyEventData {
headers: CommonStudyEventDataHeaders;
payload: undefined;
payload: string | undefined;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for JSON.parse calls on eventData.payload without guards
rg -n -B2 -A2 'JSON\.parse\(.*\.payload\)' --type ts

Repository: gridsuite/gridstudy-app

Length of output: 2310


🏁 Script executed:

#!/bin/bash
# Check the full context of JSON.parse calls with try-catch blocks
echo "=== use-workspace-notifications.ts context ===" 
sed -n '36,60p' src/components/workspace/hooks/use-workspace-notifications.ts | cat -n

echo -e "\n=== use-get-study-impacts.ts context ===" 
sed -n '55,70p' src/hooks/use-get-study-impacts.ts | cat -n

echo -e "\n=== use-update-equipments-on-notification.ts context ===" 
sed -n '110,125p' src/components/spreadsheet-view/hooks/use-update-equipments-on-notification.ts | cat -n

echo -e "\n=== Verify type definition ===" 
sed -n '384,387p' src/types/notification-types.ts | cat -n

Repository: gridsuite/gridstudy-app

Length of output: 3403


🏁 Script executed:

#!/bin/bash
# Get broader context around the JSON.parse in use-get-study-impacts.ts
sed -n '50,80p' src/hooks/use-get-study-impacts.ts | cat -n

# Also search for any other JSON.parse calls on payload that might have different patterns
echo -e "\n=== Search for any other payload parsing patterns ===" 
rg 'payload' src/hooks/use-get-study-impacts.ts -B3 -A3 | cat -n

Repository: gridsuite/gridstudy-app

Length of output: 2400


🏁 Script executed:

#!/bin/bash
# Get the full function context - find where this callback starts
# Look backwards from line 50
sed -n '30,80p' src/hooks/use-get-study-impacts.ts | cat -n

Repository: gridsuite/gridstudy-app

Length of output: 2799


🏁 Script executed:

#!/bin/bash
# Comprehensive search for all JSON.parse calls involving payload
echo "=== All JSON.parse patterns with payload ===" 
rg 'JSON\.parse.*payload' --type ts -n

# Also check if there are try-catch blocks in the full handleStudyNotification function
echo -e "\n=== Full handleStudyNotification function ===" 
grep -n 'handleStudyNotification' src/hooks/use-get-study-impacts.ts
# Get the full function using line numbers
sed -n '49,100p' src/hooks/use-get-study-impacts.ts | head -50

Repository: gridsuite/gridstudy-app

Length of output: 3182


Add null check for payload in use-get-study-impacts.ts before parsing.

CommonStudyEventData.payload is typed as string | undefined, but use-get-study-impacts.ts line 62 calls JSON.parse(eventData.payload) without a guard. If payload is undefined, this will throw a runtime error. The other call sites in use-workspace-notifications.ts and use-update-equipments-on-notification.ts already have proper try-catch error handling.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/types/notification-types.ts` around lines 384 - 387, The code in
use-get-study-impacts.ts parses eventData.payload with JSON.parse without
guarding for undefined; update the logic (where JSON.parse(eventData.payload) is
called) to first check that eventData.payload is a non-empty string (or truthy)
and only then attempt JSON.parse, and wrap the parse in a try-catch similar to
use-workspace-notifications.ts so undefined or invalid JSON does not throw —
reference the CommonStudyEventData type, the eventData variable, and the
JSON.parse call in use-get-study-impacts.ts to locate and fix the issue.

Comment on lines +686 to 688
export function isNodSubTreeCreatedNotification(notif: CommonStudyEventData): notif is SubtreeCreatedEventData {
return notif.headers?.updateType === NotificationType.SUBTREE_CREATED;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for usages of the misspelled function name
rg -n "isNodSubTreeCreatedNotification"

Repository: gridsuite/gridstudy-app

Length of output: 503


Rename function to fix typo: isNodSubTreeCreatedNotificationisNodeSubTreeCreatedNotification

The function name is missing the letter 'e' in "Node". Update the function definition and its imports/usages across the codebase for consistency.

Changes required

src/types/notification-types.ts:686

-export function isNodSubTreeCreatedNotification(notif: CommonStudyEventData): notif is SubtreeCreatedEventData {
+export function isNodeSubTreeCreatedNotification(notif: CommonStudyEventData): notif is SubtreeCreatedEventData {
     return notif.headers?.updateType === NotificationType.SUBTREE_CREATED;
 }

src/components/graph/menus/root-network/use-root-network-search-notifications.ts:19, :46

  • Update the import and usage of this function accordingly
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/types/notification-types.ts` around lines 686 - 688, Rename the predicate
function isNodSubTreeCreatedNotification to isNodeSubTreeCreatedNotification in
the declaration inside notification-types (update the export name) and update
all imports/usages to the new name (e.g., inside
useRootNetworkSearchNotifications and any other modules importing it); ensure
TypeScript types and any type-guard signatures remain identical so only the
identifier changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant