Files
supabase/apps/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsx
T
6d121f1546 feat(dashboard): Add column-level privileges management (#13745)
* add privileges page

* fix

* fix: could have both global and column-specific privileges + is global per privilege

* Trigger Build

* load permissions data for current table only

* fix style for light theme

* add a guide to Privileges

* Update apps/docs/pages/guides/auth/column-level-security.mdx

Co-authored-by: Greg Richardson <greg.nmr@gmail.com>

* Update apps/docs/pages/guides/auth/column-level-security.mdx

Co-authored-by: Greg Richardson <greg.nmr@gmail.com>

* Update doc

* Update docs

* progress moving privileges to pg-meta

* split up column privileges and table privileges

* adds loading state to toggles

* Add feature flag, move from auth section to database section

* Cleanup

* Update meta store table lists, add header warnings, update docs

* Add alert if any columns have privilages

* Spelling

* fix api.d.ts

* fix link

* move to auth pages

* only show editable schemas

* limit roles

* auto select first table

* remove roles meta store

* inline toggling

* apply privileges all at once

* add preview and use new switch component

* fix spelling

* handle long column names

* warn users about losing changes when changing schemas/tables

* remove strange docs link

* fix feature preview after reload

* use shadcn select

* Update select.tsx

* Minor UX fixes

* Update toast

* Update feature preview image for cls

* update cls warnings

* update warnings

* fix toast message

* Add migration alert to preview page

* Add link to CLS from feature preview, and add empty state on CLS page if feature preview is not enabled

* Shift local storage keys hard code to lib/constants

---------

Co-authored-by: Copple <10214025+kiwicopple@users.noreply.github.com>
Co-authored-by: Greg Richardson <greg.nmr@gmail.com>
Co-authored-by: Terry Sutton <saltcod@gmail.com>
Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-01-10 17:39:03 +11:00

72 lines
2.4 KiB
TypeScript

import { noop } from 'lodash'
import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { EMPTY_OBJ } from 'lib/void'
type FeaturePreviewContextType = {
flags: { [key: string]: boolean }
onUpdateFlag: (key: string, value: boolean) => void
}
const FeaturePreviewContext = createContext<FeaturePreviewContextType>({
flags: EMPTY_OBJ,
onUpdateFlag: noop,
})
export const useFeaturePreviewContext = () => useContext(FeaturePreviewContext)
export const FeaturePreviewContextProvider = ({ children }: PropsWithChildren<{}>) => {
const [flags, setFlags] = useState({
[LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT]: false,
[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]: false,
[LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT]: false,
[LOCAL_STORAGE_KEYS.UI_PREVIEW_CLS]: false,
})
useEffect(() => {
if (typeof window !== 'undefined') {
setFlags({
[LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT) === 'true',
[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL) === 'true',
[LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT) === 'true',
[LOCAL_STORAGE_KEYS.UI_PREVIEW_CLS]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_CLS) === 'true',
})
}
}, [])
const value = {
flags,
onUpdateFlag: (key: string, value: boolean) => {
if (typeof window !== 'undefined') {
window.localStorage.setItem(key, value.toString())
}
const updatedFlags = { ...flags, [key]: value }
setFlags(updatedFlags)
},
}
return <FeaturePreviewContext.Provider value={value}>{children}</FeaturePreviewContext.Provider>
}
// Helpers
export const useIsAPIDocsSidePanelEnabled = () => {
const { flags } = useFeaturePreviewContext()
return flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]
}
export const useIsRLSAIAssistantEnabled = () => {
const { flags } = useFeaturePreviewContext()
return flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT]
}
export const useIsColumnLevelPrivilegesEnabled = () => {
const { flags } = useFeaturePreviewContext()
return flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_CLS]
}