diff --git a/src/Explorer/Controls/Settings/SettingsComponent.tsx b/src/Explorer/Controls/Settings/SettingsComponent.tsx index bb3c030de..88e83a83f 100644 --- a/src/Explorer/Controls/Settings/SettingsComponent.tsx +++ b/src/Explorer/Controls/Settings/SettingsComponent.tsx @@ -186,7 +186,6 @@ export class SettingsComponent extends React.Component = ({ collection }) => { + // If this container itself defines a materialized view, skip rendering this component. const isTargetContainer = !!collection?.materializedViewDefinition(); - const materializedViews = collection?.materializedViews() ?? []; - if (isTargetContainer) { return null; } - const showEditor = materializedViews.length > 0; - const jsonValue = JSON.stringify( - materializedViews.map((view) => ({ - name: view.id, - definition: view.id, - })), - null, - 2, - ); - return ( @@ -34,14 +23,7 @@ export const MaterializedViewComponent: React.FC about how to define materialized views and how to use them. - - {showEditor && } - - console.log("Add view clicked")} - /> + ); }; diff --git a/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.tsx b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.tsx index e7cf076b9..b8c532e66 100644 --- a/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.tsx +++ b/src/Explorer/Controls/Settings/SettingsSubComponents/MaterializedViewSourceComponent.tsx @@ -1,17 +1,48 @@ +import { PrimaryButton } from "@fluentui/react"; import { loadMonaco } from "Explorer/LazyMonaco"; +import { useDatabases } from "Explorer/useDatabases"; import * as monaco from "monaco-editor"; import React, { useEffect, useRef } from "react"; +import * as ViewModels from "../../../../Contracts/ViewModels"; + export interface MaterializedViewSourceComponentProps { - jsonValue: string; + collection: ViewModels.Collection; } -export const MaterializedViewSourceComponent: React.FC = ({ jsonValue }) => { +export const MaterializedViewSourceComponent: React.FC = ({ collection }) => { const editorContainerRef = useRef(null); const editorRef = useRef(null); + // Get the materialized views from the provided collection. + const materializedViews = collection?.materializedViews() ?? []; + + // Helper function to fetch the view definition by matching viewId with collection id. + const getViewDefinition = (viewId: string): string => { + let definition = ""; + useDatabases.getState().databases.forEach((database) => { + database.collections().forEach((coll) => { + const materializedViewDefinition = coll.materializedViewDefinition(); + if (materializedViewDefinition && coll.id() === viewId) { + definition = materializedViewDefinition.definition; + } + }); + }); + return definition; + }; + + // Build the JSON value for the editor using the fetched definitions. + const jsonValue = JSON.stringify( + materializedViews.map((view) => ({ + name: view.id, + definition: getViewDefinition(view.id), + })), + null, + 2, + ); + + // Initialize Monaco editor with the computed JSON value. useEffect(() => { let disposed = false; - const initMonaco = async () => { const monacoInstance = await loadMonaco(); if (disposed || !editorContainerRef.current) return; @@ -24,13 +55,13 @@ export const MaterializedViewSourceComponent: React.FC { disposed = true; editorRef.current?.dispose(); }; - }, []); + }, [jsonValue]); + // Update the editor when the jsonValue changes. useEffect(() => { if (editorRef.current) { editorRef.current.setValue(jsonValue); @@ -38,9 +69,21 @@ export const MaterializedViewSourceComponent: React.FC +
+
+ console.log("Add view clicked")} + /> +
); };