@@ -2,16 +2,23 @@ import { PropsWithChildren, useCallback, useRef, useState } from "react"
22import { ViewConfigurationContext , ViewConfiguration } from "./view-configuration-context"
33import { sdk } from "../../lib/client"
44import { toast } from "@medusajs/ui"
5+ import { useFeatureFlag } from "../feature-flag-provider"
56
67export const ViewConfigurationProvider = ( { children } : PropsWithChildren ) => {
78 const [ viewConfigurations ] = useState < Map < string , ViewConfiguration [ ] > > ( new Map ( ) )
89 const [ activeViews ] = useState < Map < string , ViewConfiguration > > ( new Map ( ) )
910 const [ isLoading ] = useState < Map < string , boolean > > ( new Map ( ) )
11+ const isViewConfigEnabled = useFeatureFlag ( "view_configurations" )
1012
1113 // Use ref to track ongoing requests to prevent duplicate fetches
1214 const fetchingRef = useRef < Map < string , Promise < ViewConfiguration [ ] > > > ( new Map ( ) )
1315
1416 const getViewConfigurations = useCallback ( async ( entity : string ) : Promise < ViewConfiguration [ ] > => {
17+ // Return empty array if feature is disabled
18+ if ( ! isViewConfigEnabled ) {
19+ return [ ]
20+ }
21+
1522 // Check cache first
1623 if ( viewConfigurations . has ( entity ) ) {
1724 return viewConfigurations . get ( entity ) !
@@ -44,9 +51,14 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
4451
4552 fetchingRef . current . set ( entity , fetchPromise )
4653 return fetchPromise
47- } , [ viewConfigurations , isLoading ] )
54+ } , [ viewConfigurations , isLoading , isViewConfigEnabled ] )
4855
4956 const getActiveView = useCallback ( async ( entity : string ) : Promise < ViewConfiguration | null > => {
57+ // Return null if feature is disabled
58+ if ( ! isViewConfigEnabled ) {
59+ return null
60+ }
61+
5062 // Check cache first
5163 if ( activeViews . has ( entity ) ) {
5264 return activeViews . get ( entity )
@@ -63,9 +75,14 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
6375 console . error ( "Failed to fetch active view configuration:" , error )
6476 return null
6577 }
66- } , [ activeViews ] )
78+ } , [ activeViews , isViewConfigEnabled ] )
6779
6880 const setActiveView = useCallback ( async ( entity : string , viewConfigurationId : string ) => {
81+ // Do nothing if feature is disabled
82+ if ( ! isViewConfigEnabled ) {
83+ return
84+ }
85+
6986 try {
7087 await sdk . admin . viewConfiguration . setActive ( {
7188 entity,
@@ -82,11 +99,16 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
8299 console . error ( "Failed to set active view configuration:" , error )
83100 toast . error ( "Failed to set active view" )
84101 }
85- } , [ activeViews , getViewConfigurations ] )
102+ } , [ activeViews , getViewConfigurations , isViewConfigEnabled ] )
86103
87104 const createViewConfiguration = useCallback ( async (
88105 config : Omit < ViewConfiguration , "id" | "created_at" | "updated_at" >
89106 ) : Promise < ViewConfiguration > => {
107+ // Throw error if feature is disabled
108+ if ( ! isViewConfigEnabled ) {
109+ throw new Error ( "View configurations feature is not enabled" )
110+ }
111+
90112 try {
91113 const response = await sdk . admin . viewConfiguration . create ( config )
92114 const newConfig = response . view_configuration
@@ -101,12 +123,17 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
101123 toast . error ( errorMessage )
102124 throw error
103125 }
104- } , [ viewConfigurations ] )
126+ } , [ viewConfigurations , isViewConfigEnabled ] )
105127
106128 const updateViewConfiguration = useCallback ( async (
107129 id : string ,
108130 config : Partial < ViewConfiguration >
109131 ) : Promise < ViewConfiguration > => {
132+ // Throw error if feature is disabled
133+ if ( ! isViewConfigEnabled ) {
134+ throw new Error ( "View configurations feature is not enabled" )
135+ }
136+
110137 try {
111138 const response = await sdk . admin . viewConfiguration . update ( id , config )
112139 const updatedConfig = response . view_configuration
@@ -128,9 +155,14 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
128155 toast . error ( errorMessage )
129156 throw error
130157 }
131- } , [ viewConfigurations , activeViews ] )
158+ } , [ viewConfigurations , activeViews , isViewConfigEnabled ] )
132159
133160 const deleteViewConfiguration = useCallback ( async ( id : string ) => {
161+ // Throw error if feature is disabled
162+ if ( ! isViewConfigEnabled ) {
163+ throw new Error ( "View configurations feature is not enabled" )
164+ }
165+
134166 try {
135167 // First get the config to know which entity to invalidate
136168 const configs = Array . from ( viewConfigurations . values ( ) ) . flat ( )
@@ -152,7 +184,7 @@ export const ViewConfigurationProvider = ({ children }: PropsWithChildren) => {
152184 toast . error ( "Failed to delete view" )
153185 throw error
154186 }
155- } , [ viewConfigurations , activeViews ] )
187+ } , [ viewConfigurations , activeViews , isViewConfigEnabled ] )
156188
157189 const invalidateCache = useCallback ( ( entity : string ) => {
158190 viewConfigurations . delete ( entity )
0 commit comments