The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Chore/custom content hook (#38073)
Joshen Limbc1c050b1d5d
diff --git a/apps/studio/components/interfaces/Organization/Documents/CustomDocument.tsx b/apps/studio/components/interfaces/Organization/Documents/CustomDocument.tsxnew file mode 100644index 00000000000..76c9b4ca7de--- /dev/null+++ b/apps/studio/components/interfaces/Organization/Documents/CustomDocument.tsx@@ -0,0 +1,33 @@+import {+ ScaffoldContainer,+ ScaffoldSection,+ ScaffoldSectionContent,+ ScaffoldSectionDetail,+} from 'components/layouts/Scaffold'+import { CustomContentTypes } from 'hooks/custom-content/CustomContent.types'+import { ExternalLink } from 'lucide-react'+import { Button } from 'ui'++interface CustomDocumentProps {+ doc: CustomContentTypes['organizationLegalDocuments'][number]+}++export const CustomDocument = ({ doc }: CustomDocumentProps) => {+ return (+ <ScaffoldContainer id={doc.id}>+ <ScaffoldSection>+ <ScaffoldSectionDetail className="sticky top-12 flex flex-col gap-y-8">+ <p className="text-base m-0">{doc.name}</p>+ <p className="text-sm text-foreground-light m-0">{doc.description}</p>+ </ScaffoldSectionDetail>+ <ScaffoldSectionContent className="flex items-center justify-center h-full">+ <Button asChild type="default" iconRight={<ExternalLink />}>+ <a download href={doc.action.url} target="_blank" rel="noreferrer noopener">+ {doc.action.text}+ </a>+ </Button>+ </ScaffoldSectionContent>+ </ScaffoldSection>+ </ScaffoldContainer>+ )+}diff --git a/apps/studio/components/interfaces/Organization/Documents/Documents.tsx b/apps/studio/components/interfaces/Organization/Documents/Documents.tsxindex 84ecf7b32fe..1b35df888dc 100644--- a/apps/studio/components/interfaces/Organization/Documents/Documents.tsx+++ b/apps/studio/components/interfaces/Organization/Documents/Documents.tsx@@ -1,6 +1,9 @@import Link from 'next/link'import { ScaffoldContainer, ScaffoldDivider, ScaffoldSection } from 'components/layouts/Scaffold'+import { useCustomContent } from 'hooks/custom-content/useCustomContent'+import { Fragment } from 'react'+import { CustomDocument } from './CustomDocument'import { DPA } from './DPA'import { HIPAA } from './HIPAA'import { SecurityQuestionnaire } from './SecurityQuestionnaire'@@ -8,6 +11,19 @@ import { SOC2 } from './SOC2'import { TIA } from './TIA'const Documents = () => {+ const { organizationLegalDocuments } = useCustomContent(['organization:legal_documents'])++ if (Array.isArray(organizationLegalDocuments)) {+ return organizationLegalDocuments.map((doc, idx) => {+ return (+ <Fragment key={doc.id}>+ <CustomDocument doc={doc} />+ {idx !== organizationLegalDocuments.length - 1 && <ScaffoldDivider />}+ </Fragment>+ )+ })+ }+return (<><ScaffoldContainer id="dpa">diff --git a/apps/studio/hooks/custom-content/CustomContent.types.ts b/apps/studio/hooks/custom-content/CustomContent.types.tsnew file mode 100644index 00000000000..0c0e6be07e3--- /dev/null+++ b/apps/studio/hooks/custom-content/CustomContent.types.ts@@ -0,0 +1,8 @@+export type CustomContentTypes = {+ organizationLegalDocuments: {+ id: string+ name: string+ description: string+ action: { text: string; url: string }+ }[]+}diff --git a/apps/studio/hooks/custom-content/custom-content.json b/apps/studio/hooks/custom-content/custom-content.jsonnew file mode 100644index 00000000000..5b7291b0d30--- /dev/null+++ b/apps/studio/hooks/custom-content/custom-content.json@@ -0,0 +1,5 @@+{+ "$schema": "./custom-content.schema.json",++ "organization:legal_documents": null+}diff --git a/apps/studio/hooks/custom-content/custom-content.sample.json b/apps/studio/hooks/custom-content/custom-content.sample.jsonnew file mode 100644index 00000000000..1a6af8b9418--- /dev/null+++ b/apps/studio/hooks/custom-content/custom-content.sample.json@@ -0,0 +1,24 @@+{+ "$schema": "./custom-content.schema.json",++ "organization:legal_documents": [+ {+ "id": "doc1",+ "name": "Document 1",+ "description": "This is a description of Document 1",+ "action": {+ "text": "Download document",+ "url": "https://supabase.com"+ }+ },+ {+ "id": "doc2",+ "name": "Document 2",+ "description": "This is a description of Document 2",+ "action": {+ "text": "Download document",+ "url": "https://supabase.com"+ }+ }+ ]+}diff --git a/apps/studio/hooks/custom-content/custom-content.schema.json b/apps/studio/hooks/custom-content/custom-content.schema.jsonnew file mode 100644index 00000000000..8c645146a82--- /dev/null+++ b/apps/studio/hooks/custom-content/custom-content.schema.json@@ -0,0 +1,31 @@+{+ "$schema": "http://json-schema.org/draft-07/schema#",+ "type": "object",+ "properties": {+ "$schema": {+ "type": "string"+ },++ "organization:legal_documents": {+ "type": ["array", "null"],+ "description": "Renders a provided set of documents under the organization legal documents page",+ "items": {+ "type": "object",+ "properties": {+ "id": { "type": "string" },+ "name": { "type": "string" },+ "description": { "type": "string" },+ "action": {+ "type": "object",+ "properties": {+ "text": { "type": "string" },+ "url": { "type": "string" }+ }+ }+ }+ }+ }+ },+ "required": ["organization:legal_documents"],+ "additionalProperties": false+}diff --git a/apps/studio/hooks/custom-content/useCustomContent.test.ts b/apps/studio/hooks/custom-content/useCustomContent.test.tsnew file mode 100644index 00000000000..e6ca936f758--- /dev/null+++ b/apps/studio/hooks/custom-content/useCustomContent.test.ts@@ -0,0 +1,36 @@+import { renderHook, cleanup } from '@testing-library/react'+import { beforeEach, describe, expect, it, vi } from 'vitest'++beforeEach(() => {+ vi.clearAllMocks()+ vi.resetModules()+ cleanup()+})++describe('useCustomContent', () => {+ it('should return null if content is not found in the custom-content.json file', async () => {+ vi.doMock('./custom-content.json', () => ({+ default: {+ 'organization:legal_documents': null,+ },+ }))++ const { useCustomContent } = await import('./useCustomContent')+ const { result } = renderHook(() => useCustomContent(['organization:legal_documents']))+ expect(result.current.organizationLegalDocuments).toEqual(null)+ })++ it('should return the content for the key passed in if it exists in the custom-content.json file', async () => {+ vi.doMock('./custom-content.json', () => ({+ default: {+ 'organization:legal_documents': {+ someValue: 'foo',+ },+ },+ }))++ const { useCustomContent } = await import('./useCustomContent')+ const { result } = renderHook(() => useCustomContent(['organization:legal_documents']))+ expect(result.current.organizationLegalDocuments).toEqual({ someValue: 'foo' })+ })+})diff --git a/apps/studio/hooks/custom-content/useCustomContent.ts b/apps/studio/hooks/custom-content/useCustomContent.tsnew file mode 100644index 00000000000..0d1b2342642--- /dev/null+++ b/apps/studio/hooks/custom-content/useCustomContent.ts@@ -0,0 +1,44 @@+import customContentRaw from './custom-content.json'+import { CustomContentTypes } from './CustomContent.types'++// [Joshen] See if we can de-dupe any of the logic here with enabled-features+// For now just getting something working going first+// Also not sure if CustomContentTypes is the right way to go here with trying to dynamically type++const customContentStaticObj = customContentRaw as Omit<typeof customContentRaw, '$schema'>+type CustomContent = keyof typeof customContentStaticObj++type SnakeToCamelCase<S extends string> = S extends `${infer First}_${infer Rest}`+ ? `${First}${SnakeToCamelCase<Capitalize<Rest>>}`+ : S++type CustomContentToCamelCase<S extends CustomContent> = S extends `${infer P}:${infer R}`+ ? `${SnakeToCamelCase<P>}${Capitalize<SnakeToCamelCase<R>>}`+ : SnakeToCamelCase<S>++function contentToCamelCase(feature: CustomContent) {+ return feature+ .replace(/:/g, '_')+ .split('_')+ .map((word, index) => (index === 0 ? word : word[0].toUpperCase() + word.slice(1)))+ .join('') as CustomContentToCamelCase<typeof feature>+}++const useCustomContent = <T extends CustomContent[]>(+ contents: T+): {+ [key in CustomContentToCamelCase<T[number]>]:+ | (typeof customContentStaticObj)[CustomContent]+ | CustomContentTypes[CustomContentToCamelCase<T[number]>]+} => {+ // [Joshen] Running into some TS errors without the `as` here - must be overlooking something super simple+ return Object.fromEntries(+ contents.map((content) => [contentToCamelCase(content), customContentStaticObj[content]])+ ) as {+ [key in CustomContentToCamelCase<T[number]>]:+ | (typeof customContentStaticObj)[CustomContent]+ | CustomContentTypes[CustomContentToCamelCase<T[number]>]+ }+}++export { useCustomContent }