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 @@ -16,7 +16,7 @@ import { useSetRecoilComponentState } from '@/ui/utilities/state/component-state
import { useGetCurrentViewOnly } from '@/views/hooks/useGetCurrentViewOnly';
import { ViewKey } from '@/views/types/ViewKey';
import { ViewType, viewTypeIconMapping } from '@/views/types/ViewType';
import { useDeleteViewFromCurrentState } from '@/views/view-picker/hooks/useDeleteViewFromCurrentState';
import { useDestroyViewFromCurrentState } from '@/views/view-picker/hooks/useDestroyViewFromCurrentState';
import { viewPickerReferenceViewIdComponentState } from '@/views/view-picker/states/viewPickerReferenceViewIdComponentState';
import { useLingui } from '@lingui/react/macro';
import { useRecoilValue } from 'recoil';
Expand Down Expand Up @@ -78,7 +78,7 @@ export const ObjectOptionsDropdownCustomView = ({

const visibleFieldsCount = visibleBoardFields.length;

const { deleteViewFromCurrentState } = useDeleteViewFromCurrentState();
const { destroyViewFromCurrentState } = useDestroyViewFromCurrentState();
const setViewPickerReferenceViewId = useSetRecoilComponentState(
viewPickerReferenceViewIdComponentState,
);
Expand All @@ -88,7 +88,7 @@ export const ObjectOptionsDropdownCustomView = ({
return;
}
setViewPickerReferenceViewId(customViewData?.id);
deleteViewFromCurrentState();
destroyViewFromCurrentState();
closeDropdown();
onBackToDefault?.();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import { isDefined } from 'twenty-shared/utils';
import { v4 } from 'uuid';
import {
type CreateCoreViewMutationVariables,
type DeleteCoreViewMutationVariables,
type DestroyCoreViewMutationVariables,
type UpdateCoreViewMutationVariables,
useCreateCoreViewMutation,
useDeleteCoreViewMutation,
useDestroyCoreViewMutation,
useUpdateCoreViewMutation,
ViewType,
} from '~/generated/graphql';

export const usePersistView = () => {
const [createCoreViewMutation] = useCreateCoreViewMutation();
const [updateCoreViewMutation] = useUpdateCoreViewMutation();
const [deleteCoreViewMutation] = useDeleteCoreViewMutation();
const [destroyCoreViewMutation] = useDestroyCoreViewMutation();
const { triggerViewGroupOptimisticEffectAtViewCreation } =
useViewsSideEffectsOnViewGroups();

Expand Down Expand Up @@ -124,14 +124,14 @@ export const usePersistView = () => {
[updateCoreViewMutation, handleMetadataError, enqueueErrorSnackBar],
);

const deleteView = useCallback(
const destroyView = useCallback(
async (
variables: DeleteCoreViewMutationVariables,
variables: DestroyCoreViewMutationVariables,
): Promise<
MetadataRequestResult<Awaited<ReturnType<typeof deleteCoreViewMutation>>>
MetadataRequestResult<Awaited<ReturnType<typeof destroyCoreViewMutation>>>
> => {
try {
const result = await deleteCoreViewMutation({
const result = await destroyCoreViewMutation({
variables,
});

Expand All @@ -154,12 +154,12 @@ export const usePersistView = () => {
};
}
},
[deleteCoreViewMutation, handleMetadataError, enqueueErrorSnackBar],
[destroyCoreViewMutation, handleMetadataError, enqueueErrorSnackBar],
);

return {
createView,
updateView,
deleteView,
destroyView,
};
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { ViewType } from '@/views/types/ViewType';
import { useCreateViewFromCurrentState } from '@/views/view-picker/hooks/useCreateViewFromCurrentState';
import { useDeleteViewFromCurrentState } from '@/views/view-picker/hooks/useDeleteViewFromCurrentState';
import { useDestroyViewFromCurrentState } from '@/views/view-picker/hooks/useDestroyViewFromCurrentState';
import { useGetAvailableFieldsForCalendar } from '@/views/view-picker/hooks/useGetAvailableFieldsForCalendar';
import { useGetAvailableFieldsForKanban } from '@/views/view-picker/hooks/useGetAvailableFieldsForKanban';
import { useViewPickerMode } from '@/views/view-picker/hooks/useViewPickerMode';
Expand Down Expand Up @@ -32,7 +32,7 @@ export const ViewPickerCreateButton = () => {
);

const { createViewFromCurrentState } = useCreateViewFromCurrentState();
const { deleteViewFromCurrentState } = useDeleteViewFromCurrentState();
const { destroyViewFromCurrentState } = useDestroyViewFromCurrentState();

const handleCreateButtonClick = () => {
createViewFromCurrentState();
Expand All @@ -42,7 +42,7 @@ export const ViewPickerCreateButton = () => {
return (
<Button
title={t`Delete`}
onClick={deleteViewFromCurrentState}
onClick={destroyViewFromCurrentState}
accent="danger"
fullWidth
size="small"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { ViewType } from '@/views/types/ViewType';
import { useCreateViewFromCurrentState } from '@/views/view-picker/hooks/useCreateViewFromCurrentState';
import { useDeleteViewFromCurrentState } from '@/views/view-picker/hooks/useDeleteViewFromCurrentState';
import { useDestroyViewFromCurrentState } from '@/views/view-picker/hooks/useDestroyViewFromCurrentState';
import { useGetAvailableFieldsForKanban } from '@/views/view-picker/hooks/useGetAvailableFieldsForKanban';
import { useViewPickerMode } from '@/views/view-picker/hooks/useViewPickerMode';
import { viewPickerIsPersistingComponentState } from '@/views/view-picker/states/viewPickerIsPersistingComponentState';
Expand All @@ -24,13 +24,13 @@ export const ViewPickerEditButton = () => {
);

const { createViewFromCurrentState } = useCreateViewFromCurrentState();
const { deleteViewFromCurrentState } = useDeleteViewFromCurrentState();
const { destroyViewFromCurrentState } = useDestroyViewFromCurrentState();

if (viewPickerMode === 'edit') {
return (
<Button
title={t`Delete`}
onClick={deleteViewFromCurrentState}
onClick={destroyViewFromCurrentState}
accent="danger"
fullWidth
size="small"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useCloseDropdown } from '@/ui/layout/dropdown/hooks/useCloseDropdown';
import { MenuItemWithOptionDropdown } from '@/ui/navigation/menu-item/components/MenuItemWithOptionDropdown';
import { useSetRecoilComponentState } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentState';
import { type View } from '@/views/types/View';
import { useDeleteViewFromCurrentState } from '@/views/view-picker/hooks/useDeleteViewFromCurrentState';
import { useDestroyViewFromCurrentState } from '@/views/view-picker/hooks/useDestroyViewFromCurrentState';
import { useViewPickerMode } from '@/views/view-picker/hooks/useViewPickerMode';
import { viewPickerReferenceViewIdComponentState } from '@/views/view-picker/states/viewPickerReferenceViewIdComponentState';
import { useLingui } from '@lingui/react/macro';
Expand Down Expand Up @@ -48,7 +48,7 @@ export const ViewPickerOptionDropdown = ({
const { t } = useLingui();
const { closeDropdown } = useCloseDropdown();
const { getIcon } = useIcons();
const { deleteViewFromCurrentState } = useDeleteViewFromCurrentState();
const { destroyViewFromCurrentState } = useDestroyViewFromCurrentState();
const setViewPickerReferenceViewId = useSetRecoilComponentState(
viewPickerReferenceViewIdComponentState,
);
Expand All @@ -70,7 +70,7 @@ export const ViewPickerOptionDropdown = ({

const handleDelete = () => {
setViewPickerReferenceViewId(view.id);
deleteViewFromCurrentState();
destroyViewFromCurrentState();
closeDropdown(dropdownId);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { viewPickerIsDirtyComponentState } from '@/views/view-picker/states/view
import { viewPickerIsPersistingComponentState } from '@/views/view-picker/states/viewPickerIsPersistingComponentState';
import { viewPickerReferenceViewIdComponentState } from '@/views/view-picker/states/viewPickerReferenceViewIdComponentState';

export const useDeleteViewFromCurrentState = (viewBarInstanceId?: string) => {
export const useDestroyViewFromCurrentState = (viewBarInstanceId?: string) => {
const { closeAndResetViewPicker } = useCloseAndResetViewPicker();

const viewPickerIsPersistingCallbackState = useRecoilComponentCallbackState(
Expand Down Expand Up @@ -44,9 +44,9 @@ export const useDeleteViewFromCurrentState = (viewBarInstanceId?: string) => {

const { changeView } = useChangeView();

const { deleteView } = usePersistView();
const { destroyView } = usePersistView();

const deleteViewFromCurrentState = useRecoilCallback(
const destroyViewFromCurrentState = useRecoilCallback(
({ set, snapshot }) =>
async () => {
set(viewPickerIsPersistingCallbackState, true);
Expand Down Expand Up @@ -74,14 +74,14 @@ export const useDeleteViewFromCurrentState = (viewBarInstanceId?: string) => {
views.filter((view) => view.id !== viewPickerReferenceViewId),
);

await deleteView({ id: viewPickerReferenceViewId });
await destroyView({ id: viewPickerReferenceViewId });
},
[
currentView,
closeAndResetViewPicker,
objectMetadataItem.id,
changeView,
deleteView,
destroyView,
viewPickerIsDirtyCallbackState,
Comment on lines 79 to 85
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The application crashes with a TypeError when deleting the last view for an object because the code attempts to access the .id property of undefined.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

In useDestroyViewFromCurrentState.ts, when a user destroys the last remaining view for an object, the application will crash. The logic attempts to switch to a new view by filtering the list of views to find a remaining one. When no views are left, this filter returns an empty array. The code then attempts to access the id property of the first element of this empty array ([0].id), which evaluates to undefined.id. This triggers an unhandled TypeError, causing a crash.

💡 Suggested Fix

Before calling changeView, verify that the filtered list of remaining views is not empty. If there are remaining views, proceed to change the view. If not, handle this edge case, for example by navigating to a default state or preventing the deletion of the last view entirely.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location:
packages/twenty-front/src/modules/views/view-picker/hooks/useDestroyViewFromCurrentState.ts#L79-L85

Potential issue: In `useDestroyViewFromCurrentState.ts`, when a user destroys the last
remaining view for an object, the application will crash. The logic attempts to switch
to a new view by filtering the list of views to find a remaining one. When no views are
left, this filter returns an empty array. The code then attempts to access the `id`
property of the first element of this empty array (`[0].id`), which evaluates to
`undefined.id`. This triggers an unhandled `TypeError`, causing a crash.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8124595

viewPickerIsPersistingCallbackState,
viewPickerReferenceViewIdCallbackState,
Expand All @@ -90,6 +90,6 @@ export const useDeleteViewFromCurrentState = (viewBarInstanceId?: string) => {
);

return {
deleteViewFromCurrentState,
destroyViewFromCurrentState,
};
};
Loading