Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ const DeleteReleaseChannelDialog: React.FC<DeleteReleaseChannelDialogProps> = ({
}) => {
const [open, setOpen] = useState(false);
const { removeReleaseChannelId } = useReleaseChannelDrawer();
const { removeReleaseChannel } = useReleaseFilter();
const { setFilter } = useReleaseFilter();
const router = useRouter();
const deleteReleaseChannel =
api.deployment.releaseChannel.delete.useMutation();
const onDelete = () =>
deleteReleaseChannel
.mutateAsync(releaseChannelId)
.then(() => removeReleaseChannelId())
.then(() => removeReleaseChannel())
.then(() => setFilter(undefined, null))
.then(() => router.refresh())
.then(() => setOpen(false));
Comment on lines +49 to 51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling to the deletion flow

The promise chain lacks error handling, which could lead to an inconsistent state if any operation fails. Consider adding proper error handling and user feedback.

  const onDelete = () =>
    deleteReleaseChannel
      .mutateAsync(releaseChannelId)
      .then(() => removeReleaseChannelId())
      .then(() => setFilter(undefined, null))
      .then(() => router.refresh())
-     .then(() => setOpen(false));
+     .then(() => setOpen(false))
+     .catch((error) => {
+       console.error('Failed to delete release channel:', error);
+       // TODO: Show error notification to user
+     });

Committable suggestion skipped: line range outside the PR's diff.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import { useReleaseFilter } from "./useReleaseFilter";
type ReleaseConditionDialogProps = {
condition?: ReleaseCondition;
deploymentId?: string;
onChange: (condition: ReleaseCondition | undefined) => void;
onChange: (
condition: ReleaseCondition | undefined,
releaseChannelId?: string | null,
) => void;
releaseChannels?: SCHEMA.ReleaseChannel[];
children: React.ReactNode;
};
Expand All @@ -52,8 +55,7 @@ export const ReleaseConditionDialog: React.FC<ReleaseConditionDialogProps> = ({
}) => {
const [open, setOpen] = useState(false);
const [error, setError] = useState<string | null>(null);
const { releaseChannelId, setReleaseChannel, removeReleaseChannel } =
useReleaseFilter();
const { setFilter, releaseChannelId } = useReleaseFilter();

const [localReleaseChannelId, setLocalReleaseChannelId] = useState<
string | undefined
Expand All @@ -79,14 +81,14 @@ export const ReleaseConditionDialog: React.FC<ReleaseConditionDialogProps> = ({
>
<Tabs
defaultValue={
releaseChannels.length > 0 ? "release-channels" : "new-filter"
releaseChannelId != null ? "release-channels" : "new-filter"
}
className="space-y-4"
onValueChange={(value) => {
if (value === "new-filter") {
setLocalCondition(defaultCondition);
setLocalReleaseChannelId(undefined);
}
if (value === "new-filter")
setLocalCondition(localCondition ?? defaultCondition);
if (value === "release-channels")
setLocalReleaseChannelId(releaseChannelId);
}}
>
{releaseChannels.length > 0 && (
Expand Down Expand Up @@ -138,7 +140,10 @@ export const ReleaseConditionDialog: React.FC<ReleaseConditionDialogProps> = ({
(rc) => rc.id === localReleaseChannelId,
);
if (releaseChannel == null) return;
setReleaseChannel(releaseChannel);
setFilter(
releaseChannel.releaseFilter ?? undefined,
localReleaseChannelId,
);
setOpen(false);
setError(null);
}}
Expand Down Expand Up @@ -175,7 +180,6 @@ export const ReleaseConditionDialog: React.FC<ReleaseConditionDialogProps> = ({
<div className="flex-grow" />
<Button
onClick={() => {
console.log(">>> localCondition", localCondition);
if (
localCondition != null &&
!isValidReleaseCondition(localCondition)
Expand All @@ -185,8 +189,7 @@ export const ReleaseConditionDialog: React.FC<ReleaseConditionDialogProps> = ({
);
return;
}
removeReleaseChannel();
onChange(localCondition);
onChange(localCondition, null);
setOpen(false);
setError(null);
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type * as SCHEMA from "@ctrlplane/db/schema";
import type { ReleaseCondition } from "@ctrlplane/validators/releases";
import { useCallback, useMemo } from "react";
import { useRouter, useSearchParams } from "next/navigation";
Expand All @@ -24,54 +23,33 @@ export const useReleaseFilter = () => {
);

const setFilter = useCallback(
(filter: ReleaseCondition | undefined) => {
(filter?: ReleaseCondition, releaseChannelId?: string | null) => {
if (filter == null) {
const query = new URLSearchParams(window.location.search);
query.delete("filter");
if (releaseChannelId === null) query.delete("release-channel");
if (releaseChannelId != null)
query.set("release_channel_id", releaseChannelId);
router.replace(`?${query.toString()}`);
return;
}

const filterJson = LZString.compressToEncodedURIComponent(
const filterJsonHash = LZString.compressToEncodedURIComponent(
JSON.stringify(filter),
);
const query = new URLSearchParams(window.location.search);
query.set("filter", filterJson);
query.set("filter", filterJsonHash);
if (releaseChannelId != null)
query.set("release_channel_id", releaseChannelId);
if (releaseChannelId === null) query.delete("release_channel_id");
router.replace(`?${query.toString()}`);
},
[router],
);

const setReleaseChannel = useCallback(
(releaseChannel: SCHEMA.ReleaseChannel) => {
const query = new URLSearchParams(window.location.search);
query.set("release-channel", releaseChannel.id);
if (releaseChannel.releaseFilter != null) {
const filterJson = LZString.compressToEncodedURIComponent(
JSON.stringify(releaseChannel.releaseFilter),
);
query.set("filter", filterJson);
}

router.replace(`?${query.toString()}`);
router.refresh();
},
[router],
);

const removeReleaseChannel = useCallback(() => {
const query = new URLSearchParams(window.location.search);
query.delete("release-channel");
query.delete("filter");
router.replace(`?${query.toString()}`);
router.refresh();
}, [router]);

return {
filter,
setFilter,
releaseChannelId,
setReleaseChannel,
removeReleaseChannel,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
)}
</div>
</ReleaseConditionDialog>
{filter != null && releaseChannel != null && (
{releaseChannel != null && (
<div className="flex items-center gap-2">
<span>{releaseChannel.name}</span>
<Button
Expand Down Expand Up @@ -222,6 +222,7 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
"border-b",
)}
onClick={(e) => e.stopPropagation()}
key={env.id}
>
<div className="flex h-full w-full items-center justify-center">
{showRelease && (
Expand Down
Loading