mirror of
https://github.com/LemmyNet/joinlemmy-site.git
synced 2024-11-28 07:11:18 +00:00
Adding random app sort. Fixes #268
This commit is contained in:
parent
15bd9d2844
commit
9732b03864
3 changed files with 12 additions and 8 deletions
|
@ -13,6 +13,7 @@ import {
|
||||||
} from "./app-definitions";
|
} from "./app-definitions";
|
||||||
import { Icon } from "./icon";
|
import { Icon } from "./icon";
|
||||||
import { I18nKeys } from "i18next";
|
import { I18nKeys } from "i18next";
|
||||||
|
import { sortRandom } from "../utils";
|
||||||
|
|
||||||
const TitleBlock = () => (
|
const TitleBlock = () => (
|
||||||
<div className="flex flex-col items-center pt-16 mb-4">
|
<div className="flex flex-col items-center pt-16 mb-4">
|
||||||
|
@ -146,6 +147,9 @@ export class Apps extends Component<any, State> {
|
||||||
apps = apps.filter(a => a.platforms.includes(this.state.platform));
|
apps = apps.filter(a => a.platforms.includes(this.state.platform));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Random sort
|
||||||
|
apps = sortRandom(apps);
|
||||||
|
|
||||||
this.setState({ apps });
|
this.setState({ apps });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Helmet } from "inferno-helmet";
|
||||||
import { i18n, LANGUAGES } from "../i18next";
|
import { i18n, LANGUAGES } from "../i18next";
|
||||||
import { T } from "inferno-i18next";
|
import { T } from "inferno-i18next";
|
||||||
import { instance_stats } from "../instance_stats";
|
import { instance_stats } from "../instance_stats";
|
||||||
import { getQueryParams, mdToHtml, numToSI } from "../utils";
|
import { getQueryParams, mdToHtml, numToSI, sortRandom } from "../utils";
|
||||||
import { Badge, SELECT_CLASSES, SectionTitle, TEXT_GRADIENT } from "./common";
|
import { Badge, SELECT_CLASSES, SectionTitle, TEXT_GRADIENT } from "./common";
|
||||||
import {
|
import {
|
||||||
INSTANCE_HELPERS,
|
INSTANCE_HELPERS,
|
||||||
|
@ -351,13 +351,6 @@ const LEAST_ACTIVE_SORT: Sort = {
|
||||||
|
|
||||||
const SORTS: Sort[] = [RANDOM_SORT, MOST_ACTIVE_SORT, LEAST_ACTIVE_SORT];
|
const SORTS: Sort[] = [RANDOM_SORT, MOST_ACTIVE_SORT, LEAST_ACTIVE_SORT];
|
||||||
|
|
||||||
function sortRandom(instances: any[]): any[] {
|
|
||||||
return instances
|
|
||||||
.map(value => ({ value, sort: Math.random() }))
|
|
||||||
.sort((a, b) => a.sort - b.sort)
|
|
||||||
.map(({ value }) => value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function sortActive(instances: any[]): any[] {
|
function sortActive(instances: any[]): any[] {
|
||||||
return instances.sort(
|
return instances.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
|
|
|
@ -57,3 +57,10 @@ export function monthsBetween(startDate: Date, endDate: Date) {
|
||||||
// Convert back to days and return
|
// Convert back to days and return
|
||||||
return Math.round(differenceMs / oneMonth);
|
return Math.round(differenceMs / oneMonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sortRandom<T>(list: T[]): T[] {
|
||||||
|
return list
|
||||||
|
.map(value => ({ value, sort: Math.random() }))
|
||||||
|
.sort((a, b) => a.sort - b.sort)
|
||||||
|
.map(({ value }) => value);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue