start using the $discord global everywhere

pull/98/head
dperolio 2 years ago
parent c93dbbe570
commit f2aa93b899
No known key found for this signature in database
GPG Key ID: 4191689562D51409

@ -1,20 +1,21 @@
// getLocale /*
// setLocale * getLocale
// getLanguage * setLocale
// getLanguages * getLanguage
// getMessage * getLanguages
// getMessages * getMessage
// getMessagesByString * getMessages
// getMessagesByAddon * getMessagesByString
// getAllMessages * getMessagesByAddon
// registerMessage * getAllMessages
// registerMessages * registerMessage
// unregisterMessage * registerMessages
// unregisterMessages * unregisterMessage
// getMessagesByLocale * unregisterMessages
* getMessagesByLocale
*/
import { getModule, FluxDispatcher } from '@vizality/webpack'; import { getModule, FluxDispatcher } from '@vizality/webpack';
import { Constants } from '@discord/constants';
import { API } from '@vizality/entities'; import { API } from '@vizality/entities';
import i18n from '@vizality/i18n'; import i18n from '@vizality/i18n';
@ -51,7 +52,7 @@ export default class I18n extends API {
async start () { async start () {
locale = getModule('locale', 'theme')?.locale; locale = getModule('locale', 'theme')?.locale;
FluxDispatcher.subscribe(Constants.ActionTypes.I18N_LOAD_SUCCESS, this._handleLocaleChange); FluxDispatcher.subscribe($discord.constants.ActionTypes.I18N_LOAD_SUCCESS, this._handleLocaleChange);
this.injectAllStrings(__coreMessages); this.injectAllStrings(__coreMessages);
} }
@ -62,11 +63,13 @@ export default class I18n extends API {
* Need to store the original strings somewhere when overriding in order to revert * Need to store the original strings somewhere when overriding in order to revert
* on stop. Maybe this._original? * on stop. Maybe this._original?
*/ */
// [ 'messages', 'defaultMessages' ].forEach(obj => { /*
// Object.keys(i18n._proxyContext[obj]) * [ 'messages', 'defaultMessages' ].forEach(obj => {
// .filter(key => Object.keys(this.messages[this.locale]).filter(k => k === key)) * Object.keys(i18n._proxyContext[obj])
// .forEach(key => delete i18n._proxyContext[obj][key]); * .filter(key => Object.keys(this.messages[this.locale]).filter(k => k === key))
// }); * .forEach(key => delete i18n._proxyContext[obj][key]);
* });
*/
this.removeAllListeners(); this.removeAllListeners();
delete vizality.api.i18n; delete vizality.api.i18n;
} }

