Hangar/frontend/plugins/utils.ts

248 lines
8.3 KiB
TypeScript
Raw Normal View History

2021-02-06 00:56:21 +08:00
import { Context } from '@nuxt/types';
import { Inject } from '@nuxt/types/app';
import { AxiosError } from 'axios';
2021-02-11 18:39:29 +08:00
import filesize from 'filesize';
2021-02-11 19:48:52 +08:00
// TODO fix it complaining about no type declaration file
// @ts-ignore
import { contrastRatio, HexToRGBA, parseHex } from 'vuetify/es5/util/colorUtils';
2021-02-10 12:40:34 +08:00
import { HangarApiException, HangarValidationException } from 'hangar-api';
2021-02-09 02:26:18 +08:00
import { HangarUser } from 'hangar-internal';
2021-02-11 14:17:29 +08:00
import { TranslateResult } from 'vue-i18n';
2021-02-06 00:56:21 +08:00
import { NamedPermission } from '~/types/enums';
import { RootState } from '~/store';
2021-02-09 02:26:18 +08:00
import { NotifPayload } from '~/store/snackbar';
2021-02-07 11:22:37 +08:00
import { AuthState } from '~/store/auth';
2021-01-23 02:44:49 +08:00
2021-02-05 14:51:51 +08:00
type Validation = (v: string) => boolean | string;
2021-02-11 14:17:29 +08:00
type ValidationArgument = (field?: string | TranslateResult) => Validation;
2021-02-05 14:51:51 +08:00
function handleRequestError(err: AxiosError, error: Context['error']) {
if (!err.isAxiosError) {
// everything should be an AxiosError
error({
statusCode: 500,
});
console.log(err);
} else if (err.response) {
2021-02-10 12:40:34 +08:00
if (err.response.data.isHangarApiException) {
const data: HangarApiException = err.response.data;
error({
statusCode: data.httpError.statusCode,
message: data.message,
});
2021-02-10 12:40:34 +08:00
} else if (err.response.data.isHangarValidationException) {
const data: HangarValidationException = err.response.data;
error({
statusCode: data.httpError.statusCode,
message: data.fieldErrors.map((f) => f.errorMsg).join(', '),
});
} else {
error({
statusCode: err.response.status,
message: err.response.statusText,
});
}
} else {
error({
message: "This shouldn't happen...",
});
console.log(err);
}
}
2021-02-06 00:56:21 +08:00
const createUtil = ({ store, error }: Context) => {
2021-01-23 02:44:49 +08:00
class Util {
2021-02-09 02:26:18 +08:00
dummyUser() {
return ({
2021-02-06 00:56:21 +08:00
name: 'Dummy',
2021-02-05 23:29:14 +08:00
id: 42,
tagline: null,
createdAt: this.prettyDate(new Date()),
roles: [],
headerData: {
2021-02-06 00:56:21 +08:00
globalPermission: '',
2021-02-08 06:06:06 +08:00
unreadNotifications: 1,
projectApprovals: 3,
reviewQueueCount: 0,
unresolvedFlags: 2,
2021-02-05 23:29:14 +08:00
},
2021-02-06 00:56:21 +08:00
joinDate: this.prettyDate(new Date()),
2021-02-09 02:26:18 +08:00
} as unknown) as HangarUser;
2021-02-05 23:29:14 +08:00
}
2021-02-08 06:06:06 +08:00
avatarUrl(name: string): string {
return `/avatar/${name}?size=120x120`;
2021-01-23 02:44:49 +08:00
}
2021-02-05 23:29:14 +08:00
forumUrl(name: string): string {
2021-01-23 02:44:49 +08:00
return 'https://papermc.io/forums/u/' + name;
}
2021-02-09 02:26:18 +08:00
prettyDate(date: Date | string): string {
if (typeof date === 'string') {
date = new Date(date);
}
2021-02-05 04:30:47 +08:00
return date.toLocaleDateString(undefined, {
day: 'numeric',
month: 'long',
year: 'numeric',
});
2021-01-23 02:44:49 +08:00
}
2021-01-23 15:37:57 +08:00
2021-02-11 18:39:29 +08:00
formatSize(input: any) {
return filesize(input);
}
2021-02-11 19:48:52 +08:00
white = HexToRGBA(parseHex('#ffffff'));
black = HexToRGBA(parseHex('#000000'));
isDark(hex: string): boolean {
const rgba = HexToRGBA(parseHex(hex));
return contrastRatio(rgba, this.white) > 2;
}
isLight(hex: string): boolean {
const rgba = HexToRGBA(parseHex(hex));
return contrastRatio(rgba, this.black) > 2;
}
2021-02-05 08:22:22 +08:00
/**
* Checks if the supplier permission has all named permissions.
* @param namedPermission perms required
*/
2021-02-07 11:22:37 +08:00
hasPerms(...namedPermission: NamedPermission[]): boolean {
const perms = (store.state.auth as AuthState).routePermissions;
if (perms === null) return false;
const _perms: bigint = BigInt('0b' + perms);
let result = true;
2021-02-05 08:22:22 +08:00
for (const np of namedPermission) {
const perm = (store.state as RootState).permissions.get(np);
if (!perm) {
throw new Error(namedPermission + ' is not valid');
}
2021-02-07 11:22:37 +08:00
const val = BigInt('0b' + perm.permission.toString(2));
result = result && (_perms & val) === val;
2021-01-23 15:37:57 +08:00
}
2021-02-05 08:22:22 +08:00
return result;
}
2021-02-07 11:22:37 +08:00
isCurrentUser(id: number): boolean {
return (store.state.auth as AuthState).user?.id === id;
}
2021-02-07 13:44:53 +08:00
isLoggedIn(): boolean {
return (store.state.auth as AuthState).user != null;
}
2021-02-05 08:22:22 +08:00
/**
* Used when an auth error should redirect to 404
* @param err the axios request error
*/
handlePageRequestError(err: AxiosError) {
handleRequestError(err, error);
}
/**
* Used for showing error popups. See handlePageRequestError to show an error page.
* @param err the axios request error
* @param msg optional error message
*/
handleRequestError(err: AxiosError, msg: string | undefined = 'Could not load data') {
if (process.server) {
handleRequestError(err, error);
return;
}
if (!err.isAxiosError) {
// everything should be an AxiosError
error({
statusCode: 500,
});
console.log(err);
} else if (err.response) {
2021-02-11 20:37:06 +08:00
// TODO check is msg is a i18n key and use that instead (use $te to check existence of a key)
2021-02-10 12:40:34 +08:00
if (err.response.data.isHangarApiException) {
const data: HangarApiException = err.response.data;
2021-02-09 02:26:18 +08:00
store.dispatch('snackbar/SHOW_NOTIF', {
message: msg ? `${msg}: ${data.message}` : data.message,
2021-02-09 02:26:18 +08:00
color: 'error',
timeout: 3000,
2021-02-09 02:26:18 +08:00
} as NotifPayload);
2021-02-10 12:40:34 +08:00
} else if (err.response.data.isHangarValidationException) {
const data: HangarValidationException = err.response.data;
for (const fieldError of data.fieldErrors) {
store.dispatch('snackbar/SHOW_NOTIF', {
message: fieldError.errorMsg,
color: 'error',
timeout: 3000,
} as NotifPayload);
}
if (msg) {
store.dispatch('snackbar/SHOW_NOTIF', {
message: msg,
color: 'error',
timeout: 3000,
} as NotifPayload);
}
2021-02-05 08:22:22 +08:00
} else {
2021-02-09 02:26:18 +08:00
store.dispatch('snackbar/SHOW_NOTIF', {
message: msg ? `${msg}: ${err.response.statusText}` : err.response.statusText,
2021-02-09 02:26:18 +08:00
color: 'error',
timeout: 2000,
2021-02-09 02:26:18 +08:00
} as NotifPayload);
2021-02-05 08:22:22 +08:00
}
2021-01-23 15:37:57 +08:00
} else {
console.log(err);
2021-01-23 15:37:57 +08:00
}
}
2021-02-05 14:51:51 +08:00
$vc: Record<string, ValidationArgument> = {
2021-02-11 14:17:29 +08:00
require: ((name: string | TranslateResult = 'Field') => (v: string) => !!v || `${name} is required`) as ValidationArgument,
2021-02-05 14:51:51 +08:00
};
$v: Record<string, Validation> = {};
2021-02-06 03:50:18 +08:00
2021-02-09 02:26:18 +08:00
error(err: string | NotifPayload) {
2021-02-06 03:50:18 +08:00
if (typeof err === 'string') {
2021-02-09 02:26:18 +08:00
store.dispatch('snackbar/SHOW_NOTIF', { message: err, color: 'error' } as NotifPayload);
2021-02-06 03:50:18 +08:00
} else {
2021-02-09 02:26:18 +08:00
store.dispatch('snackbar/SHOW_NOTIF', err);
2021-02-06 03:50:18 +08:00
}
}
2021-02-09 02:26:18 +08:00
success(msg: string) {
store.dispatch('snackbar/SHOW_NOTIF', { message: msg, color: 'success' } as NotifPayload);
}
2021-01-23 02:44:49 +08:00
}
return new Util();
};
type utilType = ReturnType<typeof createUtil>;
declare module 'vue/types/vue' {
interface Vue {
$util: utilType;
}
}
declare module '@nuxt/types' {
interface NuxtAppOptions {
$util: utilType;
}
interface Context {
$util: utilType;
}
}
declare module 'vuex/types/index' {
2021-01-31 10:00:11 +08:00
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
2021-01-23 02:44:49 +08:00
interface Store<S> {
$util: utilType;
}
}
export default (ctx: Context, inject: Inject) => {
inject('util', createUtil(ctx));
};