Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Dec 20, 2024
1 parent 0741ad4 commit c4d6e1f
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/hooks/use-notifications-url-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { NOTIFICATIONS_URL_KEYS, PREFIX_CONFIG_NOTIFICATION_WS } from '../components/utils/notificationsProvider-utils';
import { NOTIFICATIONS_URL_KEYS, PREFIX_CONFIG_NOTIFICATION_WS } from 'components/utils/notificationsProvider-utils';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { type AppState } from '../redux/reducer';
import { getUrlWithToken, getWsBase } from '../services/utils';
import { APP_NAME } from '../utils/config-params';
import { type AppState } from 'redux/reducer';
import { getUrlWithToken, getWsBase } from 'services/utils';
import { APP_NAME } from 'utils/config-params';

export default function useNotificationsUrlGenerator() {
const useNotificationsUrlGenerator = (): Record<NOTIFICATIONS_URL_KEYS, string | undefined> => {
// The websocket API doesn't allow relative urls
const wsBase = getWsBase();
const tokenId = useSelector((state: AppState) => state.user?.id_token);

// return a mapper with NOTIFICATIONS_URL_KEYS and undefined value if URL is not yet buildable (tokenId)
// it will be used to register listeners as soon as possible.
return useMemo(
() =>
({
[NOTIFICATIONS_URL_KEYS.CONFIG]: tokenId
? getUrlWithToken(
`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?${new URLSearchParams({
appName: APP_NAME,
})}`
)
: undefined,
} satisfies Record<NOTIFICATIONS_URL_KEYS, string | undefined>),
() => ({
[NOTIFICATIONS_URL_KEYS.CONFIG]: tokenId
? getUrlWithToken(
`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?${new URLSearchParams({
appName: APP_NAME,
})}`
)
: undefined,
}),
[wsBase, tokenId]
);
}
};

export default useNotificationsUrlGenerator;

0 comments on commit c4d6e1f

Please sign in to comment.