Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streamer mode fix #512

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions raw_package/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<esphome-main
version="{{ version }}"
docsLink="{{ docs_link }}"
{% if streamer_mode %}streamerMode="true"{% end %}
grahambrown11 marked this conversation as resolved.
Show resolved Hide resolved
{% if login_enabled %}logoutUrl="{{ relative_url }}logout"{% end %}
></esphome-main>
{% end %}
Expand Down
4 changes: 3 additions & 1 deletion src/components/remote-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class ESPHomeRemoteProcess extends HTMLElement {
}
${coloredConsoleStyles}
</style>
<div class="log"></div>
<div class="log${
balloob marked this conversation as resolved.
Show resolved Hide resolved
ColoredConsole.blurSecrets ? " blur-secrets" : ""
}"></div>
`;

const coloredConsole = new ColoredConsole(shadowRoot.querySelector("div")!);
Expand Down
77 changes: 68 additions & 9 deletions src/editor/esphome-editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import { LitElement, html } from "lit";
import { customElement, property, query } from "lit/decorators.js";
import { customElement, property, query, state } from "lit/decorators.js";
import "@material/mwc-dialog";
import "@material/mwc-button";
import "@material/mwc-snackbar";
Expand All @@ -12,6 +12,7 @@ import type { Snackbar } from "@material/mwc-snackbar";
import { fireEvent } from "../util/fire-event";
import { debounce } from "../util/debounce";
import "./monaco-provider";
import { mdiIncognito } from "@mdi/js";

// WebSocket URL Helper
const loc = window.location;
Expand All @@ -34,19 +35,22 @@ class ESPHomeEditor extends LitElement {
private editorActiveSecrets = false;

@property() public fileName!: string;
@property({ type: Boolean }) streamerMode = false;
@query("mwc-snackbar", true) private _snackbar!: Snackbar;
@query("main", true) private container!: HTMLElement;
@query(".esphome-header", true) private editor_header!: HTMLElement;
@state() private _showSecrets = false;

createRenderRoot() {
return this;
}

protected render() {
const isSecrets =
this.fileName === "secrets.yaml" || this.fileName === "secrets.yml";
private _isSecrets() {
return this.fileName === "secrets.yaml" || this.fileName === "secrets.yml";
}

return html`
protected render() {
const commonStyle = html`
grahambrown11 marked this conversation as resolved.
Show resolved Hide resolved
<style>
html,
body {
Expand All @@ -59,7 +63,7 @@ class ESPHomeEditor extends LitElement {
align-items: center;
align-content: stretch;
}
h2 {
.esphome-header h2 {
line-height: 100%;
/* this margin, padding stretches the container, offsetHeight does not calculate margin of .editor-header */
padding: 0.8rem 0.5rem 1rem 0.5rem;
Expand All @@ -70,6 +74,60 @@ class ESPHomeEditor extends LitElement {
white-space: nowrap;
text-overflow: ellipsis;
}
</style>
`;
const isSecrets = this._isSecrets();
if (isSecrets && this.streamerMode && !this._showSecrets) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels weird to put it in this component. Instead, this dialog should be it’s own code that is called from the “Secrets” button. When user confirms, it will continue to open the editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be an approach, but this will work for when I've implemented routing the secrets page will have it;s own url /secrets the button will be a link to the page not a change in state...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can always attach an event listener to the link to avoid it linking in streamer mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you're not ok with this being in the editor.
There was already logic for the editor to handle secrets differently (no install & ace websocket not started)
so I don't see why it cannot be given 1 extra parameter (streamer mode) in order to show the warning 1st

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A component should do 1 thing. This makes it very complicated with doing completely different things. Also, it's a prompt to a user so we should follow the convention of asking that as a dialog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK created a dialog in streamer-warning.ts
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balloob are you happy with this change?

return html`
${commonStyle}
<style>
.secrets-warning-container {
text-align: center;
padding: 40px;
color: var(--primary-text-color);
}
h1 {
font-size: 2.5rem;
line-height: 110%;
font-weight: 400;
margin: 1rem 0 0.65rem 0;
}
esphome-svg-icon {
--mdc-icon-size: 56px;
}
</style>
<div class="esphome-header">
<mwc-icon-button
icon="clear"
@click=${this._handleClose}
aria-label="close"
></mwc-icon-button>
<h2>${this.fileName}</h2>
</div>
<main>
<div class="secrets-warning-container">
<esphome-svg-icon .path=${mdiIncognito}></esphome-svg-icon>
<h1>Show Secrets Warning</h1>
<p>
Streamer mode enabled, are you sure you want to disclose your
secrets?
</p>
<mwc-button
raised
label="Show My Secrets"
@click="${() => (this._showSecrets = true)}"
></mwc-button>
</div>
</main>
`;
} else if (isSecrets && this.streamerMode && this._showSecrets) {
// as the 1st update was skipped, fire firstUpdated again
setTimeout(() => this.firstUpdated(), 50);
}

