Don't show list of instances by default

Very basic sorting too
This commit is contained in:
Lim Chee Aun 2023-08-22 20:16:09 +08:00
parent e88b24fe6f
commit 4aaf308d6e
2 changed files with 33 additions and 6 deletions

View file

@ -51,3 +51,11 @@
margin: 0;
padding: 0;
}
#instances-eg {
margin: 0.2em 0 0;
padding: 8px;
height: 2.5em;
color: var(--text-insignificant-color);
font-style: italic;
}

View file

@ -83,6 +83,24 @@ function Login() {
submitInstance(instanceURL);
};
const instancesSuggestions = instanceText
? instancesList
.filter((instance) => instance.includes(instanceText))
.sort((a, b) => {
// Move text that starts with instanceText to the start
const aStartsWith = a
.toLowerCase()
.startsWith(instanceText.toLowerCase());
const bStartsWith = b
.toLowerCase()
.startsWith(instanceText.toLowerCase());
if (aStartsWith && !bStartsWith) return -1;
if (!aStartsWith && bStartsWith) return 1;
return 0;
})
.slice(0, 10)
: [];
return (
<main id="login" style={{ textAlign: 'center' }}>
<form onSubmit={onSubmit}>
@ -107,11 +125,9 @@ function Login() {
setInstanceText(e.target.value);
}}
/>
<ul id="instances-suggestions">
{instancesList
.filter((instance) => instance.includes(instanceText))
.slice(0, 10)
.map((instance) => (
{instancesSuggestions?.length > 0 ? (
<ul id="instances-suggestions">
{instancesSuggestions.map((instance) => (
<li>
<button
type="button"
@ -124,7 +140,10 @@ function Login() {
</button>
</li>
))}
</ul>
</ul>
) : (
<div id="instances-eg">e.g. &ldquo;mastodon.social&rsquo;</div>
)}
{/* <datalist id="instances-list">
{instancesList.map((instance) => (
<option value={instance} />