fix: do single credentials better
This commit is contained in:
parent
2b5c94d508
commit
6cf77c264a
|
@ -235,6 +235,7 @@ export const CredentialsInput: FC<{
|
||||||
)}
|
)}
|
||||||
{supportsUserPassword && (
|
{supportsUserPassword && (
|
||||||
<UserPasswordCredentialsModal
|
<UserPasswordCredentialsModal
|
||||||
|
credentialsFieldName={selfKey}
|
||||||
open={isUserPasswordCredentialsModalOpen}
|
open={isUserPasswordCredentialsModalOpen}
|
||||||
onClose={() => setUserPasswordCredentialsModalOpen(false)}
|
onClose={() => setUserPasswordCredentialsModalOpen(false)}
|
||||||
onCredentialsCreate={(creds) => {
|
onCredentialsCreate={(creds) => {
|
||||||
|
@ -299,13 +300,29 @@ export const CredentialsInput: FC<{
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: This is a mess that won't scale. We need to refactor this single credential logic
|
const getCredentialCounts = () => ({
|
||||||
const singleCredential =
|
apiKeys: savedApiKeys.length,
|
||||||
savedApiKeys.length === 1 && savedOAuthCredentials.length === 0
|
oauth: savedOAuthCredentials.length,
|
||||||
? savedApiKeys[0]
|
userPass: savedUserPasswordCredentials.length,
|
||||||
: savedOAuthCredentials.length === 1 && savedApiKeys.length === 0
|
});
|
||||||
? savedOAuthCredentials[0]
|
|
||||||
: null;
|
const getSingleCredential = () => {
|
||||||
|
const counts = getCredentialCounts();
|
||||||
|
const totalCredentials = Object.values(counts).reduce(
|
||||||
|
(sum, count) => sum + count,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (totalCredentials !== 1) return null;
|
||||||
|
|
||||||
|
if (counts.apiKeys === 1) return savedApiKeys[0];
|
||||||
|
if (counts.oauth === 1) return savedOAuthCredentials[0];
|
||||||
|
if (counts.userPass === 1) return savedUserPasswordCredentials[0];
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const singleCredential = getSingleCredential();
|
||||||
|
|
||||||
if (singleCredential) {
|
if (singleCredential) {
|
||||||
if (!selectedCredentials) {
|
if (!selectedCredentials) {
|
||||||
|
@ -541,11 +558,12 @@ export const APIKeyCredentialsModal: FC<{
|
||||||
};
|
};
|
||||||
|
|
||||||
export const UserPasswordCredentialsModal: FC<{
|
export const UserPasswordCredentialsModal: FC<{
|
||||||
|
credentialsFieldName: string;
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onCredentialsCreate: (creds: CredentialsMetaInput) => void;
|
onCredentialsCreate: (creds: CredentialsMetaInput) => void;
|
||||||
}> = ({ open, onClose, onCredentialsCreate }) => {
|
}> = ({ credentialsFieldName, open, onClose, onCredentialsCreate }) => {
|
||||||
const credentials = useCredentials();
|
const credentials = useCredentials(credentialsFieldName);
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
username: z.string().min(1, "Username is required"),
|
username: z.string().min(1, "Username is required"),
|
||||||
|
|
Loading…
Reference in New Issue