@@ -9,7 +9,8 @@ import { Button } from "@/components/ui/button"
99import { ScrollArea } from "@/components/ui/scroll-area"
1010import { FolderPlus , Download , Upload } from "lucide-react"
1111import { useCollectionStore } from "@/store/collections"
12- import { Tab } from "@/types"
12+ import { Collection , SavedRequest , Tab } from "@/types"
13+ import { useEnvironmentStore } from "@/store/environments"
1314import { getRequestNameFromUrl } from "@/utils/url"
1415import {
1516 DropdownMenu ,
@@ -49,6 +50,8 @@ export const CollectionsPanel = forwardRef<HTMLDivElement, CollectionsPanelProps
4950 importFromPostman,
5051 } = useCollectionStore ( )
5152
53+ const { environments, activeEnvironmentId, setActiveEnvironment, setVariable } =
54+ useEnvironmentStore ( )
5255 const [ expandedCollections , setExpandedCollections ] = useState < Set < string > > ( new Set ( ) )
5356 const [ openapiUrlModalOpen , setOpenapiUrlModalOpen ] = useState ( false )
5457 const [ openapiRawModalOpen , setOpenapiRawModalOpen ] = useState ( false )
@@ -91,15 +94,30 @@ export const CollectionsPanel = forwardRef<HTMLDivElement, CollectionsPanelProps
9194 onOpenChange ( false )
9295 }
9396
94- const handleSelectSavedRequest = ( request : Parameters < typeof savedRequestToTab > [ 0 ] ) => {
95- handleSelectRequest ( savedRequestToTab ( request ) )
97+ /**
98+ * Switch to the collection's environment before opening anything from it.
99+ * A collection written against `{{baseUrl}}` is meaningless without the
100+ * environment that defines it, and silently sending a dev request at prod
101+ * (or the reverse) is exactly the mistake worth designing out.
102+ */
103+ const activateCollectionEnvironment = ( collection ?: Collection ) => {
104+ if ( ! collection ?. environmentId ) return
105+ if ( collection . environmentId === activeEnvironmentId ) return
106+ if ( ! environments . some ( ( env ) => env . id === collection . environmentId ) ) return
107+ setActiveEnvironment ( collection . environmentId )
108+ }
109+
110+ const handleSelectSavedRequest = ( request : SavedRequest , collection ?: Collection ) => {
111+ activateCollectionEnvironment ( collection )
112+ handleSelectRequest ( savedRequestToTab ( request , collection ) )
96113 }
97114
98115 const handleRestoreAllRequests = ( collectionId : string ) => {
99116 const targetCollection = collections . find ( ( collection ) => collection . id === collectionId )
100117 if ( ! targetCollection ) return
118+ activateCollectionEnvironment ( targetCollection )
101119 targetCollection . requests . forEach ( ( request ) => {
102- onRequestSelect ( savedRequestToTab ( request ) )
120+ onRequestSelect ( savedRequestToTab ( request , targetCollection ) )
103121 } )
104122 onOpenChange ( false )
105123 }
@@ -192,20 +210,39 @@ export const CollectionsPanel = forwardRef<HTMLDivElement, CollectionsPanelProps
192210 * host has no reason to send Access-Control-Allow-Origin for a desktop
193211 * app's origin.
194212 */
195- const handleOpenapiImport = ( apiDoc : unknown , baseUrl : string ) => {
213+ const handleOpenapiImport = (
214+ apiDoc : unknown ,
215+ baseUrl : string ,
216+ baseUrlVariable ?: string
217+ ) => {
196218 try {
197- const importedCollections = importFromOpenapi ( apiDoc , baseUrl ) ;
219+ const importedCollections = importFromOpenapi ( apiDoc , baseUrl , { baseUrlVariable } ) ;
198220 const requestCount = importedCollections . reduce ( ( sum , c ) => sum + c . requests . length , 0 ) ;
199221
200222 if ( requestCount === 0 ) {
201223 toast . error ( "No operations found in that document — is it an OpenAPI spec?" ) ;
202224 return ;
203225 }
204226
227+ // Seed the variable so the collection works immediately, rather than
228+ // importing 19 requests that all point at an undefined {{baseUrl}}.
229+ let variableNote = "" ;
230+ if ( baseUrlVariable ) {
231+ if ( activeEnvironmentId ) {
232+ setVariable ( baseUrlVariable , baseUrl ) ;
233+ const envName = environments . find ( ( env ) => env . id === activeEnvironmentId ) ?. name
234+ variableNote = ` — {{${ baseUrlVariable } }} set${ envName ? ` in ${ envName } ` : "" } ` ;
235+ } else {
236+ variableNote = ` — set {{${ baseUrlVariable } }} in an environment to use it` ;
237+ }
238+ }
239+
205240 importCollections ( importedCollections ) ;
206241 setOpenapiUrlModalOpen ( false ) ;
207242 setOpenapiRawModalOpen ( false ) ;
208- toast . success ( `Imported ${ requestCount } request${ requestCount === 1 ? "" : "s" } from OpenAPI` ) ;
243+ toast . success (
244+ `Imported ${ requestCount } request${ requestCount === 1 ? "" : "s" } from OpenAPI${ variableNote } `
245+ ) ;
209246 } catch ( error ) {
210247 if ( shouldLogImportErrors ) {
211248 console . error ( "Error importing OpenAPI:" , error ) ;
0 commit comments