Skip to content

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

Use timestamp info for api keys last used (#41180)

Joshen Lim0b517360cbc6
diff --git a/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx b/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx
index 2763ef35217..cce0d94e439 100644
--- a/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx
+++ b/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx
@@ -1,7 +1,8 @@
-import { TextConfirmModal } from 'components/ui/TextConfirmModalWrapper'
-import type { APIKeysData } from 'data/api-keys/api-keys-query'
import { motion } from 'framer-motion'
import { MoreVertical } from 'lucide-react'
+
+import { TextConfirmModal } from 'components/ui/TextConfirmModalWrapper'
+import type { APIKeysData } from 'data/api-keys/api-keys-query'
import {
Button,
DropdownMenu,
@@ -10,6 +11,7 @@ import {
TableCell,
TableRow,
} from 'ui'
+import { ShimmeringLoader, TimestampInfo } from 'ui-patterns'
import { APIKeyDeleteDialog } from './APIKeyDeleteDialog'
import { ApiKeyPill } from './ApiKeyPill'
@@ -17,16 +19,18 @@ export const APIKeyRow = ({
apiKey,
lastSeen,
isDeleting,
+ isDeleteModalOpen,
+ isLoadingLastSeen,
onDelete,
setKeyToDelete,
- isDeleteModalOpen,
}: {
apiKey: Extract<APIKeysData[number], { type: 'secret' | 'publishable' }>
- lastSeen?: { timestamp: string }
+ lastSeen?: { timestamp: number; relative: string }
isDeleting: boolean
+ isDeleteModalOpen: boolean
+ isLoadingLastSeen: boolean
onDelete: () => void
setKeyToDelete: (id: string | null) => void
- isDeleteModalOpen: boolean
}) => {
const MotionTableRow = motion.create(TableRow)
@@ -59,8 +63,18 @@ export const APIKeyRow = ({
</TableCell>
<TableCell className="py-2 min-w-0 whitespace-nowrap hidden lg:table-cell">
- <div className="truncate" title={lastSeen?.timestamp || 'Never used'}>
- {lastSeen?.timestamp ?? <span className="text-foreground-lighter">Never used</span>}
+ <div className="truncate" title={lastSeen?.timestamp.toString() || 'Never used'}>
+ {isLoadingLastSeen ? (
+ <ShimmeringLoader />
+ ) : lastSeen?.timestamp ? (
+ <TimestampInfo
+ className="text-sm"
+ utcTimestamp={lastSeen?.timestamp}
+ label={lastSeen.relative}
+ />
+ ) : (
+ <span className="text-foreground-lighter">Never used</span>
+ )}
</div>
</TableCell>
diff --git a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx
index c3d55af77a0..def75dd351d 100644
--- a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx
+++ b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx
@@ -24,10 +24,10 @@ import CreateSecretAPIKeyDialog from './CreateSecretAPIKeyDialog'
import { useApiKeysVisibility } from './hooks/useApiKeysVisibility'
interface LastSeenData {
- [hash: string]: { timestamp: string }
+ [hash: string]: { timestamp: number; relative: string }
}
-function useLastSeen(projectRef: string): LastSeenData {
+function useLastSeen(projectRef: string): { data?: LastSeenData; isLoading: boolean } {
const now = useRef(new Date()).current
const query = useLogsQuery(projectRef, {
@@ -38,17 +38,23 @@ function useLastSeen(projectRef: string): LastSeenData {
return useMemo(() => {
if (query.isLoading || !query.logData) {
- return {}
+ return { data: undefined, isLoading: query.isLoading }
}
const now = dayjs()
- return (query.logData as unknown as { timestamp: number; hash: string }[]).reduce((a, i) => {
- a[i.hash] = {
- timestamp: `${dayjs.duration(now.diff(dayjs(i.timestamp))).humanize(false)} ago`,
- }
- return a
- }, {} as LastSeenData)
+ const lastSeen = (query.logData as unknown as { timestamp: number; hash: string }[]).reduce(
+ (a, i) => {
+ a[i.hash] = {
+ timestamp: i.timestamp,
+ relative: `${dayjs.duration(now.diff(dayjs(i.timestamp))).humanize(false)} ago`,
+ }
+ return a
+ },
+ {} as LastSeenData
+ )
+
+ return { data: lastSeen, isLoading: query.isLoading }
}, [query])
}
@@ -63,7 +69,7 @@ export const SecretAPIKeys = () => {
isError: isErrorApiKeys,
} = useAPIKeysQuery({ projectRef, reveal: false }, { enabled: canReadAPIKeys })
- const lastSeen = useLastSeen(projectRef ?? '')
+ const { data: lastSeen, isLoading: isLoadingLastSeen } = useLastSeen(projectRef ?? '')
const secretApiKeys = useMemo(
() =>
@@ -144,7 +150,7 @@ export const SecretAPIKeys = () => {
<TableRow className="bg-200">
<TableHead>Name</TableHead>
<TableHead>API Key</TableHead>
- <TableHead className="hidden lg:table-cell">Last Seen</TableHead>
+ <TableHead className="hidden lg:table-cell">Last Used</TableHead>
<TableHead />
</TableRow>
</TableHeader>
@@ -153,7 +159,8 @@ export const SecretAPIKeys = () => {
<APIKeyRow
key={apiKey.id}
apiKey={apiKey}
- lastSeen={lastSeen[apiKey.hash]}
+ lastSeen={lastSeen?.[apiKey.hash]}
+ isLoadingLastSeen={isLoadingLastSeen}
isDeleting={apiKeyToDelete?.id === apiKey.id && isDeletingAPIKey}
onDelete={() => onDeleteAPIKey(apiKey)}
setKeyToDelete={setAPIKeyToDelete}