summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/chart-lib.ts2
-rw-r--r--src/misc/chart-memory.ts139
-rw-r--r--src/misc/chart.ts4
-rw-r--r--src/misc/createResource.ts45
-rw-r--r--src/misc/errors.ts4
-rw-r--r--src/misc/i18n.ts2
-rw-r--r--src/misc/motion.ts3
-rw-r--r--src/misc/request-helper.ts25
-rw-r--r--src/misc/shallowEqual.ts31
-rw-r--r--src/misc/storage.ts2
10 files changed, 26 insertions, 231 deletions
diff --git a/src/misc/chart-lib.ts b/src/misc/chart-lib.ts
index b704309..fb5298d 100644
--- a/src/misc/chart-lib.ts
+++ b/src/misc/chart-lib.ts
@@ -23,7 +23,7 @@ Chart.register(
TimeScale,
Filler,
Legend,
- Tooltip
+ Tooltip,
);
export { Chart };
diff --git a/src/misc/chart-memory.ts b/src/misc/chart-memory.ts
deleted file mode 100644
index aca9982..0000000
--- a/src/misc/chart-memory.ts
+++ /dev/null
@@ -1,139 +0,0 @@
-import { createAsset } from 'use-asset';
-
-import prettyBytes from './pretty-bytes';
-export const chartJSResource = createAsset(() => {
- return import('~/misc/chart-lib');
-});
-
-export const commonDataSetProps = {
- borderWidth: 1.5,
- pointRadius: 0,
- tension: 0.4,
- fill: true,
- pointHitRadius: 10,
- pointHoverRadius: 4,
-};
-
-export const memoryChartOptions: any = {
- responsive: true,
- maintainAspectRatio: false,
- parsing: false,
- animation: {
- duration: 1000,
- easing: 'linear',
- },
- animations: {
- y: {
- duration: 0,
- },
- x: {
- duration: 0,
- },
- },
- transitions: {
- active: {
- animation: {
- duration: 0,
- },
- },
- },
- elements: {
- line: {
- tension: 0.4,
- },
- },
- interaction: {
- mode: 'index',
- intersect: false,
- },
- plugins: {
- legend: {
- display: true,
- position: 'top',
- align: 'end',
- labels: {
- boxWidth: 12,
- usePointStyle: true,
- pointStyle: 'circle',
- padding: 15,
- },
- },
- tooltip: {
- enabled: true,
- mode: 'index',
- intersect: false,
- padding: 10,
- backgroundColor: 'rgba(0, 0, 0, 0.7)',
- titleColor: '#fff',
- bodyColor: '#fff',
- borderColor: 'rgba(255, 255, 255, 0.1)',
- borderWidth: 1,
- callbacks: {
- label(context) {
- let label = context.dataset.label || '';
- if (label) {
- label += ': ';
- }
- if (context.parsed.y !== null) {
- label += prettyBytes(context.parsed.y);
- }
- return label;
- },
- },
- },
- },
- scales: {
- x: {
- type: 'time',
- display: false,
- },
- y: {
- type: 'linear',
- display: true,
- beginAtZero: true,
- grace: '5%',
- grid: {
- display: true,
- color: 'rgba(128, 128, 128, 0.1)',
- drawTicks: false,
- },
- border: {
- display: false,
- dash: [5, 5],
- },
- ticks: {
- maxTicksLimit: 3,
- callback(value: number) {
- return prettyBytes(value);
- },
- },
- },
- },
-};
-
-export const chartStyles = [
- {
- inuse: {
- backgroundColor: 'rgba(81, 168, 221, 0.5)',
- borderColor: 'rgb(81, 168, 221)',
- },
- },
- {
- inuse: {
- backgroundColor: 'rgba(245,78,162,0.6)',
- borderColor: 'rgba(245,78,162,1)',
- },
- },
- {
- inuse: {
- backgroundColor: 'rgba(94, 175, 223, 0.3)',
- borderColor: 'rgb(94, 175, 223)',
- },
- },
- {
- inuse: {
- backgroundColor: 'rgba(242, 174, 62, 0.3)',
- borderColor: 'rgb(242, 174, 62)',
- },
- },
-];
diff --git a/src/misc/chart.ts b/src/misc/chart.ts
index 167caa7..234cabb 100644
--- a/src/misc/chart.ts
+++ b/src/misc/chart.ts
@@ -1,6 +1,8 @@
+import type { TooltipItem } from 'chart.js';
import { createAsset } from 'use-asset';
import prettyBytes from './pretty-bytes';
+
export const chartJSResource = createAsset(() => {
return import('~/misc/chart-lib');
});
@@ -69,7 +71,7 @@ export const commonChartOptions: any = {
borderColor: 'rgba(255, 255, 255, 0.1)',
borderWidth: 1,
callbacks: {
- label(context) {
+ label(context: TooltipItem<'line'>) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
diff --git a/src/misc/createResource.ts b/src/misc/createResource.ts
deleted file mode 100644
index 9ff57e5..0000000
--- a/src/misc/createResource.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-// from https://gist.github.com/ryanflorence/e10cc9dbc0e259759ec942ba82e5b57c
-export function createResource(getPromise: (key: string) => Promise<any>) {
- let cache = {};
- const inflight = {};
- const errors = {};
-
- function load(key = 'default') {
- inflight[key] = getPromise(key)
- .then((val) => {
- delete inflight[key];
- cache[key] = val;
- })
- .catch((error) => {
- errors[key] = error;
- });
- return inflight[key];
- }
-
- function preload(key = 'default') {
- if (cache[key] !== undefined || inflight[key]) return;
- load(key);
- }
-
- function read(key = 'default') {
- if (cache[key] !== undefined) {
- return cache[key];
- } else if (errors[key]) {
- throw errors[key];
- } else if (inflight[key]) {
- throw inflight[key];
- } else {
- throw load(key);
- }
- }
-
- function clear(key: 'default') {
- if (key) {
- delete cache[key];
- } else {
- cache = {};
- }
- }
-
- return { preload, read, clear };
-}
diff --git a/src/misc/errors.ts b/src/misc/errors.ts
index 18480f5..2eba9ba 100644
--- a/src/misc/errors.ts
+++ b/src/misc/errors.ts
@@ -1,6 +1,8 @@
export const DOES_NOT_SUPPORT_FETCH = 0;
-export const errors = {
+type ErrorInfo = { message: string; detail?: string };
+
+export const errors: { default: ErrorInfo; [code: number]: ErrorInfo } = {
[DOES_NOT_SUPPORT_FETCH]: {
message: 'Browser not supported!',
detail: 'This browser does not support "fetch", please choose another one.',
diff --git a/src/misc/i18n.ts b/src/misc/i18n.ts
index c416e6c..11ab3b6 100644
--- a/src/misc/i18n.ts
+++ b/src/misc/i18n.ts
@@ -26,7 +26,7 @@ i18next
_options: any,
url: string,
_payload: any,
- callback: BackendRequestCallback
+ callback: BackendRequestCallback,
) {
let p: PromiseLike<{ data: any }>;
diff --git a/src/misc/motion.ts b/src/misc/motion.ts
deleted file mode 100644
index 7fac864..0000000
--- a/src/misc/motion.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-import { createResource } from './createResource';
-
-export const framerMotionResouce = createResource(() => import('framer-motion'));
diff --git a/src/misc/request-helper.ts b/src/misc/request-helper.ts
index db28cde..2fbfecc 100644
--- a/src/misc/request-helper.ts
+++ b/src/misc/request-helper.ts
@@ -4,7 +4,7 @@ import { ClashAPIConfig, LogsAPIConfig } from '~/types';
const headersCommon = { 'Content-Type': 'application/json' };
function genCommonHeaders({ secret }: { secret?: string }) {
- const h = { ...headersCommon };
+ const h: Record<string, string> = { ...headersCommon };
if (secret) {
h['Authorization'] = `Bearer ${secret}`;
}
@@ -25,21 +25,30 @@ export function getURLAndInit({ baseURL, secret }: ClashAPIConfig) {
};
}
+// mihomo 出错时返回 { "message": "..." }
+export async function readErrorMessage(res: Response) {
+ try {
+ const payload = await res.json();
+ if (payload && typeof payload.message === 'string') return payload.message;
+ } catch {
+ // 不是 JSON,退回状态行
+ }
+ return res.statusText || String(res.status);
+}
+
export function buildWebSocketURL(apiConfig: ClashAPIConfig, endpoint: string) {
const { baseURL, secret } = apiConfig;
- const params = new URLSearchParams({
- token: secret,
- });
+ // 没有 secret 时不能带 token,URLSearchParams 会把 undefined 变成字面量 "undefined"
+ const params = new URLSearchParams(secret ? { token: secret } : {});
return buildWebSocketURLBase(baseURL, params, endpoint);
}
export function buildLogsWebSocketURL(apiConfig: LogsAPIConfig, endpoint: string) {
const { baseURL, secret, logLevel } = apiConfig;
- const params = new URLSearchParams({
- token: secret,
- level: logLevel,
- });
+ const params = new URLSearchParams(
+ secret ? { token: secret, level: logLevel } : { level: logLevel },
+ );
return buildWebSocketURLBase(baseURL, params, endpoint);
}
diff --git a/src/misc/shallowEqual.ts b/src/misc/shallowEqual.ts
deleted file mode 100644
index 6a4fa96..0000000
--- a/src/misc/shallowEqual.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-const hasOwn = Object.prototype.hasOwnProperty;
-
-function is(x, y) {
- if (x === y) {
- return x !== 0 || y !== 0 || 1 / x === 1 / y;
- } else {
-
- return x !== x && y !== y;
- }
-}
-
-export default function shallowEqual(objA, objB) {
- if (is(objA, objB)) return true;
-
- if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
- return false;
- }
-
- const keysA = Object.keys(objA);
- const keysB = Object.keys(objB);
-
- if (keysA.length !== keysB.length) return false;
-
- for (let i = 0; i < keysA.length; i++) {
- if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
- return false;
- }
- }
-
- return true;
-}
diff --git a/src/misc/storage.ts b/src/misc/storage.ts
index 15d85d3..56dcdd6 100644
--- a/src/misc/storage.ts
+++ b/src/misc/storage.ts
@@ -12,7 +12,7 @@ function loadState() {
}
}
-function saveState(state) {
+function saveState(state: unknown) {
try {
const serialized = JSON.stringify(state);
localStorage.setItem(StorageKey, serialized);