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

(WIP) PupFrame #94

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class DPanelApp extends LitElement {
this.router.addAfterHook(() => store.updateState({ appContext: { menuVisible: false }}))

// Clear some contexts on route change
this.router.addBeforeHook(() => store.clearContext(['pupContext']));
this.router.addBeforeHook(() => store.clearContext(['pupContext, guiContext']));

if (isUnauthedRoute()) {
setTimeout(() => { this.ready = true; }, 1500)
Expand Down
27 changes: 11 additions & 16 deletions src/pages/page-iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,14 @@ class IframeView extends LitElement {
}
`;

// Whenever pupContext source is available, set ready to true.
updated(changedProperties) {
super.updated(changedProperties);
if (changedProperties.has('pupContext')) {
this.ready = !!this.context.store.pupContext.manifest.gui.source;
this.requestUpdate();
}
}

constructor() {
super();

// Subscribe to changes of pupContext
this.context = new StoreSubscriber(this, store)

// Ready state is dependent on the pupContext having an iframe source.
this.ready = !!this.context.store.pupContext.manifest.gui.source
this.ready = !!this.context?.store?.guiContext?.ready;

this.iframeElement = null;
this.debouncedHandleResize = debounce(this.handleResize, 300);
Expand All @@ -59,7 +50,7 @@ class IframeView extends LitElement {
connectedCallback() {
super.connectedCallback();
this.updateComplete.then(() => {
if (!this.ready) return
if (!this.context?.store?.guiContext?.ready) return

this.iframeElement = this.shadowRoot.querySelector('iframe');

Expand Down Expand Up @@ -114,8 +105,8 @@ class IframeView extends LitElement {
}

render() {
const { pupContext } = this.context.store
if (!this.ready) {
const { guiContext } = this.context.store;
if (!guiContext.ready) {
return html`
<div id="IframeContainer">
<div id="LoaderContainer">
Expand All @@ -125,11 +116,15 @@ class IframeView extends LitElement {
`
}

if (this.ready) {
if (guiContext.ready) {
const iframeURL = `${window.location.protocol}//${window.location.hostname}:${guiContext.port}`
return html`

<div id="IframeContainer">
<iframe src="${pupContext.manifest.gui.source}" frameBorder="0"></iframe>
<iframe
src="${iframeURL}"
frameBorder="0"
sandbox="allow-scripts"
></iframe>
</div>
`;
}
Expand Down
6 changes: 2 additions & 4 deletions src/pages/page-pup-library-listing/renders/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ export function renderActions(labels, hasLogs) {
size="large"
variant="warning"
?disabled="${statusId !== 'running'}"
@click=${() => {
window.open(`${window.location.protocol}//${window.location.hostname}:${entry.port}/`, "_blank");
}}
">
href="/pups/${pkg.state.id}/ui/${encodeURIComponent(entry.name)}"
>
<sl-icon slot="prefix" name="stars"></sl-icon>
Launch ${entry.name}
</sl-button>
Expand Down
11 changes: 11 additions & 0 deletions src/router/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
isAuthed,
loadPup,
setActiveGui,
asPage,
withDialog,
performLogout,
Expand Down Expand Up @@ -43,6 +44,16 @@ export const routes = [
pageTitle: "Settings",
before: [isAuthed, asPage, withDialog],
},
{
path: "/pups/:pupid/ui/:guiname",
component: "x-page-pup-iframe",
dynamicTitle: true,
before: [isAuthed, asPage],
pageAction: "back",
animate: true,
before: [loadPup, asPage],
after: [setActiveGui],
},
{
path: "/pups",
component: "x-page-pup-library",
Expand Down
40 changes: 39 additions & 1 deletion src/router/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export async function loadPup(context, commands) {
store.updateState({
pupContext: { ...pup, ready: true, result: 200 },
});

// Attach pup to route context in case subsequent route middleware
// wish to refer to it.
context.pup = pup

} catch (error) {
console.warn("[500] loadPup middleware: failure.", error);
store.updateState({
Expand All @@ -69,6 +74,39 @@ export async function loadPup(context, commands) {
}
}

export async function setActiveGui(context, commands) {
const pupId = context.params.pupid;
const guiName = decodeURIComponent(context.params.guiname);

try {
// Attempt to get the pup from memory
let pup = pkgController.getPupMaster({ pupId, lookupType: 'byStatePupId' }).pup;

// Find the pup gui details
const guis = pup.state.webUIs || [];
const gui = guis.find((g) => g.name === guiName)

if (!gui) {
throw new Error(`Cannot find gui by name: ${guiName}`)
}

store.updateState({
guiContext: {
ready: true,
result: 200,
id: guiName,
...gui
},
});

} catch (err) {
console.log('woof', err);
store.updateState({
pupContext: { ready: true, result: 500 },
});
}
}

export function isAuthed(context, commands) {
if (store.networkContext.token) {
return undefined;
Expand All @@ -84,7 +122,7 @@ export function performLogout(context, commands) {

export function asPage(context, commands) {
if (context.route.dynamicTitle) {
context.route.pageTitle = decodeURIComponent(context.params.pupname);
context.route.pageTitle = decodeURIComponent(context.params.pupname || context.params.guiname);
context.route.pageAction = "back";
}

Expand Down
7 changes: 7 additions & 0 deletions src/state/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Store {
ready: false,
result: null,
};
this.guiContext = {
id: null,
ready: false,
};
this.promptContext = {
display: false,
name: "transaction",
Expand Down Expand Up @@ -161,6 +165,9 @@ class Store {
if (partialState.pupContext) {
this.pupContext = { ...this.pupContext, ...partialState.pupContext };
}
if (partialState.guiContext) {
this.guiContext = { ...this.guiContext, ...partialState.guiContext };
}
if (partialState.promptContext) {
this.promptContext = {
...this.promptContext,
Expand Down