Skip to content

Destroy core view#16868

Merged
Weiko merged 5 commits intotwentyhq:mainfrom
BOHEUS:destroy_core_view
Jan 6, 2026
Merged

Destroy core view#16868
Weiko merged 5 commits intotwentyhq:mainfrom
BOHEUS:destroy_core_view

Conversation

@BOHEUS
Copy link
Copy Markdown
Collaborator

@BOHEUS BOHEUS commented Dec 30, 2025

Solution for #16526

Copilot AI review requested due to automatic review settings December 30, 2025 19:34
Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@BOHEUS
Copy link
Copy Markdown
Collaborator Author

BOHEUS commented Dec 30, 2025

Not sure if destroyView function in usePersistView.ts should be removed if it's not used anywhere

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 6 files

@BOHEUS
Copy link
Copy Markdown
Collaborator Author

BOHEUS commented Dec 30, 2025

Also, should be a upgrade command removing all views with deletedAt != null?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses issue #16526 where viewField records were not being soft-deleted when their parent view was deleted, leaving orphan records in the database. The solution changes the view deletion behavior from soft-delete to hard-delete by switching from deleteCoreView to destroyCoreView mutations. This leverages the database's CASCADE behavior to automatically remove all related child records (viewField, viewFilter, viewSort, viewGroup, viewFilterGroup) when a view is permanently deleted.

Key changes:

  • Renamed useDeleteViewFromCurrentState hook to useDestroyViewFromCurrentState to reflect the new deletion behavior
  • Added destroyView function to usePersistView hook that calls the destroyCoreView GraphQL mutation
  • Updated all view deletion call sites to use the new destroyView function instead of deleteView

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/twenty-front/src/modules/views/view-picker/hooks/useDestroyViewFromCurrentState.ts Renamed hook from useDeleteViewFromCurrentState and updated to use destroyView instead of deleteView
packages/twenty-front/src/modules/views/view-picker/components/ViewPickerOptionDropdown.tsx Updated import to use renamed useDestroyViewFromCurrentState hook; added unused imports for destroy mutations
packages/twenty-front/src/modules/views/view-picker/components/ViewPickerEditButton.tsx Updated import to use renamed useDestroyViewFromCurrentState hook
packages/twenty-front/src/modules/views/view-picker/components/ViewPickerCreateButton.tsx Updated import to use renamed useDestroyViewFromCurrentState hook
packages/twenty-front/src/modules/views/hooks/internal/usePersistView.ts Added destroyView function and useDestroyCoreViewMutation hook to support hard deletion of views
packages/twenty-front/src/modules/object-record/object-options-dropdown/components/ObjectOptionsDropdownCustomView.tsx Updated import to use renamed useDestroyViewFromCurrentState hook
Comments suppressed due to low confidence (1)

packages/twenty-front/src/modules/views/view-picker/hooks/useDestroyViewFromCurrentState.ts:50

  • The internal variable is still named deleteViewFromCurrentState but the hook has been renamed to useDestroyViewFromCurrentState. For consistency with the new naming convention, this variable should be renamed to destroyViewFromCurrentState to match the hook name and avoid confusion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 30, 2025

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:15812

This environment will automatically shut down when the PR is closed or after 5 hours.

Copy link
Copy Markdown
Contributor

@prastoin prastoin left a comment

Choose a reason for hiding this comment

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

Hey there thanks for your contribution !
About to push a fix for the naming

@prastoin
Copy link
Copy Markdown
Contributor

prastoin commented Dec 31, 2025

Even though we might end up renaming the destroy ( hard delete ) to delete ( currently soft delete ) in the future, for the moment it's still two distinct operations so lets keep naming explicit for the moment

@prastoin prastoin self-assigned this Dec 31, 2025
@prastoin
Copy link
Copy Markdown
Contributor

prastoin commented Dec 31, 2025

Also, should be a upgrade command removing all views with deletedAt != null?

For the moment I think it's over-engineered, lets consider implementing such a command when we decide to totally deprecate soft deletion from now core view-xx entities

Copy link
Copy Markdown
Contributor

@prastoin prastoin left a comment

Choose a reason for hiding this comment

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

Image

Copy link
Copy Markdown
Member

@Weiko Weiko left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 79 to 85
[
currentView,
closeAndResetViewPicker,
objectMetadataItem.id,
changeView,
deleteView,
destroyView,
viewPickerIsDirtyCallbackState,
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

@Weiko Weiko merged commit 1113c20 into twentyhq:main Jan 6, 2026
61 checks passed
@twenty-eng-sync
Copy link
Copy Markdown

Hey @Weiko! After you've done the QA of your Pull Request, you can mark it as done here. Thank you!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 6, 2026

Thanks @BOHEUS for your contribution!
This marks your 47th PR on the repo. You're top 1% of all our contributors 🎉
See contributor page - Share on LinkedIn - Share on Twitter

Contributions

@BOHEUS BOHEUS deleted the destroy_core_view branch January 6, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants