import { useLocation } from '@tanstack/react-router' import { LOCAL_STORAGE_KEYS, useParams } from 'common' import { CircleHelpIcon, FileIcon, MoreVerticalIcon, RotateCcwIcon } from 'lucide-react' import { AnimatePresence, motion, useReducedMotion } from 'motion/react' import { useCallback, useEffect, useRef, useState } from 'react' import { Badge, Button, cn } from 'ui' import { BannerCard } from '../BannerCard' import { BANNER_ID, useBannerStack } from '../BannerStackProvider' import { useFeaturePreviewContext, useFeaturePreviewModal, } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext' import { BroomSparklesIcon } from '@/components/interfaces/Storage/BroomSparklesIcon' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useProjectStorageConfigQuery } from '@/data/config/project-storage-config-query' import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage' export const BannerStorageVersioningManager = () => { const { ref } = useParams() const pathname = useLocation({ select: (location) => location.pathname }) const { isInitialized } = useFeaturePreviewContext() const { data } = useProjectStorageConfigQuery({ projectRef: ref }) const isObjectVersioningAvailable = !!data?.capabilities?.object_versioning const [isDismissed, setIsDismissed] = useLocalStorageQuery( LOCAL_STORAGE_KEYS.STORAGE_VERSIONING_PREVIEW_BANNER_DISMISSED(ref ?? '_'), false ) const { addBanner, dismissBanner } = useBannerStack() const handleDismiss = useCallback(() => { setIsDismissed(true) dismissBanner(BANNER_ID.STORAGE_VERSIONING) }, [dismissBanner, setIsDismissed]) const isStorageFilesPage = pathname.startsWith(`/project/${ref}/storage/files`) const canShowBanner = isStorageFilesPage && isInitialized && isObjectVersioningAvailable && !isDismissed useEffect(() => { if (!canShowBanner) { dismissBanner(BANNER_ID.STORAGE_VERSIONING) return } addBanner({ id: BANNER_ID.STORAGE_VERSIONING, isDismissed: false, priority: 1, content: , }) }, [addBanner, canShowBanner, dismissBanner, handleDismiss]) useEffect(() => () => dismissBanner(BANNER_ID.STORAGE_VERSIONING), [dismissBanner]) return null } const BannerStorageVersioning = ({ onDismiss }: { onDismiss: () => void }) => { const { flags, onUpdateFlag } = useFeaturePreviewContext() const { selectFeaturePreview } = useFeaturePreviewModal() const isPreviewEnabled = flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_STORAGE_VERSIONING] === true return (
New Public Alpha

Storage Object Versioning
and Lifecycle Policies

Retain versions of files and automatically expire them with lifecycle policies.

{isPreviewEnabled ? ( ) : ( <> } onClick={() => selectFeaturePreview(LOCAL_STORAGE_KEYS.UI_PREVIEW_STORAGE_VERSIONING) } tooltip={{ content: { side: 'bottom', text: 'More information' } }} /> )}
) } const INITIAL_VERSIONS = [ { id: 'v4', versionId: '7f3a9c21b8046d5e', size: '1.2 MB', isCurrent: true }, { id: 'v2', versionId: '2b8e1d64a9703cf2', size: '980 KB', isCurrent: false }, ] const shortDemoVersionId = (versionId: string) => `${versionId.slice(0, 6)}…${versionId.slice(-2)}` const VersionHistoryPreview = () => { const [versions, setVersions] = useState(INITIAL_VERSIONS) const [isAtRetentionLimit, setIsAtRetentionLimit] = useState(false) const [isRemovingVersion, setIsRemovingVersion] = useState(false) const nextVersionNumber = useRef(5) const shouldReduceMotion = useReducedMotion() useEffect(() => { let timeout: ReturnType | undefined let phase: 'upload' | 'expire' = 'upload' let isPageActive = document.visibilityState === 'visible' && document.hasFocus() const schedulePhase = () => { if (!isPageActive || timeout) return timeout = setTimeout( () => { timeout = undefined if (phase === 'upload') { const versionNumber = nextVersionNumber.current nextVersionNumber.current += 1 setVersions((currentVersions) => [ { id: `v${versionNumber}`, versionId: `a7c3d812e4f9b2${versionNumber.toString(16).padStart(2, '0')}`, size: '1.3 MB', isCurrent: true, }, ...currentVersions.map((version) => ({ ...version, isCurrent: false })), ]) setIsAtRetentionLimit(true) phase = 'expire' } else { setVersions((currentVersions) => currentVersions.slice(0, -1)) setIsRemovingVersion(true) setIsAtRetentionLimit(false) phase = 'upload' } schedulePhase() }, phase === 'upload' ? 1800 : 3200 ) } const syncPageActivity = () => { const isActive = document.visibilityState === 'visible' && document.hasFocus() if (isActive === isPageActive) return isPageActive = isActive clearTimeout(timeout) timeout = undefined if (isPageActive) schedulePhase() } document.addEventListener('visibilitychange', syncPageActivity) window.addEventListener('focus', syncPageActivity) window.addEventListener('blur', syncPageActivity) schedulePhase() return () => { clearTimeout(timeout) document.removeEventListener('visibilitychange', syncPageActivity) window.removeEventListener('focus', syncPageActivity) window.removeEventListener('blur', syncPageActivity) } }, []) return (
Versions {versions.length + (isRemovingVersion ? 1 : 0)}
    setIsRemovingVersion(false)}> {versions.map((version, index) => { const isCurrent = version.isCurrent const isExpiring = isAtRetentionLimit && index === versions.length - 1 return ( {shortDemoVersionId(version.versionId)} {version.size} {isCurrent && ( Current )} {!isCurrent && ( )} {!isCurrent && ( )} ) })}
) }