1
1
import { localized , msg , str } from "@lit/localize" ;
2
2
import { type SlDialog } from "@shoelace-style/shoelace" ;
3
+ import clsx from "clsx" ;
3
4
import { html , type PropertyValues } from "lit" ;
4
5
import { customElement , property , state } from "lit/decorators.js" ;
5
6
import { createRef , ref , type Ref } from "lit/directives/ref.js" ;
@@ -10,6 +11,7 @@ import { type Entries } from "type-fest";
10
11
import z from "zod" ;
11
12
12
13
import { BtrixElement } from "@/classes/BtrixElement" ;
14
+ import { cellInputStyle } from "@/components/ui/data-grid/data-grid-cell" ;
13
15
import { type RowEditEventDetail } from "@/components/ui/data-grid/data-grid-row" ;
14
16
import {
15
17
GridColumnType ,
@@ -31,7 +33,14 @@ const PlansResponseSchema = z.object({
31
33
32
34
type PlansResponse = z . infer < typeof PlansResponseSchema > ;
33
35
34
- const LABELS = {
36
+ const LABELS : {
37
+ [ key in keyof OrgQuotas ] : {
38
+ label : string ;
39
+ type : "number" | "bytes" ;
40
+ scale ?: number ;
41
+ adjustmentOnly ?: boolean ;
42
+ } ;
43
+ } = {
35
44
maxConcurrentCrawls : {
36
45
label : msg ( "Max Concurrent Crawls" ) ,
37
46
type : "number" ,
@@ -52,17 +61,13 @@ const LABELS = {
52
61
extraExecMinutes : {
53
62
label : msg ( "Extra Execution Minutes" ) ,
54
63
type : "number" ,
64
+ adjustmentOnly : true ,
55
65
} ,
56
66
giftedExecMinutes : {
57
67
label : msg ( "Gifted Execution Minutes" ) ,
58
68
type : "number" ,
69
+ adjustmentOnly : true ,
59
70
} ,
60
- } as const satisfies {
61
- [ key in keyof OrgQuotas ] : {
62
- label : string ;
63
- type : "number" | "bytes" ;
64
- scale ?: number ;
65
- } ;
66
71
} ;
67
72
68
73
@customElement ( "btrix-org-quota-editor" )
@@ -111,7 +116,7 @@ export class OrgQuotaEditor extends BtrixElement {
111
116
const items = entries . map ( ( [ key , value ] ) => {
112
117
const labelConfig = LABELS [ key ] ;
113
118
let currentAdjustment = this . orgQuotaAdjustments [ key ] || 0 ;
114
- if ( labelConfig . type === "bytes" ) {
119
+ if ( labelConfig . scale != undefined ) {
115
120
currentAdjustment = Math . floor (
116
121
currentAdjustment / labelConfig . scale ,
117
122
) ;
@@ -135,47 +140,95 @@ export class OrgQuotaEditor extends BtrixElement {
135
140
{
136
141
label : msg ( "Value" ) ,
137
142
field : "current" ,
138
- editable : false ,
143
+ editable : ( item ) => item && ! LABELS [ item . key ] . adjustmentOnly ,
144
+ inputType : GridColumnType . Number ,
139
145
width : "1fr" ,
140
- renderCell : ( { item } ) => {
146
+ renderEditCell : ( { item } ) => {
147
+ const key = item . key ;
148
+ let value = item . current ;
149
+ const labelConfig = LABELS [ key ] ;
150
+
151
+ if ( labelConfig . scale != undefined ) {
152
+ value = Math . floor ( value / labelConfig . scale ) ;
153
+ }
154
+ return html `< sl-input
155
+ class =${ clsx ( cellInputStyle ) }
156
+ type ="number"
157
+ value="${ value } "
158
+ min="0"
159
+ step="1"
160
+ >
161
+ ${ labelConfig . type === "bytes"
162
+ ? html `< span class ="whitespace-nowrap " slot ="suffix "
163
+ > GB</ span
164
+ > `
165
+ : "" }
166
+ </ sl-input > ` ;
167
+ } ,
168
+ } ,
169
+ {
170
+ label : msg ( "Adjustment" ) ,
171
+ field : "adjustment" ,
172
+ editable : true ,
173
+ width : "2fr" ,
174
+ inputType : GridColumnType . Number ,
175
+ renderEditCell : ( { item } ) => {
141
176
const key = item . key ;
142
- const value = item . current ;
177
+ let value = this . orgQuotaAdjustments [ key ] ?? 0 ;
143
178
const labelConfig = LABELS [ key ] ;
144
179
const format = ( v : number , isInitialValue = true ) =>
145
180
this . format ( v , labelConfig . type , isInitialValue ) ;
146
181
147
- return this . orgQuotaAdjustments [ key ]
182
+ const displayValue = this . orgQuotaAdjustments [ key ]
148
183
? Math . sign ( this . orgQuotaAdjustments [ key ] ) === 1
149
184
? html `< span class ="tabular-nums ">
150
- < b > ${ format ( value + this . orgQuotaAdjustments [ key ] ) } </ b >
185
+ < b
186
+ > ${ format (
187
+ ( this . org ?. quotas [ key ] || 0 ) +
188
+ this . orgQuotaAdjustments [ key ] ,
189
+ ) } </ b
190
+ >
151
191
< span class ="inline-block ">
152
- (${ format ( value , false ) }
192
+ (${ format ( this . org ?. quotas [ key ] || 0 , false ) }
153
193
< span class ="whitespace-nowrap text-green-600 ">
154
194
+ ${ format ( this . orgQuotaAdjustments [ key ] ) } </ span
155
195
> )</ span
156
196
> </ span
157
197
> `
158
198
: html `< span class ="tabular-nums ">
159
- < b > ${ format ( value + this . orgQuotaAdjustments [ key ] ) } </ b >
199
+ < b
200
+ > ${ format (
201
+ ( this . org ?. quotas [ key ] || 0 ) +
202
+ this . orgQuotaAdjustments [ key ] ,
203
+ ) } </ b
204
+ >
160
205
< span class ="inline-block ">
161
- (${ format ( value , false ) }
206
+ (${ format ( this . org ?. quotas [ key ] || 0 , false ) }
162
207
< span class ="whitespace-nowrap text-red-600 ">
163
208
- ${ format ( - this . orgQuotaAdjustments [ key ] ) } </ span
164
209
> )</ span
165
210
> </ span
166
211
> `
167
- : format ( value ) ;
212
+ : null ;
213
+ if ( labelConfig . scale != undefined ) {
214
+ value = Math . floor ( value / labelConfig . scale ) ;
215
+ }
216
+ return html `< sl-input
217
+ class =${ clsx ( cellInputStyle ) }
218
+ type ="number"
219
+ value="${ value } "
220
+ min=${ - 1 * item . current }
221
+ step="1"
222
+ >
223
+ ${ labelConfig . type === "bytes"
224
+ ? html `< span class ="whitespace-nowrap " slot ="suffix "
225
+ > GB</ span
226
+ > `
227
+ : "" }
228
+ </ sl-input >
229
+ ${ displayValue } ` ;
168
230
} ,
169
231
} ,
170
- {
171
- label : msg ( "Adjustment" ) ,
172
- field : "adjustment" ,
173
- editable : true ,
174
- width : "1fr" ,
175
- inputType : GridColumnType . Number ,
176
- min : ( item ) => - 1 * ( item ?. current ?? 0 ) ,
177
- step : 1 ,
178
- } ,
179
232
] ;
180
233
181
234
return html `
@@ -267,13 +320,22 @@ export class OrgQuotaEditor extends BtrixElement {
267
320
const key = event . detail . rowKey as keyof OrgQuotas ;
268
321
let value = Number ( event . detail . value ) ;
269
322
const labelConfig = LABELS [ key ] ;
270
- if ( labelConfig . type === "bytes" ) {
323
+ if ( labelConfig . scale != undefined ) {
271
324
value = Math . floor ( value * labelConfig . scale ) ;
272
325
}
273
- this . orgQuotaAdjustments = {
274
- ...this . orgQuotaAdjustments ,
275
- [ key ] : value ,
276
- } ;
326
+ if ( event . detail . field === "adjustment" ) {
327
+ console . log ( "adjustment" , value ) ;
328
+ this . orgQuotaAdjustments = {
329
+ ...this . orgQuotaAdjustments ,
330
+ [ key ] : value ,
331
+ } ;
332
+ } else if ( event . detail . field === "current" ) {
333
+ console . log ( "current" , value ) ;
334
+ this . orgQuotaAdjustments = {
335
+ ...this . orgQuotaAdjustments ,
336
+ [ key ] : value - ( this . org ?. quotas [ key ] || 0 ) ,
337
+ } ;
338
+ }
277
339
} }
278
340
> </ btrix-data-grid >
279
341
` ;
0 commit comments