github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/snapshot.tsx (about)

     1  // Functions for interacting with snapshot UI elements.
     2  
     3  import React, { PropsWithChildren, useContext, useMemo } from "react"
     4  import { Flag, useFeatures } from "./feature"
     5  import { usePathBuilder } from "./PathBuilder"
     6  
     7  export type SnapshotAction = {
     8    enabled: boolean
     9    openModal: (dialogAnchor?: HTMLElement | null) => void
    10    currentSnapshotTime?: {
    11      tiltUpTime?: string
    12      createdAt?: string
    13    }
    14  }
    15  
    16  export type SnapshotProviderProps = Pick<
    17    SnapshotAction,
    18    "openModal" | "currentSnapshotTime"
    19  >
    20  
    21  const snapshotActionContext = React.createContext<SnapshotAction>({
    22    enabled: true,
    23    openModal: (dialogAnchor?: HTMLElement | null) => {},
    24  })
    25  
    26  export function useSnapshotAction(): SnapshotAction {
    27    return useContext(snapshotActionContext)
    28  }
    29  
    30  export function SnapshotActionProvider(
    31    props: PropsWithChildren<SnapshotProviderProps>
    32  ) {
    33    let openModal = props.openModal
    34    let features = useFeatures()
    35    let pathBuilder = usePathBuilder()
    36    let showSnapshot =
    37      features.isEnabled(Flag.Snapshots) && !pathBuilder.isSnapshot()
    38  
    39    let snapshotAction = useMemo(() => {
    40      return {
    41        enabled: showSnapshot,
    42        openModal: openModal,
    43        currentSnapshotTime: props.currentSnapshotTime,
    44      }
    45    }, [showSnapshot, openModal])
    46  
    47    return (
    48      <snapshotActionContext.Provider value={snapshotAction}>
    49        {props.children}
    50      </snapshotActionContext.Provider>
    51    )
    52  }
    53  
    54  export let SnapshotActionTestProvider = snapshotActionContext.Provider