0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/console/src/hooks/use-connector-form-config-parser.tsx

37 lines
1,014 B
TypeScript

import type { ConnectorResponse } from '@logto/schemas';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import type { ConnectorFormType } from '@/types/connector';
import { parseFormConfig } from '@/utils/connector-form';
import { safeParseJson } from '@/utils/json';
const useJsonStringConfigParser = () => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
return (config: string) => {
if (!config) {
toast.error(t('connector_details.save_error_empty_config'));
return;
}
const result = safeParseJson(config);
if (!result.success) {
toast.error(result.error);
return;
}
return result.data;
};
};
export const useConnectorFormConfigParser = () => {
const parseJsonConfig = useJsonStringConfigParser();
return (data: ConnectorFormType, formItems: ConnectorResponse['formItems']) => {
return formItems ? parseFormConfig(data, formItems) : parseJsonConfig(data.config);
};
};