File size: 3,915 Bytes
ec4551b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import React, { FC, useCallback, useEffect, useMemo } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { IntegrationContext } from '@gitroom/frontend/components/launches/helpers/use.integration';
import dayjs from 'dayjs';
import useSWR, { useSWRConfig } from 'swr';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { continueProviderList } from '@gitroom/frontend/components/new-launch/providers/continue-provider/list';
import { newDayjs } from '@gitroom/frontend/components/layout/set.timezone';
import { useModals } from '@gitroom/frontend/components/layout/new-modal';
export const Null: FC<{
onSave: (data: any) => Promise<void>;
existingId: string[];
}> = () => null;
export const ContinueProvider: FC = () => {
const { mutate } = useSWRConfig();
const fetch = useFetch();
const searchParams = useSearchParams();
const added = searchParams.get('added');
const continueId = searchParams.get('continue');
const router = useRouter();
const load = useCallback(async (path: string) => {
const list = (await (await fetch(path)).json()).integrations;
return list;
}, []);
const { data: integrations } = useSWR('/integrations/list', load, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
revalidateOnMount: true,
refreshWhenHidden: false,
refreshWhenOffline: false,
fallbackData: [],
});
const refreshList = useCallback(() => {
mutate('/integrations/list');
const url = new URL(window.location.href);
url.searchParams.delete('added');
url.searchParams.delete('continue');
router.push(url.toString());
}, []);
const Provider = useMemo(() => {
if (!added) {
return Null;
}
return (
continueProviderList[added as keyof typeof continueProviderList] || Null
);
}, [added]);
if (!added || !continueId || !integrations) {
return null;
}
return (
<ContinueModal
refreshList={refreshList}
added={added}
continueId={continueId}
integrations={integrations.map((p: any) => p.internalId)}
provider={Provider}
/>
);
};
const ModalContent: FC<{
continueId: string;
added: any;
provider: any;
closeModal: () => void;
integrations: string[];
}> = ({ continueId, added, provider: Provider, closeModal, integrations }) => {
const fetch = useFetch();
const onSave = useCallback(
async (data: any) => {
await fetch(`/integrations/provider/${continueId}/connect`, {
method: 'POST',
body: JSON.stringify(data),
});
closeModal();
},
[continueId, closeModal]
);
return (
<IntegrationContext.Provider
value={{
date: newDayjs(),
value: [],
allIntegrations: [],
integration: {
editor: 'normal',
additionalSettings: '',
display: '',
time: [
{
time: 0,
},
],
id: continueId,
type: '',
name: '',
picture: '',
inBetweenSteps: true,
changeNickName: false,
changeProfilePicture: false,
identifier: added,
},
}}
>
<Provider onSave={onSave} existingId={integrations} />
</IntegrationContext.Provider>
);
};
const ContinueModal: FC<{
continueId: string;
added: any;
provider: any;
integrations: string[];
refreshList: () => void;
}> = (props) => {
const modals = useModals();
useEffect(() => {
modals.openModal({
title: 'Configure Channel',
children: (close) => (
<ModalContent
{...props}
closeModal={() => {
props.refreshList();
close();
}}
/>
),
});
}, []);
return null;
};
|