Fix remaining typescript issues, enable tsc (#32840)

Fixes 79 typescript errors. Discovered at least two bugs in
`notifications.ts`, and I'm pretty sure this feature was at least
partially broken and may still be, I don't really know how to test it.

After this, only like ~10 typescript errors remain in the codebase but
those are harder to solve.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind 2024-12-15 22:02:32 +01:00 committed by GitHub
parent 74b06d4f5c
commit c8ea41b049
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 152 additions and 134 deletions

View file

@ -14,25 +14,25 @@ export function initNotificationsTable() {
window.addEventListener('pageshow', (e) => {
if (e.persisted) { // page was restored from bfcache
const table = document.querySelector('#notification_table');
const unreadCountEl = document.querySelector('.notifications-unread-count');
const unreadCountEl = document.querySelector<HTMLElement>('.notifications-unread-count');
let unreadCount = parseInt(unreadCountEl.textContent);
for (const item of table.querySelectorAll('.notifications-item[data-remove="true"]')) {
item.remove();
unreadCount -= 1;
}
unreadCountEl.textContent = unreadCount;
unreadCountEl.textContent = String(unreadCount);
}
});
// mark clicked unread links for deletion on bfcache restore
for (const link of table.querySelectorAll('.notifications-item[data-status="1"] .notifications-link')) {
link.addEventListener('click', (e) => {
link.addEventListener('click', (e : MouseEvent & {target: HTMLElement}) => {
e.target.closest('.notifications-item').setAttribute('data-remove', 'true');
});
}
}
async function receiveUpdateCount(event) {
async function receiveUpdateCount(event: MessageEvent) {
try {
const data = JSON.parse(event.data);
@ -50,7 +50,7 @@ export function initNotificationCount() {
if (!document.querySelector('.notification_count')) return;
let usingPeriodicPoller = false;
const startPeriodicPoller = (timeout, lastCount) => {
const startPeriodicPoller = (timeout: number, lastCount?: number) => {
if (timeout <= 0 || !Number.isFinite(timeout)) return;
usingPeriodicPoller = true;
lastCount = lastCount ?? getCurrentCount();
@ -72,13 +72,13 @@ export function initNotificationCount() {
type: 'start',
url: `${window.location.origin}${appSubUrl}/user/events`,
});
worker.port.addEventListener('message', (event) => {
worker.port.addEventListener('message', (event: MessageEvent) => {
if (!event.data || !event.data.type) {
console.error('unknown worker message event', event);
return;
}
if (event.data.type === 'notification-count') {
const _promise = receiveUpdateCount(event.data);
receiveUpdateCount(event); // no await
} else if (event.data.type === 'no-event-source') {
// browser doesn't support EventSource, falling back to periodic poller
if (!usingPeriodicPoller) startPeriodicPoller(notificationSettings.MinTimeout);
@ -118,10 +118,10 @@ export function initNotificationCount() {
}
function getCurrentCount() {
return document.querySelector('.notification_count').textContent;
return Number(document.querySelector('.notification_count').textContent ?? '0');
}
async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
async function updateNotificationCountWithCallback(callback: (timeout: number, newCount: number) => void, timeout: number, lastCount: number) {
const currentCount = getCurrentCount();
if (lastCount !== currentCount) {
callback(notificationSettings.MinTimeout, currentCount);
@ -149,10 +149,9 @@ async function updateNotificationTable() {
if (notificationDiv) {
try {
const params = new URLSearchParams(window.location.search);
params.set('div-only', true);
params.set('sequence-number', ++notificationSequenceNumber);
const url = `${appSubUrl}/notifications?${params.toString()}`;
const response = await GET(url);
params.set('div-only', String(true));
params.set('sequence-number', String(++notificationSequenceNumber));
const response = await GET(`${appSubUrl}/notifications?${params.toString()}`);
if (!response.ok) {
throw new Error('Failed to fetch notification table');
@ -169,7 +168,7 @@ async function updateNotificationTable() {
}
}
async function updateNotificationCount() {
async function updateNotificationCount(): Promise<number> {
try {
const response = await GET(`${appSubUrl}/notifications/new`);
@ -185,9 +184,9 @@ async function updateNotificationCount() {
el.textContent = `${data.new}`;
}
return `${data.new}`;
return data.new as number;
} catch (error) {
console.error(error);
return '0';
return 0;
}
}