Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

6.3 KB
Raw Blame
import { ComputedRef, WritableComputedRef } from "vue"
import type { HoppGQLRequest, HoppRESTRequest } from "@hoppscotch/data"
import type { Workspace } from "../workspace.service"

/**
 * A snapshot of the tab's state for one protocol, taken when the user
 * switches away from it.
 */
export type ProtocolDraft<T> = {
  request: T
  /**
   * The tab's dirty flag at snapshot time. Restoring a draft returns the tab
   * to a state it was already in, so the flag travels with it — a saved
   * request that round-trips REST → GQL → REST comes back clean instead of
   * prompting to save content identical to what's stored.
   */
  isDirty: boolean
}

/**
 * Per-tab shadow drafts of the other protocol's request, used by the
 * REST/GraphQL protocol switcher to preserve in-flight edits across switches.
 * Lives on the tab object so it automatically survives close → reopen and is
 * garbage-collected when the tab is permanently destroyed.
 */
export type ProtocolDrafts = {
  rest?: ProtocolDraft<HoppRESTRequest>
  gql?: ProtocolDraft<HoppGQLRequest>
}

/**
 * Represents a tab in HoppScotch.
 * @template Doc The type of the document associated with the tab.
 */
export type HoppTab<Doc> = {
  /** The unique identifier of the tab. */
  id: string
  /** The document associated with the tab. */
  document: Doc
  /**
   * The workspace this tab is attached to, captured at creation/restore.
   * Set automatically by `WorkspaceTabsService`; standalone tab services
   * (e.g., `GQLTabService` for the legacy `/graphql` page) leave this undefined.
   */
  workspaceHandle?: Workspace
  /**
   * Shadow drafts of the opposite-protocol request for round-trip preservation
   * across `ProtocolSwitcher` swaps. Populated by `WorkspaceTabsService` only.
   */
  protocolDrafts?: ProtocolDrafts
}

export type PersistableTabState<Doc> = {
  lastActiveTabID: string
  orderedDocs: Array<{
    tabID: string
    doc: Doc
    /**
     * Opposite-protocol shadow drafts (see `HoppTab.protocolDrafts`) — persisted
     * so a protocol switch's unsaved draft survives a page refresh.
     */
    protocolDrafts?: ProtocolDrafts
  }>
}

/**
 * Represents a service for managing tabs with documents.
 * @template Doc - The type of document associated with each tab.
 */
export interface TabService<Doc> {
  /**
   * Gets the current active tab.
   */
  currentActiveTab: ComputedRef<HoppTab<Doc>>

  /**
   * Creates a new tab with the given document and sets it as the active tab.
   * @param document - The document to associate with the new tab.
   * @returns The newly created tab.
   */
  createNewTab(document: Doc): HoppTab<Doc>

  /**
   * Gets an array of all tabs.
   * @returns An array of all tabs.
   */
  getTabs(): HoppTab<Doc>[]

  /**
   * Gets the currently active tab.
   * @returns The active tab or null if no tab is active.
   */
  getActiveTab(): HoppTab<Doc> | null

  /**
   * Sets the active tab by its ID.
   * @param tabID - The ID of the tab to set as active.
   */
  setActiveTab(tabID: string): void

  /**
   * Loads tabs and their ordering from a persisted state.
   * @param data - The persisted tab state to load.
   */
  loadTabsFromPersistedState(data: PersistableTabState<Doc>): void

  /**
   * Gets a read-only computed reference to the active tabs.
   * @returns A computed reference to the active tabs.
   */
  getActiveTabs(): Readonly<ComputedRef<HoppTab<Doc>[]>>

  /**
   * Gets a computed reference to a specific tab by its ID.
   * @param tabID - The ID of the tab to retrieve.
   * @returns A computed reference to the specified tab.
   * @throws An error if the tab with the specified ID does not exist.
   */
  getTabRef(tabID: string): WritableComputedRef<HoppTab<Doc>>

  /**
   * Updates the properties of a tab.
   * @param tabUpdate - The updated tab object.
   */
  updateTab(tabUpdate: HoppTab<Doc>): void

  /**
   * Updates the ordering of tabs by moving a tab from one index to another.
   * @param fromIndex - The current index of the tab to move.
   * @param toIndex - The target index where the tab should be moved to.
   */
  updateTabOrdering(fromIndex: number, toIndex: number): void

  /**
   * Closes the tab with the specified ID.
   * @param tabID - The ID of the tab to close.
   */
  /** @returns whether the tab was actually closed (refused for the last open tab) */
  closeTab(tabID: string): boolean

  /**
   * Closes all tabs except the one with the specified ID.
   * @param tabID - The ID of the tab to keep open.
   */
  closeOtherTabs(tabID: string): void

  /**
   * Navigates to the next tab in the tab order.
   */
  goToNextTab(): void

  /**
   * Navigates to the previous tab in the tab order.
   */
  goToPreviousTab(): void

  /**
   * NOTE: Currently inert, plumbing is done, some platform issues around shortcuts, WIP for future.
   * Navigates to a tab by its index position (1-based).
   * @param index - The 1-based index of the tab to navigate to.
   */
  goToTabByIndex(index: number): void

  /**
   * Navigates to the first tab in the tab order.
   */
  goToFirstTab(): void

  /**
   * Navigates to the last tab in the tab order.
   */
  goToLastTab(): void

  /**
   * Reopens the most recently closed tab.
   * @returns True if a tab was reopened, false if no closed tabs are available.
   */
  reopenClosedTab(): boolean

  /**
   * Navigates forward through the MRU list (to older tabs).
   * Each call moves one step forward in the MRU history.
   */
  goToMRUTab(): void

  /**
   * Navigates backward through the MRU list (to more recent tabs).
   * Each call moves one step backward in the MRU history.
   */
  goToPreviousMRUTab(): void

  /**
   * Commits the current MRU navigation selection.
   * Should be called when the modifier key is released to finalize the tab switch.
   */
  commitMRUNavigation(): void

  /**
   * Resets MRU navigation state without committing.
   */
  resetMRUNavigation(): void

  /**
   * Gets a computed reference to a persistable tab state.
   * @returns A computed reference to a persistable tab state object.
   */
  persistableTabState: ComputedRef<PersistableTabState<Doc>>

  /**
   * Gets computed references to tabs that match a specified condition.
   * @param func - A function that defines the condition for selecting tabs.
   * @returns An array of computed references to matching tabs.
   */
  getTabsRefTo(
    func: (tab: HoppTab<Doc>) => boolean
  ): WritableComputedRef<HoppTab<Doc>>[]
}