mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-10 06:52:05 +01:00
Analytics for themes
This commit is contained in:
@@ -21,7 +21,8 @@ export type CommandResultView = {
|
|||||||
view: ViewFactory
|
view: ViewFactory
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AppStateChangeHandler = (state: AppState) => void;
|
export type AppStateAttribute = keyof AppState;
|
||||||
|
export type AppStateChangeHandler = (state: AppState, attribute : AppStateAttribute) => void;
|
||||||
|
|
||||||
type ViewFactory = () => JSX.Element;
|
type ViewFactory = () => JSX.Element;
|
||||||
|
|
||||||
@@ -65,12 +66,12 @@ export default class AppState {
|
|||||||
const key = generateKey();
|
const key = generateKey();
|
||||||
this.commandResults.unshift({ key, input, view });
|
this.commandResults.unshift({ key, input, view });
|
||||||
log.debug(`command result added: ${input}`);
|
log.debug(`command result added: ${input}`);
|
||||||
this.triggerChanged();
|
this.triggerChanged('commandResults');
|
||||||
}
|
}
|
||||||
|
|
||||||
clearCommandResults() {
|
clearCommandResults() {
|
||||||
this.commandResults = [];
|
this.commandResults = [];
|
||||||
this.triggerChanged();
|
this.triggerChanged('commandResults');
|
||||||
}
|
}
|
||||||
|
|
||||||
removeResult(index: number) {
|
removeResult(index: number) {
|
||||||
@@ -78,68 +79,72 @@ export default class AppState {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
this.commandResults.splice(index, 1);
|
this.commandResults.splice(index, 1);
|
||||||
this.triggerChanged();
|
this.triggerChanged('commandResults');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleEmphasizeBytes(value?: boolean) {
|
toggleEmphasizeBytes(value?: boolean) {
|
||||||
this.emphasizeBytes = value !== null && value !== undefined ? value : !this.emphasizeBytes;
|
this.emphasizeBytes = value !== null && value !== undefined ? value : !this.emphasizeBytes;
|
||||||
this.triggerChanged();
|
this.triggerChanged('emphasizeBytes');
|
||||||
}
|
}
|
||||||
|
|
||||||
onChange(handler: AppStateChangeHandler) {
|
onChange(handler: AppStateChangeHandler) {
|
||||||
this.changeHandlers.push(handler);
|
this.changeHandlers.push(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
triggerChanged() {
|
triggerChanged(attribute: AppStateAttribute) {
|
||||||
this.changeHandlers.forEach(h => h(this));
|
this.changeHandlers.forEach(h => h(this, attribute));
|
||||||
}
|
}
|
||||||
|
|
||||||
setUiTheme(theme: string) {
|
setUiTheme(theme: string) {
|
||||||
|
|
||||||
|
if(this.uiTheme === theme)
|
||||||
|
return;
|
||||||
|
|
||||||
this.uiTheme = theme;
|
this.uiTheme = theme;
|
||||||
this.triggerChanged();
|
this.triggerChanged('uiTheme');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleDebugMode() {
|
toggleDebugMode() {
|
||||||
this.debugMode = !this.debugMode;
|
this.debugMode = !this.debugMode;
|
||||||
this.triggerChanged();
|
this.triggerChanged('debugMode');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleShowSettings() {
|
toggleShowSettings() {
|
||||||
this.showSettings = !this.showSettings;
|
this.showSettings = !this.showSettings;
|
||||||
this.triggerChanged();
|
this.triggerChanged('showSettings');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleAnnotateTypes(value?: boolean) {
|
toggleAnnotateTypes(value?: boolean) {
|
||||||
this.annotateTypes = value !== null && value !== undefined ? value : !this.annotateTypes;
|
this.annotateTypes = value !== null && value !== undefined ? value : !this.annotateTypes;
|
||||||
this.triggerChanged();
|
this.triggerChanged('annotateTypes');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleDimExtrBits() {
|
toggleDimExtrBits() {
|
||||||
this.dimExtraBits = !this.dimExtraBits;
|
this.dimExtraBits = !this.dimExtraBits;
|
||||||
this.triggerChanged();
|
this.triggerChanged('dimExtraBits');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleCenteredLayout(value?: boolean) {
|
toggleCenteredLayout(value?: boolean) {
|
||||||
this.centeredLayout = value !== null && value !== undefined ? value : !this.centeredLayout;
|
this.centeredLayout = value !== null && value !== undefined ? value : !this.centeredLayout;
|
||||||
this.triggerChanged();
|
this.triggerChanged('centeredLayout');
|
||||||
}
|
}
|
||||||
|
|
||||||
registerVisit() {
|
registerVisit() {
|
||||||
this.pageVisitsCount++;
|
this.pageVisitsCount++;
|
||||||
this.triggerChanged();
|
this.triggerChanged('pageVisitsCount');
|
||||||
}
|
}
|
||||||
|
|
||||||
onDonationClicked(): boolean {
|
onDonationClicked(): boolean {
|
||||||
if (this.donationClicked === true) return false;
|
if (this.donationClicked === true) return false;
|
||||||
|
|
||||||
this.donationClicked = true;
|
this.donationClicked = true;
|
||||||
this.triggerChanged();
|
this.triggerChanged('donationClicked');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
setCookieDisclaimerHidden(value: boolean) {
|
setCookieDisclaimerHidden(value: boolean) {
|
||||||
this.cookieDisclaimerHidden = value;
|
this.cookieDisclaimerHidden = value;
|
||||||
this.triggerChanged();
|
this.triggerChanged('cookieDisclaimerHidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
getPersistData(): PersistedAppData {
|
getPersistData(): PersistedAppData {
|
||||||
|
|||||||
@@ -25,10 +25,7 @@ const shellModule = {
|
|||||||
cmd.command("light", () => appState.setUiTheme('light'));
|
cmd.command("light", () => appState.setUiTheme('light'));
|
||||||
cmd.command("midnight", () => appState.setUiTheme('midnight'));
|
cmd.command("midnight", () => appState.setUiTheme('midnight'));
|
||||||
cmd.command("settings", () => appState.toggleShowSettings());
|
cmd.command("settings", () => appState.toggleShowSettings());
|
||||||
cmd.command("bladerunner", () => {
|
cmd.command("bladerunner", () => appState.setUiTheme('bladerunner'));
|
||||||
appState.setUiTheme('bladerunner');
|
|
||||||
sendAnalyticsEvent({eventCategory: "UI", eventAction: "ThemeChanged", eventLabel: "bladerunner"});
|
|
||||||
});
|
|
||||||
cmd.command("bladerunner-easter", (c: CommandInput) => {
|
cmd.command("bladerunner-easter", (c: CommandInput) => {
|
||||||
document.querySelector('.app-root')!.scrollTo(0, 0);
|
document.querySelector('.app-root')!.scrollTo(0, 0);
|
||||||
cmd.execute("bladerunner");
|
cmd.execute("bladerunner");
|
||||||
@@ -70,38 +67,51 @@ const shellModule = {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if(appState.env !== 'prod') {
|
if(appState.env !== 'prod') {
|
||||||
|
|
||||||
// Default command for development purposes
|
// Default command for development purposes
|
||||||
cmd.command({
|
registerDefaultCommand(cmd, appState);
|
||||||
canHandle: (s: string) => s.indexOf('default') === 0,
|
|
||||||
handle: (s: CommandInput) => {
|
|
||||||
|
|
||||||
const executeCommand = (c: string) => {
|
|
||||||
|
|
||||||
if(c.length === 0) {
|
|
||||||
return "Default comand: " + localStorage.getItem(STARTUP_COMMAND_KEY);
|
|
||||||
}
|
|
||||||
else if(c === 'clear') {
|
|
||||||
localStorage.removeItem(STARTUP_COMMAND_KEY);
|
|
||||||
return "Default startup command cleared";
|
|
||||||
}
|
|
||||||
|
|
||||||
localStorage.setItem(STARTUP_COMMAND_KEY, c);
|
|
||||||
return `Default startup command saved: ${c}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const command = s.input.substring(7).trim();
|
|
||||||
const result = executeCommand(command);
|
|
||||||
appState.addCommandResult(s.input, () => <TextResultView text={result} />);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
cmd.onError((input: string, err: Error) => appState.addCommandResult(input, () => <ErrorResultView errorMessage={err.toString()} />));
|
cmd.onError((input: string, err: Error) => appState.addCommandResult(input, () => <ErrorResultView errorMessage={err.toString()} />));
|
||||||
|
|
||||||
|
registerStateChangeHandlers(appState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default shellModule;
|
export default shellModule;
|
||||||
|
|
||||||
|
|
||||||
|
function registerStateChangeHandlers(appState: AppState) {
|
||||||
|
appState.onChange((state: AppState, attribute: keyof AppState) => {
|
||||||
|
if (attribute === 'uiTheme') {
|
||||||
|
sendAnalyticsEvent({ eventCategory: "UI", eventAction: `set_theme_${state.uiTheme}` });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerDefaultCommand(cmd: CmdShell, appState: AppState) {
|
||||||
|
cmd.command({
|
||||||
|
canHandle: (s: string) => s.indexOf('default') === 0,
|
||||||
|
handle: (s: CommandInput) => {
|
||||||
|
|
||||||
|
const executeCommand = (c: string) => {
|
||||||
|
|
||||||
|
if (c.length === 0) {
|
||||||
|
return "Default comand: " + localStorage.getItem(STARTUP_COMMAND_KEY);
|
||||||
|
}
|
||||||
|
else if (c === 'clear') {
|
||||||
|
localStorage.removeItem(STARTUP_COMMAND_KEY);
|
||||||
|
return "Default startup command cleared";
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem(STARTUP_COMMAND_KEY, c);
|
||||||
|
return `Default startup command saved: ${c}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const command = s.input.substring(7).trim();
|
||||||
|
const result = executeCommand(command);
|
||||||
|
appState.addCommandResult(s.input, () => <TextResultView text={result} />);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user