mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-08 20:25:19 +02:00
refactor: update connector mutation atoms to handle optional searchSpaceId and improve query invalidation logic
This commit is contained in:
parent
629f6f9cf5
commit
5d2da0847e
2 changed files with 36 additions and 43 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import { atomWithMutation } from "jotai-tanstack-query";
|
||||
import { toast } from "sonner";
|
||||
import type {
|
||||
CreateConnectorRequest,
|
||||
DeleteConnectorRequest,
|
||||
|
|
@ -17,15 +16,16 @@ export const createConnectorMutationAtom = atomWithMutation((get) => {
|
|||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId ?? ""),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: CreateConnectorRequest) => {
|
||||
return connectorsApiService.createConnector(request);
|
||||
},
|
||||
|
||||
onSuccess: () => {
|
||||
if (!searchSpaceId) return;
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId),
|
||||
});
|
||||
},
|
||||
};
|
||||
|
|
@ -35,15 +35,16 @@ export const updateConnectorMutationAtom = atomWithMutation((get) => {
|
|||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId ?? ""),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: UpdateConnectorRequest) => {
|
||||
return connectorsApiService.updateConnector(request);
|
||||
},
|
||||
|
||||
onSuccess: (_, request: UpdateConnectorRequest) => {
|
||||
if (!searchSpaceId) return;
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.connectors.byId(String(request.id)),
|
||||
|
|
@ -56,15 +57,16 @@ export const deleteConnectorMutationAtom = atomWithMutation((get) => {
|
|||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
mutationKey: cacheKeys.connectors.all(searchSpaceId ?? ""),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: DeleteConnectorRequest) => {
|
||||
return connectorsApiService.deleteConnector(request);
|
||||
},
|
||||
|
||||
onSuccess: (_, request: DeleteConnectorRequest) => {
|
||||
if (!searchSpaceId) return;
|
||||
queryClient.setQueryData(
|
||||
cacheKeys.connectors.all(searchSpaceId!),
|
||||
cacheKeys.connectors.all(searchSpaceId),
|
||||
(oldData: GetConnectorsResponse | undefined) => {
|
||||
if (!oldData) return oldData;
|
||||
return oldData.filter((connector) => connector.id !== request.id);
|
||||
|
|
@ -88,9 +90,9 @@ export const indexConnectorMutationAtom = atomWithMutation((get) => {
|
|||
},
|
||||
|
||||
onSuccess: (response: IndexConnectorResponse) => {
|
||||
toast.success(response.message);
|
||||
if (!searchSpaceId) return;
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId!),
|
||||
queryKey: cacheKeys.connectors.all(searchSpaceId),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.connectors.byId(String(response.connector_id)),
|
||||
|
|
|
|||
|
|
@ -295,6 +295,7 @@ export const useConnectorDialog = () => {
|
|||
connectingConnectorType,
|
||||
viewingAccountsType,
|
||||
viewingMCPList,
|
||||
setIsOpen,
|
||||
]);
|
||||
|
||||
// Detect OAuth success / Failure and transition to config view
|
||||
|
|
@ -345,12 +346,13 @@ export const useConnectorDialog = () => {
|
|||
const connectorId = parseInt(params.connectorId, 10);
|
||||
newConnector = result.data.find((c: SearchSourceConnector) => c.id === connectorId);
|
||||
|
||||
// If we found the connector, find the matching OAuth/Composio connector by type
|
||||
if (newConnector) {
|
||||
oauthConnector =
|
||||
OAUTH_CONNECTORS.find((c) => c.connectorType === newConnector!.connector_type) ||
|
||||
COMPOSIO_CONNECTORS.find((c) => c.connectorType === newConnector!.connector_type);
|
||||
}
|
||||
// If we found the connector, find the matching OAuth/Composio connector by type
|
||||
if (newConnector) {
|
||||
const connectorType = newConnector.connector_type;
|
||||
oauthConnector =
|
||||
OAUTH_CONNECTORS.find((c) => c.connectorType === connectorType) ||
|
||||
COMPOSIO_CONNECTORS.find((c) => c.connectorType === connectorType);
|
||||
}
|
||||
}
|
||||
|
||||
// If we don't have a connector yet, try to find by connector param
|
||||
|
|
@ -359,11 +361,12 @@ export const useConnectorDialog = () => {
|
|||
OAUTH_CONNECTORS.find((c) => c.id === params.connector) ||
|
||||
COMPOSIO_CONNECTORS.find((c) => c.id === params.connector);
|
||||
|
||||
if (oauthConnector) {
|
||||
newConnector = result.data.find(
|
||||
(c: SearchSourceConnector) => c.connector_type === oauthConnector!.connectorType
|
||||
);
|
||||
}
|
||||
if (oauthConnector) {
|
||||
const oauthConnectorType = oauthConnector.connectorType;
|
||||
newConnector = result.data.find(
|
||||
(c: SearchSourceConnector) => c.connector_type === oauthConnectorType
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (newConnector && oauthConnector) {
|
||||
|
|
@ -401,7 +404,7 @@ export const useConnectorDialog = () => {
|
|||
// Invalid query params - log but don't crash
|
||||
console.warn("Invalid connector popup query params in OAuth success handler:", error);
|
||||
}
|
||||
}, [searchParams, searchSpaceId, refetchAllConnectors]);
|
||||
}, [searchParams, searchSpaceId, refetchAllConnectors, setIsOpen]);
|
||||
|
||||
// Handle OAuth connection
|
||||
const handleConnectOAuth = useCallback(
|
||||
|
|
@ -516,7 +519,7 @@ export const useConnectorDialog = () => {
|
|||
} finally {
|
||||
setConnectingId(null);
|
||||
}
|
||||
}, [searchSpaceId, createConnector, refetchAllConnectors]);
|
||||
}, [searchSpaceId, createConnector, refetchAllConnectors, setIsOpen]);
|
||||
|
||||
// Handle connecting non-OAuth connectors (like Tavily API)
|
||||
const handleConnectNonOAuth = useCallback(
|
||||
|
|
@ -676,15 +679,11 @@ export const useConnectorDialog = () => {
|
|||
},
|
||||
});
|
||||
|
||||
const successMessage =
|
||||
currentConnectorType === "MCP_CONNECTOR"
|
||||
? `${connector.name} added successfully`
|
||||
: `${connectorTitle} connected and indexing started!`;
|
||||
toast.success(successMessage, {
|
||||
description: periodicEnabledForIndexing
|
||||
? `Periodic sync enabled every ${getFrequencyLabel(frequencyMinutesForIndexing)}.`
|
||||
: "You can continue working while we sync your data.",
|
||||
});
|
||||
const successMessage =
|
||||
currentConnectorType === "MCP_CONNECTOR"
|
||||
? `${connector.name} added successfully`
|
||||
: `${connectorTitle} connected and syncing started!`;
|
||||
toast.success(successMessage);
|
||||
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete("modal");
|
||||
|
|
@ -784,7 +783,6 @@ export const useConnectorDialog = () => {
|
|||
updateConnector,
|
||||
indexConnector,
|
||||
router,
|
||||
getFrequencyLabel,
|
||||
]
|
||||
);
|
||||
|
||||
|
|
@ -1012,11 +1010,7 @@ export const useConnectorDialog = () => {
|
|||
);
|
||||
}
|
||||
|
||||
toast.success(`${indexingConfig.connectorTitle} indexing started`, {
|
||||
description: periodicEnabled
|
||||
? `Periodic sync enabled every ${getFrequencyLabel(frequencyMinutes)}.`
|
||||
: "You can continue working while we sync your data.",
|
||||
});
|
||||
toast.success(`${indexingConfig.connectorTitle} indexing started`);
|
||||
|
||||
// Update URL - the effect will handle closing the modal and clearing state
|
||||
const url = new URL(window.location.href);
|
||||
|
|
@ -1047,7 +1041,6 @@ export const useConnectorDialog = () => {
|
|||
updateConnector,
|
||||
periodicEnabled,
|
||||
frequencyMinutes,
|
||||
getFrequencyLabel,
|
||||
router,
|
||||
indexingConnectorConfig,
|
||||
]
|
||||
|
|
@ -1428,9 +1421,7 @@ export const useConnectorDialog = () => {
|
|||
end_date: endDateStr,
|
||||
},
|
||||
});
|
||||
toast.success("Indexing started", {
|
||||
description: "You can continue working while we sync your data.",
|
||||
});
|
||||
toast.success("Indexing started");
|
||||
|
||||
// Invalidate queries to refresh data
|
||||
queryClient.invalidateQueries({
|
||||
|
|
@ -1447,7 +1438,7 @@ export const useConnectorDialog = () => {
|
|||
}
|
||||
}
|
||||
},
|
||||
[searchSpaceId, indexConnector, queryClient]
|
||||
[searchSpaceId, indexConnector]
|
||||
);
|
||||
|
||||
// Handle going back from edit view
|
||||
|
|
@ -1529,7 +1520,7 @@ export const useConnectorDialog = () => {
|
|||
}
|
||||
}
|
||||
},
|
||||
[activeTab, isStartingIndexing, isDisconnecting, isSaving, isCreatingConnector]
|
||||
[activeTab, isStartingIndexing, isDisconnecting, isSaving, isCreatingConnector, setIsOpen]
|
||||
);
|
||||
|
||||
// Handle tab change
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue