Skip to content

Conversation

@matejkubinec
Copy link
Contributor

@matejkubinec matejkubinec commented Dec 19, 2025

const { refetch: refetchServiceTypes } = useServiceTypes({
enabled: false,
});

Choose a reason for hiding this comment

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

The main thing I noticed - listener cleanup, the listeners are being added, but I don't see them getting removed:

messenger.addListener({
type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});

This could cause a memory leak if the component remounts (like during navigation) - you'd keep stacking up listeners. The fix is pretty simple, though, just need to return a cleanup function:

useEffect(() => {
const settingsListener = messenger.addListener({
type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});

const serviceListener = messenger.addListener({
  type: 'SERVICE_ADDED',
  onMessage: () => refetchServiceTypes(),
});

return () => {
  messenger.removeListener(settingsListener);
  messenger.removeListener(serviceListener);
};

}, [refetchSettings, refetchServiceTypes]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I've updated the unregister method in messenger to clear the listeners.


const { refetch: refetchSettings } = useSettings({
enabled: false,
});

Choose a reason for hiding this comment

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

Quick question about initial data loading

I see you're creating these queries with enabled: false:

const { refetch: refetchSettings } = useSettings({
enabled: false,
});

Just want to make sure I understand - are these queries being run somewhere else on initial load? If not, the UI might not have this data until the first event comes in. Might be worth a quick double-check, or if it's intentional, maybe add a comment so future devs know what's up ))))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The initial fetch of settings is in SettingsProvider. This is just used for refetching when triggered from the Grafana side.

type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});

Choose a reason for hiding this comment

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

A few other things to consider (not blockers, just ideas):

Error handling on refetch:
Right now if the refetch fails, it'll be silent. Maybe worth catching errors and showing a toast or logging them? Totally up to you though:

onMessage: async () => {
try {
await refetchSettings();
} catch (error) {
console.error('Failed to refetch settings:', error);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there's a need to notify the user about it failing.

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.

5 participants