about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2024-10-10 22:15:28 -0700
committerGitHub <noreply@github.com>2024-10-10 22:15:28 -0700
commit5ebb630c9d9c462ca6c33f36dc470b53f88b62a5 (patch)
treea9b6ffc685afa8bcd987f5bc2e76cf00bfd7e0e3
parent9a769b9a038fe32ca2faaaf7fd914e1081ad76e9 (diff)
downloadvoidsky-5ebb630c9d9c462ca6c33f36dc470b53f88b62a5.tar.zst
Improve sort & filter of language options in search (#5709)
-rw-r--r--src/view/screens/Search/Search.tsx23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/view/screens/Search/Search.tsx b/src/view/screens/Search/Search.tsx
index 3acb31c05..97888eec5 100644
--- a/src/view/screens/Search/Search.tsx
+++ b/src/view/screens/Search/Search.tsx
@@ -22,7 +22,7 @@ import {useLingui} from '@lingui/react'
 import AsyncStorage from '@react-native-async-storage/async-storage'
 import {useFocusEffect, useNavigation} from '@react-navigation/native'
 
-import {LANGUAGES} from '#/lib/../locale/languages'
+import {APP_LANGUAGES, LANGUAGES} from '#/lib/../locale/languages'
 import {createHitslop} from '#/lib/constants'
 import {HITSLOP_10} from '#/lib/constants'
 import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
@@ -349,14 +349,31 @@ function SearchLanguageDropdown({
         key: '*',
       },
     ].concat(
-      LANGUAGES.filter(l => Boolean(l.code2))
+      LANGUAGES.filter(
+        (lang, index, self) =>
+          Boolean(lang.code2) && // reduce to the code2 varieties
+          index === self.findIndex(t => t.code2 === lang.code2), // remove dupes (which will happen)
+      )
         .map(l => ({
           label: l.name,
           inputLabel: l.name,
           value: l.code2,
           key: l.code2 + l.code3,
         }))
-        .sort(a => (contentLanguages.includes(a.value) ? -1 : 1)),
+        .sort((a, b) => {
+          // prioritize user's languages
+          const aIsUser = contentLanguages.includes(a.value)
+          const bIsUser = contentLanguages.includes(b.value)
+          if (aIsUser && !bIsUser) return -1
+          if (bIsUser && !aIsUser) return 1
+          // prioritize "common" langs in the network
+          const aIsCommon = !!APP_LANGUAGES.find(al => al.code2 === a.value)
+          const bIsCommon = !!APP_LANGUAGES.find(al => al.code2 === b.value)
+          if (aIsCommon && !bIsCommon) return -1
+          if (bIsCommon && !aIsCommon) return 1
+          // fall back to alphabetical
+          return a.label.localeCompare(b.label)
+        }),
     )
   }, [_, contentLanguages])