mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
c24ace801b
GitOrigin-RevId: 7a0d45e17d9905fa75569e2d19ca59caa4a41565
26 lines
766 B
TypeScript
26 lines
766 B
TypeScript
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>) => {
|
|
const scopeRef = useRef<typeof scope | null>(null)
|
|
if (scopeRef.current === null) {
|
|
scopeRef.current = scope
|
|
}
|
|
|
|
useLayoutEffect(() => {
|
|
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))
|
|
}
|
|
}
|
|
}, [])
|
|
}
|