2023-12-05 04:40:49 -05:00
|
|
|
import { createContext, useContext, useEffect } from 'react'
|
2023-11-23 07:03:22 -05:00
|
|
|
import { useConnectionContext } from '@/features/ide-react/context/connection-context'
|
|
|
|
import { useEditorContext } from '@/shared/context/editor-context'
|
|
|
|
import getMeta from '@/utils/meta'
|
2023-12-05 04:40:49 -05:00
|
|
|
import {
|
|
|
|
Permissions,
|
|
|
|
PermissionsLevel,
|
|
|
|
} from '@/features/ide-react/types/permissions'
|
|
|
|
import useScopeValue from '@/shared/hooks/use-scope-value'
|
|
|
|
import { DeepReadonly } from '../../../../../types/utils'
|
2023-11-23 07:03:22 -05:00
|
|
|
|
2023-12-05 04:40:49 -05:00
|
|
|
const PermissionsContext = createContext<Permissions | undefined>(undefined)
|
2023-11-23 07:03:22 -05:00
|
|
|
|
2023-12-05 04:40:49 -05:00
|
|
|
const permissionsMap: DeepReadonly<Record<PermissionsLevel, Permissions>> = {
|
|
|
|
readOnly: {
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
admin: false,
|
|
|
|
comment: true,
|
|
|
|
},
|
|
|
|
readAndWrite: {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
admin: false,
|
|
|
|
comment: true,
|
|
|
|
},
|
|
|
|
owner: {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
admin: true,
|
|
|
|
comment: true,
|
|
|
|
},
|
2023-11-23 07:03:22 -05:00
|
|
|
}
|
2023-12-05 04:40:49 -05:00
|
|
|
|
|
|
|
const anonymousPermissionsMap: typeof permissionsMap = {
|
|
|
|
readOnly: { ...permissionsMap.readOnly, comment: false },
|
|
|
|
readAndWrite: { ...permissionsMap.readAndWrite, comment: false },
|
|
|
|
owner: { ...permissionsMap.owner, comment: false },
|
2023-11-23 07:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export const PermissionsProvider: React.FC = ({ children }) => {
|
2023-12-05 04:40:49 -05:00
|
|
|
const [permissions, setPermissions] =
|
|
|
|
useScopeValue<Readonly<Permissions>>('permissions')
|
2023-11-23 07:03:22 -05:00
|
|
|
const { connectionState } = useConnectionContext()
|
2023-12-05 04:40:49 -05:00
|
|
|
const { permissionsLevel } = useEditorContext() as {
|
|
|
|
permissionsLevel: PermissionsLevel
|
|
|
|
}
|
2023-11-23 07:03:22 -05:00
|
|
|
const anonymous = getMeta('ol-anonymous') as boolean | undefined
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-12-05 04:40:49 -05:00
|
|
|
const activePermissionsMap = anonymous
|
|
|
|
? anonymousPermissionsMap
|
|
|
|
: permissionsMap
|
|
|
|
setPermissions(activePermissionsMap[permissionsLevel])
|
|
|
|
}, [anonymous, permissionsLevel, setPermissions])
|
2023-11-23 07:03:22 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (connectionState.forceDisconnected) {
|
|
|
|
setPermissions(prevState => ({ ...prevState, write: false }))
|
|
|
|
}
|
2023-12-05 04:40:49 -05:00
|
|
|
}, [connectionState.forceDisconnected, setPermissions])
|
2023-11-23 07:03:22 -05:00
|
|
|
|
|
|
|
return (
|
2023-12-05 04:40:49 -05:00
|
|
|
<PermissionsContext.Provider value={permissions}>
|
2023-11-23 07:03:22 -05:00
|
|
|
{children}
|
|
|
|
</PermissionsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-05 04:40:49 -05:00
|
|
|
export function usePermissionsContext() {
|
2023-11-23 07:03:22 -05:00
|
|
|
const context = useContext(PermissionsContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'usePermissionsContext is only available inside PermissionsProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|