Skip to content

Commit

Permalink
fix: unmount component on destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Nov 10, 2021
1 parent 73b91e5 commit ba4fbd1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
18 changes: 9 additions & 9 deletions app/assets/javascripts/components/ComponentView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ const avoidFlickerTimeout = 7;

export const ComponentView: FunctionalComponent<IProps> = observer(
({
application,
appState,
onLoad,
componentUuid,
templateComponent,
manualDealloc = false,
}) => {
application,
appState,
onLoad,
componentUuid,
templateComponent,
manualDealloc = false,
}) => {
const liveComponentRef = useRef<LiveItem<SNComponent> | null>(null);
const iframeRef = useRef<HTMLIFrameElement>(null);

Expand Down Expand Up @@ -74,7 +74,7 @@ export const ComponentView: FunctionalComponent<IProps> = observer(
}

const offlineRestricted = component.offlineOnly && !isDesktopApplication();
const hasUrlError = function() {
const hasUrlError = function () {
if (isDesktopApplication()) {
return !component.local_url && !component.hasValidHostedUrl();
} else {
Expand Down Expand Up @@ -121,7 +121,7 @@ export const ComponentView: FunctionalComponent<IProps> = observer(
}
}, [isIssueOnLoading]);

const handleIframeLoadTimeout =useCallback(async () => {
const handleIframeLoadTimeout = useCallback(async () => {
if (isLoading) {
setIsLoading(false);
setIsIssueOnLoading(true);
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FunctionComponent, h, render } from 'preact';
import { unmountComponentAtNode } from 'preact/compat';
import { StateUpdater, useCallback, useState } from 'preact/hooks';
import { useEffect } from 'react';

Expand Down Expand Up @@ -70,6 +71,9 @@ export function toDirective<Props>(
$onChanges() {
render(h(component, $scope), $element[0]);
},
$onDestroy() {
unmountComponentAtNode($element[0]);
},
};
},
],
Expand Down
18 changes: 9 additions & 9 deletions app/assets/javascripts/views/editor/editor_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
}

private async reloadEditor() {
const newEditor = this.application.componentManager!.editorForNote(
const newEditor = this.application.componentManager.editorForNote(
this.note
);
/** Editors cannot interact with template notes so the note must be inserted */
Expand All @@ -334,7 +334,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
});
this.reloadFont();
}
this.application.componentManager!.contextItemDidChangeInArea(
this.application.componentManager.contextItemDidChangeInArea(
ComponentArea.Editor
);
}
Expand Down Expand Up @@ -718,7 +718,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {

registerComponentHandler() {
this.unregisterComponent =
this.application.componentManager!.registerHandler({
this.application.componentManager.registerHandler({
identifier: 'editor',
areas: [ComponentArea.EditorStack, ComponentArea.Editor],
contextRequestHandler: (componentUuid) => {
Expand Down Expand Up @@ -747,15 +747,15 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
if (this.note) {
for (const component of stackComponents) {
if (component.active) {
this.application.componentManager!.setComponentHidden(
this.application.componentManager.setComponentHidden(
component,
!component.isExplicitlyEnabledForItem(this.note.uuid)
);
}
}
}
await this.setState({ stackComponents });
this.application.componentManager!.contextItemDidChangeInArea(
this.application.componentManager.contextItemDidChangeInArea(
ComponentArea.EditorStack
);
}
Expand All @@ -766,15 +766,15 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {

async toggleStackComponentForCurrentItem(component: SNComponent) {
const hidden =
this.application.componentManager!.isComponentHidden(component);
this.application.componentManager.isComponentHidden(component);
if (hidden || !component.active) {
this.application.componentManager!.setComponentHidden(component, false);
this.application.componentManager.setComponentHidden(component, false);
await this.associateComponentWithCurrentNote(component);
this.application.componentManager!.contextItemDidChangeInArea(
this.application.componentManager.contextItemDidChangeInArea(
ComponentArea.EditorStack
);
} else {
this.application.componentManager!.setComponentHidden(component, true);
this.application.componentManager.setComponentHidden(component, true);
await this.disassociateComponentWithCurrentNote(component);
}
this.application.sync();
Expand Down

0 comments on commit ba4fbd1

Please sign in to comment.