Skip to content

Commit

Permalink
Improve slack channel selection state (#7074)
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd authored Sep 3, 2024
1 parent 0bc1d64 commit 3a5bf61
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions front/components/assistant_builder/SlackIntegration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export function SlackIntegration({
owner,
slackDataSourceView,
}: SlacIntegrationProps) {
const [newSelection, setNewSelection] = useState<SlackChannel[] | null>(null);
const [newSelection, setNewSelection] = useState<SlackChannel[]>([]);

useEffect(() => {
if (existingSelection.length > 0 && newSelection === null) {
if (existingSelection.length > 0 && newSelection.length === 0) {
setNewSelection(existingSelection);
}
}, [existingSelection, newSelection]);
Expand All @@ -51,39 +51,20 @@ export function SlackIntegration({
const { internalId, title } = node;

setNewSelection((prevSelection) => {
if (!prevSelection) {
return [
{
slackChannelId: internalId,
slackChannelName: title,
},
];
}

// Create a copy of the previous selection.
const updatedSelection = [...prevSelection];

// Find the index of the channel in the selection.
const index = updatedSelection.findIndex(
(channel) => channel.slackChannelId === internalId
const channel = { slackChannelId: internalId, slackChannelName: title };
const index = prevSelection.findIndex(
(c) => c.slackChannelId === internalId
);

if (newPermission === "read_write") {
// If the channel isn't already in the selection, add it.
if (index === -1) {
updatedSelection.push({
slackChannelId: internalId,
slackChannelName: title,
});
}
} else {
// If the channel is in the selection, remove it.
if (index !== -1) {
updatedSelection.splice(index, 1);
}
if (newPermission === "read_write" && index === -1) {
return [...prevSelection, channel];
}

if (newPermission !== "read_write" && index !== -1) {
return prevSelection.filter((_, i) => i !== index);
}

return updatedSelection;
return prevSelection;
});
},
[setNewSelection]
Expand Down

0 comments on commit 3a5bf61

Please sign in to comment.