return html`
${commonStyle}
<style>
mwc-icon-button {
--mdc-icon-button-size: 32px;
}
Expand Down Expand Up @@ -137,6 +195,10 @@ class ESPHomeEditor extends LitElement {
}

firstUpdated() {
const isSecrets = this._isSecrets();
if (isSecrets && this.streamerMode && !this._showSecrets) {
return;
}
// @ts-ignore
self.MonacoEnvironment = {
getWorkerUrl: function (moduleId: string, label: string) {
Expand All @@ -159,9 +221,6 @@ class ESPHomeEditor extends LitElement {
'ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace',
});

const isSecrets =
this.fileName === "secrets.yaml" || this.fileName === "secrets.yml";

getFile(this.fileName).then((response) => {
if (response === null && isSecrets) {
response = EMPTY_SECRETS;
Expand Down
10 changes: 8 additions & 2 deletions src/esphome-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import "./components/esphome-header-menu";
import "./components/esphome-fab";
import { LitElement, html, PropertyValues } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { ColoredConsole } from "./util/console-color";

@customElement("esphome-main")
class ESPHomeMainView extends LitElement {
@property() version = "unknown";

@property() docsLink = "";

@property() logoutUrl?: string;
@property({ type: Boolean }) streamerMode = false;

@state() private editing?: string;

Expand All @@ -27,6 +27,7 @@ class ESPHomeMainView extends LitElement {
<esphome-editor
@close=${this._handleEditorClose}
fileName=${this.editing}
.streamerMode=${this.streamerMode}
></esphome-editor>
`;
}
Expand Down Expand Up @@ -61,6 +62,11 @@ class ESPHomeMainView extends LitElement {
return this;
}

connectedCallback() {
ColoredConsole.blurSecrets = this.streamerMode;
super.connectedCallback();
}

protected firstUpdated(changedProps: PropertyValues): void {
super.firstUpdated(changedProps);
document.body.addEventListener<any>("edit-file", (ev) => {
Expand Down
19 changes: 17 additions & 2 deletions src/util/console-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface ConsoleState {
}

export class ColoredConsole {
public static blurSecrets = false;
grahambrown11 marked this conversation as resolved.
Show resolved Hide resolved

public state: ConsoleState = {
bold: false,
italic: false,
Expand All @@ -24,7 +26,9 @@ export class ColoredConsole {
constructor(public targetElement: HTMLElement) {}

logs(): string {
return this.targetElement.innerText;
const logs = this.targetElement.cloneNode(true) as HTMLElement;
logs.querySelectorAll(".log-secret").forEach((e) => (e.innerHTML = ""));
grahambrown11 marked this conversation as resolved.
Show resolved Hide resolved
return logs.innerText;
}

addLine(line: string) {
Expand Down Expand Up @@ -228,9 +232,11 @@ export const coloredConsoleStyles = `
-ms-user-select: none;
user-select: none;
}
.log.blur-secrets .log-secret {
filter: blur(5px);
}
.log-secret-redacted {
opacity: 0;
width: 1px;
font-size: 1px;
}
.log-fg-black {
Expand Down Expand Up @@ -281,4 +287,13 @@ export const coloredConsoleStyles = `
.log-bg-white {
background-color: rgb(255, 255, 255);
}
@media not screen {
grahambrown11 marked this conversation as resolved.
Show resolved Hide resolved
.log-secret {
display: none;
}
.log-secret-redacted {
opacity: 0;
font-size: 12px;
}
}
`;