@ -9,13 +9,11 @@
import { assertString, toSnakeCase } from '@vizality/util/string'; import { assertString, toSnakeCase } from '@vizality/util/string';
import { Sidebar } from '@vizality/components/dashboard'; import { Sidebar } from '@vizality/components/dashboard';
import { Routes as _Routes } from '@discord/constants';
import { Events, Regexes } from '@vizality/constants'; import { Events, Regexes } from '@vizality/constants';
import { assertObject } from '@vizality/util/object'; import { assertObject } from '@vizality/util/object';
import { isComponent } from '@vizality/util/react'; import { isComponent } from '@vizality/util/react';
import { getCaller } from '@vizality/util/file'; import { getCaller } from '@vizality/util/file';
import { getModule } from '@vizality/webpack'; import { getModule } from '@vizality/webpack';
import { router } from '@discord/modules';
import { API } from '@vizality/entities'; import { API } from '@vizality/entities';
import { isValidElement } from 'react'; import { isValidElement } from 'react';
@ -72,7 +70,7 @@ export default class Routes extends API {
history.shift(); history.shift();
const match = history.find(location => !location.includes('/vizality')); const match = history.find(location => !location.includes('/vizality'));
const route = match.replace(new RegExp(Regexes.DISCORD), ''); const route = match.replace(new RegExp(Regexes.DISCORD), '');
router.replaceWith(route); $discord.modules.router.replaceWith(route);
} }
} catch (err) { } catch (err) {
return this.error(this._labels.concat('restorePreviousRoute'), err); return this.error(this._labels.concat('restorePreviousRoute'), err);
@ -233,10 +231,10 @@ export default class Routes extends API {
if (!pathOrRouteId.startsWith('/')) { if (!pathOrRouteId.startsWith('/')) {
switch (pathOrRouteId) { switch (pathOrRouteId) {
case 'private': path = '/channels/@me/'; break; case 'private': path = '/channels/@me/'; break;
case 'discover': path = _Routes.GUILD_DISCOVERY; break; case 'discover': path = $discord.constants.Routes.GUILD_DISCOVERY; break;
case 'friends': path = _Routes.FRIENDS; break; case 'friends': path = $discord.constants.Routes.FRIENDS; break;
case 'library': path = _Routes.APPLICATION_LIBRARY; break; case 'library': path = $discord.constants.Routes.APPLICATION_LIBRARY; break;
case 'nitro': path = _Routes.APPLICATION_STORE; break; case 'nitro': path = $discord.constants.Routes.APPLICATION_STORE; break;
default: path = `/vizality/${pathOrRouteId}`; default: path = `/vizality/${pathOrRouteId}`;
} }
} else { } else {
@ -247,7 +245,7 @@ export default class Routes extends API {
* Check if it's a Vizality route and has a # in the path. * Check if it's a Vizality route and has a # in the path.
*/ */
if (path.startsWith('/vizality/') && path.includes('#')) { if (path.startsWith('/vizality/') && path.includes('#')) {
router.transitionTo(path); $discord.modules.router.transitionTo(path);
const hash = path.split('#')[1]; const hash = path.split('#')[1];
/** /**
* This is bad, but currently it's the only way I really know how to do it. * This is bad, but currently it's the only way I really know how to do it.
@ -273,7 +271,7 @@ export default class Routes extends API {
/** /**
* Go to the route. * Go to the route.
*/ */
return router.transitionTo(path); return $discord.modules.router.transitionTo(path);
} catch (err) { } catch (err) {
return this.error(err); return this.error(err);
} }
@ -340,10 +338,10 @@ export default class Routes extends API {
const location = {}; const location = {};
const routes = { const routes = {
private: '/channels/@me/', private: '/channels/@me/',
discover: _Routes.GUILD_DISCOVERY, discover: $discord.constants.Routes.GUILD_DISCOVERY,
friends: _Routes.FRIENDS, friends: $discord.constants.Routes.FRIENDS,
library: _Routes.APPLICATION_LIBRARY, library: $discord.constants.Routes.APPLICATION_LIBRARY,
nitro: _Routes.APPLICATION_STORE, nitro: $discord.constants.Routes.APPLICATION_STORE,
guild: '/channels/', guild: '/channels/',
settings: '/vizality/settings', settings: '/vizality/settings',
plugins: '/vizality/plugins', plugins: '/vizality/plugins',

@ -8,7 +8,6 @@ import { isString, toKebabCase } from '@vizality/util/string';
import { getOwnerInstance } from '@vizality/util/react'; import { getOwnerInstance } from '@vizality/util/react';
import { waitForElement } from '@vizality/util/dom'; import { waitForElement } from '@vizality/util/dom';
import { patch, unpatch } from '@vizality/patcher'; import { patch, unpatch } from '@vizality/patcher';
import { Constants } from '@discord/constants';
import { getModule } from '@vizality/webpack'; import { getModule } from '@vizality/webpack';
/** /**
@ -16,7 +15,7 @@ import { getModule } from '@vizality/webpack';
* @see {@link https://discord.com/developers/docs/resources/channel#channel-object-channel-types|Discord} * @see {@link https://discord.com/developers/docs/resources/channel#channel-object-channel-types|Discord}
*/ */
const CHANNEL_TYPES = {}; const CHANNEL_TYPES = {};
for (const type of Object.entries(Constants.ChannelTypes)) { for (const type of Object.entries($discord.constants.ChannelTypes)) {
if (isString(type[1])) { if (isString(type[1])) {
[ , CHANNEL_TYPES[Number(type[0])] ] = type; [ , CHANNEL_TYPES[Number(type[0])] ] = type;
} }

@ -7,7 +7,6 @@
import { toHash, isString, toKebabCase } from '@vizality/util/string'; import { toHash, isString, toKebabCase } from '@vizality/util/string';
import { patch, unpatch } from '@vizality/patcher'; import { patch, unpatch } from '@vizality/patcher';
import { findInTree } from '@vizality/util/react'; import { findInTree } from '@vizality/util/react';
import { Constants } from '@discord/constants';
import { getModule } from '@vizality/webpack'; import { getModule } from '@vizality/webpack';
import { Regexes } from '@vizality/constants'; import { Regexes } from '@vizality/constants';
@ -16,7 +15,7 @@ import { Regexes } from '@vizality/constants';
* @see {@link https://discord.com/developers/docs/resources/channel#message-object-message-types|Discord} * @see {@link https://discord.com/developers/docs/resources/channel#message-object-message-types|Discord}
*/ */
const MESSAGE_TYPES = {}; const MESSAGE_TYPES = {};
for (const type of Object.entries(Constants.MessageTypes)) { for (const type of Object.entries($discord.constants.MessageTypes)) {
if (type[0] !== '0' && isString(type[1])) { if (type[0] !== '0' && isString(type[1])) {
[ , MESSAGE_TYPES[Number(type[0])] ] = type; [ , MESSAGE_TYPES[Number(type[0])] ] = type;
} }

@ -5,11 +5,9 @@
*/ */
import { getModule, FluxDispatcher } from '@vizality/webpack'; import { getModule, FluxDispatcher } from '@vizality/webpack';
import { Regexes, Protocols } from '@vizality/constants';
import { findInReactTree } from '@vizality/util/react'; import { findInReactTree } from '@vizality/util/react';
import { patch, unpatch } from '@vizality/patcher'; import { patch, unpatch } from '@vizality/patcher';
import { Constants } from '@discord/constants';
import { Regexes, Protocols } from '@vizality/constants';
import { fetchUser } from '@discord/user';
export const labels = [ 'Components', 'Anchor' ]; export const labels = [ 'Components', 'Anchor' ];
@ -102,7 +100,7 @@ export default builtin => {
if (!userId) { if (!userId) {
return; return;
} }
const user = await fetchUser(userId); const user = await $discord.users.fetchUser(userId);
if (!user) { if (!user) {
return vizality.api.notifications.sendToast({ return vizality.api.notifications.sendToast({
id: 'VIZALITY_USER_NOT_FOUND', id: 'VIZALITY_USER_NOT_FOUND',
@ -112,7 +110,7 @@ export default builtin => {
}); });
} }
return FluxDispatcher.dirtyDispatch({ return FluxDispatcher.dirtyDispatch({
type: Constants.ActionTypes.USER_PROFILE_MODAL_OPEN, type: $discord.constants.ActionTypes.USER_PROFILE_MODAL_OPEN,
userId userId
}); });
} catch (err) { } catch (err) {

@ -5,7 +5,6 @@
*/ */
import { getModule } from '@vizality/webpack'; import { getModule } from '@vizality/webpack';
import { router } from '@discord/modules';
export const labels = [ 'Misc', 'Route' ]; export const labels = [ 'Misc', 'Route' ];
@ -36,7 +35,7 @@ export default () => {
/** /**
* Add a route listener that will trigger handleRouteChange method on navigation. * Add a route listener that will trigger handleRouteChange method on navigation.
*/ */
router?.listeners?.add(handleRouteChange); $discord.modules.router.listeners.add(handleRouteChange);
/** /**
* Remove attributes and remove listeners on unload. * Remove attributes and remove listeners on unload.
@ -45,6 +44,6 @@ export default () => {
root.removeAttribute('vz-route'); root.removeAttribute('vz-route');
root.removeAttribute('vz-guild-id'); root.removeAttribute('vz-guild-id');
root.removeAttribute('vz-channel-id'); root.removeAttribute('vz-channel-id');
router?.listeners?.delete(handleRouteChange); $discord.modules.router.listeners.delete(handleRouteChange);
}; };
}; };

@ -5,7 +5,6 @@
*/ */
import { getModule, FluxDispatcher } from '@vizality/webpack'; import { getModule, FluxDispatcher } from '@vizality/webpack';
import { Constants } from '@discord/constants';
export const labels = [ 'Misc', 'Settings' ]; export const labels = [ 'Misc', 'Settings' ];
@ -41,7 +40,7 @@ export default () => {
/** /**
* Subscribe to Discord's user settings uppdate flux store. * Subscribe to Discord's user settings uppdate flux store.
*/ */
FluxDispatcher?.subscribe(Constants.ActionTypes.USER_SETTINGS_UPDATE, handleSettingsChange); FluxDispatcher?.subscribe($discord.constants.ActionTypes.USER_SETTINGS_UPDATE, handleSettingsChange);
/** /**
* Remove attributes and unsubscribe from Flux on unload. * Remove attributes and unsubscribe from Flux on unload.
@ -49,6 +48,6 @@ export default () => {
return () => { return () => {
root?.removeAttribute('vz-theme'); root?.removeAttribute('vz-theme');
root?.removeAttribute('vz-mode'); root?.removeAttribute('vz-mode');
FluxDispatcher?.unsubscribe(Constants.ActionTypes.USER_SETTINGS_UPDATE, handleSettingsChange); FluxDispatcher?.unsubscribe($discord.constants.ActionTypes.USER_SETTINGS_UPDATE, handleSettingsChange);
}; };
}; };

@ -1,8 +1,6 @@
import { FluxDispatcher } from '@vizality/webpack'; import { FluxDispatcher } from '@vizality/webpack';
import { toPlural } from '@vizality/util/string'; import { toPlural } from '@vizality/util/string';
import { Constants } from '@discord/constants';
import { error } from '@vizality/util/logger'; import { error } from '@vizality/util/logger';
import { fetchUser } from '@discord/user';
import React, { memo } from 'react'; import React, { memo } from 'react';
import AsyncComponent from './AsyncComponent'; import AsyncComponent from './AsyncComponent';
@ -24,7 +22,7 @@ export default memo(({ onClick, href, className, userId, children, type, addonId
vz-user-id={userId} vz-user-id={userId}
vz-addon-id={addonId} vz-addon-id={addonId}
href={type === 'user' && userId href={type === 'user' && userId
? `${window.location.origin}${Constants.Endpoints.USERS}/${userId}` ? `${window.location.origin}${$discord.constants.Endpoints.USERS}/${userId}`
: type === 'plugin' || type === 'theme' && addonId : type === 'plugin' || type === 'theme' && addonId
? `${window.location.origin}/vizality/${toPlural(type)}/${addonId}}` ? `${window.location.origin}/vizality/${toPlural(type)}/${addonId}}`
: href : href
@ -32,7 +30,7 @@ export default memo(({ onClick, href, className, userId, children, type, addonId
onClick={async evt => { onClick={async evt => {
try { try {
/** /**
* *
*/ */
if (!userId && !addonId && !href) { if (!userId && !addonId && !href) {
if (!onClick) { if (!onClick) {
@ -42,7 +40,7 @@ export default memo(({ onClick, href, className, userId, children, type, addonId
} }
/** /**
* *
*/ */
if (href?.startsWith('#')) { if (href?.startsWith('#')) {
evt?.preventDefault?.(); evt?.preventDefault?.();
@ -65,11 +63,11 @@ export default memo(({ onClick, href, className, userId, children, type, addonId
} }
/** /**
* *
*/ */
if (type === 'user') { if (type === 'user') {
evt.preventDefault(); evt.preventDefault();
const user = await fetchUser(userId); const user = await $discord.users.fetchUser(userId);
if (!user) { if (!user) {
return vizality.api.notifications.sendToast({ return vizality.api.notifications.sendToast({
id: 'VIZALITY_USER_NOT_FOUND', id: 'VIZALITY_USER_NOT_FOUND',
@ -79,13 +77,13 @@ export default memo(({ onClick, href, className, userId, children, type, addonId
}); });
} }
return FluxDispatcher.dirtyDispatch({ return FluxDispatcher.dirtyDispatch({
type: Constants.ActionTypes.USER_PROFILE_MODAL_OPEN, type: $discord.constants.ActionTypes.USER_PROFILE_MODAL_OPEN,
userId userId
}); });
} }
/** /**
* *
*/ */
if (type === 'plugin' || type === 'theme') { if (type === 'plugin' || type === 'theme') {
evt.preventDefault(); evt.preventDefault();

@ -4,7 +4,7 @@ import { getModule } from '@vizality/webpack';
import React, { memo } from 'react'; import React, { memo } from 'react';
/** /**
* *
* @component * @component
*/ */
export default memo(({ className, wrapperClassName, children }) => { export default memo(({ className, wrapperClassName, children }) => {

Loading…
Cancel
Save