-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e25913
commit 1f51bda
Showing
7 changed files
with
159 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,49 +1,84 @@ | ||
import { broadcast } from 'libs/zustand-broadcast' | ||
import { useEffect, useState } from 'react' | ||
import { create } from 'zustand' | ||
import { shared } from 'libs/zustand-shared' | ||
// import { create } from 'zustand' | ||
import { createJSONStorage, persist } from 'zustand/middleware' | ||
import { createStore } from 'zustand/vanilla' | ||
|
||
// avoid to display debug tools on orchestra page | ||
let isOrchestraPage = false | ||
|
||
const useOrchestraStore = create( | ||
const ID = 'orchestra' | ||
let store = createStore( | ||
persist(() => ({}), { | ||
name: 'orchestra', | ||
name: ID, | ||
storage: createJSONStorage(() => localStorage), | ||
}), | ||
) | ||
|
||
broadcast(useOrchestraStore, 'orchestra') | ||
|
||
export function useOrchestra() { | ||
const values = useOrchestraStore() | ||
store = shared(store, ID) | ||
|
||
const [isVisible, setIsVisible] = useState(false) | ||
class Toggle { | ||
constructor(id, content) { | ||
this.id = id | ||
this.content = content | ||
this.domElement = document.createElement('button') | ||
this.domElement.innerText = content | ||
this.domElement.title = id | ||
this.domElement.style.fontSize = '64px' | ||
this.domElement.addEventListener('click', this.onToggle, false) | ||
} | ||
|
||
useEffect(() => { | ||
setIsVisible(!isOrchestraPage) | ||
}, []) | ||
onToggle = () => { | ||
store.setState((state) => ({ ...state, [this.id]: !state[this.id] })) | ||
} | ||
|
||
return isVisible && values | ||
destroy() { | ||
this.domElement.removeEventListener('click', this.onToggle, false) | ||
this.domElement.remove() | ||
} | ||
} | ||
|
||
// to be added to debug pages | ||
export function OrchestraToggle({ children, title, id }) { | ||
isOrchestraPage = true | ||
|
||
return ( | ||
<button | ||
onClick={() => { | ||
useOrchestraStore.setState((state) => { | ||
const clone = { ...state } | ||
clone[id] = !clone[id] | ||
return clone | ||
}) | ||
}} | ||
title={title} | ||
style={{ fontSize: '64px' }} | ||
> | ||
{children} | ||
</button> | ||
) | ||
class Orchestra { | ||
constructor() { | ||
this.domElement = document.createElement('div') | ||
|
||
this.isDebug = false | ||
this.toggles = [] | ||
} | ||
|
||
get state() { | ||
return !this.isDebug && store.getState() | ||
} | ||
|
||
subscribe(callback) { | ||
if (!this.isDebug) store.subscribe(callback) | ||
} | ||
|
||
add(id, content) { | ||
// check if already exists | ||
if (this.toggles.find((toggle) => toggle.id === id)) return this | ||
|
||
const toggle = new Toggle(id, content) | ||
this.toggles.push(toggle) | ||
this.domElement.appendChild(toggle.domElement) | ||
|
||
return this | ||
} | ||
|
||
remove(id) { | ||
const toggle = this.toggles.find((toggle) => toggle.id === id) | ||
// this.domElement.removeChild(toggle.domElement) | ||
toggle.destroy() | ||
this.toggles = this.toggles.filter((toggle) => toggle.id !== id) | ||
|
||
return this | ||
} | ||
} | ||
|
||
const isClient = typeof window !== 'undefined' | ||
|
||
export default isClient && new Orchestra() | ||
|
||
// To be added to debug page | ||
// Orchestra.isDebug = true | ||
// Orchestra.add('studio', '⚙️') | ||
// Orchestra.add('stats', '📈') | ||
// Orchestra.add('grid', '🌐') | ||
// Orchestra.add('dev', '🚧') | ||
// document.body.appendChild(Orchestra.domElement) |
This file contains 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,34 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import Orchestra from '.' | ||
|
||
export function useOrchestra() { | ||
const [state, setState] = useState({}) | ||
|
||
useEffect(() => { | ||
setState(Orchestra.state) | ||
Orchestra.subscribe((state) => { | ||
setState(state) | ||
}) | ||
}, []) | ||
|
||
return state | ||
} | ||
|
||
export function OrchestraToggle({ id, children }) { | ||
if (Orchestra) Orchestra.isDebug = true | ||
|
||
const elementRef = useRef() | ||
|
||
useEffect(() => { | ||
Orchestra.add(id, children) | ||
const toggle = Orchestra.toggles.find((toggle) => toggle.id === id) | ||
elementRef.current.appendChild(toggle.domElement) | ||
|
||
return () => { | ||
Orchestra.remove(id) | ||
toggle.domElement.remove() | ||
} | ||
}, [id, children]) | ||
|
||
return <span ref={elementRef} /> | ||
} |
This file contains 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,42 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
// avoid to display debug tools on orchestra page | ||
let isOrchestraPage = false | ||
|
||
let useOrchestraStore = create( | ||
persist(() => ({}), { | ||
name: 'orchestra', | ||
storage: createJSONStorage(() => localStorage), | ||
}), | ||
) | ||
|
||
useOrchestraStore = shared(useOrchestraStore, 'orchestra') | ||
|
||
export function useOrchestra() { | ||
const values = useOrchestraStore() | ||
|
||
const [isVisible, setIsVisible] = useState(false) | ||
|
||
useEffect(() => { | ||
setIsVisible(!isOrchestraPage) | ||
}, []) | ||
|
||
return isVisible && values | ||
} | ||
|
||
// to be added to debug pages | ||
export function OrchestraToggle({ children, title, id }) { | ||
isOrchestraPage = true | ||
|
||
return ( | ||
<button | ||
onClick={() => { | ||
useOrchestraStore.setState((state) => ({ [id]: !state[id] })) | ||
}} | ||
title={title} | ||
style={{ fontSize: '64px' }} | ||
> | ||
{children} | ||
</button> | ||
) | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,23 +1,15 @@ | ||
import { OrchestraToggle } from 'libs/orchestra' | ||
import { OrchestraToggle } from 'libs/orchestra/react' | ||
import { forwardRef } from 'react' | ||
|
||
const Orchestra = forwardRef(function Orchestra({}) { | ||
const OrchestraPage = forwardRef(function OrchestraPage({}) { | ||
return ( | ||
<> | ||
<OrchestraToggle title="studio" id="studio"> | ||
⚙️ | ||
</OrchestraToggle> | ||
<OrchestraToggle title="performance" id="stats"> | ||
📈 | ||
</OrchestraToggle> | ||
<OrchestraToggle title="grid" id="grid"> | ||
🌐 | ||
</OrchestraToggle> | ||
<OrchestraToggle title="dev" id="dev"> | ||
🚧 | ||
</OrchestraToggle> | ||
<OrchestraToggle id="studio">⚙️</OrchestraToggle> | ||
<OrchestraToggle id="stats">📈</OrchestraToggle> | ||
<OrchestraToggle id="grid">🌐</OrchestraToggle> | ||
<OrchestraToggle id="dev">🚧</OrchestraToggle> | ||
</> | ||
) | ||
}) | ||
|
||
export default Orchestra | ||
export default OrchestraPage |
1f51bda
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.
"⚡️ Lighthouse report for the changes in this commit:
🟢 Performance: 100
🟢 Accessibility: 97
🟢 Best practices: 92
🟠 SEO: 75
🔴 PWA: 40
Lighthouse ran on https://satus-nf92x8x77-studio-freight.vercel.app/"