Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: add basic saving functionality

Jonathan Summers-Muiree84e0e2af21
diff --git a/studio/components/interfaces/Settings/Logs/Logs.SavedQueriesItem.tsx b/studio/components/interfaces/Settings/Logs/Logs.SavedQueriesItem.tsx
new file mode 100644
index 00000000000..2c11be5349d
--- /dev/null
+++ b/studio/components/interfaces/Settings/Logs/Logs.SavedQueriesItem.tsx
@@ -0,0 +1,46 @@
+import dayjs from 'dayjs'
+import { FC } from 'react'
+import { observer } from 'mobx-react-lite'
+import { Button, IconClipboard, IconGlobe, IconPlay } from '@supabase/ui'
+import Table from 'components/to-be-cleaned/Table'
+
+import { useRouter } from 'next/router'
+import { useStore } from 'hooks'
+// import _functionDropdown from './_functionDropdown'
+
+interface Props {
+ item: any
+}
+
+const SavedQueriesItem: FC<Props> = ({ item }: Props) => {
+ console.log('item in component', item)
+ return (
+ <Table.tr key={item.id}>
+ <Table.td className="whitespace-nowrap">
+ <div className="flex items-center gap-2">
+ <span className="text-sm text-scale-1200">{item.name}</span>
+ </div>
+ </Table.td>
+ <Table.td className="">
+ <span className="text-scale-1100">{item.description}</span>
+ </Table.td>
+ <Table.td className="">
+ <span className="text-scale-1100">
+ {dayjs(item.inserted_at).format('DD MMM, YYYY HH:mm')}
+ </span>
+ </Table.td>
+ <Table.td className="">
+ <span className="text-scale-1100">
+ {dayjs(item.updated_at).format('DD MMM, YYYY HH:mm')}
+ </span>
+ </Table.td>
+ <Table.td className=" text-right">
+ <Button type="alternative" iconRight={<IconPlay size={10} />}>
+ Run
+ </Button>
+ </Table.td>
+ </Table.tr>
+ )
+}
+
+export default observer(SavedQueriesItem)
diff --git a/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx b/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
index 7c4ad9c1f42..dcfe5a32ea9 100644
--- a/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
@@ -65,7 +65,7 @@ bg-panel-header-light dark:bg-panel-header-dark
Clear query
</Button>
{onSave && (
- <Button type="default" disabled>
+ <Button type="default" onClick={() => onSave()} disabled={!hasEditorValue}>
Save query
</Button>
)}
diff --git a/studio/components/layouts/LogsExplorerLayout/LogsExplorerLayout.tsx b/studio/components/layouts/LogsExplorerLayout/LogsExplorerLayout.tsx
index 068e340b8d2..9c3e96e4bb2 100644
--- a/studio/components/layouts/LogsExplorerLayout/LogsExplorerLayout.tsx
+++ b/studio/components/layouts/LogsExplorerLayout/LogsExplorerLayout.tsx
@@ -1,11 +1,31 @@
-import React from 'react'
+import React, { useEffect } from 'react'
import BaseLayout from 'components/layouts'
import { observer } from 'mobx-react-lite'
-import { withAuth } from 'hooks'
-import { Badge, IconList } from '@supabase/ui'
+import { useStore, withAuth } from 'hooks'
+import { Badge, IconList, Loading } from '@supabase/ui'
import LogsNavigation from 'components/interfaces/Settings/Logs/LogsNavigation'
-const PageLayout = ({ subtitle, children }: { subtitle?: React.ReactNode; children?: React.ReactNode }) => {
+const PageLayout = ({
+ subtitle,
+ children,
+}: {
+ subtitle?: React.ReactNode
+ children?: React.ReactNode
+}) => {
+ const { content, ui } = useStore()
+
+ useEffect(() => {
+ content.load()
+ }, [ui.selectedProject])
+
+ if (!content.isInitialized) {
+ return (
+ <BaseLayout>
+ <Loading active={true}>loading</Loading>
+ </BaseLayout>
+ )
+ }
+
return (
<BaseLayout>
<div className="h-full flex flex-col flex-grow">
diff --git a/studio/pages/project/[ref]/logs-explorer/index.tsx b/studio/pages/project/[ref]/logs-explorer/index.tsx
index bc6a563ece8..038a27c4684 100644
--- a/studio/pages/project/[ref]/logs-explorer/index.tsx
+++ b/studio/pages/project/[ref]/logs-explorer/index.tsx
@@ -2,8 +2,8 @@ import React, { useEffect, useState } from 'react'
import { NextPage } from 'next'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { Typography, IconAlertCircle, Card, Input, Alert } from '@supabase/ui'
-import { withAuth } from 'hooks'
+import { Typography, IconAlertCircle, Card, Input, Alert, Modal, Form, Button } from '@supabase/ui'
+import { useStore, withAuth } from 'hooks'
import CodeEditor from 'components/ui/CodeEditor'
import {
LogsQueryPanel,
@@ -17,14 +17,20 @@ import useLogsQuery from 'hooks/analytics/useLogsQuery'
import { LogsExplorerLayout } from 'components/layouts'
import ShimmerLine from 'components/ui/ShimmerLine'
import LoadingOpacity from 'components/ui/LoadingOpacity'
+import { LogSqlSnippets, UserContent } from 'types'
+import toast from 'react-hot-toast'
export const LogsExplorerPage: NextPage = () => {
const router = useRouter()
const { ref, q } = router.query
const [editorId, setEditorId] = useState<string>(uuidv4())
const [editorValue, setEditorValue] = useState<string>('')
+ const [saveModalOpen, setSaveModalOpen] = useState<boolean>(false)
+
+ const { content } = useStore()
const [{ logData, error, isLoading }, { changeQuery, runQuery }] = useLogsQuery(ref as string)
+
useEffect(() => {
// on mount, set initial values
if (q !== undefined && q !== '') {
@@ -64,6 +70,10 @@ export const LogsExplorerPage: NextPage = () => {
setEditorId(uuidv4())
}
+ function handleOnSave() {
+ setSaveModalOpen(!saveModalOpen)
+ }
+
return (
<LogsExplorerLayout>
<div className="h-full flex flex-col flex-grow gap-4">
@@ -75,6 +85,7 @@ export const LogsExplorerPage: NextPage = () => {
hasEditorValue={Boolean(editorValue)}
templates={TEMPLATES.filter((template) => template.mode === 'custom')}
onSelectTemplate={onSelectTemplate}
+ onSave={handleOnSave}
/>
<div className="min-h-[7rem] h-48">
@@ -96,6 +107,82 @@ export const LogsExplorerPage: NextPage = () => {
</LoadingOpacity>
</div>
</div>
+ <Modal
+ size="medium"
+ onCancel={() => setSaveModalOpen(!saveModalOpen)}
+ header="Save log query"
+ visible={saveModalOpen}
+ hideFooter
+ >
+ <Form
+ initialValues={{
+ name: '',
+ desdcription: '',
+ }}
+ onSubmit={async (values: any, { setSubmitting }: any) => {
+ setSubmitting(true)
+
+ const payload: UserContent = {
+ name: values.name,
+ description: values.name,
+ type: 'log_sql',
+ content: {
+ content_id: editorId,
+ sql: editorValue,
+ schema_version: '1',
+ favorite: false,
+ },
+ visibility: 'user',
+ }
+
+ try {
+ const { data: query, error } = await content.create(payload)
+ if (error) throw error
+ console.log(query)
+ setSubmitting(false)
+ setSaveModalOpen(false)
+ toast.success(`Saved "${values.name}" log query`)
+ router.push(`/project/${ref}/logs-explorer/saved`)
+ } catch (error: any) {
+ console.error(error)
+ setSubmitting(false)
+ setSaveModalOpen(false)
+ toast.error(error.message)
+ }
+ }}
+ >
+ {({ isSubmitting }: any) => (
+ <>
+ <div className="py-4">
+ <Modal.Content>
+ <div className="space-y-6">
+ <Input layout="horizontal" label="Name" id="name" />
+ <Input.TextArea
+ layout="horizontal"
+ labelOptional="Optional"
+ label="Description"
+ id="description"
+ rows={2}
+ />
+ </div>
+ </Modal.Content>
+ </div>
+ <div className="bg-scale-300 py-3 border-t">
+ <Modal.Content>
+ <div className="flex gap-2 items-center justify-end">
+ <Button size="tiny" type="default">
+ Cancel
+ </Button>
+ <Button size="tiny" loading={isSubmitting}>
+ Save
+ </Button>
+ </div>
+ </Modal.Content>
+ </div>
+ </>
+ )}
+ </Form>
+ </Modal>
</LogsExplorerLayout>
)
}
diff --git a/studio/pages/project/[ref]/logs-explorer/saved.tsx b/studio/pages/project/[ref]/logs-explorer/saved.tsx
new file mode 100644
index 00000000000..530969e0ada
--- /dev/null
+++ b/studio/pages/project/[ref]/logs-explorer/saved.tsx
@@ -0,0 +1,67 @@
+import React, { useEffect } from 'react'
+import { NextPage } from 'next'
+import { useRouter } from 'next/router'
+import { observer } from 'mobx-react-lite'
+import {
+ IconCode,
+ Badge,
+ IconDatabase,
+ Icon,
+ IconCloud,
+ Loading,
+ IconSearch,
+ Input,
+} from '@supabase/ui'
+import { useStore, withAuth } from 'hooks'
+import { LogsTableName } from 'components/interfaces/Settings/Logs'
+import LogsExplorerLayout from 'components/layouts/LogsExplorerLayout/LogsExplorerLayout'
+import CardButton from 'components/ui/CardButton'
+import { toJS } from 'mobx'
+
+import Table from 'components/to-be-cleaned/Table'
+import LogsSavedQueriesItem from 'components/interfaces/Settings/Logs/Logs.SavedQueriesItem'
+
+export const LogsExplorerPage: NextPage = () => {
+ const { content, ui } = useStore()
+
+ // if (content.isLoading) return <LogsExplorerLayout />
+
+ useEffect(() => {
+ content.load()
+ }, [ui.selectedProject])
+
+ if (content.isLoaded)
+ return (
+ <LogsExplorerLayout>
+ <Loading active={true}>loading</Loading>
+ </LogsExplorerLayout>
+ )
+
+ const saved = content.list()
+
+ console.log('saved in parent', saved)
+
+ return (
+ <LogsExplorerLayout>
+ <div className="flex flex-col gap-3">
+ <Table
+ head={
+ <>
+ <Table.th>Name</Table.th>
+ <Table.th>Description</Table.th>
+ <Table.th className="hidden 2xl:table-cell">Created</Table.th>
+ <Table.th className="hidden 2xl:table-cell">Last updated</Table.th>
+ <Table.th className="hidden 2xl:table-cell"></Table.th>
+ </>
+ }
+ body={
+ saved.length > 0 &&
+ saved[0].map((item: any) => <LogsSavedQueriesItem key={item.id} item={item} />)
+ }
+ />
+ </div>
+ </LogsExplorerLayout>
+ )
+}
+
+export default withAuth(observer(LogsExplorerPage))
diff --git a/studio/stores/content/ProjectContentStore.ts b/studio/stores/content/ProjectContentStore.ts
index 4dd3e2c00ea..fec3a950d00 100644
--- a/studio/stores/content/ProjectContentStore.ts
+++ b/studio/stores/content/ProjectContentStore.ts
@@ -12,9 +12,11 @@ import { API_URL } from 'lib/constants'
export interface IProjectContentStore {
isLoading: boolean
isInitialized: boolean
+ isLoaded: boolean
error: any
load: () => void
+ create: (x: UserContent) => { data: UserContent; error: { error: { message: string } } }
list: (filter?: any) => any[]
reports: (filter?: any) => any[]
sqlSnippets: (filter?: any) => any[]
@@ -51,6 +53,10 @@ export default class ProjectContentStore implements IProjectContentStore {
return this.state !== this.STATES.INITIAL
}
+ get isLoaded() {
+ return this.state !== this.STATES.LOADED
+ }
+
async fetchData() {
const headers = {
'Content-Type': 'application/json',
@@ -86,6 +92,7 @@ export default class ProjectContentStore implements IProjectContentStore {
list(filter?: any) {
const arr = Object.values(this.data)
+ console.log('arr', arr)
if (!!filter) {
return arr.filter(filter).sort((a, b) => a.name.localeCompare(b.name))
} else {
@@ -110,6 +117,7 @@ export default class ProjectContentStore implements IProjectContentStore {
}
sqlSnippets(filter?: any) {
+ console.log('i ran')
let arr = Object.values(this.data)
if (!!filter) {
const arr_filtered = arr
diff --git a/studio/types/userContent.ts b/studio/types/userContent.ts
index 35c79fac7d3..1219ea809b3 100644
--- a/studio/types/userContent.ts
+++ b/studio/types/userContent.ts
@@ -2,9 +2,9 @@ export interface UserContent {
id?: string
name: string
description?: string
- type: 'sql' | 'report'
+ type: 'sql' | 'report' | 'log_sql'
visibility: 'user' | 'project' | 'org' | 'public'
- content: Dashboards.Content | SqlSnippets.Content
+ content: Dashboards.Content | SqlSnippets.Content | LogSqlSnippets.Content
owner_id?: number // user id
last_updated_by?: number // user id
inserted_at?: string // '2021-08-26T08:24:52.040695+00:00'
@@ -120,3 +120,25 @@ export namespace SqlSnippets {
favorite: boolean
}
}
+
+export namespace LogSqlSnippets {
+ /**
+ * To be stored in the database: public.user_content.content
+ * In this case there is only one thing to store, but it's good to
+ * nest it in an object for future expansion.
+ */
+ export interface Content {
+ // unique id of the sql snippet, possibly to used so snippets can support versioning
+ content_id: string
+
+ // A full SQL query - this will be hashed on the /content endpoint
+ sql: string
+
+ // we can add some versioning to this schema in case we need to change the format.
+ schema_version: string
+
+ // show sql snippet as a favorite.
+ // this could be problematic if sql snippets have visibility that is != 'user'
+ favorite: boolean
+ }
+}