Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 1 | // Copyright 2024 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 | const ratings = { |
| 6 | good: 'is good', |
| 7 | 'needs-improvement': 'needs improvement', |
| 8 | poor: 'is poor', |
| 9 | }; |
| 10 | |
| 11 | const compares = { |
| 12 | better: 'significantly better than', |
| 13 | worse: 'significantly worse than', |
| 14 | similar: 'similar to', |
| 15 | }; |
| 16 | |
| 17 | function camelize(string) { |
| 18 | return string.replace(/-./g, x => x[1].toUpperCase()); |
| 19 | } |
| 20 | |
| 21 | function capitalizeFirstLetter(string) { |
| 22 | return string.charAt(0).toUpperCase() + string.slice(1); |
| 23 | } |
| 24 | |
| 25 | console.log(`// Copyright 2024 The Chromium Authors. All rights reserved. |
| 26 | // Use of this source code is governed by a BSD-style license that can be |
| 27 | // found in the LICENSE file. |
| 28 | |
| 29 | import * as i18n from '../../../core/i18n/i18n.js'; |
| 30 | |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 31 | import {type MetricRating} from './Utils.js'; |
| 32 | |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 33 | // This file is auto-generated by scripts/generate_metric_compare_strings.js. |
| 34 | // |
| 35 | // If you need to update one or more of these strings, it is preferable to modify the script |
| 36 | // and write stdout to this file (Minor formatting differences may apply). |
| 37 | |
| 38 | const UIStrings = {`); |
| 39 | for (const [rating, ratingText] of Object.entries(ratings)) { |
| 40 | for (const [compare, compareText] of Object.entries(compares)) { |
| 41 | const tag = camelize(rating) + capitalizeFirstLetter(compare) + 'Compare'; |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 42 | const transition = compare === 'similar' ? 'and' : 'but'; |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 43 | console.log(` /** |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 44 | * @description Text block that compares a local metric value to real user experiences. "local" refers to a developers local testing environment. |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 45 | * @example {LCP} PH1 |
| 46 | * @example {500 ms} PH2 |
| 47 | */ |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 48 | ${tag}: 'Your local {PH1} {PH2} ${ratingText}, ${transition} is ${compareText} your users’ experience.',`); |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 49 | } |
| 50 | const tag = camelize(rating) + 'Summarized'; |
| 51 | console.log(` /** |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 52 | * @description Text block that summarize a local metric value. "local" refers to a developers local testing environment. |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 53 | * @example {LCP} PH1 |
| 54 | * @example {500 ms} PH2 |
| 55 | */ |
| 56 | ${tag}: 'Your local {PH1} {PH2} ${ratingText}.',`); |
| 57 | } |
Adam Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame] | 58 | for (const [localRating, localRatingText] of Object.entries(ratings)) { |
| 59 | for (const [fieldRating, fieldRatingText] of Object.entries(ratings)) { |
| 60 | const tag = camelize(localRating) + capitalizeFirstLetter(camelize(fieldRating)) + 'DetailedCompare'; |
| 61 | const transition = localRating === fieldRating ? 'Additionally' : 'However'; |
| 62 | console.log(` /** |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 63 | * @description Text block that compares a local metric value to real user experiences. "field data" refers to performance data collected from real users on the site. "local" refers to a developers local testing environment. |
Adam Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame] | 64 | * @example {LCP} PH1 |
| 65 | * @example {500 ms} PH2 |
| 66 | * @example {400 ms} PH3 |
| 67 | * @example {40%} PH4 |
| 68 | */ |
| 69 | ${tag}: 'Your local {PH1} {PH2} ${localRatingText} and is rated the same as {PH4} of real-user {PH1} experiences. ${ |
| 70 | transition}, the field data 75th percentile {PH1} {PH3} ${fieldRatingText}.',`); |
| 71 | } |
| 72 | } |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 73 | console.log(`}; |
| 74 | |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 75 | const str_ = i18n.i18n.registerUIStrings('panels/timeline/components/MetricCompareStrings.ts', UIStrings); |
| 76 | |
| 77 | export type CompareRating = ${Object.keys(compares).map(c => `'${c}'`).join('|')};`); |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 78 | |
| 79 | console.log(` |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 80 | export function renderCompareText(options: {metric: string, rating: MetricRating, compare?: CompareRating, localValue: Element}): Element { |
| 81 | const {rating, compare} = options; |
| 82 | const values = { |
| 83 | PH1: options.metric, |
| 84 | PH2: options.localValue, |
| 85 | }; |
Adam Raine | 3442a3e | 2024-08-02 13:34:09 | [diff] [blame] | 86 | `); |
| 87 | |
| 88 | for (const rating of Object.keys(ratings)) { |
| 89 | for (const compare of Object.keys(compares)) { |
| 90 | const tag = camelize(rating) + capitalizeFirstLetter(compare) + 'Compare'; |
| 91 | console.log(` if (rating === '${rating}' && compare === '${compare}') { |
| 92 | return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values); |
| 93 | }`); |
| 94 | } |
| 95 | const tag = camelize(rating) + 'Summarized'; |
| 96 | console.log(` if (rating === '${rating}' && !compare) { |
| 97 | return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values); |
| 98 | }`); |
| 99 | } |
| 100 | console.log(` |
| 101 | throw new Error('Compare string not found'); |
| 102 | }`); |
Adam Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame] | 103 | |
| 104 | console.log(` |
Adam Raine | 552aa9b | 2024-08-23 05:52:40 | [diff] [blame^] | 105 | export function renderDetailedCompareText(options: {metric: string, localRating: MetricRating, fieldRating?: MetricRating, localValue: Element, fieldValue: Element, percent: string}): Element { |
| 106 | const {localRating, fieldRating} = options; |
| 107 | const values = { |
| 108 | PH1: options.metric, |
| 109 | PH2: options.localValue, |
| 110 | PH3: options.fieldValue, |
| 111 | PH4: options.percent, |
| 112 | }; |
Adam Raine | 17f93b8 | 2024-08-07 15:07:11 | [diff] [blame] | 113 | `); |
| 114 | |
| 115 | for (const localRating of Object.keys(ratings)) { |
| 116 | for (const fieldRating of Object.keys(ratings)) { |
| 117 | const tag = camelize(localRating) + capitalizeFirstLetter(camelize(fieldRating)) + 'DetailedCompare'; |
| 118 | console.log(` if (localRating === '${localRating}' && fieldRating === '${fieldRating}') { |
| 119 | return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values); |
| 120 | }`); |
| 121 | } |
| 122 | const tag = camelize(localRating) + 'Summarized'; |
| 123 | console.log(` if (localRating === '${localRating}' && !fieldRating) { |
| 124 | return i18n.i18n.getFormatLocalizedString(str_, UIStrings.${tag}, values); |
| 125 | }`); |
| 126 | } |
| 127 | console.log(` |
| 128 | throw new Error('Detailed compare string not found'); |
| 129 | }`); |