|
| 1 | +import { makeAutoObservable, observable } from 'mobx'; |
| 2 | +import { Store } from 'store'; |
| 3 | +import * as LIT from 'types/generated/lit-sessions_pb'; |
| 4 | +import { MAX_DATE } from 'util/constants'; |
| 5 | + |
| 6 | +export default class AddSessionView { |
| 7 | + private _store: Store; |
| 8 | + |
| 9 | + label = ''; |
| 10 | + permissionType = 'admin'; |
| 11 | + editing = false; |
| 12 | + permissions: { [key: string]: boolean } = { |
| 13 | + openChannel: false, |
| 14 | + closeChannel: false, |
| 15 | + setFees: false, |
| 16 | + loop: false, |
| 17 | + pool: false, |
| 18 | + viewActivity: false, |
| 19 | + send: false, |
| 20 | + receive: false, |
| 21 | + }; |
| 22 | + expiration = 'never'; |
| 23 | + expirationDate = ''; |
| 24 | + showAdvanced = false; |
| 25 | + proxy = ''; |
| 26 | + |
| 27 | + constructor(store: Store) { |
| 28 | + makeAutoObservable( |
| 29 | + this, |
| 30 | + { |
| 31 | + permissions: observable.deep, |
| 32 | + }, |
| 33 | + { deep: false, autoBind: true }, |
| 34 | + ); |
| 35 | + |
| 36 | + this._store = store; |
| 37 | + } |
| 38 | + |
| 39 | + // |
| 40 | + // Private helper functions |
| 41 | + // |
| 42 | + |
| 43 | + private setAllPermissions(value: boolean) { |
| 44 | + Object.keys(this.permissions).forEach(permissionName => { |
| 45 | + this.permissions[permissionName] = value; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + // |
| 50 | + // Actions |
| 51 | + // |
| 52 | + |
| 53 | + setLabel(label: string) { |
| 54 | + this.label = label; |
| 55 | + } |
| 56 | + |
| 57 | + setExpiration(expiration: string) { |
| 58 | + this.expiration = expiration; |
| 59 | + } |
| 60 | + |
| 61 | + setExpirationDate(expirationDate: string) { |
| 62 | + this.expirationDate = expirationDate; |
| 63 | + } |
| 64 | + |
| 65 | + setProxy(proxy: string) { |
| 66 | + this.proxy = proxy; |
| 67 | + } |
| 68 | + |
| 69 | + setPermissionType(permissionType: string) { |
| 70 | + this.permissionType = permissionType; |
| 71 | + |
| 72 | + switch (permissionType) { |
| 73 | + case 'admin': |
| 74 | + this.setAllPermissions(true); |
| 75 | + break; |
| 76 | + |
| 77 | + case 'read-only': |
| 78 | + this.setAllPermissions(false); |
| 79 | + this.permissions.viewActivity = true; |
| 80 | + break; |
| 81 | + |
| 82 | + case 'liquidity': |
| 83 | + this.setAllPermissions(false); |
| 84 | + this.permissions.setFees = true; |
| 85 | + this.permissions.loop = true; |
| 86 | + this.permissions.pool = true; |
| 87 | + break; |
| 88 | + |
| 89 | + case 'payments': |
| 90 | + this.setAllPermissions(false); |
| 91 | + this.permissions.send = true; |
| 92 | + this.permissions.receive = true; |
| 93 | + break; |
| 94 | + |
| 95 | + case 'custom': |
| 96 | + // We don't need to change anything, let the user customize permissions how they want |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + togglePermission(permission: string) { |
| 102 | + this.setPermissionType('custom'); |
| 103 | + this.permissions[permission] = !this.permissions[permission]; |
| 104 | + } |
| 105 | + |
| 106 | + toggleEditing() { |
| 107 | + this.editing = !this.editing; |
| 108 | + } |
| 109 | + |
| 110 | + toggleAdvanced() { |
| 111 | + this.showAdvanced = !this.showAdvanced; |
| 112 | + } |
| 113 | + |
| 114 | + cancel() { |
| 115 | + this.label = ''; |
| 116 | + this.permissionType = 'admin'; |
| 117 | + this.editing = false; |
| 118 | + this.setAllPermissions(false); |
| 119 | + this.expiration = 'never'; |
| 120 | + this.showAdvanced = false; |
| 121 | + } |
| 122 | + |
| 123 | + // |
| 124 | + // Async Actions |
| 125 | + // |
| 126 | + |
| 127 | + async handleSubmit() { |
| 128 | + if (this.permissionType === 'custom') { |
| 129 | + this._store.settingsStore.sidebarVisible = false; |
| 130 | + this._store.router.push('/connect/custom'); |
| 131 | + } else { |
| 132 | + const sessionType = |
| 133 | + this.permissionType === 'admin' |
| 134 | + ? LIT.SessionType.TYPE_MACAROON_ADMIN |
| 135 | + : LIT.SessionType.TYPE_MACAROON_READONLY; |
| 136 | + |
| 137 | + const session = await this._store.sessionStore.addSession( |
| 138 | + this.label, |
| 139 | + sessionType, |
| 140 | + MAX_DATE, |
| 141 | + true, |
| 142 | + ); |
| 143 | + |
| 144 | + if (session) { |
| 145 | + this.cancel(); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments