Skip to content

Commit

Permalink
fix: check if user is entitled to theme before activating it (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Feb 16, 2022
1 parent 1983b94 commit ca3112d
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions app/assets/javascripts/services/themeManager.ts
Expand Up @@ -69,26 +69,51 @@ export class ThemeManager extends ApplicationService {

async onAppEvent(event: ApplicationEvent) {
super.onAppEvent(event);
if (event === ApplicationEvent.SignedOut) {
this.deactivateAllThemes();
this.activeThemes = [];
this.application?.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
} else if (event === ApplicationEvent.StorageReady) {
await this.activateCachedThemes();
} else if (event === ApplicationEvent.FeaturesUpdated) {
this.reloadThemeStatus();
} else if (event === ApplicationEvent.Launched) {
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', this.colorSchemeEventHandler);
} else if (event === ApplicationEvent.PreferencesChanged) {
const prefersDarkColorScheme = window.matchMedia(
'(prefers-color-scheme: dark)'
);
this.setThemeAsPerColorScheme(prefersDarkColorScheme.matches);
switch (event) {
case ApplicationEvent.SignedOut: {
this.deactivateAllThemes();
this.activeThemes = [];
this.application?.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
break;
}
case ApplicationEvent.StorageReady: {
await this.activateCachedThemes();
break;
}
case ApplicationEvent.FeaturesUpdated: {
this.reloadThemeStatus();
break;
}
case ApplicationEvent.Launched: {
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', this.colorSchemeEventHandler);
break;
}
case ApplicationEvent.PreferencesChanged: {
const prefersDarkColorScheme = window.matchMedia(
'(prefers-color-scheme: dark)'
);
this.setThemeAsPerColorScheme(prefersDarkColorScheme.matches);
break;
}
case ApplicationEvent.LocalDataLoaded: {
const themes = this.application.getDisplayableItems(
ContentType.Theme
) as SNTheme[];
themes.forEach((theme) => {
if (
theme.active &&
this.application.getFeatureStatus(theme.identifier) !==
FeatureStatus.Entitled
) {
this.application.toggleTheme(theme);
}
});
}
}
}

Expand Down Expand Up @@ -181,6 +206,12 @@ export class ThemeManager extends ApplicationService {
if (this.activeThemes.find((uuid) => uuid === theme.uuid)) {
return;
}
if (
this.application.getFeatureStatus(theme.identifier) !==
FeatureStatus.Entitled
) {
return;
}
this.activeThemes.push(theme.uuid);
const url = this.application.componentManager.urlForComponent(theme);
if (!url) {
Expand Down

0 comments on commit ca3112d

Please sign in to comment.