]> Untitled Git - lemmy-ui.git/commitdiff
feat: Default language dropdowns to user's primary language
authorJay Sitter <jay@jaysitter.com>
Thu, 22 Jun 2023 04:54:07 +0000 (00:54 -0400)
committerJay Sitter <jay@jaysitter.com>
Thu, 22 Jun 2023 04:54:07 +0000 (00:54 -0400)
src/shared/components/comment/comment-form.tsx
src/shared/components/post/post-form.tsx
src/shared/utils/app/index.ts
src/shared/utils/app/user-primary-language.ts [new file with mode: 0644]

index 294960a812d35711983c4e33728189ae74fb7e36..11366fc4b95a572308b19dfae1ed1f29be36390e 100644 (file)
@@ -1,4 +1,5 @@
 import { myAuthRequired } from "@utils/app";
+import getUserPrimaryLanguage from "@utils/app/user-primary-language";
 import { capitalizeFirstLetter } from "@utils/helpers";
 import { Component } from "inferno";
 import { T } from "inferno-i18next-dess";
@@ -40,6 +41,10 @@ export class CommentForm extends Component<CommentFormProps, any> {
           : undefined
         : undefined;
 
+    const userPrimaryLanguage = getUserPrimaryLanguage(
+      UserService?.Instance?.myUserInfo
+    );
+
     return (
       <div
         className={["comment-form", "mb-3", this.props.containerClass].join(
@@ -49,6 +54,7 @@ export class CommentForm extends Component<CommentFormProps, any> {
         {UserService.Instance.myUserInfo ? (
           <MarkdownTextArea
             initialContent={initialContent}
+            initialLanguageId={userPrimaryLanguage}
             showLanguage
             buttonTitle={this.buttonTitle}
             finished={this.props.finished}
index d2793fa2810e529f2c78e0be0dd8f9c058cec1de..337cc6e3e43bcb7b0a88238a82dcf7e5ef0bd906 100644 (file)
@@ -4,6 +4,7 @@ import {
   myAuth,
   myAuthRequired,
 } from "@utils/app";
+import getUserPrimaryLanguage from "@utils/app/user-primary-language";
 import {
   capitalizeFirstLetter,
   debounce,
@@ -323,8 +324,9 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
   }
 
   render() {
-    const firstLang = this.state.form.language_id;
-    const selectedLangs = firstLang ? Array.of(firstLang) : undefined;
+    const userPrimaryLanguage = getUserPrimaryLanguage(
+      UserService?.Instance?.myUserInfo
+    );
 
     const url = this.state.form.url;
 
@@ -494,8 +496,8 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
         </div>
         <LanguageSelect
           allLanguages={this.props.allLanguages}
+          selectedLanguageIds={[userPrimaryLanguage]}
           siteLanguages={this.props.siteLanguages}
-          selectedLanguageIds={selectedLangs}
           multiple={false}
           onChange={this.handleLanguageChange}
         />
index cdae267786be2bb37e40caa4146615dcd842f873..2d2f09e6f1e3edfde60653c7fc6d065c43ae393a 100644 (file)
@@ -52,6 +52,7 @@ import showScores from "./show-scores";
 import siteBannerCss from "./site-banner-css";
 import updateCommunityBlock from "./update-community-block";
 import updatePersonBlock from "./update-person-block";
+import getUserPrimaryLanguage from "./user-primary-language";
 
 export {
   buildCommentsTree,
@@ -86,6 +87,7 @@ export {
   getIdFromProps,
   getRecipientIdFromProps,
   getUpdatedSearchId,
+  getUserPrimaryLanguage,
   initializeSite,
   insertCommentIntoTree,
   isAuthPath,
diff --git a/src/shared/utils/app/user-primary-language.ts b/src/shared/utils/app/user-primary-language.ts
new file mode 100644 (file)
index 0000000..06ae259
--- /dev/null
@@ -0,0 +1,9 @@
+import { UserService } from "../../services";
+
+export default function getUserPrimaryLanguage(
+  myUserInfo = UserService.Instance.myUserInfo
+): number {
+  // Get first language in discussion languages array that isn't equal to "0",
+  // which is the language selection "Undetermined"
+  return myUserInfo?.discussion_languages.find(lang => lang !== 0) || 0;
+}