Adrià Vilanova MartÃnez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame^] | 1 | // Copyright 2022 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {CustomQuestion, IssueCategory, SelectMenuOption, IssueWizardPersona} from "./IssueWizardTypes"; |
| 6 | |
| 7 | |
| 8 | const CHROME_VERSION_REX = /chrome\/(\d|\.)+/i; |
| 9 | // this function is used to get the issue list belong to different persona |
| 10 | // when a user group is selected a list of related issue categories will show up |
| 11 | export function GetCategoriesByPersona (categories: IssueCategory[]): Map<IssueWizardPersona, SelectMenuOption[]> { |
| 12 | const categoriesByPersona = new Map<IssueWizardPersona, SelectMenuOption[]>(); |
| 13 | |
| 14 | categories.forEach((category) => { |
| 15 | if (category.enabled) { |
| 16 | const currentIssuePersona = category.persona; |
| 17 | const currentCategories = categoriesByPersona.get(currentIssuePersona) ?? []; |
| 18 | currentCategories.push({ |
| 19 | name: category.name, |
| 20 | description: category.description, |
| 21 | }); |
| 22 | categoriesByPersona.set(currentIssuePersona, currentCategories); |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | return categoriesByPersona; |
| 27 | } |
| 28 | |
| 29 | // this function is used to get the customer questions belong to different issue category |
| 30 | // the customer question page will render base on these data |
| 31 | export function GetQuestionsByCategory(categories: IssueCategory[]): Map<string, CustomQuestion[] | null> { |
| 32 | const questionsByCategory = new Map<string, CustomQuestion[] | null>(); |
| 33 | categories.forEach((category) => { |
| 34 | questionsByCategory.set(category.name, category.customQuestions ?? null); |
| 35 | }) |
| 36 | return questionsByCategory; |
| 37 | } |
| 38 | |
| 39 | // this function is used to convert the options list fit for render use SelectMenu |
| 40 | export function GetSelectMenuOptions(optionsList: string[]): SelectMenuOption[] { |
| 41 | const selectMenuOptionList = new Array<SelectMenuOption>(); |
| 42 | optionsList.forEach((option) => { |
| 43 | selectMenuOptionList.push({name: option}); |
| 44 | }); |
| 45 | return selectMenuOptionList; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Detects the user's operating system. |
| 50 | */ |
| 51 | export function getOs() { |
| 52 | const userAgent = window.navigator.userAgent, |
| 53 | platform = window.navigator.platform, |
| 54 | macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'], |
| 55 | windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'], |
| 56 | iosPlatforms = ['iPhone', 'iPad', 'iPod']; |
| 57 | |
| 58 | if (macosPlatforms.indexOf(platform) !== -1) { |
| 59 | return'Mac OS'; |
| 60 | } else if (iosPlatforms.indexOf(platform) !== -1) { |
| 61 | return 'iOS'; |
| 62 | } else if (windowsPlatforms.indexOf(platform) !== -1) { |
| 63 | return 'Windows'; |
| 64 | } else if (/Android/.test(userAgent)) { |
| 65 | return 'Android'; |
| 66 | } else if (/Linux/.test(platform)) { |
| 67 | return 'Linux'; |
| 68 | } else if (/\bCrOS\b/.test(userAgent)) { |
| 69 | return 'Chrome OS'; |
| 70 | } |
| 71 | |
| 72 | return 'Unknown / Other'; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | // this function is used to get the tip belong to different issue category |
| 77 | // used for render detail page |
| 78 | export function getTipByCategory(categories: IssueCategory[]): Map<string, string> { |
| 79 | const tipByCategory = new Map<string, string>(); |
| 80 | categories.forEach((category) => { |
| 81 | if (category.tip) { |
| 82 | tipByCategory.set(category.name, category.tip); |
| 83 | } |
| 84 | }) |
| 85 | return tipByCategory; |
| 86 | } |
| 87 | |
| 88 | // this function is used to get the component value for each issue category used for make issue |
| 89 | export function getCompValByCategory(categories: IssueCategory[]): Map<string, string> { |
| 90 | const compValByCategory = new Map<string, string>(); |
| 91 | categories.forEach((category) => { |
| 92 | if (category.component) { |
| 93 | compValByCategory.set(category.name, category.component); |
| 94 | } |
| 95 | }) |
| 96 | return compValByCategory; |
| 97 | } |
| 98 | |
| 99 | export function getLabelsByCategory(categories: IssueCategory[]): Map<string, Array<string>> { |
| 100 | const labelsByCategory = new Map<string, Array<string>>(); |
| 101 | categories.forEach((category) => { |
| 102 | if (category.labels) { |
| 103 | labelsByCategory.set(category.name, category.labels); |
| 104 | } |
| 105 | }) |
| 106 | return labelsByCategory; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | export function buildIssueDescription( |
| 111 | reproduceStep: string, |
| 112 | description: string, |
| 113 | comments: string, |
| 114 | os: string, |
| 115 | chromeVersion: string, |
| 116 | channel: string, |
| 117 | ): string { |
| 118 | const issueDescription = |
| 119 | "<b>Steps to reproduce the problem:</b>\n" + reproduceStep.trim() + "\n\n" |
| 120 | + "<b>Problem Description:</b>\n" + description.trim() + "\n\n" |
| 121 | + "<b>Additional Comments:</b>\n" + comments.trim() + "\n\n" |
| 122 | + "<b>Chrome version: </b>" + chromeVersion.trim() + " <b>Channel: </b>" + channel + "\n\n" |
| 123 | + "<b>OS:</b>" + os.trim(); |
| 124 | return issueDescription; |
| 125 | } |
| 126 | |
| 127 | export function buildIssueLabels(category: string, osName: string, chromeVersion: string, configLabels: Array<string> | null | undefined): Array<any> { |
| 128 | const labels = [ |
| 129 | {label:'via-wizard-'+category}, |
| 130 | {label:'Pri-2'}, |
| 131 | ]; |
| 132 | |
| 133 | const os = osName.split(' ')[0]; |
| 134 | if (os !== 'Unknown/Other') { |
| 135 | labels.push({ |
| 136 | label: 'OS-'+os |
| 137 | }) |
| 138 | } |
| 139 | const mainChromeVersion = chromeVersion.split('.').length > 0 ? chromeVersion.split('.')[0] : null; |
| 140 | if (mainChromeVersion !== null) { |
| 141 | labels.push({ |
| 142 | label:'Needs-Triage-M'+mainChromeVersion |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | if (configLabels) { |
| 147 | configLabels.forEach((v) => { |
| 148 | labels.push({label: v}); |
| 149 | }) |
| 150 | } |
| 151 | return labels; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | export function getChromeVersion() { |
| 156 | const userAgent = window.navigator.userAgent; |
| 157 | var browser= userAgent.match(CHROME_VERSION_REX) || []; |
| 158 | if (browser.length > 0) { |
| 159 | return browser[0].split('/')[1]; |
| 160 | } |
| 161 | return "<Copy from:'about:version'>"; |
| 162 | } |