mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-12-06 19:56:45 +10:30
feat: add TOS popup and new update vid
This commit is contained in:
1 parent
95908baa8e
commit
fea017bbd7
8 files changed
+397
-5
No files matched your search
@@ -2,7 +2,7 @@
|
||||
import Switch from "@/interface/components/Switch.svelte";
|
||||
import Button from "@/interface/components/Button.svelte";
|
||||
import { settingsState } from "@/seqta/utils/listeners/SettingsState";
|
||||
import { showPrivacyNotification } from "@/seqta/utils/Openers/OpenPrivacyNotification";
|
||||
import { openTermsOfServicePopup } from "@/seqta/utils/Openers/OpenTermsOfServicePopup";
|
||||
import { showThemeOfTheMonthPopupNow } from "@/seqta/utils/Openers/OpenThemeOfTheMonthPopup";
|
||||
import { closeExtensionPopup } from "@/seqta/utils/Closers/closeExtensionPopup";
|
||||
import { getSnapshotForUpload } from "@/seqta/utils/cloudSettingsSync";
|
||||
@@ -139,19 +139,20 @@
|
||||
</div>
|
||||
<div class="flex justify-between items-center px-4 py-3">
|
||||
<div class="pr-4">
|
||||
<h2 class="text-sm font-bold">Show Privacy Notification</h2>
|
||||
<h2 class="text-sm font-bold">Show Terms of Service</h2>
|
||||
<p class="text-xs text-zinc-600 dark:text-zinc-300">
|
||||
Show the privacy notification popup on next page load
|
||||
Show the terms of service acceptance popup (same as startup queue)
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
onClick={async () => {
|
||||
settingsState.termsOfServiceLastAccepted = undefined;
|
||||
settingsState.privacyStatementShown = false;
|
||||
settingsState.privacyStatementLastUpdated = undefined;
|
||||
closeExtensionPopup();
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
await showPrivacyNotification();
|
||||
await openTermsOfServicePopup(undefined, { force: true });
|
||||
}}
|
||||
text="Show Now"
|
||||
/>
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
jest.mock("../DevApiBase", () => ({
|
||||
getApiBase: () => "https://betterseqta.org",
|
||||
}));
|
||||
|
||||
import {
|
||||
getFallbackPrivacyTerms,
|
||||
markTermsOfServiceAccepted,
|
||||
shouldShowTermsOfServicePopup,
|
||||
} from "./OpenTermsOfServicePopup";
|
||||
|
||||
const SAMPLE_TERMS = {
|
||||
last_updated: "2024-06-15T12:00:00Z",
|
||||
whatsnew_html: "<div class=\"whatsnewTextContainer\"><p>Terms body</p></div>",
|
||||
};
|
||||
|
||||
describe("shouldShowTermsOfServicePopup", () => {
|
||||
it("shows when the user has never accepted", () => {
|
||||
expect(
|
||||
shouldShowTermsOfServicePopup({ termsOfServiceLastAccepted: undefined }, SAMPLE_TERMS),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("shows when the remote policy was updated after acceptance", () => {
|
||||
expect(
|
||||
shouldShowTermsOfServicePopup(
|
||||
{ termsOfServiceLastAccepted: "2024-01-01T00:00:00Z" },
|
||||
SAMPLE_TERMS,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("does not show when the user accepted the current version", () => {
|
||||
expect(
|
||||
shouldShowTermsOfServicePopup(
|
||||
{ termsOfServiceLastAccepted: SAMPLE_TERMS.last_updated },
|
||||
SAMPLE_TERMS,
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("treats legacy privacy acceptance as terms acceptance", () => {
|
||||
expect(
|
||||
shouldShowTermsOfServicePopup(
|
||||
{
|
||||
privacyStatementShown: true,
|
||||
privacyStatementLastUpdated: SAMPLE_TERMS.last_updated,
|
||||
},
|
||||
SAMPLE_TERMS,
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("markTermsOfServiceAccepted", () => {
|
||||
it("stores the accepted version and legacy privacy flags", () => {
|
||||
const state = {
|
||||
termsOfServiceLastAccepted: undefined as string | undefined,
|
||||
privacyStatementShown: false,
|
||||
privacyStatementLastUpdated: undefined as string | undefined,
|
||||
};
|
||||
|
||||
markTermsOfServiceAccepted(state, SAMPLE_TERMS.last_updated);
|
||||
|
||||
expect(state.termsOfServiceLastAccepted).toBe(SAMPLE_TERMS.last_updated);
|
||||
expect(state.privacyStatementShown).toBe(true);
|
||||
expect(state.privacyStatementLastUpdated).toBe(SAMPLE_TERMS.last_updated);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getFallbackPrivacyTerms", () => {
|
||||
it("returns bundled fallback content when the API is unavailable", () => {
|
||||
const fallback = getFallbackPrivacyTerms();
|
||||
expect(fallback.last_updated).toBeTruthy();
|
||||
expect(fallback.whatsnew_html).toContain("whatsnewTextContainer");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,291 @@
|
||||
import stringToHTML from "../stringToHTML";
|
||||
import { settingsState } from "../listeners/SettingsState";
|
||||
import { getApiBase } from "../DevApiBase";
|
||||
import { closePopup, openPopup } from "./PopupManager";
|
||||
import fallbackTerms from "@/exampleprivacyresponse.json";
|
||||
import type { SettingsState } from "@/types/storage";
|
||||
|
||||
export const PRIVACY_POLICY_URL = "https://betterseqta.org/privacy";
|
||||
|
||||
const PRIVACY_FETCH_TIMEOUT_MS = 8_000;
|
||||
const POPUP_STYLE_ID = "bsplus-tos-popup-styles";
|
||||
|
||||
export interface PrivacyTermsPayload {
|
||||
last_updated: string;
|
||||
whatsnew_html: string;
|
||||
}
|
||||
|
||||
function isPrivacyTermsPayload(data: unknown): data is PrivacyTermsPayload {
|
||||
return (
|
||||
!!data &&
|
||||
typeof data === "object" &&
|
||||
typeof (data as PrivacyTermsPayload).last_updated === "string" &&
|
||||
typeof (data as PrivacyTermsPayload).whatsnew_html === "string" &&
|
||||
!!(data as PrivacyTermsPayload).whatsnew_html.trim()
|
||||
);
|
||||
}
|
||||
|
||||
export function getFallbackPrivacyTerms(): PrivacyTermsPayload {
|
||||
return fallbackTerms as PrivacyTermsPayload;
|
||||
}
|
||||
|
||||
export async function fetchPrivacyTerms(): Promise<PrivacyTermsPayload> {
|
||||
try {
|
||||
const res = await fetch(`${getApiBase()}/api/privacy`, {
|
||||
cache: "no-store",
|
||||
signal: AbortSignal.timeout(PRIVACY_FETCH_TIMEOUT_MS),
|
||||
});
|
||||
if (!res.ok) return getFallbackPrivacyTerms();
|
||||
const data: unknown = await res.json();
|
||||
if (!isPrivacyTermsPayload(data)) return getFallbackPrivacyTerms();
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.warn("[TermsOfService] Failed to fetch privacy terms:", err);
|
||||
return getFallbackPrivacyTerms();
|
||||
}
|
||||
}
|
||||
|
||||
function termsVersionIsNewer(
|
||||
remoteLastUpdated: string,
|
||||
acceptedLastUpdated?: string,
|
||||
): boolean {
|
||||
if (!acceptedLastUpdated) return true;
|
||||
const remote = Date.parse(remoteLastUpdated);
|
||||
const accepted = Date.parse(acceptedLastUpdated);
|
||||
if (Number.isNaN(remote) || Number.isNaN(accepted)) {
|
||||
return remoteLastUpdated !== acceptedLastUpdated;
|
||||
}
|
||||
return remote > accepted;
|
||||
}
|
||||
|
||||
function resolvedTermsAcceptance(
|
||||
state: Pick<
|
||||
SettingsState,
|
||||
| "termsOfServiceLastAccepted"
|
||||
| "privacyStatementShown"
|
||||
| "privacyStatementLastUpdated"
|
||||
>,
|
||||
): string | undefined {
|
||||
if (state.termsOfServiceLastAccepted) return state.termsOfServiceLastAccepted;
|
||||
if (state.privacyStatementShown && state.privacyStatementLastUpdated) {
|
||||
return state.privacyStatementLastUpdated;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function shouldShowTermsOfServicePopup(
|
||||
state: Pick<
|
||||
SettingsState,
|
||||
| "termsOfServiceLastAccepted"
|
||||
| "privacyStatementShown"
|
||||
| "privacyStatementLastUpdated"
|
||||
>,
|
||||
terms: PrivacyTermsPayload,
|
||||
): boolean {
|
||||
return termsVersionIsNewer(terms.last_updated, resolvedTermsAcceptance(state));
|
||||
}
|
||||
|
||||
export function markTermsOfServiceAccepted(
|
||||
state: Pick<
|
||||
SettingsState,
|
||||
"termsOfServiceLastAccepted" | "privacyStatementShown" | "privacyStatementLastUpdated"
|
||||
>,
|
||||
lastUpdated: string,
|
||||
): void {
|
||||
state.termsOfServiceLastAccepted = lastUpdated;
|
||||
state.privacyStatementShown = true;
|
||||
state.privacyStatementLastUpdated = lastUpdated;
|
||||
}
|
||||
|
||||
function ensurePopupStyles() {
|
||||
if (document.getElementById(POPUP_STYLE_ID)) return;
|
||||
const style = document.createElement("style");
|
||||
style.id = POPUP_STYLE_ID;
|
||||
style.textContent = `
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-body {
|
||||
display: flex !important;
|
||||
flex-direction: column !important;
|
||||
gap: 0.75rem !important;
|
||||
min-height: 0 !important;
|
||||
flex: 1 1 auto !important;
|
||||
width: 92% !important;
|
||||
margin: 0 auto 1rem !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-intro {
|
||||
margin: 0 !important;
|
||||
font-size: 1rem !important;
|
||||
line-height: 1.5 !important;
|
||||
flex-shrink: 0 !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-scroll {
|
||||
flex: 1 1 auto !important;
|
||||
min-height: 8rem !important;
|
||||
max-height: min(42vh, 28rem) !important;
|
||||
overflow: auto !important;
|
||||
border: 1px solid rgba(17, 24, 39, 0.12) !important;
|
||||
border-radius: 0.75rem !important;
|
||||
padding: 0.85rem 1rem !important;
|
||||
background: rgba(17, 24, 39, 0.03) !important;
|
||||
font-size: 0.95rem !important;
|
||||
line-height: 1.55 !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay.bsplus-tos-overlay--dark .bsplus-tos-scroll {
|
||||
border-color: rgba(255, 255, 255, 0.12) !important;
|
||||
background: rgba(255, 255, 255, 0.04) !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-actions {
|
||||
display: flex !important;
|
||||
flex-direction: column !important;
|
||||
gap: 0.65rem !important;
|
||||
flex-shrink: 0 !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-btn {
|
||||
width: 100% !important;
|
||||
min-height: 2.75rem !important;
|
||||
padding: 0.75rem 1rem !important;
|
||||
border: none !important;
|
||||
border-radius: 0.75rem !important;
|
||||
font-size: 0.95rem !important;
|
||||
font-weight: 600 !important;
|
||||
font-family: inherit !important;
|
||||
cursor: pointer !important;
|
||||
transition: all 0.2s ease-in-out !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-btn--primary {
|
||||
background: #2563eb !important;
|
||||
color: #ffffff !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-btn--secondary {
|
||||
background: rgba(17, 24, 39, 0.08) !important;
|
||||
color: inherit !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay.bsplus-tos-overlay--dark .bsplus-tos-btn--secondary {
|
||||
background: rgba(255, 255, 255, 0.1) !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-btn:hover:not(:disabled) {
|
||||
transform: scale(1.02) !important;
|
||||
}
|
||||
|
||||
#whatsnewbk.bsplus-tos-overlay .bsplus-tos-btn:active:not(:disabled) {
|
||||
transform: scale(0.98) !important;
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
function buildTermsScrollContent(html: string): HTMLElement {
|
||||
const parsed = stringToHTML(html);
|
||||
const root = parsed.firstElementChild as HTMLElement | null;
|
||||
const scroll = document.createElement("div");
|
||||
scroll.className = "bsplus-tos-scroll privacyStatement";
|
||||
scroll.setAttribute("tabindex", "0");
|
||||
scroll.setAttribute("aria-label", "Terms of service and privacy policy");
|
||||
|
||||
if (root?.classList.contains("whatsnewTextContainer")) {
|
||||
scroll.innerHTML = root.innerHTML;
|
||||
} else if (root) {
|
||||
scroll.append(root);
|
||||
} else {
|
||||
scroll.innerHTML = parsed.innerHTML;
|
||||
}
|
||||
|
||||
return scroll;
|
||||
}
|
||||
|
||||
export async function openTermsOfServicePopup(
|
||||
onDismissed?: () => void,
|
||||
options?: { terms?: PrivacyTermsPayload; force?: boolean },
|
||||
) {
|
||||
if (document.getElementById("whatsnewbk")) {
|
||||
onDismissed?.();
|
||||
return;
|
||||
}
|
||||
|
||||
const terms = options?.terms ?? (await fetchPrivacyTerms());
|
||||
if (!options?.force && !shouldShowTermsOfServicePopup(settingsState, terms)) {
|
||||
onDismissed?.();
|
||||
return;
|
||||
}
|
||||
|
||||
ensurePopupStyles();
|
||||
|
||||
const header = stringToHTML(
|
||||
/* html */
|
||||
`<div class="whatsnewHeader">
|
||||
<h1>Terms of Service</h1>
|
||||
<p>Review the terms below, then accept or dismiss for now</p>
|
||||
</div>`,
|
||||
).firstChild as HTMLElement;
|
||||
|
||||
const scrollContent = buildTermsScrollContent(terms.whatsnew_html);
|
||||
|
||||
const body = document.createElement("div");
|
||||
body.className = "whatsnewTextContainer bsplus-tos-body";
|
||||
|
||||
const intro = stringToHTML(/* html */ `
|
||||
<p class="bsplus-tos-intro">
|
||||
Do you accept the terms of service? Read the full policy at
|
||||
<a href="${PRIVACY_POLICY_URL}" target="_blank" rel="noopener noreferrer">betterseqta.org/privacy</a>.
|
||||
</p>
|
||||
`).firstChild as HTMLElement;
|
||||
|
||||
const actions = stringToHTML(/* html */ `
|
||||
<div class="bsplus-tos-actions">
|
||||
<button type="button" id="bsplus-tos-accept" class="bsplus-tos-btn bsplus-tos-btn--primary">
|
||||
I accept the terms of service
|
||||
</button>
|
||||
<button type="button" id="bsplus-tos-decline" class="bsplus-tos-btn bsplus-tos-btn--secondary">
|
||||
Not now
|
||||
</button>
|
||||
</div>
|
||||
`).firstChild as HTMLElement;
|
||||
|
||||
body.append(intro, scrollContent, actions);
|
||||
|
||||
const isDark = settingsState.DarkMode;
|
||||
|
||||
openPopup({
|
||||
header,
|
||||
content: [body],
|
||||
containerClass: "whatsnewContainer--scrollBody whatsnewContainer--compact",
|
||||
backgroundClass: isDark
|
||||
? "bsplus-tos-overlay bsplus-tos-overlay--dark"
|
||||
: "bsplus-tos-overlay",
|
||||
hideCloseButton: true,
|
||||
closeOnBackdrop: false,
|
||||
closeOnEscape: false,
|
||||
animateSelector: ".bsplus-tos-body > *",
|
||||
afterClose: onDismissed,
|
||||
actions: [
|
||||
{
|
||||
id: "bsplus-tos-accept",
|
||||
onClick: () => {
|
||||
markTermsOfServiceAccepted(settingsState, terms.last_updated);
|
||||
void closePopup();
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "bsplus-tos-decline",
|
||||
onClick: () => {
|
||||
void closePopup();
|
||||
},
|
||||
},
|
||||
],
|
||||
onReady: ({ container }) => {
|
||||
container.setAttribute("role", "dialog");
|
||||
container.setAttribute("aria-modal", "true");
|
||||
container.setAttribute("aria-labelledby", "bsplus-tos-title");
|
||||
const title = container.querySelector(".whatsnewHeader h1");
|
||||
if (title) title.id = "bsplus-tos-title";
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -11,6 +11,11 @@ import {
|
||||
shouldShowCoursesAssessmentsFixPopup,
|
||||
} from "./OpenCoursesAssessmentsFixPopup";
|
||||
import { syncApiBaseToBackground } from "../DevApiBase";
|
||||
import {
|
||||
fetchPrivacyTerms,
|
||||
openTermsOfServicePopup,
|
||||
shouldShowTermsOfServicePopup,
|
||||
} from "./OpenTermsOfServicePopup";
|
||||
|
||||
type QueueStep = (goNext: () => void) => void;
|
||||
|
||||
@@ -18,7 +23,8 @@ type QueueStep = (goNext: () => void) => void;
|
||||
* Runs startup modals in order: What's New (if the extension just updated),
|
||||
* Courses/Assessments fix notice (only if features are off),
|
||||
* Theme of the Month (when the user hasn't dismissed this calendar month),
|
||||
* then feedback reply notifications for pending submissions.
|
||||
* feedback reply notifications for pending submissions, then terms of service
|
||||
* acceptance (always last).
|
||||
*/
|
||||
export async function runStartupPopupQueue() {
|
||||
// Make sure the background script knows about any dev-mode API override
|
||||
@@ -50,6 +56,17 @@ export async function runStartupPopupQueue() {
|
||||
steps.push(feedbackReplyStep);
|
||||
}
|
||||
|
||||
steps.push((goNext) => {
|
||||
void (async () => {
|
||||
const privacyTerms = await fetchPrivacyTerms();
|
||||
if (!shouldShowTermsOfServicePopup(settingsState, privacyTerms)) {
|
||||
goNext();
|
||||
return;
|
||||
}
|
||||
await openTermsOfServicePopup(goNext, { terms: privacyTerms });
|
||||
})();
|
||||
});
|
||||
|
||||
function runNext() {
|
||||
const step = steps.shift();
|
||||
if (step) step(runNext);
|
||||
|
||||
@@ -24,6 +24,7 @@ const OPTIONAL_UNSET_MEANS_DEFAULT_KEYS = [
|
||||
"selectedFont",
|
||||
"privacyStatementShown",
|
||||
"privacyStatementLastUpdated",
|
||||
"termsOfServiceLastAccepted",
|
||||
"bsCloudAutoSyncAnnouncementShown",
|
||||
"coursesAssessmentsFixPopupShown",
|
||||
"themeOfTheMonthDismissedMonth",
|
||||
|
||||
@@ -35,6 +35,7 @@ const SETTINGS_STORAGE_KEYS = [
|
||||
"mockNotices",
|
||||
"hideSensitiveContent",
|
||||
"privacyStatementLastUpdated",
|
||||
"termsOfServiceLastAccepted",
|
||||
"themeOfTheMonthDismissedMonth",
|
||||
"themeOfTheMonthLastSeenId",
|
||||
...PLUGIN_SETTINGS_KEYS,
|
||||
|
||||
@@ -36,6 +36,8 @@ export interface SettingsState {
|
||||
justupdated?: boolean;
|
||||
privacyStatementShown?: boolean;
|
||||
privacyStatementLastUpdated?: string;
|
||||
/** ISO timestamp from `/api/privacy` `last_updated` when the user accepted ToS. */
|
||||
termsOfServiceLastAccepted?: string;
|
||||
/** One-time announcement: BS Cloud automatic settings sync (last in startup popup queue). */
|
||||
bsCloudAutoSyncAnnouncementShown?: boolean;
|
||||
/** One-time notice when BetterSEQTA features are off after the Courses/Assessments fix. */
|
||||
|
||||
Reference in new issue
Block a user