fix(assessments): show letter grades not undefined%

cannot triple confirm that this works for all schools but it works for my assessments that are letter-based
This commit is contained in:
2026-06-22 21:47:03 +09:30
parent 6f7788497a
commit 37ae32be59
12 changed files with 619 additions and 101 deletions
@@ -1,19 +1,43 @@
import type { Plugin } from "../../core/types";
import { booleanSetting } from "@/plugins/core/settingsHelpers";
import { isSeqtaEngageExperience } from "@/seqta/utils/isSeqtaEngage";
import { verboseInfo } from "@/utils/verboseLog";
import {
type ArchivesByUser,
fetchAllNotifications,
mergeNotificationsIntoArchive,
resolveNotificationUserKey,
} from "./archive";
import { mountArchiveUI } from "./archiveUI";
import styles from "./styles.css?inline";
const notificationCollectorSettings = {
saveLocally: booleanSetting({
default: true,
title: "Save notification history locally",
description:
"Keeps a per-account copy in extension storage. SEQTA removes notifications after about a year; these stay until you clear extension data.",
}),
} as const;
interface NotificationCollectorStorage {
lastNotificationCount: number;
lastCheckedTime: string;
consecutiveErrors: number;
archivesByUser: ArchivesByUser;
}
const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
const notificationCollectorPlugin: Plugin<
typeof notificationCollectorSettings,
NotificationCollectorStorage
> = {
id: "notificationCollector",
name: "Notification Collector",
description: "Collects and displays SEQTA notifications",
version: "1.0.0",
settings: {},
description:
"Tracks notifications and saves a local per-account archive that outlasts SEQTA's server retention",
version: "1.1.0",
settings: notificationCollectorSettings,
styles,
disableToggle: true,
run: async (api) => {
@@ -21,24 +45,51 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
return () => {};
}
await api.storage.loaded;
await api.settings.loaded;
let pollInterval: number | null = null;
let isVisible = !document.hidden;
let baseInterval = 30000; // 30 seconds
const maxInterval = 300000; // 5 minutes max
let archiveSyncInFlight = false;
const baseInterval = 30000;
const maxInterval = 300000;
// Store last notification count in storage
if (!api.storage.lastNotificationCount) {
api.storage.lastNotificationCount = 0;
}
if (!api.storage.consecutiveErrors) {
api.storage.consecutiveErrors = 0;
}
if (!api.storage.archivesByUser) {
api.storage.archivesByUser = {};
}
const syncArchive = async () => {
if (!api.settings.saveLocally || archiveSyncInFlight) return;
archiveSyncInFlight = true;
try {
const userKey = await resolveNotificationUserKey();
if (!userKey) return;
const notifications = await fetchAllNotifications();
const archivesByUser = { ...(api.storage.archivesByUser ?? {}) };
const existing = archivesByUser[userKey] ?? {};
const merged = mergeNotificationsIntoArchive(existing, notifications);
if (JSON.stringify(existing) !== JSON.stringify(merged)) {
archivesByUser[userKey] = merged;
api.storage.archivesByUser = archivesByUser;
}
} catch (error) {
console.warn("[BetterSEQTA+] Notification archive sync failed:", error);
} finally {
archiveSyncInFlight = false;
}
};
const checkNotifications = async () => {
// Skip if tab is not visible to save battery
if (!isVisible) {
return;
}
if (!isVisible) return;
try {
const alertDiv = document.querySelector(
@@ -49,30 +100,17 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
alertDiv.textContent = api.storage.lastNotificationCount.toString();
}
const response = await fetch(
`${location.origin}/seqta/student/heartbeat?`,
{
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
timestamp: "1970-01-01 00:00:00.0",
hash: "#?page=/home",
}),
},
);
const notifications = await fetchAllNotifications();
const notificationCount = notifications.length;
const data = await response.json();
// Store notification count for history
const notificationCount = data.payload.notifications.length;
api.storage.lastNotificationCount = notificationCount;
api.storage.lastCheckedTime = new Date().toISOString();
// Reset error count on success
api.storage.consecutiveErrors = 0;
if (api.settings.saveLocally) {
await syncArchive();
}
if (alertDiv) {
alertDiv.textContent = notificationCount.toString();
} else {
@@ -86,7 +124,6 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
};
const getNextInterval = () => {
// Exponential backoff on errors, max 5 minutes
const errorMultiplier = Math.min(
Math.pow(2, api.storage.consecutiveErrors || 0),
10,
@@ -95,17 +132,14 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
};
const startPolling = () => {
if (pollInterval) return; // Already polling
if (pollInterval) return;
checkNotifications();
const scheduleNext = () => {
const interval = getNextInterval();
pollInterval = window.setTimeout(() => {
checkNotifications().then(() => {
if (pollInterval) {
// Only continue if not stopped
scheduleNext();
}
if (pollInterval) scheduleNext();
});
}, interval);
};
@@ -130,29 +164,43 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
}
};
// Listen for visibility changes to pause/resume polling
const handleVisibilityChange = () => {
isVisible = !document.hidden;
if (isVisible && !pollInterval) {
// Resume polling when tab becomes visible
const alertDiv = document.querySelector(
"[class*='notifications__bubble___']",
);
if (alertDiv) {
startPolling();
}
if (alertDiv) startPolling();
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
api.seqta.onMount("[class*='notifications__bubble___']", (_) => {
const pageChangeUnregister = api.seqta.onPageChange((page) => {
if (page === "notifications" && api.settings.saveLocally) {
void syncArchive();
}
});
mountArchiveUI(api, resolveNotificationUserKey);
api.seqta.onMount("[class*='notifications__bubble___']", () => {
startPolling();
if (api.settings.saveLocally) {
void syncArchive();
}
});
api.seqta.onMount("[class*='notifications__list___']", () => {
if (api.settings.saveLocally) {
void syncArchive();
}
});
return () => {
stopPolling();
document.removeEventListener("visibilitychange", handleVisibilityChange);
pageChangeUnregister.unregister();
};
},
};