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/useOpenAppList.ts

60 lines
1.2 KiB
TypeScript

import {
postRequest,
appendUrlSearch,
useAsyncRefresh,
useLocation,
urlSearchParse,
isValidStr,
useNavigate,
} from '@capital/common';
import { useEffect, useState } from 'react';
import { OpenApp } from './types';
/**
* 开放应用列表
*/
export function useOpenAppList() {
const [selectedAppId, setSelectedAppId] = useState<string | null>(null);
const {
loading,
value: allApps = [],
refresh,
} = useAsyncRefresh(async (): Promise<OpenApp[]> => {
const { data } = await postRequest('/openapi/app/all');
return data ?? [];
}, []);
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
// 仅初始化的时候才处理
const { appId } = urlSearchParse(location.search, {
ignoreQueryPrefix: true,
});
if (isValidStr(appId)) {
setSelectedAppId(appId);
}
}, []);
return {
loading,
allApps,
refresh,
appInfo: allApps.find((a) => a._id === selectedAppId),
/**
* 设置当前选中的开放app
*/
handleSetSelectedApp(appId: string) {
navigate({
search: appendUrlSearch({
appId,
}),
});
setSelectedAppId(appId);
},
};
}