Browse Source

Maintenance: Desktop view - Added improvements for ticket sidebar and data handling (e.g. apollo cache).

Dominik Klein 3 months ago
parent
commit
0917981bd7

+ 1 - 32
app/frontend/apps/desktop/entities/user/current/stores/taskbarTabs.ts

@@ -350,38 +350,7 @@ export const useUserCurrentTaskbarTabsStore = defineStore(
     }
 
     const taskbarUpdateMutation = new MutationHandler(
-      useUserCurrentTaskbarItemUpdateMutation({
-        update: (cache, { data }) => {
-          if (!data) return
-
-          const { userCurrentTaskbarItemUpdate } = data
-          if (!userCurrentTaskbarItemUpdate?.taskbarItem) return
-
-          const existingTaskbarItemList =
-            cache.readQuery<UserCurrentTaskbarItemListQuery>({
-              query: UserCurrentTaskbarItemListDocument,
-            })
-
-          const listIndex =
-            existingTaskbarItemList?.userCurrentTaskbarItemList?.findIndex(
-              (taskbarTab) =>
-                taskbarTab.id === userCurrentTaskbarItemUpdate?.taskbarItem?.id,
-            )
-
-          if (!listIndex) return
-
-          existingTaskbarItemList?.userCurrentTaskbarItemList?.splice(
-            listIndex,
-            1,
-            userCurrentTaskbarItemUpdate?.taskbarItem,
-          )
-
-          cache.writeQuery({
-            query: UserCurrentTaskbarItemListDocument,
-            data: existingTaskbarItemList,
-          })
-        },
-      }),
+      useUserCurrentTaskbarItemUpdateMutation(),
     )
 
     const updateTaskbarTab = (taskbarTabId: ID, taskbarTab: UserTaskbarTab) => {

+ 9 - 0
app/frontend/apps/desktop/server/apollo/cache/initializer/ticketRedirect.ts

@@ -0,0 +1,9 @@
+// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+import registerRedirect from '#shared/server/apollo/cache/utils/registerRedirect.ts'
+
+import type { InMemoryCacheConfig } from '@apollo/client/cache/inmemory/types'
+
+export default function register(config: InMemoryCacheConfig) {
+  return registerRedirect(config, 'ticket', 'Ticket')
+}

+ 35 - 0
app/frontend/shared/server/apollo/cache/utils/registerRedirect.ts

@@ -0,0 +1,35 @@
+// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+import type { InMemoryCacheConfig } from '@apollo/client/cache/inmemory/types'
+
+// If a new array is returned and length differs, cache doesn't like it,
+// and we need to tell apollo that it needs to just replace current state altogether
+// this should be used only, if you are not using pagination.
+export default function registerIncomingMerge(
+  config: InMemoryCacheConfig,
+  fieldName: string,
+  referenceTypename: string,
+): InMemoryCacheConfig {
+  config.typePolicies ||= {}
+  config.typePolicies.Query ||= {}
+  config.typePolicies.Query.fields ||= {}
+  config.typePolicies.Query.fields[fieldName] = {
+    read(_, { args, toReference }) {
+      if (!args) return undefined
+
+      let { id } = args
+      if (!('id' in args)) {
+        id = args[fieldName][`${fieldName}Id`]
+      }
+
+      if (!id) return undefined
+
+      return toReference({
+        __typename: referenceTypename,
+        id,
+      })
+    },
+  }
+
+  return config
+}

+ 1 - 10
app/frontend/shared/server/apollo/client.ts

@@ -26,16 +26,7 @@ export const createApolloClient = (
       // https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy
       watchQuery: {
         fetchPolicy: 'cache-and-network',
-        nextFetchPolicy(currentFetchPolicy, { initialFetchPolicy, reason }) {
-          // If the initial fetch policy is cache-first, switch to cache-only to not trigger unwanted network requests.
-          if (
-            initialFetchPolicy === 'cache-first' &&
-            reason !== 'variables-changed'
-          ) {
-            return 'cache-only'
-          }
-
-          // Leave all other fetch policies unchanged.
+        nextFetchPolicy(currentFetchPolicy) {
           return currentFetchPolicy
         },
       },