-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add necessary scaffolding for enabling LV on dashboard #5930
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a7a4c26
Use forked version of
zoldar f91c217
Add necessary scaffolding for enabling LV on dashboard
zoldar 05f72e2
Implement basics for LV pages breakdown
zoldar 154ba8b
Make tile and tabs latency friendly
zoldar 496469b
Bring back eslint-disable pragma in live_socket.js
zoldar 17d5005
Document the code somewhat
zoldar 2165b8d
Fix live navigation callback in React
zoldar d86f71a
Make dashboard components inside portals testable
zoldar 467ccac
Add very rudimentary basic tests
zoldar e2dc1a6
Fix typo
zoldar 7ae1a68
Fix eslint pragma in `live_socket.js`
zoldar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * Component used for embedding LiveView components inside React. | ||
| * | ||
| * The content of the portal is completely excluded from React re-renders with | ||
| * a hardwired `React.memo`. | ||
| */ | ||
|
|
||
| import React from 'react' | ||
| import classNames from 'classnames' | ||
|
|
||
| const MIN_HEIGHT = 380 | ||
|
|
||
| type LiveViewPortalProps = { | ||
| id: string | ||
| className?: string | ||
| } | ||
|
|
||
| export const LiveViewPortal = React.memo( | ||
| function ({ id, className }: LiveViewPortalProps) { | ||
| return ( | ||
| <div | ||
| id={id} | ||
| className={classNames('group', className)} | ||
| style={{ width: '100%', border: '0', minHeight: MIN_HEIGHT }} | ||
| > | ||
| <div | ||
| className="w-full flex flex-col justify-center group-has-[[data-phx-teleported]]:hidden" | ||
| style={{ minHeight: MIN_HEIGHT }} | ||
| > | ||
| <div className="mx-auto loading"> | ||
| <div /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| }, | ||
| () => true | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /** | ||
| * Hook used by LiveView dashboard. | ||
| * | ||
| * Defines various widgets to use by various dashboard specific components. | ||
| */ | ||
|
|
||
| const WIDGETS = { | ||
| // Hook widget delegating navigation events to and from React. | ||
| // Necessary to emulate navigation events in LiveView with pushState | ||
| // manipulation disabled. | ||
| 'dashboard-root': { | ||
| initialize: function () { | ||
| this.url = window.location.href | ||
|
|
||
| addListener.bind(this)('click', document.body, (e) => { | ||
| const type = e.target.dataset.type || null | ||
|
|
||
| if (type === 'dashboard-link') { | ||
| this.url = e.target.href | ||
| const uri = new URL(this.url) | ||
| // Domain is dropped from URL prefix, because that's what react-dom-router | ||
| // expects. | ||
| const path = '/' + uri.pathname.split('/').slice(2).join('/') | ||
| this.el.dispatchEvent( | ||
| new CustomEvent('dashboard:live-navigate', { | ||
ukutaht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bubbles: true, | ||
| detail: { path: path, search: uri.search } | ||
| }) | ||
| ) | ||
|
|
||
| this.pushEvent('handle_dashboard_params', { url: this.url }) | ||
|
|
||
| e.preventDefault() | ||
| } | ||
| }) | ||
|
|
||
| // Browser back and forward navigation triggers that event. | ||
| addListener.bind(this)('popstate', window, () => { | ||
| if (this.url !== window.location.href) { | ||
| this.pushEvent('handle_dashboard_params', { | ||
| url: window.location.href | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| // Navigation events triggered from liveview are propagated via this | ||
| // handler. | ||
| addListener.bind(this)('dashboard:live-navigate-back', window, (e) => { | ||
| if ( | ||
| typeof e.detail.search === 'string' && | ||
| this.url !== window.location.href | ||
| ) { | ||
| this.pushEvent('handle_dashboard_params', { | ||
| url: window.location.href | ||
| }) | ||
| } | ||
| }) | ||
| }, | ||
| cleanup: function () { | ||
| removeListeners.bind(this)() | ||
| } | ||
| }, | ||
| // Hook widget for optimistic loading of tabs and | ||
| // client-side persistence of selection using localStorage. | ||
| tabs: { | ||
| initialize: function () { | ||
| const domain = getDomain(window.location.href) | ||
|
|
||
| addListener.bind(this)('click', this.el, (e) => { | ||
| const button = e.target.closest('button') | ||
| const tab = button && button.dataset.tab | ||
|
|
||
| if (tab) { | ||
| const label = button.dataset.label | ||
| const storageKey = button.dataset.storageKey | ||
| const activeClasses = button.dataset.activeClasses | ||
| const inactiveClasses = button.dataset.inactiveClasses | ||
| const title = this.el | ||
| .closest('[data-tile]') | ||
| .querySelector('[data-title]') | ||
|
|
||
| title.innerText = label | ||
|
|
||
| this.el.querySelectorAll(`button[data-tab] span`).forEach((s) => { | ||
| s.className = inactiveClasses | ||
| }) | ||
|
|
||
| button.querySelector('span').className = activeClasses | ||
|
|
||
| if (storageKey) { | ||
| localStorage.setItem(`${storageKey}__${domain}`, tab) | ||
| } | ||
| } | ||
| }) | ||
| }, | ||
| cleanup: function () { | ||
| removeListeners.bind(this)() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getDomain(url) { | ||
| const uri = typeof url === 'object' ? url : new URL(url) | ||
| return uri.pathname.split('/')[1] | ||
| } | ||
|
|
||
| function addListener(eventName, listener, callback) { | ||
| this.listeners = this.listeners || [] | ||
|
|
||
| listener.addEventListener(eventName, callback) | ||
|
|
||
| this.listeners.push({ | ||
| element: listener, | ||
| event: eventName, | ||
| callback: callback | ||
| }) | ||
| } | ||
|
|
||
| function removeListeners() { | ||
| if (this.listeners) { | ||
| this.listeners.forEach((l) => { | ||
| l.element.removeEventListener(l.event, l.callback) | ||
| }) | ||
|
|
||
| this.listeners = null | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| mounted() { | ||
| this.widget = this.el.getAttribute('data-widget') | ||
|
|
||
| this.initialize() | ||
| }, | ||
|
|
||
| updated() { | ||
| this.initialize() | ||
| }, | ||
|
|
||
| reconnected() { | ||
| this.initialize() | ||
| }, | ||
|
|
||
| destroyed() { | ||
| this.cleanup() | ||
| }, | ||
|
|
||
| initialize() { | ||
| this.cleanup() | ||
| WIDGETS[this.widget].initialize.bind(this)() | ||
| }, | ||
|
|
||
| cleanup() { | ||
| WIDGETS[this.widget].cleanup.bind(this)() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'm not a fan of introducing this new concept (widget). It took a bit of jumping around and reading the code to understand it's just a liveview hook. Why not call them what they are - hooks - and register them directly instead of via
data-widget? This way anyone who's familiar with liveview (and the concept of hooks) understands immediately what's going on without having to learn and get used to an intermediary layer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way - I do like the shared structure for adding/removing listeners and cleaning them up. In Prima I have the same problem where all the components duplicate some amount of this structure.
I wonder if this can be solved via some kind of mixin or inheritance instead. I haven't really experimented with sharing behaviour between hooks yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My worry was about having to add multitude of hooks, majority of them concerned with dashboard specific stuff. I'm not saying it's rational 😅 And you are right, it's not worth introducing cognitive load of a new concept I came up with "on the go". I'll see what can we do about structure sharing.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yeah I don't think there's a problem with having many hooks. Surely
hooksis just a map in liveview that looks them up in the same way asWIDGETS[this.widget].It is a bit annoying to have to manually import and register them all when initializing a liveSocket. But I think there's also value in being explicit rather than magical. For namespacing and organization perhaps we can make something like this:
There's also a new ColocatedHook feature in Phoenix. I haven't tried it, not sure if/how it would work if you want to import some shared helpers or dependencies in these co-located hooks for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about the approach taken in #5937 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!