perf: prevent background from being mounted when not in use

This commit is contained in:
SethBurkart123
2024-02-09 18:22:58 +11:00
parent 410b817cca
commit 871cef91cb
9 changed files with 53 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"transformers": {
"raw:*": ["@parcel/transformer-raw"]
}
}
+3
View File
@@ -3,6 +3,7 @@
"version": "3.2.4", "version": "3.2.4",
"type": "module", "type": "module",
"description": "BetterSEQTA+ is a browser extension that adds features to SEQTA.", "description": "BetterSEQTA+ is a browser extension that adds features to SEQTA.",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": { "scripts": {
"dev": "parcel watch manifest.json --host localhost --config @parcel/config-webextension --no-hmr --no-content-hash", "dev": "parcel watch manifest.json --host localhost --config @parcel/config-webextension --no-hmr --no-content-hash",
"build": "parcel build manifest.json --config @parcel/config-webextension --no-content-hash --no-cache --no-source-maps", "build": "parcel build manifest.json --config @parcel/config-webextension --no-content-hash --no-cache --no-source-maps",
@@ -24,6 +25,7 @@
"devDependencies": { "devDependencies": {
"@parcel/config-webextension": "^2.11.0", "@parcel/config-webextension": "^2.11.0",
"@parcel/optimizer-data-url": "^2.11.0", "@parcel/optimizer-data-url": "^2.11.0",
"@parcel/packager-ts": "2.11.0",
"@parcel/transformer-inline-string": "^2.11.0", "@parcel/transformer-inline-string": "^2.11.0",
"@parcel/transformer-sass": "2.11.0", "@parcel/transformer-sass": "2.11.0",
"assert": "^2.0.0", "assert": "^2.0.0",
@@ -50,6 +52,7 @@
"yarn": "^1.22.21" "yarn": "^1.22.21"
}, },
"dependencies": { "dependencies": {
"@parcel/transformer-raw": "^2.11.0",
"@sentry/browser": "^7.85.0", "@sentry/browser": "^7.85.0",
"@sentry/react": "^7.88.0", "@sentry/react": "^7.88.0",
"@sentry/webpack-plugin": "^2.10.2", "@sentry/webpack-plugin": "^2.10.2",
@@ -3,6 +3,7 @@ import { downloadPresetBackground, openDB, readAllData, writeData } from "../hoo
import presetBackgrounds from "../assets/presetBackgrounds"; import presetBackgrounds from "../assets/presetBackgrounds";
import "./BackgroundSelector.css"; import "./BackgroundSelector.css";
import { disableTheme } from "../hooks/ThemeManagment"; import { disableTheme } from "../hooks/ThemeManagment";
import browser from "webextension-polyfill";
// Custom Types and Interfaces // Custom Types and Interfaces
export interface Background { export interface Background {
@@ -21,12 +22,27 @@ interface BackgroundSelectorProps {
isEditMode: boolean; isEditMode: boolean;
} }
async function GetTheme() {
return localStorage.getItem('selectedBackground');
}
async function SetTheme(theme: string) {
localStorage.setItem('selectedBackground', theme);
await browser.storage.local.set({ theme });
}
function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: BackgroundSelectorProps) { function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: BackgroundSelectorProps) {
const [backgrounds, setBackgrounds] = useState<Background[]>([]); const [backgrounds, setBackgrounds] = useState<Background[]>([]);
const [selectedBackground, setSelectedBackground] = useState<string | null>(localStorage.getItem('selectedBackground')); const [selectedBackground, setSelectedBackground] = useState<string | null>();
const [downloadedPresetIds, setDownloadedPresetIds] = useState<string[]>([]); const [downloadedPresetIds, setDownloadedPresetIds] = useState<string[]>([]);
const [downloadProgress, setDownloadProgress] = useState<Record<string, number>>({}); const [downloadProgress, setDownloadProgress] = useState<Record<string, number>>({});
useEffect(() => {
GetTheme().then((theme) => {
setSelectedBackground(theme);
});
}, []);
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>): Promise<void> => { const handleFileChange = async (e: ChangeEvent<HTMLInputElement>): Promise<void> => {
const file = e.target.files?.[0]; const file = e.target.files?.[0];
if (!file) return; if (!file) return;
@@ -77,7 +93,7 @@ function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: Backg
disableTheme(); disableTheme();
setSelectedType('background'); setSelectedType('background');
setSelectedBackground(fileId); setSelectedBackground(fileId);
localStorage.setItem('selectedBackground', fileId); SetTheme(fileId);
}; };
const deleteBackground = async (fileId: string): Promise<void> => { const deleteBackground = async (fileId: string): Promise<void> => {
@@ -97,7 +113,7 @@ function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: Backg
setSelectedType('background'); setSelectedType('background');
disableTheme(); disableTheme();
setSelectedBackground(null); setSelectedBackground(null);
localStorage.removeItem('selectedBackground'); SetTheme('');
}; };
const calcCircumference = (radius: number) => 2 * Math.PI * radius; const calcCircumference = (radius: number) => 2 * Math.PI * radius;
+1
View File
@@ -1,5 +1,6 @@
export interface SettingsState { export interface SettingsState {
notificationCollector: boolean; notificationCollector: boolean;
theme: string;
lessonAlerts: boolean; lessonAlerts: boolean;
telemetry: boolean; telemetry: boolean;
animatedBackground: boolean; animatedBackground: boolean;
+7 -1
View File
@@ -1,7 +1,13 @@
import backgroundPage from 'url:./background/background.html' import backgroundPage from 'url:./background/background.html'
import browser from 'webextension-polyfill';
import { SettingsState } from '../../types/storage';
export async function appendBackgroundToUI() { export async function appendBackgroundToUI() {
console.log('Starting appendBackgroundToUI...'); const settings = await browser.storage.local.get() as SettingsState;
console.log(settings.theme);
if (settings.theme == '') return;
const parent = document.getElementById('container'); const parent = document.getElementById('container');
+1 -1
View File
@@ -24,6 +24,6 @@
<!-- Container for the media --> <!-- Container for the media -->
<div id="media-container"></div> <div id="media-container"></div>
<script src="background.ts"></script> <script src="./background.ts"></script>
</body> </body>
</html> </html>
+4 -4
View File
@@ -19,7 +19,7 @@ const openDB = (): Promise<IDBDatabase> => {
request.onsuccess = () => resolve(request.result); request.onsuccess = () => resolve(request.result);
request.onupgradeneeded = (event: IDBVersionChangeEvent) => { request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
// @ts-expect-error // @ts-expect-error - The event type is not recognized by TypeScript
event?.target?.result.createObjectStore('backgrounds', { keyPath: 'id' }); event?.target?.result.createObjectStore('backgrounds', { keyPath: 'id' });
}; };
}); });
@@ -27,7 +27,9 @@ const openDB = (): Promise<IDBDatabase> => {
const readData = async (): Promise<Data | null> => { const readData = async (): Promise<Data | null> => {
const selectedBackground = localStorage.getItem('selectedBackground'); const selectedBackground = localStorage.getItem('selectedBackground');
if (!selectedBackground) {
//const selectedBackground = localStorage.getItem('selectedBackground');
if (!selectedBackground || selectedBackground === '') {
return null; return null;
} }
@@ -46,8 +48,6 @@ const updateBackground = async (): Promise<void> => {
try { try {
const data = await readData(); const data = await readData();
if (!data) { if (!data) {
console.log('No data found in IndexedDB.');
const container = document.getElementById('media-container'); const container = document.getElementById('media-container');
const currentMedia = container?.querySelector('.current-media'); const currentMedia = container?.querySelector('.current-media');
if (currentMedia) { if (currentMedia) {
+11
View File
@@ -11,6 +11,7 @@ import {
} from '../../SEQTA'; } from '../../SEQTA';
import { updateBgDurations } from '../ui/Animation'; import { updateBgDurations } from '../ui/Animation';
import { getDarkMode, updateAllColors } from '../ui/colors/Manager'; import { getDarkMode, updateAllColors } from '../ui/colors/Manager';
import { appendBackgroundToUI } from '../ui/ImageBackgrounds';
export default class StorageListener { export default class StorageListener {
@@ -21,6 +22,7 @@ export default class StorageListener {
} }
handleStorageChanges(changes: any) { handleStorageChanges(changes: any) {
console.log('Storage changed:', changes);
Object.keys(changes).forEach((changeKey) => { Object.keys(changes).forEach((changeKey) => {
switch (changeKey) { switch (changeKey) {
@@ -81,6 +83,15 @@ export default class StorageListener {
} }
break; break;
case 'theme':
if (changes.theme.newValue === '' && changes.theme.oldValue !== '') {
document.querySelector('iframe#background')?.remove();
} else if (changes.theme.newValue !== '' && changes.theme.oldValue === '') {
appendBackgroundToUI();
}
break;
// Add default case if you need to handle a case where changeKey does not match any case // Add default case if you need to handle a case where changeKey does not match any case
default: default:
// Handle unknown changeKey if necessary // Handle unknown changeKey if necessary
+1
View File
@@ -1,5 +1,6 @@
export interface SettingsState { export interface SettingsState {
DarkMode: boolean; DarkMode: boolean;
theme: string;
animatedbk: boolean; animatedbk: boolean;
bksliderinput: string; bksliderinput: string;
customshortcuts: CustomShortcut[]; customshortcuts: CustomShortcut[];