1- import React , { createContext , useContext } from "react" ;
2- import { CharmManager } from "@commontools/charm" ;
1+ import React , { createContext , useContext , useMemo } from "react" ;
2+ import { CharmManager , createStorage } from "@commontools/charm" ;
33import { useParams } from "react-router-dom" ;
4- import { createStorage } from "@commontools/charm" ;
54
65export type CharmManagerContextType = {
76 charmManager : CharmManager ;
@@ -14,25 +13,18 @@ export const CharmsManagerProvider: React.FC<{ children: React.ReactNode }> = ({
1413 const { replicaName } = useParams < { replicaName : string } > ( ) ;
1514 const effectiveReplica = replicaName || "common-knowledge" ;
1615
17- // NOTE(ja): disable switching replicas until
18- // https://github.com/commontoolsinc/labs/issues/377 is fixed
19- // const [charmManager, setCharmManager] = useState<CharmManager>(defaultManager);
20- // const previousReplicaRef = useRef<string | undefined>();
21-
22- // useEffect(() => {
23- // if (previousReplicaRef.current === effectiveReplica) {
24- // return;
25- // }
26- // previousReplicaRef.current = effectiveReplica;
27-
28- // Create new charm manager instance with updated replica
29- const storageType = ( import . meta as any ) . env . VITE_STORAGE_TYPE ?? "remote" ;
30- const storage = storageType === "remote" ?
31- createStorage ( { type : "remote" , replica : effectiveReplica , url : new URL ( location . href ) } ) :
32- createStorage ( { type : storageType as "memory" | "local" } ) ;
33- const charmManager = new CharmManager ( storage ) ;
34- // setCharmManager(manager);
35- // }, [effectiveReplica]);
16+ const charmManager = useMemo ( ( ) => {
17+ const storageType = ( import . meta as any ) . env . VITE_STORAGE_TYPE ?? "remote" ;
18+ const storage =
19+ storageType === "remote"
20+ ? createStorage ( {
21+ type : "remote" ,
22+ replica : effectiveReplica ,
23+ url : new URL ( location . href ) ,
24+ } )
25+ : createStorage ( { type : storageType as "memory" | "local" } ) ;
26+ return new CharmManager ( storage ) ;
27+ } , [ effectiveReplica ] ) ;
3628
3729 return (
3830 < CharmManagerContext . Provider value = { { charmManager, currentReplica : effectiveReplica } } >
0 commit comments