Skip to content

Commit

Permalink
Add view state to method modeling panel
Browse files Browse the repository at this point in the history
This adds a view state to the method modeling panel similar to the
model editor. This will be used to send the state of the show multiple
models feature flag to the webview so this can be used to selectively
show/hide components in the method modeling panel.
  • Loading branch information
koesie10 committed Oct 6, 2023
1 parent 20c6392 commit cf0057e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
11 changes: 10 additions & 1 deletion extensions/ql-vscode/src/common/interface-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import { ErrorLike } from "../common/errors";
import { DataFlowPaths } from "../variant-analysis/shared/data-flow-paths";
import { Method, Usage } from "../model-editor/method";
import { ModeledMethod } from "../model-editor/modeled-method";
import { ModelEditorViewState } from "../model-editor/shared/view-state";
import {
MethodModelingPanelViewState,
ModelEditorViewState,
} from "../model-editor/shared/view-state";
import { Mode } from "../model-editor/shared/mode";
import { QueryLanguage } from "./query-language";

Expand Down Expand Up @@ -625,6 +628,11 @@ export type FromMethodModelingMessage =
| RevealInEditorMessage
| StartModelingMessage;

interface SetMethodModelingPanelViewStateMessage {
t: "setMethodModelingPanelViewState";
viewState: MethodModelingPanelViewState;
}

interface SetMethodMessage {
t: "setMethod";
method: Method;
Expand All @@ -643,6 +651,7 @@ interface SetSelectedMethodMessage {
}

export type ToMethodModelingMessage =
| SetMethodModelingPanelViewStateMessage
| SetMethodMessage
| SetModeledMethodMessage
| SetMethodModifiedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DbModelingState, ModelingStore } from "../modeling-store";
import { AbstractWebviewViewProvider } from "../../common/vscode/abstract-webview-view-provider";
import { assertNever } from "../../common/helpers-pure";
import { ModelEditorViewTracker } from "../model-editor-view-tracker";
import { showMultipleModels } from "../../config";

export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
ToMethodModelingMessage,
Expand All @@ -29,11 +30,20 @@ export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
super(app, "method-modeling");
}

protected override onWebViewLoaded(): void {
this.setInitialState();
protected override async onWebViewLoaded(): Promise<void> {
await Promise.all([this.setViewState(), this.setInitialState()]);
this.registerToModelingStoreEvents();
}

private async setViewState(): Promise<void> {
await this.postMessage({
t: "setMethodModelingPanelViewState",
viewState: {
showMultipleModels: showMultipleModels(),
},
});
}

public async setMethod(method: Method): Promise<void> {
this.method = method;

Expand All @@ -45,11 +55,11 @@ export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
}
}

private setInitialState(): void {
private async setInitialState(): Promise<void> {
if (this.modelingStore.hasStateForActiveDb()) {
const selectedMethod = this.modelingStore.getSelectedMethodDetails();
if (selectedMethod) {
void this.postMessage({
await this.postMessage({
t: "setSelectedMethod",
method: selectedMethod.method,
modeledMethod: selectedMethod.modeledMethod,
Expand All @@ -64,7 +74,7 @@ export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
): Promise<void> {
switch (msg.t) {
case "viewLoaded":
this.onWebViewLoaded();
await this.onWebViewLoaded();
break;

case "telemetry":
Expand Down
4 changes: 4 additions & 0 deletions extensions/ql-vscode/src/model-editor/shared/view-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export interface ModelEditorViewState {
showMultipleModels: boolean;
mode: Mode;
}

export interface MethodModelingPanelViewState {
showMultipleModels: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type MethodModelingProps = {
modelingStatus: ModelingStatus;
method: Method;
modeledMethod: ModeledMethod | undefined;
showMultipleModels?: boolean;
onChange: (modeledMethod: ModeledMethod) => void;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import { ModeledMethod } from "../../model-editor/modeled-method";
import { vscode } from "../vscode-api";
import { NotInModelingMode } from "./NotInModelingMode";
import { NoMethodSelected } from "./NoMethodSelected";
import { MethodModelingPanelViewState } from "../../model-editor/shared/view-state";

export function MethodModelingView(): JSX.Element {
type Props = {
initialViewState?: MethodModelingPanelViewState;
};

export function MethodModelingView({ initialViewState }: Props): JSX.Element {
const [viewState, setViewState] = useState<
MethodModelingPanelViewState | undefined
>(initialViewState);
const [inModelingMode, setInModelingMode] = useState<boolean>(false);

const [method, setMethod] = useState<Method | undefined>(undefined);
Expand All @@ -31,6 +39,9 @@ export function MethodModelingView(): JSX.Element {
if (evt.origin === window.origin) {
const msg: ToMethodModelingMessage = evt.data;
switch (msg.t) {
case "setMethodModelingPanelViewState":
setViewState(msg.viewState);
break;
case "setInModelingMode":
setInModelingMode(msg.inModelingMode);
break;
Expand Down Expand Up @@ -84,6 +95,7 @@ export function MethodModelingView(): JSX.Element {
modelingStatus={modelingStatus}
method={method}
modeledMethod={modeledMethod}
showMultipleModels={viewState?.showMultipleModels}
onChange={onChange}
/>
);
Expand Down

0 comments on commit cf0057e

Please sign in to comment.