mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
889fa1a3c3
Replace alerts with notifications in the github-sync modal GitOrigin-RevId: 772add41b2d525353dc0c0362ebc64c4a023e2c3
30 lines
931 B
TypeScript
30 lines
931 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
|
|
}
|
|
if (typeof existingValue === 'object' && typeof value === 'object') {
|
|
window.overleaf.unstable.store.set(path, merge(existingValue, value))
|
|
} else {
|
|
window.overleaf.unstable.store.set(path, value)
|
|
}
|
|
}
|
|
}
|
|
}, [])
|
|
}
|