import { FieldPolicy } from '@apollo/client' ; import { Reference } from '@apollo/client/utilities' ; import { UserActivity_userActivity as UserActivity } from '../generated/server/UserActivity' ; import { handleDuplicateRef } from './handleDuplicate' ; type KeyArgs = FieldPolicy < unknown > [ 'keyArgs' ] ; export function userActivityPagination ( keyArgs = false ) { return { keyArgs , merge ( existing , incoming , { args } ) { let merged = existing ? existing . slice ( 0 ) : [ ] ; if ( args ) { const { offset = 0 } = args ; for ( let i = 0 ; i < incoming . length ; i ++ ) { merged [ offset + i ] = incoming [ i ] ; } } else { merged = [ ... merged , ... incoming ] ; } const filteredArr = merged . reduce ( ( acc , current ) => { const duplicateValue = acc . find ( ( item ) => item . postId === current . postId && item . actionType === current . actionType ) ; if ( ! duplicateValue ) { return acc . concat ( [ current ] ) ; } return acc ; } , [ ] ) ; return filteredArr ; } , } ; } export function replaceDataPagination < T = Reference > ( keyArgs = [ ] ) { return { keyArgs , merge : ( existing , incoming ) => incoming || existing || null , } ; } export function appendPagination < T = Reference > ( keyArgs = [ ] , screen ) { return { keyArgs , merge : ( existing , incoming , { args } ) => { if ( ! existing || ! incoming ) { return existing || incoming || null ; } let page ; switch ( screen ) { case 'HOME' : page = args ?. page || 0 ; incoming . users = handleDuplicateRef ( existing . users , incoming . users ) ; if ( page > 0 ) { incoming . topicList . topics = handleDuplicateRef ( existing . topicList . topics , incoming . topicList . topics ) ; } else { incoming . topicList . topics = handleDuplicateRef ( incoming . topicList . topics , existing . topicList . topics ) ; } break ; case 'SEARCH' : page = args ?. page || 1 ; if ( page > 1 ) { incoming . posts = handleDuplicateRef ( existing . posts , incoming . posts ) ; incoming . topics = handleDuplicateRef ( existing . topics , incoming . topics ) ; } break ; case 'MESSAGE_DETAIL' : page = args ?. page || 0 ; if ( page >= 0 ) { incoming . topicList . topics = handleDuplicateRef ( incoming . topicList . topics , existing . topicList . topics ) ; incoming . users = handleDuplicateRef ( incoming . users , existing . users ) ; } break ; case 'NOTIFICATIONS' : incoming . notifications = handleDuplicateRef ( existing . notifications , incoming . notifications ) ; break ; } return incoming ; } , } ; } export function prependAppendPagination < T = Reference > ( keyArgs = [ ] ) { return { keyArgs , merge : ( existing , incoming ) => { if ( ! existing || ! incoming || existing . length === 0 || incoming . length === 0 ) { return incoming || existing || null ; } if ( JSON . stringify ( existing [ existing . length - 1 ] ) < JSON . stringify ( incoming [ incoming . length - 1 ] ) ) { return handleDuplicateRef ( existing , incoming ) ; } else { return handleDuplicateRef ( incoming , existing ) ; } } , } ; }