import { AxiosError } from 'axios' ; import { action , makeObservable , observable , runInAction } from 'mobx' ; import { persist } from 'mobx-persist' ; import { Subject } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { v4 as uuidv4 } from 'uuid' ; import SUPPORTED_PRESETS from '../components/bulk-sell-column-presets-panel/supportedPresets' ; import { IApiAnnouncement } from '../interfaces/api/api-announcement.interface' ; import { IBulkSellColumnPreset } from '../interfaces/bulk-sell-column-preset.interface' ; import { IPricedItem } from '../interfaces/priced-item.interface' ; import { ISelectOption } from '../interfaces/select-option.interface' ; import { IStashTab } from '../interfaces/stash.interface' ; import { IStatusMessage } from '../interfaces/status-message.interface' ; import { ItemTableSelectionType } from '../types/item-table-selection.type' ; import { TimespanType } from '../types/timespan.type' ; import { constructCookie } from '../utils/cookie.utils' ; import { ICookie } from './../interfaces/cookie.interface' ; import { authService } from './../services/auth.service' ; import { Notification } from './domains/notification' ; import { RootStore } from './rootStore' ; export type GroupDialogType = 'create' | 'join' | undefined ; const xbox = { id : 'xbox' , value : 'xbox' , label : 'Xbox' , } ; const pc = { id : 'pc' , value : 'pc' , label : 'PC' , } ; const sony = { id : 'sony' , value : 'sony' , label : 'PlayStation' , } ; const platforms = [ pc , xbox , sony ] ; export class UiStateStore { @ observable @ persist userId : string = uuidv4 ( ) ; @ observable sessIdCookie : ICookie | undefined = undefined ; @ persist @ observable sidenavOpen : boolean = false ; @ persist @ observable toolbarTourOpen : boolean = true ; @ observable validated : boolean = false ; @ observable isValidating : boolean = false ; @ observable isSubmitting : boolean = false ; @ observable itemTablePageIndex : number = 0 ; @ observable notificationListAnchor : null | HTMLElement = null ; @ observable accountMenuAnchor : null | HTMLElement = null ; @ observable itemTableMenuAnchor : null | HTMLElement = null ; @ observable notificationList : Notification [ ] = [ ] ; @ observable initiated : boolean = false ; @ observable itemTableFilterText : string = '' ; @ observable bulkSellItemTableFilterText : string = '' ; @ observable priceTableFilterText : string = '' ; @ observable isInitiating : boolean = false ; @ observable groupDialogOpen : boolean = false ; @ observable groupDialogType : 'create' | 'join' | undefined = undefined ; @ observable groupOverviewOpen : boolean = false ; @ observable groupExists : boolean | undefined = undefined ; @ observable groupError : AxiosError | Error | undefined = undefined ; @ observable confirmClearSnapshotsDialogOpen : boolean = false ; @ observable confirmRemoveProfileDialogOpen : boolean = false ; @ observable isSnapshotting : boolean = false ; @ observable savingProfile : boolean = false ; @ observable removingProfile : boolean = false ; @ observable joiningGroup : boolean = false ; @ observable creatingGroup : boolean = false ; @ observable leavingGroup : boolean = false ; @ observable shouldShowWhatsNewModal : boolean = false ; @ observable showWhatsNewModal : boolean = false ; @ observable clearingSnapshots : boolean = false ; @ observable profilesLoaded : boolean = false ; @ observable settingsTabIndex : number = 0 ; @ observable announcementDialogOpen : boolean = false ; @ observable removeSnapshotsDialogOpen : boolean = false ; @ observable filteredStashTabs : IStashTab [ ] | undefined = undefined ; @ persist @ observable showItemTableFilter : boolean = false ; @ observable changingProfile : boolean = false ; @ persist @ observable netWorthChartExpanded : boolean = false ; @ persist @ observable tabChartExpanded : boolean = false ; @ persist @ observable netWorthItemsExpanded : boolean = true ; @ observable timeSinceLastSnapshotLabel : string | undefined = undefined ; @ observable timeSincePricesFetchedLabel : string | undefined = undefined ; @ observable statusMessage : IStatusMessage | undefined = undefined ; @ observable loginError : string | undefined = undefined ; @ persist @ observable chartTimeSpan : TimespanType = 'All time' ; @ observable itemTableSelection : ItemTableSelectionType = 'latest' ; @ observable customPriceDialogOpen : boolean = false ; @ observable selectedPricedItem : IPricedItem | undefined = undefined ; @ observable selectedPriceTableLeagueId : string | undefined = undefined ; @ observable announcementMessage : IApiAnnouncement | undefined = undefined ; @ persist ( 'list' ) @ observable platformList : ISelectOption [ ] = platforms ; @ persist ( 'list' ) @ observable itemTableColumnPresets : IBulkSellColumnPreset [ ] = SUPPORTED_PRESETS ; @ persist ( 'object' ) @ observable selectedPlatform : ISelectOption = pc ; @ observable bulkSellView : boolean = false ; @ persist ( 'object' ) @ observable bulkSellActivePreset : IBulkSellColumnPreset = SUPPORTED_PRESETS [ 0 ] ; @ persist @ observable bulkSellAskingPrice : number = 0 ; @ persist @ observable bulkSellAskingPricePercentage : number = 100 ; @ persist @ observable bulkSellGeneratedMessage : string = '' ; @ persist @ observable bulkSellGeneratingImage : boolean = false ; @ observable cancelSnapshot : Subject < boolean > = new Subject ( ) ; constructor ( private rootStore ) { makeObservable ( this ) ; } @ action setCancelSnapshot ( cancel ) { this . cancelSnapshot . next ( cancel ) ; if ( cancel ) { this . resetStatusMessage ( ) ; if ( this . rootStore . settingStore . autoSnapshotting ) { this . rootStore . accountStore . getSelectedAccount . dequeueSnapshot ( ) ; this . rootStore . accountStore . getSelectedAccount . queueSnapshot ( ) ; } this . setIsSnapshotting ( false ) ; this . cancelSnapshot . next ( ! cancel ) ; } } @ action resetStatusMessage ( ) { this . statusMessage = undefined ; } @ action . bound setSettingsTabIndex ( index ) { this . settingsTabIndex = index ; } @ action . bound setItemtableColumnPresets ( presets ) { this . itemTableColumnPresets = presets ; } @ action . bound setSelectedPlatform ( id ) { const option = this . platformList . find ( ( p ) => p . id === id ) ; if ( option ) { this . selectedPlatform = option ; } } @ action setSelectedPriceTableLeagueId ( id ) { this . selectedPriceTableLeagueId = id ; } @ action setLoginError ( error ) { this . loginError = error ; } @ action setFilteredStashTabs ( stashTabs ) { this . filteredStashTabs = stashTabs ; } @ action setChartTimeSpan ( timespan ) { this . chartTimeSpan = timespan ; } @ action setItemTableSelection ( selection ) { this . itemTableSelection = selection ; if ( selection === 'comparison' ) { this . setShowItemTableFilter ( false ) ; } } @ action setStatusMessage ( message , translateParam ? , currentCount ? , totalCount ? ) { const statusMessage = { message : message , translateParam : translateParam , currentCount : currentCount , totalCount : totalCount , } ; this . statusMessage = { ... statusMessage } ; } @ action setNetWorthItemsExpanded ( expanded ) { this . netWorthItemsExpanded = expanded ; } @ action setNetWorthChartExpanded ( expanded ) { this . netWorthChartExpanded = expanded ; } @ action setShowWhatsNewModal ( show ) { this . showWhatsNewModal = show ; } @ action setShouldShowWhatsNewModal ( show ) { this . shouldShowWhatsNewModal = show ; } @ action setTabChartExpanded ( expanded ) { this . tabChartExpanded = expanded ; } @ action incrementStatusMessageCount ( ) { if ( this . statusMessage ?. currentCount && this . statusMessage ?. totalCount && this . statusMessage ?. totalCount > this . statusMessage ?. currentCount ) { this . statusMessage . currentCount ++ ; } } @ action setTimeSinceLastSnapshotLabel ( label ) { this . timeSinceLastSnapshotLabel = label ; } @ action setTimeSincePricesFetchedLabel ( label ) { this . timeSincePricesFetchedLabel = label ; } @ action setShowItemTableFilter ( show ) { if ( ! show ) { this . setFilteredStashTabs ( undefined ) ; } this . showItemTableFilter = show ; } @ action setChangingProfile ( changing ) { this . changingProfile = changing ; } @ action setToolbarTourOpen ( open ) { this . toolbarTourOpen = open ; } @ action setAnnouncementDialogOpen ( open , announcement ? ) { this . announcementMessage = announcement ; this . announcementDialogOpen = open ; } @ action setRemoveSnapshotsDialogOpen ( open ) { this . removeSnapshotsDialogOpen = open ; } @ action setSavingProfile ( saving ) { this . savingProfile = saving ; } @ action setProfilesLoaded ( loaded ) { this . profilesLoaded = loaded ; } @ action setValidating ( validating ) { this . isValidating = validating ; } @ action setRemovingProfile ( removing ) { this . removingProfile = removing ; } @ action setJoiningGroup ( joining ) { this . joiningGroup = joining ; } @ action setCreatingGroup ( creating ) { this . creatingGroup = creating ; } @ action setLeavingGroup ( leaving ) { this . leavingGroup = leaving ; } @ action setClearingSnapshots ( clearing ) { this . clearingSnapshots = clearing ; } @ action setConfirmClearSnapshotsDialogOpen ( open ) { this . confirmClearSnapshotsDialogOpen = open ; } @ action setConfirmRemoveProfileDialogOpen ( open ) { this . confirmRemoveProfileDialogOpen = open ; } @ action setGroupExists ( exists ) { this . groupExists = exists ; } @ action setNotificationList ( list ) { this . notificationList = list ; } @ action setNotificationListAnchor ( el ) { this . notificationListAnchor = el ; } @ action setAccountMenuAnchor ( el ) { this . accountMenuAnchor = el ; } @ action setItemTableMenuAnchor ( el ) { this . itemTableMenuAnchor = el ; } @ action setGroupDialogOpen ( open , type ? ) { if ( open ) { this . groupDialogType = type ; } this . groupDialogOpen = open ; } @ action setSelectedPricedItem ( item ? ) { this . selectedPricedItem = item ; } @ action setCustomPriceDialogOpen ( open , row ? ) { this . customPriceDialogOpen = open ; this . setSelectedPricedItem ( row ) ; } @ action setSessIdCookie ( sessionId ) { const cookie = constructCookie ( sessionId ) ; return authService . setAuthCookie ( cookie ) . pipe ( map ( ( ) => { return runInAction ( ( ) => { this . sessIdCookie = cookie ; } ) ; } ) ) ; } @ action removeSessIdCookie ( ) { return authService . removeAuthCookie ( ) . pipe ( map ( ( ) => { return runInAction ( ( ) => { this . sessIdCookie = undefined ; } ) ; } ) ) ; } @ action setIsSnapshotting ( snapshotting = true ) { this . isSnapshotting = snapshotting ; } @ action getSessIdCookie ( ) { return authService . getAuthCookie ( ) ; } @ action changeItemTablePage ( index ) { this . itemTablePageIndex = index ; } @ action toggleSidenav ( open ? ) { this . groupOverviewOpen = false ; this . sidenavOpen = open !== undefined ? open : ! this . sidenavOpen ; } @ action toggleGroupOverview ( open ? ) { this . sidenavOpen = false ; this . groupOverviewOpen = open !== undefined ? open : ! this . groupOverviewOpen ; } @ action setValidated ( validated ) { this . validated = validated ; } @ action setItemTableFilterText ( text ) { this . itemTableFilterText = text ; } @ action setBulkSellItemTableFilterText ( text ) { this . bulkSellItemTableFilterText = text ; } @ action setPriceTableFilterText ( text ) { this . priceTableFilterText = text ; } @ action setInitiated ( init ) { this . initiated = init ; } @ action setSubmitting ( submitting ) { this . isSubmitting = submitting ; } @ action setIsInitiating ( initiating ) { this . isInitiating = initiating ; } @ action setGroupError ( error ) { this . groupError = error ; } @ action setBulkSellView ( active ) { this . bulkSellView = active ; } @ action . bound setBulkSellActivePreset ( preset ) { this . bulkSellActivePreset = preset ; } @ action setBulkSellAskingPrice ( price ) { this . bulkSellAskingPrice = price ; } @ action setBulkSellAskingPricePercentage ( percentage ) { this . bulkSellAskingPricePercentage = percentage ; } @ action setBulkSellGeneratedMessage ( msg ) { this . bulkSellGeneratedMessage = msg ; } @ action setBulkSellGeneratingImage ( status ) { this . bulkSellGeneratingImage = status ; } }