The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/client/startup/banners.ts

67 lines
1.5 KiB

import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { Notifications } from '../../app/notifications/client';
import { APIClient } from '../../app/utils/client';
import { IBanner, BannerPlatform } from '../../definition/IBanner';
import { Serialized } from '../../definition/Serialized';
import * as banners from '../lib/banners';
const fetchInitialBanners = async (): Promise<void> => {
const response: Serialized<{
banners: IBanner[];
}> = await APIClient.get('v1/banners', {
platform: BannerPlatform.Web,
});
for (const banner of response.banners) {
banners.open({
...banner.view,
viewId: banner.view.viewId || banner._id,
});
}
};
const handleBanner = async (event: { bannerId: string }): Promise<void> => {
const response: Serialized<{
banners: IBanner[];
}> = await APIClient.get(`v1/banners/${event.bannerId}`, {
platform: BannerPlatform.Web,
});
if (!response.banners.length) {
return banners.closeById(event.bannerId);
}
for (const banner of response.banners) {
banners.open({
...banner.view,
viewId: banner.view.viewId || banner._id,
});
}
};
const watchBanners = (): (() => void) => {
fetchInitialBanners();
Notifications.onLogged('banner-changed', handleBanner);
return (): void => {
Notifications.unLogged(handleBanner);
banners.clear();
};
};
Meteor.startup(() => {
let unwatchBanners: () => void | undefined;
Tracker.autorun(() => {
unwatchBanners?.();
if (!Meteor.userId()) {
return;
}
unwatchBanners = Tracker.nonreactive(watchBanners);
});
});