Files
FastGPT/projects/app/src/components/support/laf/LafAccountModal.tsx
Archer b520988c64 V4.8.17 feature (#3485)
* feat: add third party account config (#3443)

* temp

* editor workflow variable style

* add team to dispatch

* i18n

* delete console

* change openai account position

* fix

* fix

* fix

* fix

* fix

* 4.8.17 test (#3461)

* perf: external provider config

* perf: ui

* feat: add template config (#3434)

* change template position

* template config

* delete console

* delete

* fix

* fix

* perf: Mongo visutal field (#3464)

* remve invalid code

* perf: team member visutal code

* perf: virtual search; perf: search test data

* fix: ts

* fix: image response headers

* perf: template code

* perf: auth layout;perf: auto save (#3472)

* perf: auth layout

* perf: auto save

* perf: auto save

* fix: template guide display & http input support external variables (#3475)

* fix: template guide display

* http editor support external workflow variables

* perf: auto save;fix: ifelse checker line break; (#3478)

* perf: auto save

* perf: auto save

* fix: ifelse checker line break

* perf: doc

* perf: doc

* fix: update var type error

* 4.8.17 test (#3479)

* perf: auto save

* perf: auto save

* perf: template code

* 4.8.17 test (#3480)

* perf: auto save

* perf: auto save

* perf: model price model

* feat: add react memo

* perf: model provider filter

* fix: ts (#3481)

* perf: auto save

* perf: auto save

* fix: ts

* simple app tool select (#3473)

* workflow plugin userguide & simple tool ui

* simple tool filter

* reuse component

* change component to hook

* fix

* perf: too selector modal (#3484)

* perf: auto save

* perf: auto save

* perf: markdown render

* perf: too selector

* fix: app version require tmbId

* perf: templates refresh

* perf: templates refresh

* hide auto save error tip

* perf: toolkit guide

---------

Co-authored-by: heheer <heheer@sealos.io>
2024-12-27 20:05:12 +08:00

196 lines
5.8 KiB
TypeScript

import React, { useCallback } from 'react';
import { ModalBody, Box, Flex, Input, ModalFooter, Button, Link } from '@chakra-ui/react';
import MyModal from '@fastgpt/web/components/common/MyModal';
import { useTranslation } from 'next-i18next';
import { useForm } from 'react-hook-form';
import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { useQuery } from '@tanstack/react-query';
import MySelect from '@fastgpt/web/components/common/MySelect';
import { useSystemStore } from '@/web/common/system/useSystemStore';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { putUpdateTeam } from '@/web/support/user/team/api';
import { useUserStore } from '@/web/support/user/useUserStore';
import type { LafAccountType } from '@fastgpt/global/support/user/team/type.d';
import { postLafPat2Token, getLafApplications } from '@/web/support/laf/api';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getDocPath } from '@/web/common/system/doc';
const LafAccountModal = ({
defaultData = {
token: '',
appid: '',
pat: ''
},
onClose
}: {
defaultData?: LafAccountType;
onClose: () => void;
}) => {
const { t } = useTranslation();
const { register, handleSubmit, setValue, getValues, watch, reset } = useForm({
defaultValues: {
...defaultData,
pat: ''
}
});
const lafToken = watch('token');
const pat = watch('pat');
const appid = watch('appid');
const { feConfigs } = useSystemStore();
const { toast } = useToast();
const { userInfo, initUserInfo } = useUserStore();
const onResetForm = useCallback(() => {
reset({
token: '',
appid: '',
pat: ''
});
}, [reset]);
const { mutate: authLafPat, isLoading: isPatLoading } = useRequest({
mutationFn: async (pat) => {
const token = await postLafPat2Token(pat);
setValue('token', token);
},
errorToast: t('common:plugin.Invalid Env')
});
const { data: appListData = [] } = useQuery(
['appList', lafToken],
() => {
return getLafApplications(lafToken);
},
{
enabled: !!lafToken,
onSuccess: (data) => {
if (!getValues('appid') && data.length > 0) {
setValue('appid', data.filter((app) => app.state === 'Running')[0]?.appid);
}
},
onError: (err) => {
onResetForm();
toast({
title: getErrText(err, t('common:get_app_failed')),
status: 'error'
});
}
}
);
const { mutate: onSubmit, isLoading: isUpdating } = useRequest({
mutationFn: async (data: LafAccountType) => {
if (!userInfo?.team.teamId) return;
return putUpdateTeam({
lafAccount: data
});
},
onSuccess() {
initUserInfo();
onClose();
},
successToast: t('common:common.Update Success'),
errorToast: t('common:common.Update Failed')
});
return (
<MyModal isOpen iconSrc="support/account/laf" title={t('common:user.Laf Account Setting')}>
<ModalBody>
<Box fontSize={'sm'} color={'myGray.500'}>
<Box>{t('common:support.user.Laf account intro')}</Box>
<Box textDecoration={'underline'}>
<Link href={getDocPath('/docs/guide/workbench/workflow/laf/')} isExternal>
{t('common:support.user.Laf account course')}
</Link>
</Box>
<Box>
<Link textDecoration={'underline'} href={`${feConfigs.lafEnv}/`} isExternal>
{t('common:support.user.Go laf env', {
env: feConfigs.lafEnv?.split('//')[1]
})}
</Link>
</Box>
</Box>
<Flex alignItems={'center'} mt={5}>
<Box flex={'0 0 70px'}>PAT:</Box>
{!lafToken ? (
<>
<Input
flex={'1 0 0'}
size={'sm'}
{...register('pat')}
placeholder={t('common:plugin.Enter PAT')}
/>
<Button
ml={2}
variant={'whitePrimary'}
isDisabled={!pat}
onClick={() => {
authLafPat(pat);
}}
isLoading={isPatLoading}
>
{t('common:verification')}
</Button>
</>
) : (
<Button
variant={'whitePrimary'}
onClick={() => {
onResetForm();
putUpdateTeam({
lafAccount: { token: '', appid: '', pat: '' }
});
}}
>
{t('common:has_verification')}
</Button>
)}
</Flex>
{!!lafToken && (
<Flex alignItems={'center'} mt={5}>
<Box flex={'0 0 70px'}>{t('common:plugin.Currentapp')}</Box>
<MySelect
minW={'200px'}
list={
appListData
.filter((app) => app.state === 'Running')
.map((app) => ({
label: `${app.name}`,
value: app.appid
})) || []
}
placeholder={t('common:plugin.App')}
value={watch('appid')}
onchange={(e) => {
setValue('appid', e);
}}
{...(register('appid'), { required: true })}
/>
</Flex>
)}
</ModalBody>
<ModalFooter>
<Button
variant={'whiteBase'}
onClick={() => {
initUserInfo();
onClose();
}}
>
{t('common:common.Close')}
</Button>
{appid && (
<Button ml={3} isLoading={isUpdating} onClick={handleSubmit((data) => onSubmit(data))}>
{t('common:common.Update')}
</Button>
)}
</ModalFooter>
</MyModal>
);
};
export default LafAccountModal;