'use client';
import { useModals } from '@mantine/modals';
import React, { FC, useCallback } from 'react';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { Input } from '@gitroom/react/form/input';
import { FieldValues, FormProvider, useForm } from 'react-hook-form';
import { Button } from '@gitroom/react/form/button';
import { classValidatorResolver } from '@hookform/resolvers/class-validator';
import { ApiKeyDto } from '@gitroom/nestjs-libraries/dtos/integrations/api.key.dto';
import { useRouter } from 'next/navigation';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { isGeneral } from '@gitroom/react/helpers/is.general';
const resolver = classValidatorResolver(ApiKeyDto);
export const useAddProvider = (update?: () => void) => {
const modal = useModals();
const fetch = useFetch();
return useCallback(async () => {
const data = await (await fetch('/integrations')).json();
modal.openModal({
title: '',
withCloseButton: false,
classNames: {
modal: 'bg-transparent text-textColor',
},
children: ,
size: 'auto',
});
}, []);
};
export const AddProviderButton: FC<{ update?: () => void }> = (props) => {
const { update } = props;
const add = useAddProvider(update);
return (
);
};
export const ApiModal: FC<{
identifier: string;
name: string;
update?: () => void;
close?: () => void;
}> = (props) => {
const { update, name, close: closePopup } = props;
const fetch = useFetch();
const router = useRouter();
const modal = useModals();
const methods = useForm({
mode: 'onChange',
resolver,
});
const close = useCallback(() => {
if (closePopup) {
return closePopup();
}
modal.closeAll();
}, []);
const submit = useCallback(async (data: FieldValues) => {
const add = await fetch(
`/integrations/article/${props.identifier}/connect`,
{
method: 'POST',
body: JSON.stringify({ api: data.api }),
}
);
if (add.ok) {
if (closePopup) {
closePopup();
} else {
modal.closeAll();
}
router.refresh();
if (update) update();
return;
}
methods.setError('api', {
message: 'Invalid API key',
});
}, []);
return (
);
};
export const AddProviderComponent: FC<{
social: Array<{ identifier: string; name: string }>;
article: Array<{ identifier: string; name: string }>;
update?: () => void;
}> = (props) => {
const { update } = props;
const fetch = useFetch();
const modal = useModals();
const { social, article } = props;
const getSocialLink = useCallback(
(identifier: string) => async () => {
const { url } = await (
await fetch('/integrations/social/' + identifier)
).json();
window.location.href = url;
},
[]
);
const close = useCallback(() => {
modal.closeAll();
}, []);
const showApiButton = useCallback(
(identifier: string, name: string) => async () => {
modal.openModal({
title: '',
withCloseButton: false,
classNames: {
modal: 'bg-transparent text-textColor',
},
children: (
),
});
},
[]
);
return (
Social
{social.map((item) => (
{item.identifier === 'youtube' ? (

) : (

)}
{item.name}
))}
{!isGeneral() && (
Articles
{article.map((item) => (
))}
)}
);
};