From 89b7f8884a136bf33a40b449302391a921bbcff9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 20 Jan 2025 10:17:13 +0100 Subject: [PATCH] [Glitch] Add warning when selected and detected language is different in web UI Port 8962f1157816848b1eff8ee992410c120d4696e8 to glitch-soc Signed-off-by: Claire --- .../compose/components/language_dropdown.jsx | 16 ++-- .../containers/language_dropdown_container.js | 73 +++++++++++++++++++ .../flavours/glitch/styles/components.scss | 10 +++ 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/app/javascript/flavours/glitch/features/compose/components/language_dropdown.jsx b/app/javascript/flavours/glitch/features/compose/components/language_dropdown.jsx index a2efa0d500..82874cfbdb 100644 --- a/app/javascript/flavours/glitch/features/compose/components/language_dropdown.jsx +++ b/app/javascript/flavours/glitch/features/compose/components/language_dropdown.jsx @@ -27,6 +27,7 @@ class LanguageDropdownMenu extends PureComponent { static propTypes = { value: PropTypes.string.isRequired, + guess: PropTypes.string, frequentlyUsedLanguages: PropTypes.arrayOf(PropTypes.string).isRequired, onClose: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, @@ -81,14 +82,17 @@ class LanguageDropdownMenu extends PureComponent { }; search () { - const { languages, value, frequentlyUsedLanguages } = this.props; + const { languages, value, frequentlyUsedLanguages, guess } = this.props; const { searchValue } = this.state; if (searchValue === '') { return [...languages].sort((a, b) => { - // Push current selection to the top of the list - if (a[0] === value) { + if (guess && a[0] === guess) { // Push guessed language higher than current selection + return -1; + } else if (guess && b[0] === guess) { + return 1; + } else if (a[0] === value) { // Push current selection to the top of the list return -1; } else if (b[0] === value) { return 1; @@ -238,6 +242,7 @@ class LanguageDropdown extends PureComponent { static propTypes = { value: PropTypes.string, frequentlyUsedLanguages: PropTypes.arrayOf(PropTypes.string), + guess: PropTypes.string, intl: PropTypes.object.isRequired, onChange: PropTypes.func, }; @@ -281,7 +286,7 @@ class LanguageDropdown extends PureComponent { }; render () { - const { value, intl, frequentlyUsedLanguages } = this.props; + const { value, guess, intl, frequentlyUsedLanguages } = this.props; const { open, placement } = this.state; const current = preloadedLanguages.find(lang => lang[0] === value) ?? []; @@ -294,7 +299,7 @@ class LanguageDropdown extends PureComponent { onClick={this.handleToggle} onMouseDown={this.handleMouseDown} onKeyDown={this.handleButtonKeyDown} - className={classNames('dropdown-button', { active: open })} + className={classNames('dropdown-button', { active: open, warning: guess !== '' && guess !== value })} > {current[2] ?? value} @@ -306,6 +311,7 @@ class LanguageDropdown extends PureComponent {
lande(text), 500, { trailing: true }); + +const detectedLanguage = createSelector([ + state => state.getIn(['compose', 'text']), +], text => { + if (text.length > 20) { + const guesses = debouncedLande(text); + const [lang, confidence] = guesses[0]; + + if (confidence > 0.8) { + return ISO_639_MAP[lang]; + } + } + + return ''; +}); + const mapStateToProps = state => ({ frequentlyUsedLanguages: getFrequentlyUsedLanguages(state), value: state.getIn(['compose', 'language']), + guess: detectedLanguage(state), }); const mapDispatchToProps = dispatch => ({ diff --git a/app/javascript/flavours/glitch/styles/components.scss b/app/javascript/flavours/glitch/styles/components.scss index 9418a45617..39c7ca01cb 100644 --- a/app/javascript/flavours/glitch/styles/components.scss +++ b/app/javascript/flavours/glitch/styles/components.scss @@ -988,6 +988,16 @@ body > [data-popper-placement] { border-color: $ui-highlight-color; color: $primary-text-color; } + + &.warning { + border-color: var(--goldenrod-2); + color: var(--goldenrod-2); + + &.active { + background-color: var(--goldenrod-2); + color: var(--indigo-1); + } + } } .character-counter {