Skip to content

Commit

Permalink
clean up orchestra
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Nov 22, 2023
1 parent e4f519d commit 6764bb1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 113 deletions.
71 changes: 12 additions & 59 deletions libs/orchestra/index.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,34 @@
import { del, get, set } from 'idb-keyval'
// import { Studio } from 'libs/theatre/studio'
import { broadcast } from 'libs/zustand-broadcast'
import { createContext, useContext, useEffect } from 'react'
import { useEffect, useState } from 'react'
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { shallow } from 'zustand/shallow'
import { createWithEqualityFn } from 'zustand/traditional'
import s from './orchestra.module.scss'

// avoid to display debug tools on orchestra page
const useInternalStore = createWithEqualityFn(
(set) => ({
isVisible: true,
setIsVisible: (isVisible) => set({ isVisible }),
}),
shallow,
)

// https://github.com/pmndrs/zustand/blob/main/docs/integrations/persisting-store-data.md
const INDEXEDDB_STORAGE = {
getItem: async (name) => {
// console.log(name, 'has been retrieved')
return (await get(name)) || null
},
setItem: async (name, value) => {
// console.log(name, 'with value', value, 'has been saved')
await set(name, value)
},
removeItem: async (name) => {
// console.log(name, 'has been deleted')
await del(name)
},
}
let isOrchestraPage = false

export const useOrchestraStore = createWithEqualityFn(
const useOrchestraStore = create(
persist(() => ({}), {
name: 'orchestra',
storage: createJSONStorage(() => INDEXEDDB_STORAGE),
storage: createJSONStorage(() => localStorage),
}),
shallow,
)

broadcast(useOrchestraStore, 'orchestra')

export const OrchestraContext = createContext({})

export function useOrchestra() {
return useContext(OrchestraContext)
}
const values = useOrchestraStore()

// add around the app
export function Orchestra({ children }) {
const isVisible = useInternalStore(({ isVisible }) => isVisible)
const [isVisible, setIsVisible] = useState(false)

const value = useOrchestraStore((value) => value, shallow)
useEffect(() => {
setIsVisible(!isOrchestraPage)
}, [])

return (
<>
<OrchestraContext.Provider value={value}>
{children(isVisible ? value : {})}
</OrchestraContext.Provider>
</>
)
return isVisible && values
}

export function OrchestraToggle({ icon, title, id }) {
// useEffect(() => {
// useOrchestraStore.setState((state) => {
// const clone = { ...state }
// clone[id] = defaultValue
// return clone
// })
// }, [defaultValue])

return (
<button
onClick={() => {
Expand All @@ -90,11 +47,7 @@ export function OrchestraToggle({ icon, title, id }) {

// to be added to debug pages
export function OrchestraPage({ children }) {
useEffect(() => {
useInternalStore.setState(() => ({
isVisible: false,
}))
}, [])
isOrchestraPage = true

return <div className={s.orchestra}>{children}</div>
}
56 changes: 27 additions & 29 deletions libs/theatre/studio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function Studio() {

initialized = true
}
}, [studio, rafDriver])
}, [rafDriver])

useEffect(() => {
studio.ui.restore()
Expand All @@ -32,33 +32,31 @@ export function Studio() {
}, [])

return (
<>
<div className={s.studio}>
<button
onClick={() => {
// setVisible(!visible)
const id = project.address.projectId
const json = studio.createContentOfSaveFile(id)
const file = new File(
[jsonminify(JSON.stringify(json, null, 2))],
'config.json',
{
type: 'application/json',
},
)
const url = URL.createObjectURL(file)
const a = document.createElement('a')
a.href = url
// create title using id and date up to seconds
const title = `${id}-${new Date().toISOString().slice(0, 19)}`
a.download = title
a.click()
}}
className={s.save}
>
💾
</button>
</div>
</>
<div className={s.studio}>
<button
onClick={() => {
// setVisible(!visible)
const id = project.address.projectId
const json = studio.createContentOfSaveFile(id)
const file = new File(
[jsonminify(JSON.stringify(json, null, 2))],
'config.json',
{
type: 'application/json',
},
)
const url = URL.createObjectURL(file)
const a = document.createElement('a')
a.href = url
// create title using id and date up to seconds
const title = `${id}-${new Date().toISOString().slice(0, 19)}`
a.download = title
a.click()
}}
className={s.save}
>
💾
</button>
</div>
)
}
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const nextConfig = {
},
images: {
// ADD in case you need to import SVGs in next/image component
// dangerouslyAllowSVG: true,
dangerouslyAllowSVG: true,
// contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
remotePatterns: [
{
Expand Down
41 changes: 17 additions & 24 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeviceDetectionProvider } from 'components/device-detection'
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'
import { GTM_ID } from 'libs/analytics'
import { Orchestra } from 'libs/orchestra'
import { useOrchestra } from 'libs/orchestra'
import { useStore } from 'libs/store'
import { ProjectProvider, RafDriverProvider } from 'libs/theatre'
import dynamic from 'next/dynamic'
Expand Down Expand Up @@ -37,7 +37,7 @@ if (typeof window !== 'undefined') {

gsap.defaults({ ease: 'none' })
gsap.registerPlugin(ScrollTrigger)
ScrollTrigger.clearScrollMemory('manual')
ScrollTrigger.clearScrollMemory(window.history.scrollRestoration)
ScrollTrigger.defaults({ markers: process.env.NODE_ENV === 'development' })

// merge rafs
Expand All @@ -62,6 +62,8 @@ function MyApp({ Component, pageProps }) {
}
}, [lenis, isNavOpened])

const { stats, grid, studio, dev } = useOrchestra()

return (
<>
{/* Google Tag Manager - Global base code */}
Expand All @@ -86,28 +88,19 @@ function MyApp({ Component, pageProps }) {
)}
<RealViewport />
<DeviceDetectionProvider>
<Orchestra>
{(
{ studio, grid, stats, dev }, // check _debug/orchestra page to match ids
) => (
<ProjectProvider
id="Satus"
config="/config/Satus-2023-04-17T12_55_21.json"
>
<RafDriverProvider id="default">
<Component {...pageProps} />
{studio && <Studio />}
{stats && <Stats />}
{grid && <GridDebugger />}
{typeof document !== 'undefined' &&
document.documentElement.classList.toggle(
'dev',
Boolean(dev),
)}
</RafDriverProvider>
</ProjectProvider>
)}
</Orchestra>
<ProjectProvider
id="Satus"
config="/config/Satus-2023-04-17T12_55_21.json"
>
<RafDriverProvider id="default">
<Component {...pageProps} />
{studio && <Studio />}
{stats && <Stats />}
{grid && <GridDebugger />}
{typeof document !== 'undefined' &&
document.documentElement.classList.toggle('dev', Boolean(dev))}
</RafDriverProvider>
</ProjectProvider>
</DeviceDetectionProvider>
</>
)
Expand Down

1 comment on commit 6764bb1

@github-actions
Copy link

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: 59
🟢 Accessibility: 97
🟢 Best practices: 100
🟠 SEO: 75
🔴 PWA: 30

Lighthouse ran on https://satus-4dzbgigsd-studio-freight.vercel.app/"

Please sign in to comment.