Skip to content

Commit

Permalink
feat: feature/replace-and-find-image using generics
Browse files Browse the repository at this point in the history
  • Loading branch information
joepvandepol authored and joepvandepol committed Feb 12, 2025
1 parent 204d22a commit 77238c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/modules/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createSelector, createSlice, PayloadAction} from '@reduxjs/toolkit'
import type {ClientError, Patch, Transaction} from '@sanity/client'
import type {AttributeSet, ClientError, Patch, Transaction} from '@sanity/client'
import {
Asset,
AssetItem,
Expand Down Expand Up @@ -814,7 +814,10 @@ export const assetsUpdateImageReferencesEpic: MyEpic = (action$, state$, {client
const clonedDocument = JSON.parse(JSON.stringify(document))
const assetsToReplace = findImageAssets(clonedDocument, asset, id)
for (const assetToReplace of assetsToReplace) {
await client.patch(document._id).set(assetToReplace).commit()
await client
.patch(document._id)
.set(assetToReplace as AttributeSet)
.commit()
}
}
return assetsActions.updateImageReferencesComplete({id})
Expand Down
30 changes: 16 additions & 14 deletions src/utils/ReplaceImages.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
export function findImageAssets(
document: Record<string, unknown>,
newAsset: Record<string, unknown>,
import {Asset} from '@types'

export function findImageAssets<T extends {_id: string}>(
document: T,
newAsset: T,
assetToReplaceId: string
): any[] {
const foundEntries: any[] = []
findNestedObjects(document, foundEntries, newAsset, assetToReplaceId, '')
): T[] {
const foundEntries: T[] = []
findNestedObjects(document, foundEntries, newAsset, assetToReplaceId)
return foundEntries
}

// Because of GROQ limitations we have to filter all image assets out the document manually
function findNestedObjects(
document: any,
foundEntries: any[],
newAsset: any,
function findNestedObjects<T extends {_id?: string; _type?: string; asset?: Asset}>(
document: T,
foundEntries: T[],
newAsset: T,
assetToReplaceId: string,
currentPath: string
currentPath: string = ''
) {
if (typeof document !== 'object' || document === null) {
return
}

if (document.hasOwnProperty('_type') && document._type === 'image') {
const assetProperty = document.asset
if (document.hasOwnProperty('_type') && document._type === 'image' && document.asset) {
const assetProperty = document.asset as Asset
if (assetProperty.hasOwnProperty('_ref') && assetProperty._ref === assetToReplaceId) {
const imageObject: any = {}
document.asset._ref = newAsset._id
Expand All @@ -36,7 +38,7 @@ function findNestedObjects(
for (const key in document) {
if (typeof document[key] === 'object') {
const newPath = currentPath ? `${currentPath}` : key
findNestedObjects(document[key], foundEntries, newAsset, assetToReplaceId, newPath)
findNestedObjects(document[key] as T, foundEntries, newAsset, assetToReplaceId, newPath)
}
}
}

0 comments on commit 77238c4

Please sign in to comment.