:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(file-system): stop starring an item twice adding a duplicate shortcut (#110710)
Ben Leacfecd68ec749
diff --git a/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.test.ts b/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.test.tsindex d7107047efc3..82b475003f9f 100644--- a/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.test.ts+++ b/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.test.ts@@ -123,6 +123,19 @@ describe('projectTreeDataLogic', () => {expect(logic.values.shortcutData).toEqual(updatedShortcuts)})+ it('does not list a shortcut twice when starring returns one that already exists', async () => {+ await expectLogic(logic).toFinishAllListeners()+ const existing = { id: 'star-dash', path: 'Dashboards', type: 'dashboard', ref: '42' }+ logic.actions.loadShortcutsSuccess([existing])+ jest.spyOn(api.fileSystemShortcuts, 'create').mockResolvedValue(existing)++ await expectLogic(logic, () => {+ logic.actions.addShortcutItem({ id: 'dash', path: 'Dashboards', type: 'dashboard', ref: '42' })+ }).toFinishAllListeners()++ expect(logic.values.shortcutData.map((shortcut) => shortcut.id)).toEqual(['star-dash'])+ })+it.each([false, true])('keeps starred navigation in place unless explicitly revealed (%s)', async (explicit) => {const projectTree = projectTreeLogic({ key: 'navbar-files', root: 'project://' })projectTree.mount()diff --git a/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.tsx b/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.tsxindex 6ff128146103..23a5e3f01c4b 100644--- a/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.tsx+++ b/frontend/src/layout/panel-layout/ProjectTree/projectTreeDataLogic.tsx@@ -1074,6 +1074,11 @@ export const projectTreeDataLogic = kea<projectTreeDataLogicType>([item_name: shortcutItem.path,})lemonToast.success('Added to starred')+ // The API returns the existing shortcut when the item is already starred, so+ // appending it unconditionally would show the same star twice until a reload.+ if (values.shortcutData.some((shortcut) => shortcut.id === response.id)) {+ return values.shortcutData+ }return [...values.shortcutData, response]},reorderShortcuts: async ({ orderedIds }) => {diff --git a/posthog/api/file_system/file_system_shortcut.py b/posthog/api/file_system/file_system_shortcut.pyindex e576a713dd84..2065179ab2bf 100644--- a/posthog/api/file_system/file_system_shortcut.py+++ b/posthog/api/file_system/file_system_shortcut.py@@ -62,9 +62,23 @@ class FileSystemShortcutSerializer(FileSystemAccessLevelSerializerMixin, serialidef create(self, validated_data: dict[str, Any], *args: Any, **kwargs: Any) -> FileSystemShortcut:request = self.context["request"]team = self.context["get_team"]()+ surface = self.context.get("file_system_surface", DEFAULT_SURFACE)with transaction.atomic():# Two stars added at once would otherwise read the same last order and share it.lock_user_shortcuts(team.pk, request.user.pk)+ # Starring the same item twice must not add a second row: nothing in the schema stops+ # it, and `bulk_update` already dedupes on this key, so a single star has to agree.+ duplicate = FileSystemShortcut.objects.filter(+ surface_q(surface),+ team=team,+ user=request.user,+ path=validated_data.get("path"),+ type=validated_data.get("type") or "",+ ref=validated_data.get("ref"),+ href=validated_data.get("href"),+ ).first()+ if duplicate is not None:+ return duplicate# Place new shortcuts at the end of the user's current order so they don't jump# ahead of items the user has explicitly reordered.last_order = (@@ -77,7 +91,7 @@ class FileSystemShortcutSerializer(FileSystemAccessLevelSerializerMixin, serialifile_system_shortcut = FileSystemShortcut.objects.create(team=team,user=request.user,- surface=self.context.get("file_system_surface", DEFAULT_SURFACE),+ surface=surface,**validated_data,)return file_system_shortcutdiff --git a/posthog/api/file_system/test/test_file_system_shortcut.py b/posthog/api/file_system/test/test_file_system_shortcut.pyindex 20244446ed26..8e7985a80536 100644--- a/posthog/api/file_system/test/test_file_system_shortcut.py+++ b/posthog/api/file_system/test/test_file_system_shortcut.py@@ -203,6 +203,17 @@ class TestFileSystemShortcutAPI(APIBaseTest):self.assertEqual(response.json()[0]["id"], str(kept.id))self.assertFalse(FileSystemShortcut.objects.filter(id=removed.id).exists())+ def test_starring_the_same_item_twice_keeps_one_shortcut(self):+ payload = {"path": "Dashboards", "type": "dashboard", "ref": "42", "href": "/dashboard/42"}++ first = self.client.post(f"/api/projects/{self.team.id}/file_system_shortcut/", payload, format="json")+ second = self.client.post(f"/api/projects/{self.team.id}/file_system_shortcut/", payload, format="json")++ self.assertEqual(first.status_code, status.HTTP_201_CREATED, first.json())+ self.assertEqual(second.status_code, status.HTTP_201_CREATED, second.json())+ self.assertEqual(second.json()["id"], first.json()["id"])+ self.assertEqual(FileSystemShortcut.objects.filter(team=self.team, user=self.user).count(), 1)+def test_reorder_response_excludes_retired_shortcuts(self):retired = FileSystemShortcut.objects.create(team=self.team, path="Old link", type="link", user=self.user, order=0