@@ -3,6 +3,10 @@ import type { PrismaClient, PrismaReplicaClient } from "~/db.server";
33import { $replica , prisma } from "~/db.server" ;
44import type { Project } from "~/models/project.server" ;
55import type { User } from "~/models/user.server" ;
6+ import {
7+ selectEnvironmentVariablesFilter ,
8+ type EnvironmentVariablesFilter ,
9+ } from "~/v3/environmentVariables/environmentFilter" ;
610import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server" ;
711import type { EnvironmentVariableUpdater } from "~/v3/environmentVariables/repository" ;
812import type { SyncEnvVarsMapping , EnvSlug } from "~/v3/vercel/vercelProjectIntegrationSchema" ;
@@ -30,12 +34,21 @@ export class EnvironmentVariablesPresenter {
3034 page = 1 ,
3135 pageSize = DEFAULT_ENV_VARS_PAGE_SIZE ,
3236 search,
37+ environmentFilter,
38+ branches,
39+ currentEnvironmentSlug,
3340 } : {
3441 userId : User [ "id" ] ;
3542 projectId : Project [ "id" ] ;
3643 page ?: number ;
3744 pageSize ?: number ;
3845 search ?: string ;
46+ /** The requested filter. Without one, the current environment's type is used. */
47+ environmentFilter ?: EnvironmentVariablesFilter ;
48+ /** Preview branch names, or `ALL_BRANCHES`. Only used by the Preview filter. */
49+ branches ?: string [ ] ;
50+ /** The environment the page is opened in. Without one, every environment is listed. */
51+ currentEnvironmentSlug ?: string ;
3952 } ) {
4053 const project = await this . #replicaClient. project . findFirst ( {
4154 select : {
@@ -65,9 +78,31 @@ export class EnvironmentVariablesPresenter {
6578 { skipProjectAccessCheck : true }
6679 ) ;
6780
68- // Only load values for the environments we display. Projects can accumulate
69- // values in archived branch environments, which would otherwise all be loaded here.
70- const environmentIds = sortedEnvironments . map ( ( env ) => env . id ) ;
81+ // Narrow to the filtered environments before counting and paging, so the totals and the
82+ // pages follow the filter. Archived branch environments are never in this list, so the
83+ // values projects accumulate in them are never loaded.
84+ const currentEnvironment = currentEnvironmentSlug
85+ ? sortedEnvironments . find ( ( env ) => env . slug === currentEnvironmentSlug )
86+ : undefined ;
87+ // An archived branch isn't in the list, but its URL still opens the page: default to its type.
88+ const missingCurrentEnvironment =
89+ currentEnvironmentSlug && ! currentEnvironment
90+ ? await this . #replicaClient. runtimeEnvironment . findFirst ( {
91+ select : { type : true } ,
92+ where : { projectId : project . id , slug : currentEnvironmentSlug } ,
93+ } )
94+ : null ;
95+ const { filter, environmentIds, branchEnvironmentIds } = selectEnvironmentVariablesFilter ( {
96+ environments : sortedEnvironments ,
97+ requested : environmentFilter ,
98+ requestedBranches : branches ,
99+ currentEnvironmentId : currentEnvironment ?. id ,
100+ currentEnvironmentType : missingCurrentEnvironment ?. type ,
101+ } ) ;
102+ const filteredEnvironmentIds = new Set ( environmentIds ) ;
103+ const listedEnvironments = sortedEnvironments . filter ( ( env ) =>
104+ filteredEnvironmentIds . has ( env . id )
105+ ) ;
71106
72107 const variableWhere : Prisma . EnvironmentVariableWhereInput = {
73108 projectId : project . id ,
@@ -78,6 +113,21 @@ export class EnvironmentVariablesPresenter {
78113 const totalCount = await this . #replicaClient. environmentVariable . count ( {
79114 where : variableWhere ,
80115 } ) ;
116+ // A project with no variables in any listed environment gets the first-run empty state
117+ // whatever the filter is. Only an unsearched view with nothing in it has to look further.
118+ const projectHasNoVariables =
119+ totalCount === 0 &&
120+ ! search &&
121+ ( filter === "all" ||
122+ ( await this . #replicaClient. environmentVariable . findFirst ( {
123+ select : { id : true } ,
124+ where : {
125+ projectId : project . id ,
126+ values : {
127+ some : { environmentId : { in : boundedIn ( sortedEnvironments . map ( ( env ) => env . id ) ) } } ,
128+ } ,
129+ } ,
130+ } ) ) === null ) ;
81131 const totalPages = Math . max ( 1 , Math . ceil ( totalCount / pageSize ) ) ;
82132 const currentPage = Math . min ( Math . max ( 1 , page ) , totalPages ) ;
83133
@@ -151,7 +201,7 @@ export class EnvironmentVariablesPresenter {
151201
152202 const nonSecretItems : Array < { environmentId : string ; key : string } > = [ ] ;
153203 for ( const environmentVariable of environmentVariables ) {
154- for ( const env of sortedEnvironments ) {
204+ for ( const env of listedEnvironments ) {
155205 const valueRecord = environmentVariable . values . find ( ( v ) => v . environmentId === env . id ) ;
156206 if ( valueRecord && ! valueRecord . isSecret ) {
157207 nonSecretItems . push ( { environmentId : env . id , key : environmentVariable . key } ) ;
@@ -183,7 +233,7 @@ export class EnvironmentVariablesPresenter {
183233 this . #prismaClient
184234 ) ,
185235 environmentVariables : environmentVariables . flatMap ( ( environmentVariable ) => {
186- return sortedEnvironments . flatMap ( ( env ) => {
236+ return listedEnvironments . flatMap ( ( env ) => {
187237 const valueRecord = environmentVariable . values . find ( ( v ) => v . environmentId === env . id ) ;
188238 const isSecret = valueRecord ?. isSecret ?? false ;
189239
@@ -230,7 +280,13 @@ export class EnvironmentVariablesPresenter {
230280 ] ;
231281 } ) ;
232282 } ) ,
283+ // Every environment, not only the listed ones: the page needs them all for its filter
284+ // options, its permission lists and the create dialog.
233285 environments : sortedEnvironments ,
286+ filter,
287+ filterEnvironmentIds : environmentIds ,
288+ filterBranchEnvironmentIds : branchEnvironmentIds ?? [ ] ,
289+ projectHasNoVariables,
234290 hasStaging,
235291 pagination : {
236292 currentPage,
0 commit comments