go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/store/auth_state/auth_state.ts (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { Instance, SnapshotIn, SnapshotOut, types } from 'mobx-state-tree'; 16 17 import { AuthState } from '@/common/api/auth_state'; 18 19 export const AuthStateStore = types 20 .model('AuthStateStore', { 21 id: types.optional(types.identifierNumber, () => Math.random()), 22 /** 23 * `undefined` means the auth state is not yet initialized. (i.e. we don't 24 * know whether the user is signed in or not.) 25 * Once the auth state is initialized, it will remain that way. 26 */ 27 value: types.maybe(types.frozen<AuthState>()), 28 }) 29 // The following properties should be used in favor of `value` (e.g. 30 // `value.identity`) to avoid triggering unnecessary updates when auth state 31 // is refreshed. 32 .views((self) => ({ 33 get identity() { 34 return self.value?.identity; 35 }, 36 get email() { 37 return self.value?.email; 38 }, 39 get picture() { 40 return self.value?.picture; 41 }, 42 })) 43 .actions((self) => ({ 44 setValue(value: AuthState) { 45 self.value = value; 46 }, 47 })); 48 49 export type AuthStateStoreInstance = Instance<typeof AuthStateStore>; 50 export type AuthStateStoreSnapshotIn = SnapshotIn<typeof AuthStateStore>; 51 export type AuthStateStoreSnapshotOut = SnapshotOut<typeof AuthStateStore>;