mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-12-06 19:56:45 +10:30
Let the status file disable features per SEQTA site
This commit is contained in:
1 parent
bfe8e3645b
commit
41553a8fc4
2 files changed
+21
-6
No files matched your search
@@ -2,7 +2,7 @@ import { normalizeRemoteStatus } from "./remoteStatus";
|
||||
|
||||
describe("normalizeRemoteStatus", () => {
|
||||
it("falls back to working defaults for missing or malformed data", () => {
|
||||
const defaults = { off: false, message: null, disabledFeatures: [] };
|
||||
const defaults = { off: false, message: null, disabledFeatures: [], sites: {} };
|
||||
expect(normalizeRemoteStatus(null)).toEqual(defaults);
|
||||
expect(
|
||||
normalizeRemoteStatus({ off: "yes", message: 4, disabledFeatures: "page.news" }),
|
||||
@@ -15,7 +15,13 @@ describe("normalizeRemoteStatus", () => {
|
||||
off: true,
|
||||
message: "Back soon",
|
||||
disabledFeatures: ["page.news", 1],
|
||||
sites: { "Learn.School.edu.au": ["plugin.timetable-classmates", 2] },
|
||||
}),
|
||||
).toEqual({ off: true, message: "Back soon", disabledFeatures: ["page.news"] });
|
||||
).toEqual({
|
||||
off: true,
|
||||
message: "Back soon",
|
||||
disabledFeatures: ["page.news"],
|
||||
sites: { "learn.school.edu.au": ["plugin.timetable-classmates"] },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -4,20 +4,27 @@ export type RemoteStatus = {
|
||||
off: boolean;
|
||||
message: string | null;
|
||||
disabledFeatures: string[];
|
||||
sites: Record<string, string[]>;
|
||||
};
|
||||
|
||||
const STATUS_URL =
|
||||
"https://raw.githubusercontent.com/BetterSEQTA/BS-Status/main/config/status.json";
|
||||
export const REMOTE_STATUS_KEY = "bsplus_remote_status";
|
||||
|
||||
function stringList(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.filter((key): key is string => typeof key === "string") : [];
|
||||
}
|
||||
|
||||
export function normalizeRemoteStatus(raw: unknown): RemoteStatus {
|
||||
const r = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
|
||||
const sites = r.sites && typeof r.sites === "object" ? r.sites : {};
|
||||
return {
|
||||
off: r.off === true,
|
||||
message: typeof r.message === "string" && r.message.trim() ? r.message : null,
|
||||
disabledFeatures: Array.isArray(r.disabledFeatures)
|
||||
? r.disabledFeatures.filter((key): key is string => typeof key === "string")
|
||||
: [],
|
||||
disabledFeatures: stringList(r.disabledFeatures),
|
||||
sites: Object.fromEntries(
|
||||
Object.entries(sites).map(([host, keys]) => [host.toLowerCase(), stringList(keys)]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -30,7 +37,9 @@ let status = normalizeRemoteStatus(null);
|
||||
|
||||
export const getRemoteStatus = (): RemoteStatus => status;
|
||||
|
||||
export const isFeatureDisabled = (key: string): boolean => status.disabledFeatures.includes(key);
|
||||
export const isFeatureDisabled = (key: string): boolean =>
|
||||
status.disabledFeatures.includes(key) ||
|
||||
!!status.sites[location.hostname.toLowerCase()]?.includes(key);
|
||||
|
||||
export async function loadRemoteStatus(): Promise<void> {
|
||||
status = await readRemoteStatus();
|
||||
|
||||
Reference in new issue
Block a user