2022-03-02 15:46:12 -05:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import { useIntl } from 'react-intl'
2022-03-22 15:24:34 +01:00
import { UserSettings } from './userSettings'
2022-03-02 15:46:12 -05:00
enum ErrorId {
2022-03-22 15:24:34 +01:00
TeamUndefined = 'team-undefined' ,
2022-03-02 15:46:12 -05:00
NotLoggedIn = 'not-logged-in' ,
2022-04-25 16:11:16 -06:00
InvalidReadOnlyBoard = 'invalid-read-only-board' ,
2022-03-22 15:24:34 +01:00
BoardNotFound = 'board-not-found' ,
2022-03-02 15:46:12 -05:00
}
type ErrorDef = {
title : string
button1Enabled : boolean
button1Text : string
2022-03-22 15:24:34 +01:00
button1Redirect : string | ( ( params : URLSearchParams ) = > string )
2022-03-02 15:46:12 -05:00
button1Fill : boolean
2022-03-11 16:47:04 -05:00
button1ClearHistory : boolean
2022-03-02 15:46:12 -05:00
button2Enabled : boolean
button2Text : string
2022-03-22 15:24:34 +01:00
button2Redirect : string | ( ( params : URLSearchParams ) = > string )
2022-03-02 15:46:12 -05:00
button2Fill : boolean
2022-03-11 16:47:04 -05:00
button2ClearHistory : boolean
2022-03-02 15:46:12 -05:00
}
function errorDefFromId ( id : ErrorId | null ) : ErrorDef {
2022-03-22 15:24:34 +01:00
const errDef : ErrorDef = {
2022-03-02 15:46:12 -05:00
title : '' ,
button1Enabled : false ,
button1Text : '' ,
button1Redirect : '' ,
button1Fill : false ,
2022-03-11 16:47:04 -05:00
button1ClearHistory : false ,
2022-03-02 15:46:12 -05:00
button2Enabled : false ,
button2Text : '' ,
button2Redirect : '' ,
button2Fill : false ,
2022-03-11 16:47:04 -05:00
button2ClearHistory : false ,
2022-03-02 15:46:12 -05:00
}
const intl = useIntl ( )
switch ( id ) {
2022-03-22 15:24:34 +01:00
case ErrorId . TeamUndefined : {
errDef . title = intl . formatMessage ( { id : 'error.team-undefined' , defaultMessage : 'Not a valid team.' } )
2022-03-02 15:46:12 -05:00
errDef . button1Enabled = true
2022-03-11 16:47:04 -05:00
errDef . button1Text = intl . formatMessage ( { id : 'error.back-to-home' , defaultMessage : 'Back to Home' } )
2022-03-22 15:24:34 +01:00
errDef . button1Redirect = ( ) : string = > {
UserSettings . setLastTeamID ( null )
return window . location . origin
}
errDef . button1Fill = true
break
}
case ErrorId . BoardNotFound : {
errDef . title = intl . formatMessage ( { id : 'error.board-not-found' , defaultMessage : 'Board not found.' } )
errDef . button1Enabled = true
errDef . button1Text = intl . formatMessage ( { id : 'error.back-to-team' , defaultMessage : 'Back to team' } )
errDef . button1Redirect = '/'
2022-03-02 15:46:12 -05:00
errDef . button1Fill = true
break
}
case ErrorId . NotLoggedIn : {
errDef . title = intl . formatMessage ( { id : 'error.not-logged-in' , defaultMessage : 'Your session may have expired or you\'re not logged in. Log in again to access Boards.' } )
errDef . button1Enabled = true
2022-06-21 12:22:47 -05:00
errDef . button1Text = intl . formatMessage ( { id : 'error.go-login' , defaultMessage : 'Log in' } )
2022-03-02 15:46:12 -05:00
errDef . button1Redirect = '/login'
2022-03-22 15:24:34 +01:00
errDef . button1Redirect = ( params : URLSearchParams ) : string = > {
const r = params . get ( 'r' )
if ( r ) {
return ` /login?r= ${ r } `
}
return '/login'
}
2022-03-02 15:46:12 -05:00
errDef . button1Fill = true
break
}
2022-04-25 16:11:16 -06:00
case ErrorId . InvalidReadOnlyBoard : {
2022-09-14 15:11:50 +02:00
errDef . title = intl . formatMessage ( { id : 'error.invalid-read-only-board' , defaultMessage : 'You don\'t have access to this board. Log in to access Boards.' } )
2022-04-25 16:11:16 -06:00
errDef . button1Enabled = true
2022-07-07 16:46:53 +02:00
errDef . button1Text = intl . formatMessage ( { id : 'error.go-login' , defaultMessage : 'Log in' } )
2022-04-25 16:11:16 -06:00
errDef . button1Redirect = ( ) : string = > {
return window . location . origin
}
errDef . button1Fill = true
break
}
2022-03-02 15:46:12 -05:00
default : {
errDef . title = intl . formatMessage ( { id : 'error.unknown' , defaultMessage : 'An error occurred.' } )
errDef . button1Enabled = true
2022-03-22 15:24:34 +01:00
errDef . button1Text = intl . formatMessage ( { id : 'error.back-to-home' , defaultMessage : 'Back to Home' } )
2022-03-11 16:47:04 -05:00
errDef . button1Redirect = '/'
2022-03-02 15:46:12 -05:00
errDef . button1Fill = true
2022-03-11 16:47:04 -05:00
errDef . button1ClearHistory = true
2022-03-02 15:46:12 -05:00
break
}
}
return errDef
}
export { ErrorId , ErrorDef , errorDefFromId }