File size: 3,721 Bytes
13555f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {useIntl} from 'react-intl'

import {UserSettings} from './userSettings'

enum ErrorId {
    TeamUndefined = 'team-undefined',
    NotLoggedIn = 'not-logged-in',
    InvalidReadOnlyBoard = 'invalid-read-only-board',
    BoardNotFound = 'board-not-found',
}

type ErrorDef = {
    title: string

    button1Enabled: boolean
    button1Text: string
    button1Redirect: string | ((params: URLSearchParams) => string)
    button1Fill: boolean
    button1ClearHistory: boolean

    button2Enabled: boolean
    button2Text: string
    button2Redirect: string | ((params: URLSearchParams) => string)
    button2Fill: boolean
    button2ClearHistory: boolean
}

function errorDefFromId(id: ErrorId | null): ErrorDef {
    const errDef: ErrorDef = {
        title: '',
        button1Enabled: false,
        button1Text: '',
        button1Redirect: '',
        button1Fill: false,
        button1ClearHistory: false,
        button2Enabled: false,
        button2Text: '',
        button2Redirect: '',
        button2Fill: false,
        button2ClearHistory: false,
    }

    const intl = useIntl()

    switch (id) {
    case ErrorId.TeamUndefined: {
        errDef.title = intl.formatMessage({id: 'error.team-undefined', defaultMessage: 'Not a valid team.'})
        errDef.button1Enabled = true
        errDef.button1Text = intl.formatMessage({id: 'error.back-to-home', defaultMessage: 'Back to Home'})
        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 = '/'
        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
        errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Log in'})
        errDef.button1Redirect = '/login'
        errDef.button1Redirect = (params: URLSearchParams): string => {
            const r = params.get('r')
            if (r) {
                return `/login?r=${r}`
            }
            return '/login'
        }
        errDef.button1Fill = true
        break
    }
    case ErrorId.InvalidReadOnlyBoard: {
        errDef.title = intl.formatMessage({id: 'error.invalid-read-only-board', defaultMessage: 'You don\'t have access to this board. Log in to access Boards.'})
        errDef.button1Enabled = true
        errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Log in'})
        errDef.button1Redirect = (): string => {
            return window.location.origin
        }
        errDef.button1Fill = true
        break
    }
    default: {
        errDef.title = intl.formatMessage({id: 'error.unknown', defaultMessage: 'An error occurred.'})
        errDef.button1Enabled = true
        errDef.button1Text = intl.formatMessage({id: 'error.back-to-home', defaultMessage: 'Back to Home'})
        errDef.button1Redirect = '/'
        errDef.button1Fill = true
        errDef.button1ClearHistory = true
        break
    }
    }
    return errDef
}

export {ErrorId, ErrorDef, errorDefFromId}