StorageCredItem.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { differenceInDays } from 'date-fns'
  3. import { MoreVertical, TrashIcon } from 'lucide-react'
  4. import {
  5. Button,
  6. DropdownMenu,
  7. DropdownMenuContent,
  8. DropdownMenuItem,
  9. DropdownMenuTrigger,
  10. TableCell,
  11. TableRow,
  12. } from 'ui'
  13. import { Input } from 'ui-patterns/DataInputs/Input'
  14. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  15. export const StorageCredItem = ({
  16. description,
  17. id,
  18. created_at,
  19. access_key,
  20. onDeleteClick,
  21. }: {
  22. description: string
  23. id: string
  24. created_at: string
  25. access_key: string
  26. onDeleteClick: (id: string) => void
  27. }) => {
  28. const { can: canRemoveAccessKey } = useAsyncCheckPermissions(
  29. PermissionAction.STORAGE_ADMIN_WRITE,
  30. '*'
  31. )
  32. function daysSince(date: string) {
  33. const now = new Date()
  34. const created = new Date(date)
  35. const diffInDays = differenceInDays(now, created)
  36. if (diffInDays === 0) {
  37. return 'Today'
  38. } else if (diffInDays === 1) {
  39. return `${diffInDays} day ago`
  40. } else {
  41. return `${diffInDays} days ago`
  42. }
  43. }
  44. return (
  45. <TableRow className="h-8 text-ellipsis group">
  46. <TableCell>
  47. <span className="text-foreground">{description}</span>
  48. </TableCell>
  49. <TableCell>
  50. <Input readOnly copy value={access_key} className="font-mono" />
  51. </TableCell>
  52. <TableCell className="text-foreground-lighter whitespace-nowrap">
  53. {daysSince(created_at)}
  54. </TableCell>
  55. <TableCell className="text-right">
  56. {canRemoveAccessKey && (
  57. <DropdownMenu>
  58. <DropdownMenuTrigger asChild>
  59. <Button
  60. icon={<MoreVertical size={14} strokeWidth={1} />}
  61. type="text"
  62. className="px-1.5 text-foreground-lighter hover:text-foreground"
  63. />
  64. </DropdownMenuTrigger>
  65. <DropdownMenuContent className="max-w-40" align="end">
  66. <DropdownMenuItem
  67. className="flex gap-1.5 "
  68. onClick={(e) => {
  69. e.preventDefault()
  70. onDeleteClick(id)
  71. }}
  72. >
  73. <TrashIcon size="14" />
  74. Revoke key
  75. </DropdownMenuItem>
  76. </DropdownMenuContent>
  77. </DropdownMenu>
  78. )}
  79. </TableCell>
  80. </TableRow>
  81. )
  82. }