2022-05-16 05:38:20 -04:00
|
|
|
import { merge } from 'lodash'
|
|
|
|
import { useLayoutEffect, useRef } from 'react'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge properties with the scope object, for use in Storybook stories
|
|
|
|
*/
|
|
|
|
export const useScope = (scope: Record<string, unknown>) => {
|
2022-05-30 06:18:36 -04:00
|
|
|
const scopeRef = useRef<typeof scope | null>(null)
|
2022-05-16 05:38:20 -04:00
|
|
|
if (scopeRef.current === null) {
|
|
|
|
scopeRef.current = scope
|
|
|
|
}
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2024-06-05 04:33:11 -04:00
|
|
|
if (scopeRef.current) {
|
|
|
|
for (const [path, value] of Object.entries(scopeRef.current)) {
|
|
|
|
let existingValue: typeof value | undefined
|
|
|
|
try {
|
|
|
|
existingValue = window.overleaf.unstable.store.get(path)
|
|
|
|
} catch {
|
|
|
|
// allowed not to exist
|
|
|
|
}
|
|
|
|
window.overleaf.unstable.store.set(path, merge(existingValue, value))
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 05:38:20 -04:00
|
|
|
}, [])
|
|
|
|
}
|