فهرست منبع

swrize componentInstances and the register and get methods

kaori 3 سال پیش
والد
کامیت
89fb072614
1فایلهای تغییر یافته به همراه29 افزوده شده و 0 حذف شده
  1. 29 0
      packages/app/src/stores/editor.tsx

+ 29 - 0
packages/app/src/stores/editor.tsx

@@ -112,3 +112,32 @@ export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<str
 export const useIsEnabledUnsavedWarning = (): SWRResponse<boolean, Error> => {
   return useStaticSWR<boolean, Error>('isEnabledUnsavedWarning', undefined, { fallbackData: false });
 };
+
+
+const useComponentInstances = (): SWRResponse<{}, Error> => {
+  return useStaticSWR(
+    'componentInstances',
+    undefined,
+    { fallbackData: {} },
+  );
+};
+
+export const registerComponentInstance = (id: string, instance) => {
+  const { data: componentInstancesData, mutate: mutateComponentInstances } = useComponentInstances();
+
+  if (instance == null) {
+    throw new Error('The specified instance must not be null');
+  }
+
+  mutateComponentInstances({...componentInstancesData, [id]: instance});
+}
+
+// Get registered React component instance
+export const getComponentInstance = (id: string) => {
+  const { data: componentInstancesData } = useComponentInstances();
+
+  if(componentInstancesData == null){
+    return {}
+  }
+  return componentInstancesData[id];
+}