Skip to content

Commit

Permalink
feat: pick up the URL variables at the start of the app
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienArcellier committed Sep 24, 2024
1 parent bd9db0c commit b7f6944
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/ui/src/components/core/root/CoreRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import { useEvaluator } from "@/renderer/useEvaluator";
const ssHashChangeStub = `
def handle_hashchange(state, payload):
# The payload is a dictionary with the page key and all the route variables in the URL hash.
# For example, if the current URL is
# http://localhost:3000/#main/animal=duck&colour=yellow
# For example, if the current URL is http://localhost:3000/#main/animal=duck&colour=yellow
# you will get the following dictionary
# {
# "page_key": "main",
# "route_vars": {
# "animal": "duck",
# "colour": "yellow"
# }
# "page_key": "main",
# "route_vars": {
# "animal": "duck",
# "colour": "yellow"
# }
# }
page_key = payload.get("page_key")
Expand All @@ -39,6 +38,23 @@ def handle_hashchange(state, payload):
else:
state["message"] = "You're not in the Duck zone.`.trim();
const wfAppOpenStub = `
def handle_app_open(state):
# The payload is a dictionary with the page key and all the route variables in the URL hash.
# For example, if the current URL is http://localhost:3000/#/animal=duck&colour=yellow
# you will get the following dictionary
# {
# "page_key": "main",
# "route_vars": {
# "animal": "duck",
# "colour": "yellow"
# }
# }
page_key = payload.get("page_key")
route_vars = payload.get("route_vars")
`.trim();
const description =
"The root component of the application, which serves as the starting point of the component hierarchy.";
Expand All @@ -57,6 +73,10 @@ export default {
...sharedStyleFields,
},
events: {
"wf-app-open": {
desc: "Captures the first application load, including page key and route vars.",
stub: wfAppOpenStub,
},
"wf-hashchange": {
desc: "Capture changes to the URL hash, including page key and route vars.",
stub: ssHashChangeStub,
Expand All @@ -74,6 +94,7 @@ import {
watch,
nextTick,
onBeforeMount,
onMounted,
} from "vue";
import injectionKeys from "@/injectionKeys";
import { changePageInHash, getParsedHash } from "@/core/navigation";
Expand Down Expand Up @@ -126,12 +147,26 @@ watch(displayedPageId, (newPageId) => {
changePageInHash(pageKey);
});
function handleAppOpenChange() {
const parsedHash = getParsedHash();
const event = new CustomEvent("wf-app-open", {
detail: {
payload: parsedHash,
},
});
rootEl.value?.dispatchEvent(event);
}
onBeforeMount(() => {
window.addEventListener("hashchange", () => {
handleHashChange();
});
handleHashChange();
});
onMounted(() => {
handleAppOpenChange();
});
</script>

<style scoped>
Expand Down
10 changes: 10 additions & 0 deletions src/writer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,16 @@ def _transform_page_open(self, ev) -> str:
payload = str(ev.payload)
return payload

def _transform_app_open(self, ev) -> dict:
payload = ev.payload
page_key = payload.get("pageKey")
route_vars = dict(payload.get("routeVars"))
tf_payload = {
"page_key": page_key,
"route_vars": route_vars
}
return tf_payload

def _transform_chatbot_message(self, ev) -> dict:
payload = dict(ev.payload)
return payload
Expand Down

0 comments on commit b7f6944

Please sign in to comment.