refactor
This commit is contained in:
parent
0db9ec2d9b
commit
54a41d3c9d
|
@ -27,7 +27,14 @@ import { showMovedDialog } from '@/scripts/show-moved-dialog.js';
|
||||||
|
|
||||||
export const openingWindowsCount = ref(0);
|
export const openingWindowsCount = ref(0);
|
||||||
|
|
||||||
export async function apiErrorAlert(err: Misskey.api.APIError, endpoint?: string) {
|
export const apiWithDialog = (<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req'] = Misskey.Endpoints[E]['req']>(
|
||||||
|
endpoint: E,
|
||||||
|
data: P = {} as any,
|
||||||
|
token?: string | null | undefined,
|
||||||
|
customErrors?: Record<string, { title?: string; text: string; }>,
|
||||||
|
) => {
|
||||||
|
const promise = misskeyApi(endpoint, data, token);
|
||||||
|
promiseDialog(promise, null, async (err) => {
|
||||||
let title: string | undefined;
|
let title: string | undefined;
|
||||||
let text = err.message + '\n' + err.id;
|
let text = err.message + '\n' + err.id;
|
||||||
if (err.code === 'INTERNAL_ERROR') {
|
if (err.code === 'INTERNAL_ERROR') {
|
||||||
|
@ -48,13 +55,7 @@ export async function apiErrorAlert(err: Misskey.api.APIError, endpoint?: string
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
if (result === 'copy') {
|
if (result === 'copy') {
|
||||||
const errorReportText = [
|
copyToClipboard(`Endpoint: ${endpoint}\nInfo: ${JSON.stringify(err.info)}\nDate: ${date}`);
|
||||||
`Info: ${JSON.stringify(err.info)}`,
|
|
||||||
`Date: ${date}`,
|
|
||||||
];
|
|
||||||
if (endpoint) errorReportText.unshift(`Endpoint: ${endpoint}`);
|
|
||||||
|
|
||||||
copyToClipboard(errorReportText.join('\n'));
|
|
||||||
success();
|
success();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -67,6 +68,10 @@ export async function apiErrorAlert(err: Misskey.api.APIError, endpoint?: string
|
||||||
} else if (err.code === 'ROLE_PERMISSION_DENIED') {
|
} else if (err.code === 'ROLE_PERMISSION_DENIED') {
|
||||||
title = i18n.ts.permissionDeniedError;
|
title = i18n.ts.permissionDeniedError;
|
||||||
text = i18n.ts.permissionDeniedErrorDescription;
|
text = i18n.ts.permissionDeniedErrorDescription;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
|
} else if (customErrors && customErrors[err.code] != null) {
|
||||||
|
title = customErrors[err.code].title;
|
||||||
|
text = customErrors[err.code].text;
|
||||||
} else if (err.code.startsWith('TOO_MANY')) {
|
} else if (err.code.startsWith('TOO_MANY')) {
|
||||||
title = i18n.ts.youCannotCreateAnymore;
|
title = i18n.ts.youCannotCreateAnymore;
|
||||||
text = `${i18n.ts.error}: ${err.id}`;
|
text = `${i18n.ts.error}: ${err.id}`;
|
||||||
|
@ -79,20 +84,10 @@ export async function apiErrorAlert(err: Misskey.api.APIError, endpoint?: string
|
||||||
title,
|
title,
|
||||||
text,
|
text,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
export const apiWithDialog = (<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req'] = Misskey.Endpoints[E]['req']>(
|
|
||||||
endpoint: E,
|
|
||||||
data: P = {} as any,
|
|
||||||
token?: string | null | undefined,
|
|
||||||
) => {
|
|
||||||
const promise = misskeyApi(endpoint, data, token);
|
|
||||||
promiseDialog(promise, null, (err) => {
|
|
||||||
apiErrorAlert(err);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}) as typeof misskeyApi;
|
});
|
||||||
|
|
||||||
type Unpromise<T> = T extends Promise<infer U> ? U : never;
|
type Unpromise<T> = T extends Promise<infer U> ? U : never;
|
||||||
|
|
||||||
|
|
|
@ -240,26 +240,18 @@ function changeAvatar(ev) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatePromise = misskeyApi('i/update', {
|
await os.apiWithDialog('i/update', {
|
||||||
avatarId: originalOrCropped.id,
|
avatarId: originalOrCropped.id,
|
||||||
});
|
}, undefined, {
|
||||||
|
'AVATAR_IS_SENSITIVE': {
|
||||||
os.promiseDialog(updatePromise, (updatedUser) => {
|
|
||||||
os.success();
|
|
||||||
$i.avatarId = updatedUser.avatarId;
|
|
||||||
$i.avatarUrl = updatedUser.avatarUrl;
|
|
||||||
globalEvents.emit('requestClearPageCache');
|
|
||||||
claimAchievement('profileFilled');
|
|
||||||
}, (err) => {
|
|
||||||
if (err.code === 'AVATAR_IS_SENSITIVE') {
|
|
||||||
os.alert({
|
|
||||||
type: 'error',
|
|
||||||
title: i18n.ts.cannotSelectSensitiveMedia,
|
title: i18n.ts.cannotSelectSensitiveMedia,
|
||||||
text: i18n.ts.cannotSelectSensitiveMediaDescription,
|
text: i18n.ts.cannotSelectSensitiveMediaDescription,
|
||||||
});
|
},
|
||||||
} else {
|
}).then(() => {
|
||||||
os.apiErrorAlert(err, 'i/update');
|
$i.avatarId = originalOrCropped.id;
|
||||||
}
|
$i.avatarUrl = originalOrCropped.url;
|
||||||
|
globalEvents.emit('requestClearPageCache');
|
||||||
|
claimAchievement('profileFilled');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -300,25 +292,17 @@ function changeBanner(ev) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatePromise = misskeyApi('i/update', {
|
await os.apiWithDialog('i/update', {
|
||||||
bannerId: originalOrCropped.id,
|
bannerId: originalOrCropped.id,
|
||||||
});
|
}, undefined, {
|
||||||
|
'BANNER_IS_SENSITIVE': {
|
||||||
os.promiseDialog(updatePromise, (updatedUser) => {
|
|
||||||
os.success();
|
|
||||||
$i.bannerId = updatedUser.bannerId;
|
|
||||||
$i.bannerUrl = updatedUser.bannerUrl;
|
|
||||||
globalEvents.emit('requestClearPageCache');
|
|
||||||
}, (err) => {
|
|
||||||
if (err.code === 'BANNER_IS_SENSITIVE') {
|
|
||||||
os.alert({
|
|
||||||
type: 'error',
|
|
||||||
title: i18n.ts.cannotSelectSensitiveMedia,
|
title: i18n.ts.cannotSelectSensitiveMedia,
|
||||||
text: i18n.ts.cannotSelectSensitiveMediaDescription,
|
text: i18n.ts.cannotSelectSensitiveMediaDescription,
|
||||||
});
|
},
|
||||||
} else {
|
}).then(() => {
|
||||||
os.apiErrorAlert(err, 'i/update');
|
$i.bannerId = originalOrCropped.id;
|
||||||
}
|
$i.bannerUrl = originalOrCropped.url;
|
||||||
|
globalEvents.emit('requestClearPageCache');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue