import { useCallback } from 'react' ; import { useSWRConfig } from 'swr' ; import cloneDeep from 'lodash.clonedeep' ; import usePost from '../_rest/usePost' ; import { useAuthenticatedUser } from '../users/useUser' ; import { getGalleriesCacheKey } from '../galleries/useGalleries' ; import { GetGalleriesResponse } from '../galleries/types' ; import { UpdateCollectionHiddenRequest , UpdateCollectionHiddenResponse } from './types' ; import { getISODate } from 'utils/time' ; export default function useUpdateCollectionHidden ( ) { const updateCollection = usePost ( ) ; const authenticatedUser = useAuthenticatedUser ( ) ; const { mutate } = useSWRConfig ( ) ; return useCallback ( async ( collectionId , hidden ) => { await updateCollection < UpdateCollectionHiddenResponse , UpdateCollectionHiddenRequest > ( '/collections/update/hidden' , 'update collection hidden' , { id : collectionId , hidden , } ) ; await mutate ( getGalleriesCacheKey ( { userId : authenticatedUser . id } ) , ( value ) => { const newValue = cloneDeep < GetGalleriesResponse > ( value ) ; const gallery = newValue . galleries [ 0 ] ; const newCollections = gallery . collections . map ( ( collection ) => { if ( collection . id === collectionId ) { return { ... collection , hidden } ; } return collection ; } ) ; gallery . collections = newCollections ; gallery . last_updated = getISODate ( ) ; return newValue ; } , false ) ; } , [ updateCollection , mutate , authenticatedUser . id ] ) ; }