Skip to content

Commit

Permalink
[v1.0.2] Code cleanup and fix autoRotation setting
Browse files Browse the repository at this point in the history
  • Loading branch information
GriefMoDz committed Jan 14, 2023
1 parent 95f4a92 commit 3833102
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 183 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Expand Up @@ -7,7 +7,7 @@
"discordID": "350227339784880130",
"github": "griefmodz"
},
"version": "1.0.1",
"version": "1.0.2",
"updater": {
"type": "github",
"id": "griefmodz/statistic-counter"
Expand Down
10 changes: 5 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "statistic-counter",
"version": "1.0.1",
"version": "1.0.2",
"description": "A Replugged plug-in that has been ported from Powercord which introduces an interchangeable statistic counter in-between the home button and servers list.",
"engines": {
"node": ">=14.0.0"
Expand All @@ -11,10 +11,10 @@
"bundle": "tsx scripts/bundle.ts",
"build-and-bundle": "pnpm run build --no-install && pnpm run bundle",
"check": "tsc --noEmit",
"prettier:check": "prettier ./src ./scripts --check",
"eslint:check": "eslint ./src ./scripts",
"prettier:fix": "prettier ./src ./scripts --write",
"eslint:fix": "eslint ./src ./scripts --fix",
"prettier:check": "prettier ./src --check",
"eslint:check": "eslint ./src",
"prettier:fix": "prettier ./src --write",
"eslint:fix": "eslint ./src --fix",
"lint": "pnpm run prettier:check && pnpm run eslint:check && pnpm run check",
"lint:fix": "pnpm run prettier:fix && pnpm run eslint:fix"
},
Expand Down
11 changes: 6 additions & 5 deletions src/components/Counter.tsx
@@ -1,13 +1,14 @@
import CounterStore from '../lib/store';
import CounterStore from '@lib/store';
import ErrorBoundary from './ErrorBoundary';
import '../main.css';

import { common, webpack } from 'replugged';
import { ActionTypes, Counters } from '../lib/constants';
import type { CounterState, Dispatcher, Flux, GuildAvailabilityStore, RelationshipStore, PresenceStore } from '../types';
import { ActionTypes, Counters } from '@lib/constants';
import type { CounterState, Flux, GuildAvailabilityStore, RelationshipStore, PresenceStore } from '@types';
import type { FluxDispatcher } from 'replugged/dist/renderer/modules/webpack/common';

const Flux = common.flux as unknown as Flux;
const FluxDispatcher = common.fluxDispatcher as unknown as Dispatcher;
const FluxDispatcher = common.fluxDispatcher as unknown as FluxDispatcher;

const { getExportsForProps } = webpack;

Expand Down Expand Up @@ -76,7 +77,7 @@ function Counter(): JSX.Element {
className='statistic-counter'
onInterval={handleOnInterval}
interval={settings.get('autoRotationDelay', 3e4)}
disable={activeCounter === nextCounter ?? !settings.get('autoRotation', false)}
disable={activeCounter === nextCounter || !settings.get('autoRotation', false)}
pauseOnHover={!!settings.get('autoRotationHoverPause', true)}>
<span className={activeCounter !== nextCounter ? 'clickable' : ''} onClick={handleOnClick}>
{Messages[Counters[activeCounter].translationKey]}{counters[Counters[activeCounter].storeKey]}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ErrorBoundary.tsx
@@ -1,5 +1,5 @@
import { common } from 'replugged';
import type { ErrorBoundaryState } from '../types';
import type { ErrorBoundaryState } from '@types';

const { React } = common;

Expand Down
16 changes: 8 additions & 8 deletions src/index.tsx
@@ -1,17 +1,17 @@
import { Injector, Logger, util, webpack } from 'replugged';
import { findInReactTree, forceUpdate } from './lib/util';
import { PLUGIN_ID } from './lib/constants';
import { Counter } from './components';
import { findInReactTree, forceUpdate } from '@lib/util';
import { PLUGIN_ID } from '@lib/constants';
import { Counter } from '@components';

import type { GuildsNavComponent } from './types';
import type { GuildClasses, GuildsNavComponent } from '@types';

const inject = new Injector();
const logger = Logger.plugin(PLUGIN_ID.replace(/-/g, ' '), '#3ba55c');

let GuildClasses: { guilds: string; };
let GuildClasses: GuildClasses;

export async function start(): Promise<void> {
GuildClasses = await webpack.waitForModule<any>(webpack.filters.byProps('guilds', 'sidebar'));
GuildClasses = await webpack.waitForModule<GuildClasses>(webpack.filters.byProps('guilds', 'sidebar'));

patchGuildsNav();
}
Expand All @@ -25,7 +25,7 @@ export function stop(): void {
export async function patchGuildsNav(): Promise<void> {
const start = performance.now();

const GuildsNav: GuildsNavComponent = await webpack.waitForModule<any>(webpack.filters.bySource('guildsnav'));
const GuildsNav = await webpack.waitForModule<GuildsNavComponent>(webpack.filters.bySource('guildsnav'));

inject.after(GuildsNav, 'type', ([props], res) => {
const GuildsNavBar = findInReactTree(res, (node) => node?.props?.className?.includes(props.className));
Expand All @@ -45,7 +45,7 @@ export async function patchGuildsNav(): Promise<void> {
HomeButtonIndex && (StatisticCounterIndex = HomeButtonIndex + 1);
}

NavScroll.props.children.splice(StatisticCounterIndex, 0, <Counter/>);
NavScroll.props.children.splice(StatisticCounterIndex, 0, <Counter />);

return res;
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants.ts
@@ -1,4 +1,4 @@
import type { CounterProps, CounterType } from '../types';
import type { CounterProps, CounterType } from '@types';

export const PLUGIN_ID = 'Statistic-Counter';

Expand Down
18 changes: 9 additions & 9 deletions src/lib/store.ts
@@ -1,9 +1,9 @@
import { common, settings } from 'replugged';
import { SettingsManager } from 'replugged/dist/renderer/apis/settings';
import type { CounterSettings, CounterType, Dispatcher, Flux, Store } from '../types';
import type { CounterSettings, CounterStore as CounterStoreType, CounterType, Flux, Store } from '@types';

const Flux = common.flux as unknown as Flux;
const FluxDispatcher = common.fluxDispatcher as unknown as Dispatcher;
const FluxDispatcher = common.fluxDispatcher;

import { ActionTypes, Counters } from './constants';

Expand All @@ -16,32 +16,32 @@ interface CounterStoreState {
}

class CounterStore extends Flux.Store {
static displayName = 'StatisticCounterStore';
public static displayName = 'StatisticCounterStore';

get state(): CounterStoreState {
public get state(): CounterStoreState {
return {
activeCounter: activeCounter || this.filteredCounters[0],
nextCounter: this.nextCounter
};
}

get settings(): SettingsManager<CounterSettings> {
public get settings(): SettingsManager<CounterSettings> {
return prefs;
}

get filteredCounters(): Array<CounterType | string> {
public get filteredCounters(): Array<CounterType | string> {
return Object.keys(Counters).filter((counter) => prefs.get(counter, true));
}

get nextCounter(): CounterType | string {
public get nextCounter(): CounterType | string {
const counters = this.filteredCounters;
const currentIndex = counters.indexOf(activeCounter || counters[0]);

return currentIndex >= counters.length - 1 ? counters[0] : counters[currentIndex + 1];
}
}

export default Flux.Store?.getAll?.().find((store: Store) => store.getName() === CounterStore.displayName) ||
export default (Flux.Store?.getAll?.().find((store: Store) => store.getName() === CounterStore.displayName) ||
new CounterStore(FluxDispatcher, {
[ActionTypes.STATISTICS_COUNTER_SET_ACTIVE]: ({ counter }) => (activeCounter = counter)
});
})) as CounterStoreType;
6 changes: 3 additions & 3 deletions src/lib/util.ts
@@ -1,4 +1,4 @@
import { Injector, util } from "replugged";
import { Injector, util } from 'replugged';

const inject = new Injector();

Expand All @@ -20,12 +20,12 @@ export function findInReactTree(node: JSX.Element | JSX.Element[], predicate: Pr
}

return null;
};
}

export function forceUpdate(element: Element | null): void {
if (!element) return;

const instance = util.getOwnerInstance<any>(element);
const instance = util.getOwnerInstance<React.Component & Record<string, unknown>>(element);
if (instance) {
const forceRerender = inject.instead(instance, 'render', () => {
forceRerender();
Expand Down

0 comments on commit 3833102

Please sign in to comment.