@@ -15,6 +15,7 @@ import {
1515import { buildWorkflowSearchReplacePlan } from '@/lib/workflows/search-replace/replacements'
1616import {
1717 getWorkflowSearchCompatibleResourceMatches ,
18+ getWorkflowSearchMatchResourceGroupKey ,
1819 workflowSearchMatchMatchesQuery ,
1920} from '@/lib/workflows/search-replace/resource-resolvers'
2021import { getWorkflowSearchBlocks } from '@/lib/workflows/search-replace/state'
@@ -109,11 +110,14 @@ export function WorkflowSearchReplace() {
109110 const searchInputRef = useRef < HTMLInputElement > ( null )
110111 const [ isApplying , setIsApplying ] = useState ( false )
111112 const [ isReplaceExpanded , setIsReplaceExpanded ] = useState ( false )
113+ const [ resourceReplacementByContext , setResourceReplacementByContext ] = useState <
114+ Record < string , string >
115+ > ( { } )
112116
113117 const {
114118 isOpen,
115119 query,
116- replacement,
120+ replacement : textReplacement ,
117121 activeMatchId,
118122 position,
119123 close,
@@ -263,6 +267,27 @@ export function WorkflowSearchReplace() {
263267 )
264268 const controlTargetMatches = activeMatch ? [ activeMatch ] : [ ]
265269 const usesResourceReplacement = controlTargetMatches . some ( isConstrainedResourceMatch )
270+ const resourceReplacementContextKey =
271+ activeMatch && isConstrainedResourceMatch ( activeMatch )
272+ ? getWorkflowSearchMatchResourceGroupKey ( activeMatch )
273+ : null
274+ const replacement = resourceReplacementContextKey
275+ ? ( resourceReplacementByContext [ resourceReplacementContextKey ] ?? '' )
276+ : textReplacement
277+ const handleReplacementChange = useCallback (
278+ ( nextReplacement : string ) => {
279+ if ( ! resourceReplacementContextKey ) {
280+ setReplacement ( nextReplacement )
281+ return
282+ }
283+
284+ setResourceReplacementByContext ( ( current ) => ( {
285+ ...current ,
286+ [ resourceReplacementContextKey ] : nextReplacement ,
287+ } ) )
288+ } ,
289+ [ resourceReplacementContextKey , setReplacement ]
290+ )
266291 const compatibleResourceOptions = useMemo (
267292 ( ) => getCompatibleResourceReplacementOptions ( controlTargetMatches , resourceOptions ) ,
268293 [ controlTargetMatches , resourceOptions ]
@@ -525,7 +550,7 @@ export function WorkflowSearchReplace() {
525550 canReplaceAll = { Boolean (
526551 eligibleMatchIds . length > 0 && hasReplacement && ! allReplacementIssue
527552 ) }
528- onReplacementChange = { setReplacement }
553+ onReplacementChange = { handleReplacementChange }
529554 onReplaceActive = { handleReplaceActive }
530555 onReplaceAll = { handleReplaceAll }
531556 />
0 commit comments