Skip to content

Commit

Permalink
perf: prevent background from being mounted when not in use
Browse files Browse the repository at this point in the history
  • Loading branch information
SethBurkart123 authored and SethBurkart123 committed Feb 9, 2024
1 parent 9e57256 commit e8e8ec0
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transformers": {
"raw:*": ["@parcel/transformer-raw"]
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.2.4",
"type": "module",
"description": "BetterSEQTA+ is a browser extension that adds features to SEQTA.",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"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",
Expand All @@ -24,6 +25,7 @@
"devDependencies": {
"@parcel/config-webextension": "^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-sass": "2.11.0",
"assert": "^2.0.0",
Expand All @@ -50,6 +52,7 @@
"yarn": "^1.22.21"
},
"dependencies": {
"@parcel/transformer-raw": "^2.11.0",
"@sentry/browser": "^7.85.0",
"@sentry/react": "^7.88.0",
"@sentry/webpack-plugin": "^2.10.2",
Expand Down
22 changes: 19 additions & 3 deletions src/interface/components/BackgroundSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { downloadPresetBackground, openDB, readAllData, writeData } from "../hoo
import presetBackgrounds from "../assets/presetBackgrounds";
import "./BackgroundSelector.css";
import { disableTheme } from "../hooks/ThemeManagment";
import browser from "webextension-polyfill";

// Custom Types and Interfaces
export interface Background {
Expand All @@ -21,12 +22,27 @@ interface BackgroundSelectorProps {
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) {
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 [downloadProgress, setDownloadProgress] = useState<Record<string, number>>({});

useEffect(() => {
GetTheme().then((theme) => {
setSelectedBackground(theme);
});
}, []);

const handleFileChange = async (e: ChangeEvent<HTMLInputElement>): Promise<void> => {
const file = e.target.files?.[0];
if (!file) return;
Expand Down Expand Up @@ -77,7 +93,7 @@ function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: Backg
disableTheme();
setSelectedType('background');
setSelectedBackground(fileId);
localStorage.setItem('selectedBackground', fileId);
SetTheme(fileId);
};

const deleteBackground = async (fileId: string): Promise<void> => {
Expand All @@ -97,7 +113,7 @@ function BackgroundSelector({ selectedType, setSelectedType, isEditMode }: Backg
setSelectedType('background');
disableTheme();
setSelectedBackground(null);
localStorage.removeItem('selectedBackground');
SetTheme('');
};

const calcCircumference = (radius: number) => 2 * Math.PI * radius;
Expand Down
1 change: 1 addition & 0 deletions src/interface/types/AppProps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface SettingsState {
notificationCollector: boolean;
theme: string;
lessonAlerts: boolean;
telemetry: boolean;
animatedBackground: boolean;
Expand Down
8 changes: 7 additions & 1 deletion src/seqta/ui/ImageBackgrounds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import backgroundPage from 'url:./background/background.html'
import browser from 'webextension-polyfill';
import { SettingsState } from '../../types/storage';

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');

Expand Down
4 changes: 2 additions & 2 deletions src/seqta/ui/background/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<body>
<!-- Container for the media -->
<div id="media-container"></div>

<script src="background.ts"></script>
<script src="./background.ts"></script>
</body>
</html>
8 changes: 4 additions & 4 deletions src/seqta/ui/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ const openDB = (): Promise<IDBDatabase> => {
request.onsuccess = () => resolve(request.result);

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' });
};
});
};

const readData = async (): Promise<Data | null> => {
const selectedBackground = localStorage.getItem('selectedBackground');
if (!selectedBackground) {

//const selectedBackground = localStorage.getItem('selectedBackground');
if (!selectedBackground || selectedBackground === '') {
return null;
}

Expand All @@ -46,8 +48,6 @@ const updateBackground = async (): Promise<void> => {
try {
const data = await readData();
if (!data) {
console.log('No data found in IndexedDB.');

const container = document.getElementById('media-container');
const currentMedia = container?.querySelector('.current-media');
if (currentMedia) {
Expand Down
11 changes: 11 additions & 0 deletions src/seqta/utils/StorageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../SEQTA';
import { updateBgDurations } from '../ui/Animation';
import { getDarkMode, updateAllColors } from '../ui/colors/Manager';
import { appendBackgroundToUI } from '../ui/ImageBackgrounds';


export default class StorageListener {
Expand All @@ -21,6 +22,7 @@ export default class StorageListener {
}

handleStorageChanges(changes: any) {
console.log('Storage changed:', changes);
Object.keys(changes).forEach((changeKey) => {
switch (changeKey) {

Expand Down Expand Up @@ -80,6 +82,15 @@ export default class StorageListener {
document.documentElement.classList.remove('transparencyEffects');
}
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
default:
Expand Down
1 change: 1 addition & 0 deletions src/types/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface SettingsState {
DarkMode: boolean;
theme: string;
animatedbk: boolean;
bksliderinput: string;
customshortcuts: CustomShortcut[];
Expand Down

0 comments on commit e8e8ec0

Please sign in to comment.