You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/client/web/plugins/com.msgbyte.openapi/src/MainPanel/AppInfo/useOpenAppAction.tsx

66 lines
1.7 KiB
TypeScript

import { postRequest, useAsyncFn } from '@capital/common';
import { useOpenAppInfo } from '../context';
import type { OpenAppBot, OpenAppCapability, OpenAppOAuth } from '../types';
/**
*
*/
export function useOpenAppAction() {
const { refresh, appId, capability } = useOpenAppInfo();
const [{ loading }, handleChangeAppCapability] = useAsyncFn(
async (targetCapability: OpenAppCapability, checked: boolean) => {
const newCapability: OpenAppCapability[] = [...capability];
const findIndex = newCapability.findIndex((c) => c === targetCapability);
if (checked) {
if (findIndex === -1) {
newCapability.push(targetCapability);
}
} else {
if (findIndex !== -1) {
newCapability.splice(findIndex, 1);
}
}
await postRequest('/openapi/app/setAppCapability', {
appId,
capability: newCapability,
});
await refresh();
},
[appId, capability, refresh]
);
const [, handleUpdateOAuthInfo] = useAsyncFn(
async <T extends keyof OpenAppOAuth>(name: T, value: OpenAppOAuth[T]) => {
await postRequest('/openapi/app/setAppOAuthInfo', {
appId,
fieldName: name,
fieldValue: value,
});
await refresh();
},
[]
);
const [, handleUpdateBotInfo] = useAsyncFn(
async <T extends keyof OpenAppBot>(name: T, value: OpenAppBot[T]) => {
await postRequest('/openapi/app/setAppBotInfo', {
appId,
fieldName: name,
fieldValue: value,
});
await refresh();
},
[appId, refresh]
);
return {
loading,
handleChangeAppCapability,
handleUpdateOAuthInfo,
handleUpdateBotInfo,
};
}