Skip to content

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

Re-implemented upgrade log promps

TzeYiingbae931ad5bd6
diff --git a/studio/components/interfaces/Settings/Logs/LogPanel.tsx b/studio/components/interfaces/Settings/Logs/LogPanel.tsx
deleted file mode 100644
index 9b142437ea0..00000000000
--- a/studio/components/interfaces/Settings/Logs/LogPanel.tsx
+++ /dev/null
@@ -1,245 +0,0 @@
-import React, { FC, useEffect, useState } from 'react'
-import {
- Button,
- Input,
- Dropdown,
- Typography,
- IconChevronDown,
- IconRefreshCw,
- IconX,
- Toggle,
- IconSearch,
- IconClock,
- Popover,
-} from '@supabase/ui'
-import { LogSearchCallback, LogTemplate, TIER_QUERY_LIMITS } from '.'
-import dayjs from 'dayjs'
-import utc from 'dayjs/plugin/utc'
-import { Tier } from 'hooks/misc/useProjectSubscription'
-interface Props {
- defaultSearchValue?: string
- defaultFromValue?: string
- templates?: any
- isLoading: boolean
- isCustomQuery: boolean
- newCount: number
- onRefresh?: () => void
- onSearch?: LogSearchCallback
- onCustomClick?: () => void
- onSelectTemplate: (template: LogTemplate) => void
- isShowingEventChart: boolean
- onToggleEventChart: () => void
- tier?: Tier
-}
-
-dayjs.extend(utc)
-
-/**
- * Logs control panel header + wrapper
- */
-const LogPanel: FC<Props> = ({
- templates = [],
- isLoading,
- isCustomQuery,
- newCount,
- onRefresh,
- onSearch = () => {},
- defaultSearchValue = '',
- defaultFromValue = '',
- onCustomClick,
- onSelectTemplate,
- isShowingEventChart,
- onToggleEventChart,
- tier,
-}) => {
- const [search, setSearch] = useState('')
- const [from, setFrom] = useState({ value: '', error: '' })
- const [defaultTimestamp, setDefaultTimestamp] = useState(dayjs().utc().toISOString())
-
- // Sync local state with provided default value
- useEffect(() => {
- if (search !== defaultSearchValue) {
- setSearch(defaultSearchValue)
- }
- }, [defaultSearchValue])
-
- useEffect(() => {
- if (from.value !== defaultFromValue) {
- setFrom({ value: defaultFromValue, error: '' })
- }
- }, [defaultFromValue])
-
- const handleFromChange = (e: React.ChangeEvent<HTMLInputElement>) => {
- const value = e.target.value
- const parsed = Date.parse(value)
- const now = new Date().getTime()
- const tierQueryLimit = TIER_QUERY_LIMITS[tier?.key || 'FREE']
- if (value !== '' && isNaN(parsed)) {
- setFrom({ value, error: 'Invalid ISO 8601 timestamp' })
- } else if (new Date(value).getTime() < now - tierQueryLimit.windowSeconds * 1000) {
- setFrom({ value, error: `Query window is limited to ${tierQueryLimit.text}` })
- } else {
- setFrom({ value, error: '' })
- }
- }
- const handleFromReset = async () => {
- setFrom({ value: '', error: '' })
- const value = dayjs().utc().toISOString()
- setDefaultTimestamp(value)
- onSearch({ query: search, from: '' })
- }
-
- const handleSearch = () => onSearch({ query: search, from: from.value })
-
- const showFromReset = from.value !== ''
- return (
- <div className="bg-panel-header-light dark:bg-panel-header-dark">
- <div className="px-2 py-1 flex items-center justify-between w-full">
- <div className="flex flex-row gap-x-4 items-center">
- <Button
- type="text"
- icon={
- <div className="relative">
- {newCount > 0 && (
- <div
- className={[
- 'absolute flex items-center justify-center -top-3 right-3',
- 'h-3 w-3 z-50',
- ].join(' ')}
- >
- <div className="absolute z-20">
- <Typography.Text style={{ fontSize: '0.7rem' }} className="opacity-80">
- {newCount}
- </Typography.Text>
- </div>
- <div className="bg-green-600 rounded-full w-full h-full animate-ping opacity-40"></div>
- <div className="absolute z-60 top-0 right-0 bg-green-900 opacity-60 rounded-full w-full h-full "></div>
- </div>
- )}
- <IconRefreshCw />
- </div>
- }
- loading={isLoading}
- onClick={onRefresh}
- >
- Refresh
- </Button>
- <Dropdown
- side="bottom"
- align="start"
- overlay={templates.map((template: LogTemplate) => (
- <Dropdown.Item key={template.label} onClick={() => onSelectTemplate(template)}>
- <Typography.Text>{template.label}</Typography.Text>
- </Dropdown.Item>
- ))}
- >
- <Button as="span" type="text" iconRight={<IconChevronDown />}>
- Templates
- </Button>
- </Dropdown>
-
- <div className="flex items-center space-x-2">
- <Typography.Text type="secondary" small>
- Search logs via query
- </Typography.Text>
- <Toggle size="tiny" checked={isCustomQuery} onChange={onCustomClick} />
- </div>
- </div>
- <div className="flex items-center gap-x-4">
- {!isCustomQuery && (
- <>
- <div className="flex flex-row">
- <Popover
- side="bottom"
- align="end"
- portalled
- overlay={
- <Input
- label="From"
- labelOptional="UTC"
- value={from.value === '' ? defaultTimestamp : from.value}
- onChange={handleFromChange}
- error={from.error}
- className="w-72 p-3"
- actions={[
- <Button
- key="set"
- size="tiny"
- title="Set"
- type="secondary"
- onClick={handleSearch}
- >
- Set
- </Button>,
- ]}
- />
- }
- >
- <Button
- as="span"
- size="tiny"
- className={showFromReset ? '!rounded-r-none' : ''}
- type={showFromReset ? 'outline' : 'text'}
- icon={<IconClock size="tiny" />}
- >
- {from.value ? 'Custom' : 'Now'}
- </Button>
- </Popover>
- {showFromReset && (
- <Button
- size="tiny"
- className={showFromReset ? '!rounded-l-none' : ''}
- icon={<IconX size="tiny" />}
- type="outline"
- title="Clear timestamp filter"
- onClick={handleFromReset}
- />
- )}
- </div>
- {!isCustomQuery && (
- <div className="flex items-center space-x-2">
- <Typography.Text type="secondary" small>
- Show event chart
- </Typography.Text>
- <Toggle size="tiny" checked={isShowingEventChart} onChange={onToggleEventChart} />
- </div>
- )}
- {/* wrap with form so that if user presses enter, the search value will submit automatically */}
- <form
- id="log-panel-search"
- onSubmit={(e) => {
- // prevent redirection
- e.preventDefault()
- handleSearch()
- }}
- >
- <Input
- placeholder="Search events"
- onChange={(e) => setSearch(e.target.value)}
- value={search}
- actions={[
- search && (
- <IconX
- key="clear-search"
- size="tiny"
- className="cursor-pointer mx-1"
- title="Clear search"
- onClick={() => setSearch('')}
- />
- ),
-
- <Button key="go" size="tiny" title="Go" type="secondary" onClick={handleSearch}>
- <IconSearch size={16} />
- </Button>,
- ]}
- />
- </form>
- </>
- )}
- </div>
- </div>
- </div>
- )
-}
-
-export default LogPanel
diff --git a/studio/components/interfaces/Settings/Logs/Logs.UpgradePrompt.tsx b/studio/components/interfaces/Settings/Logs/Logs.UpgradePrompt.tsx
new file mode 100644
index 00000000000..8b7e6175dde
--- /dev/null
+++ b/studio/components/interfaces/Settings/Logs/Logs.UpgradePrompt.tsx
@@ -0,0 +1,46 @@
+import { Button } from '@supabase/ui'
+import dayjs from 'dayjs'
+import { useProjectSubscription } from 'hooks'
+import Link from 'next/link'
+import { TIER_QUERY_LIMITS } from '.'
+interface Props {
+ projectRef: string
+ from: string
+}
+
+const UpgradePrompt: React.FC<Props> = ({ projectRef, from }) => {
+ const sub = useProjectSubscription(projectRef)
+ const tier = sub?.subscription?.tier
+ const tierSupabaseProdId = tier?.supabase_prod_id.toLowerCase()
+ const queryLimitKey = Object.keys(TIER_QUERY_LIMITS).find((key) =>
+ tierSupabaseProdId?.includes(key.toLowerCase())
+ )
+
+ if (tier && !queryLimitKey) {
+ console.error('Tier is unknown, defaulting to Free')
+ }
+ const queryLimit = TIER_QUERY_LIMITS[(queryLimitKey || 'FREE') as keyof typeof TIER_QUERY_LIMITS]
+
+ const fromValue = from ? dayjs(from) : dayjs()
+ const fromMax = dayjs().startOf('day').subtract(queryLimit.value, queryLimit.unit)
+ const isExceedingLimit = fromValue.isBefore(fromMax)
+
+ return (
+ <div
+ className={`flex flex-row gap-3 items-center text-xs px-2 py-1 transition-all ${
+ isExceedingLimit
+ ? 'text-yellow-1100 rounded border border-yellow-700 bg-yellow-200 font-semibold'
+ : ''
+ }`}
+ >
+ <span>{`${queryLimit.text} retention`}</span>
+ {!tierSupabaseProdId?.includes('payg') && (
+ <Link href={`/project/${projectRef}/settings/billing`}>
+ <Button size="tiny">Upgrade</Button>
+ </Link>
+ )}
+ </div>
+ )
+}
+
+export default UpgradePrompt
diff --git a/studio/components/interfaces/Settings/Logs/Logs.constants.ts b/studio/components/interfaces/Settings/Logs/Logs.constants.ts
index f8b15fe60c4..861589f49fe 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.constants.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.constants.ts
@@ -464,7 +464,7 @@ export const getDefaultHelper = (helpers: DatetimeHelper[]) =>
helpers.find((helper) => helper.default) || helpers[0]
export const TIER_QUERY_LIMITS = {
- FREE: { text: '1 day', windowSeconds: 3600 * 24 },
- PRO: { text: '7 days', windowSeconds: 3600 * 24 * 7 },
- PAYG: { text: '3 months', windowSeconds: 3600 * 24 * 30 * 3 },
+ FREE: { text: '1 day', value: 1, unit: 'day' },
+ PRO: { text: '7 day', value: 7, unit: 'day' },
+ PAYG: { text: '3 month', value: 3, unit: 'month' },
}
diff --git a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
index bad676da212..e92cbc0e914 100644
--- a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
@@ -19,6 +19,7 @@ import PreviewFilterPanel from 'components/interfaces/Settings/Logs/PreviewFilte
import { LOGS_TABLES } from './Logs.constants'
import ShimmerLine from 'components/ui/ShimmerLine'
import LoadingOpacity from 'components/ui/LoadingOpacity'
+import UpgradePrompt from './Logs.UpgradePrompt'
/**
* Acts as a container component for the entire log display
@@ -163,10 +164,11 @@ export const LogsPreviewer: React.FC<Props> = ({
/>
</LoadingOpacity>
{!error && (
- <div className="p-2">
+ <div className="p-2 flex flex-row justify-between">
<Button onClick={loadOlder} icon={<IconRewind />} type="default">
Load older
</Button>
+ <UpgradePrompt projectRef={projectRef} from={params.iso_timestamp_start || ''}/>
</div>
)}
{error && (
diff --git a/studio/hooks/queries/useProjectSubscription.ts b/studio/hooks/queries/useProjectSubscription.ts
index d853856ed65..639b40fa3c9 100644
--- a/studio/hooks/queries/useProjectSubscription.ts
+++ b/studio/hooks/queries/useProjectSubscription.ts
@@ -8,13 +8,13 @@ import { StripeSubscription } from 'components/interfaces/Billing'
*
* @param ref project reference
*/
-export function useProjectSubscription(ref?: string) {
+export function useProjectSubscription(ref: string) {
const url = `${API_URL}/projects/${ref}/subscription`
- const { data, error } = useSWR<any>(ref ? url : null, get)
+ const { data, error } = useSWR<StripeSubscription & { error?: string }>(url, get)
const anyError = data?.error || error
return {
- subscription: anyError ? undefined : (data as StripeSubscription),
+ subscription: anyError ? null : (data as StripeSubscription),
error: anyError,
isLoading: !anyError && !data,
isError: !!anyError,
diff --git a/studio/pages/project/[ref]/logs-explorer/index.tsx b/studio/pages/project/[ref]/logs-explorer/index.tsx
index cb8978d5f24..49834c2479c 100644
--- a/studio/pages/project/[ref]/logs-explorer/index.tsx
+++ b/studio/pages/project/[ref]/logs-explorer/index.tsx
@@ -23,6 +23,7 @@ import LoadingOpacity from 'components/ui/LoadingOpacity'
import { UserContent } from 'types'
import toast from 'react-hot-toast'
import dayjs from 'dayjs'
+import UpgradePrompt from 'components/interfaces/Settings/Logs/Logs.UpgradePrompt'
export const LogsExplorerPage: NextPage = () => {
const router = useRouter()
@@ -144,12 +145,15 @@ export const LogsExplorerPage: NextPage = () => {
/>
</div>
</div>
- <div className="flex flex-col flex-grow relative pb-8">
+ <div className="flex flex-col flex-grow relative">
<LoadingOpacity active={isLoading}>
<div className="flex flex-grow h-full">
<LogTable data={logData} error={error} />
</div>
</LoadingOpacity>
+ <div className="flex flex-row justify-end mt-2">
+ <UpgradePrompt projectRef={ref as string} from={params.iso_timestamp_start || ''} />
+ </div>
</div>
</div>
<Modal
diff --git a/studio/tests/pages/projects/LogsPreviewer.test.js b/studio/tests/pages/projects/LogsPreviewer.test.js
index 9a41d6d7978..bb885579fa6 100644
--- a/studio/tests/pages/projects/LogsPreviewer.test.js
+++ b/studio/tests/pages/projects/LogsPreviewer.test.js
@@ -56,6 +56,17 @@ LogsPreviewer.mockImplementation((props) => {
)
})
+jest.mock('hooks/queries/useProjectSubscription')
+import useProjectSubscription from 'hooks/queries/useProjectSubscription'
+useProjectSubscription = jest.fn()
+useProjectSubscription.mockImplementation((ref) => ({
+ subscription: {
+ tier: {
+ supabase_prod_id: 'tier_free',
+ },
+ },
+}))
+
import { render, fireEvent, waitFor, screen, act } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { getToggleByText } from '../../helpers'
diff --git a/studio/tests/pages/projects/logs-query.test.js b/studio/tests/pages/projects/logs-query.test.js
index e0b8faa91da..71e076234fe 100644
--- a/studio/tests/pages/projects/logs-query.test.js
+++ b/studio/tests/pages/projects/logs-query.test.js
@@ -46,6 +46,17 @@ useStore.mockImplementation(() => ({
content: jest.fn(),
}))
+jest.mock('hooks/queries/useProjectSubscription')
+import useProjectSubscription from 'hooks/queries/useProjectSubscription'
+useProjectSubscription = jest.fn()
+useProjectSubscription.mockImplementation((ref) => ({
+ subscription: {
+ tier: {
+ supabase_prod_id: 'tier_free',
+ },
+ },
+}))
+
import { SWRConfig } from 'swr'
import { LogsExplorerPage as Page } from 'pages/project/[ref]/logs-explorer/index'
const LogsExplorerPage = (props) => (