Files
parcer/src/components/integration/IntegrationSettings.tsx

574 lines
17 KiB
TypeScript
Raw Normal View History

2024-11-13 00:06:32 +05:30
import React, { useState, useEffect } from "react";
2025-01-09 19:49:20 +05:30
import { GenericModal } from "../ui/GenericModal";
2024-11-13 00:06:32 +05:30
import {
MenuItem,
Typography,
CircularProgress,
Alert,
AlertTitle,
2025-01-26 21:05:08 +05:30
Chip,
2024-11-13 00:06:32 +05:30
} from "@mui/material";
2024-09-17 17:28:43 +05:30
import Button from "@mui/material/Button";
2024-10-16 22:37:53 +05:30
import TextField from "@mui/material/TextField";
2024-11-13 00:06:32 +05:30
import axios from "axios";
import { useGlobalInfoStore } from "../../context/globalInfo";
import { getStoredRecording } from "../../api/storage";
import { apiUrl } from "../../apiConfig.js";
2025-01-26 14:22:36 +05:30
import Cookies from "js-cookie";
import { useTranslation } from "react-i18next";
2024-12-06 04:29:40 +05:30
2024-09-17 20:16:50 +05:30
interface IntegrationProps {
2024-11-13 00:06:32 +05:30
isOpen: boolean;
handleStart: (data: IntegrationSettings) => void;
handleClose: () => void;
2024-09-17 17:28:43 +05:30
}
2024-12-08 04:49:29 +05:30
2024-09-17 20:16:50 +05:30
export interface IntegrationSettings {
2025-01-26 21:05:08 +05:30
spreadsheetId?: string;
spreadsheetName?: string;
airtableBaseId?: string;
airtableBaseName?: string;
2024-11-13 00:06:32 +05:30
data: string;
2025-01-26 21:05:08 +05:30
integrationType: "googleSheets" | "airtable";
2024-09-17 17:28:43 +05:30
}
2024-12-08 04:49:29 +05:30
// Helper functions to replace js-cookie functionality
const getCookie = (name: string): string | null => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
2025-01-26 14:22:36 +05:30
return parts.pop()?.split(";").shift() || null;
2024-12-08 04:49:29 +05:30
}
return null;
};
const removeCookie = (name: string): void => {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
};
2024-11-13 00:06:32 +05:30
export const IntegrationSettingsModal = ({
isOpen,
handleStart,
handleClose,
}: IntegrationProps) => {
const { t } = useTranslation();
2025-01-26 21:05:08 +05:30
const [selectedIntegrationType, setSelectedIntegrationType] = useState<
"googleSheets" | "airtable" | null
>(null);
2024-11-13 00:06:32 +05:30
const [settings, setSettings] = useState<IntegrationSettings>({
spreadsheetId: "",
spreadsheetName: "",
2025-01-26 14:22:36 +05:30
airtableBaseId: "",
airtableBaseName: "",
2024-11-13 00:06:32 +05:30
data: "",
2025-01-26 21:05:08 +05:30
integrationType: "googleSheets",
2024-11-13 00:06:32 +05:30
});
2024-10-16 22:37:53 +05:30
2025-01-26 21:05:08 +05:30
const [airtableTables, setAirtableTables] = useState<{ id: string; name: string }[]>([]);
const [spreadsheets, setSpreadsheets] = useState<
{ id: string; name: string }[]
>([]);
const [airtableBases, setAirtableBases] = useState<
{ id: string; name: string }[]
>([]);
2024-11-13 00:06:32 +05:30
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
2024-10-16 20:47:14 +05:30
2024-11-13 00:06:32 +05:30
const { recordingId, notify } = useGlobalInfoStore();
const [recording, setRecording] = useState<any>(null);
2024-10-17 14:21:22 +05:30
2024-11-13 00:06:32 +05:30
const authenticateWithGoogle = () => {
2025-01-26 21:05:08 +05:30
window.location.href = `${apiUrl}/auth/google?robotId=${recordingId}`;
2025-01-26 14:22:36 +05:30
};
2025-01-26 14:33:34 +05:30
2025-01-26 14:22:36 +05:30
const authenticateWithAirtable = () => {
2025-01-26 21:05:08 +05:30
window.location.href = `${apiUrl}/auth/airtable?robotId=${recordingId}`;
2024-11-13 00:06:32 +05:30
};
2024-09-17 17:28:43 +05:30
2025-01-26 21:05:08 +05:30
const handleIntegrationType = (type: "googleSheets" | "airtable") => {
setSelectedIntegrationType(type);
setSettings({
...settings,
integrationType: type,
});
2024-11-13 00:06:32 +05:30
};
2024-09-17 20:10:51 +05:30
2025-01-26 21:05:08 +05:30
const fetchAirtableTables = async (baseId: string) => {
2024-11-13 00:06:32 +05:30
try {
const response = await axios.get(
2025-01-26 21:05:08 +05:30
`${apiUrl}/auth/airtable/tables?baseId=${baseId}&robotId=${recordingId}`,
{
withCredentials: true,
}
);
setAirtableTables(response.data);
} catch (error: any) {
console.error(
"Error fetching Airtable tables:",
error.response?.data?.message || error.message
);
notify(
"error",
t("integration_settings.errors.fetch_error", {
message: error.response?.data?.message || error.message,
})
2024-11-13 00:06:32 +05:30
);
2025-01-26 14:22:36 +05:30
}
};
const fetchSpreadsheetFiles = async () => {
try {
2025-01-26 21:05:08 +05:30
const response = await axios.get(
`${apiUrl}/auth/gsheets/files?robotId=${recordingId}`,
{
withCredentials: true,
}
);
2024-11-13 00:06:32 +05:30
setSpreadsheets(response.data);
} catch (error: any) {
2025-01-26 21:05:08 +05:30
console.error(
"Error fetching spreadsheet files:",
error.response?.data?.message || error.message
);
notify(
"error",
t("integration_settings.errors.fetch_error", {
message: error.response?.data?.message || error.message,
})
);
2024-11-13 00:06:32 +05:30
}
};
2025-01-26 21:05:08 +05:30
console.log("recordingId", recordingId);
2025-01-26 14:22:36 +05:30
const fetchAirtableBases = async () => {
try {
2025-01-26 21:05:08 +05:30
const response = await axios.get(
`${apiUrl}/auth/airtable/bases?robotId=${recordingId}`,
{
withCredentials: true,
}
);
2025-01-26 14:22:36 +05:30
setAirtableBases(response.data);
2025-01-26 21:05:08 +05:30
console.log("Airtable bases:", response.data);
2025-01-26 14:22:36 +05:30
} catch (error: any) {
2025-01-26 21:05:08 +05:30
console.error(
"Error fetching Airtable bases:",
error.response?.data?.message || error.message
);
notify(
"error",
t("integration_settings.errors.fetch_error", {
message: error.response?.data?.message || error.message,
})
);
2025-01-26 14:22:36 +05:30
}
};
2024-11-13 00:06:32 +05:30
const handleSpreadsheetSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
2025-01-26 21:05:08 +05:30
const selectedSheet = spreadsheets.find(
(sheet) => sheet.id === e.target.value
);
2024-11-13 00:06:32 +05:30
if (selectedSheet) {
setSettings({
...settings,
spreadsheetId: selectedSheet.id,
spreadsheetName: selectedSheet.name,
});
}
};
2024-10-16 20:47:14 +05:30
2025-01-26 14:22:36 +05:30
const handleAirtableBaseSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
2025-01-26 21:05:08 +05:30
const selectedBase = airtableBases.find(
(base) => base.id === e.target.value
);
2025-01-26 14:22:36 +05:30
if (selectedBase) {
setSettings({
...settings,
airtableBaseId: selectedBase.id,
airtableBaseName: selectedBase.name,
});
}
};
2024-11-13 00:06:32 +05:30
const updateGoogleSheetId = async () => {
try {
const response = await axios.post(
`${apiUrl}/auth/gsheets/update`,
{
spreadsheetId: settings.spreadsheetId,
spreadsheetName: settings.spreadsheetName,
robotId: recordingId,
},
{ withCredentials: true }
);
2025-01-26 21:05:08 +05:30
notify(`success`, t("integration_settings.notifications.sheet_selected"));
2024-11-13 00:06:32 +05:30
console.log("Google Sheet ID updated:", response.data);
} catch (error: any) {
2025-01-26 21:05:08 +05:30
console.error(
"Error updating Google Sheet ID:",
error.response?.data?.message || error.message
);
2025-01-26 14:22:36 +05:30
}
};
const updateAirtableBaseId = async () => {
try {
const response = await axios.post(
`${apiUrl}/auth/airtable/update`,
{
baseId: settings.airtableBaseId,
baseName: settings.airtableBaseName,
robotId: recordingId,
},
{ withCredentials: true }
2024-11-13 00:06:32 +05:30
);
2025-01-26 21:05:08 +05:30
notify(`success`, t("integration_settings.notifications.base_selected"));
2025-01-26 14:22:36 +05:30
console.log("Airtable Base ID updated:", response.data);
} catch (error: any) {
2025-01-26 21:05:08 +05:30
console.error(
"Error updating Airtable Base ID:",
error.response?.data?.message || error.message
);
2024-11-13 00:06:32 +05:30
}
};
2024-10-17 19:57:09 +05:30
2024-11-13 00:06:32 +05:30
const removeIntegration = async () => {
try {
2025-01-26 21:05:08 +05:30
const endpoint =
selectedIntegrationType === "googleSheets"
? "/auth/gsheets/remove"
: "/auth/airtable/remove";
2024-11-13 00:06:32 +05:30
await axios.post(
2025-01-26 21:05:08 +05:30
`${apiUrl}${endpoint}`,
2024-11-13 00:06:32 +05:30
{ robotId: recordingId },
{ withCredentials: true }
);
setRecording(null);
setSpreadsheets([]);
2025-01-26 14:22:36 +05:30
setAirtableBases([]);
2025-01-26 21:05:08 +05:30
setSelectedIntegrationType(null);
2025-01-26 14:33:34 +05:30
setSettings({
spreadsheetId: "",
spreadsheetName: "",
airtableBaseId: "",
airtableBaseName: "",
data: "",
2025-01-26 21:05:08 +05:30
integrationType: "googleSheets",
2025-01-26 14:33:34 +05:30
});
2024-11-13 00:06:32 +05:30
} catch (error: any) {
2025-01-26 21:05:08 +05:30
console.error(
"Error removing integration:",
error.response?.data?.message || error.message
);
2024-11-13 00:06:32 +05:30
}
};
useEffect(() => {
2025-01-26 21:05:08 +05:30
const status = getCookie("robot_auth_status");
const message = getCookie("robot_auth_message");
2025-01-26 14:33:34 +05:30
2025-01-26 21:05:08 +05:30
if (status === "success" && message) {
notify("success", message);
removeCookie("robot_auth_status");
removeCookie("robot_auth_message");
}
2025-01-26 14:33:34 +05:30
2025-01-26 21:05:08 +05:30
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get("code");
if (code) {
// Determine which authentication callback to handle
// You'll need to implement similar callback logic for Airtable
}
2024-11-13 00:06:32 +05:30
2025-01-26 14:33:34 +05:30
const fetchRecordingInfo = async () => {
if (!recordingId) return;
const recording = await getStoredRecording(recordingId);
if (recording) {
setRecording(recording);
2025-01-26 21:05:08 +05:30
// Determine integration type based on existing integration
if (recording.google_sheet_id) {
setSelectedIntegrationType("googleSheets");
} else if (recording.airtable_base_id) {
setSelectedIntegrationType("airtable");
}
2025-01-26 14:33:34 +05:30
}
};
fetchRecordingInfo();
}, [recordingId]);
2025-01-26 21:05:08 +05:30
// Initial integration type selection
if (!selectedIntegrationType) {
return (
<GenericModal
isOpen={isOpen}
onClose={handleClose}
modalStyle={modalStyle}
>
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "20px",
}}
>
<Typography variant="h6" sx={{ marginBottom: "20px" }}>
{t("integration_settings.title_select_integration")}
</Typography>
<div style={{ display: "flex", gap: "20px" }}>
<Button
variant="contained"
color="primary"
onClick={() => handleIntegrationType("googleSheets")}
>
Google Sheets
</Button>
<Button
variant="contained"
color="secondary"
onClick={() => handleIntegrationType("airtable")}
>
Airtable
</Button>
</div>
</div>
</GenericModal>
);
}
2024-11-13 00:06:32 +05:30
return (
<GenericModal isOpen={isOpen} onClose={handleClose} modalStyle={modalStyle}>
2025-01-26 21:05:08 +05:30
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
marginLeft: "65px",
}}
>
<Typography variant="h6">
{selectedIntegrationType === "googleSheets"
? t("integration_settings.title_google")
: t("integration_settings.title_airtable")}
</Typography>
{recording &&
(recording.google_sheet_id || recording.airtable_base_id ? (
<>
<Alert
severity="info"
sx={{ marginTop: "10px", border: "1px solid #ff00c3" }}
>
<AlertTitle>
{t("integration_settings.alerts.success.title")}
</AlertTitle>
{selectedIntegrationType === "googleSheets" ? (
<>
{t("integration_settings.alerts.success.content", {
sheetName: recording.google_sheet_name,
})}
<a
href={`https://docs.google.com/spreadsheets/d/${recording.google_sheet_id}`}
target="_blank"
rel="noreferrer"
>
{t("integration_settings.alerts.success.here")}
</a>
</>
) : (
<>
{t("integration_settings.alerts.success.content", {
sheetName: recording.airtable_base_name,
})}
<a
href={`https://airtable.com/${recording.airtable_base_id}`}
target="_blank"
rel="noreferrer"
>
{t("integration_settings.alerts.success.here")}
</a>
</>
)}
2025-01-26 14:22:36 +05:30
<br />
2025-01-26 21:05:08 +05:30
<strong>
{t("integration_settings.alerts.success.note")}
</strong>{" "}
{t("integration_settings.alerts.success.sync_limitation")}
2025-01-26 14:22:36 +05:30
</Alert>
2025-01-26 21:05:08 +05:30
<Button
variant="outlined"
color="error"
onClick={removeIntegration}
style={{ marginTop: "15px" }}
>
{t("integration_settings.buttons.remove_integration")}
</Button>
</>
) : null)}
{!recording?.[
selectedIntegrationType === "googleSheets"
? "google_sheet_email"
: "airtable_email"
] ? (
<>
<p>{t("integration_settings.descriptions.sync_info")}</p>
<Button
variant="contained"
color="primary"
onClick={
selectedIntegrationType === "googleSheets"
? authenticateWithGoogle
: authenticateWithAirtable
}
>
{t("integration_settings.buttons.authenticate")}
2024-11-13 00:06:32 +05:30
</Button>
</>
) : (
<>
2025-01-26 21:05:08 +05:30
{recording[
selectedIntegrationType === "googleSheets"
? "google_sheet_email"
: "airtable_email"
] && (
<Typography sx={{ margin: "20px 0px 30px 0px" }}>
{t("integration_settings.descriptions.authenticated_as", {
email:
recording[
selectedIntegrationType === "googleSheets"
? "google_sheet_email"
: "airtable_email"
],
})}
</Typography>
)}
{loading ? (
<CircularProgress sx={{ marginBottom: "15px" }} />
) : error ? (
<Typography color="error">{error}</Typography>
) : (selectedIntegrationType === "googleSheets"
? spreadsheets
: airtableBases
).length === 0 ? (
2024-11-13 00:06:32 +05:30
<>
2025-01-26 21:05:08 +05:30
<div style={{ display: "flex", gap: "10px" }}>
<Button
variant="outlined"
color="primary"
onClick={
selectedIntegrationType === "googleSheets"
? fetchSpreadsheetFiles
: fetchAirtableBases
}
>
{t("integration_settings.buttons.fetch_sheets")}
</Button>
<Button
variant="outlined"
color="error"
onClick={removeIntegration}
>
{t("integration_settings.buttons.remove_integration")}
</Button>
</div>
2024-11-13 00:06:32 +05:30
</>
) : (
<>
2025-01-26 21:05:08 +05:30
<TextField
sx={{ marginBottom: "15px" }}
select
label={t("integration_settings.fields.select_sheet")}
required
value={
selectedIntegrationType === "googleSheets"
? settings.spreadsheetId
: settings.airtableBaseId
}
onChange={
selectedIntegrationType === "googleSheets"
? handleSpreadsheetSelect
: handleAirtableBaseSelect
}
fullWidth
>
{(selectedIntegrationType === "googleSheets"
? spreadsheets
: airtableBases
).map((item) => (
<MenuItem key={item.id} value={item.id}>
{item.name}
</MenuItem>
))}
</TextField>
{(selectedIntegrationType === "googleSheets"
? settings.spreadsheetId
: settings.airtableBaseId) && (
<Typography sx={{ marginBottom: "10px" }}>
{t("integration_settings.fields.selected_sheet", {
name:
selectedIntegrationType === "googleSheets"
? spreadsheets.find(
(s) => s.id === settings.spreadsheetId
)?.name
: airtableBases.find(
(b) => b.id === settings.airtableBaseId
)?.name,
id:
selectedIntegrationType === "googleSheets"
? settings.spreadsheetId
: settings.airtableBaseId,
})}
2024-11-13 00:06:32 +05:30
</Typography>
)}
2025-01-26 21:05:08 +05:30
<Button
variant="contained"
color="primary"
onClick={() => {
if (selectedIntegrationType === "googleSheets") {
updateGoogleSheetId();
} else {
updateAirtableBaseId();
}
handleStart(settings);
}}
style={{ marginTop: "10px" }}
disabled={
!(selectedIntegrationType === "googleSheets"
? settings.spreadsheetId
: settings.airtableBaseId) || loading
}
>
{t("integration_settings.buttons.submit")}
</Button>
2024-11-13 00:06:32 +05:30
</>
)}
</>
)}
</div>
</GenericModal>
);
2024-09-17 20:10:51 +05:30
};
export const modalStyle = {
2024-11-13 00:06:32 +05:30
top: "40%",
left: "50%",
transform: "translate(-50%, -50%)",
width: "50%",
backgroundColor: "background.paper",
p: 4,
height: "fit-content",
display: "block",
padding: "20px",
2025-01-26 21:05:08 +05:30
};