Skip to content

Commit

Permalink
Remove knowledge of default sorting order from parent component
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-cummins committed Mar 13, 2024
1 parent ef1515c commit 6eacd0e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/components/extensions-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useState } from "react"
import Filters from "./filters/filters"
import ExtensionCard from "./extension-card"
import styled from "styled-components"
import { timestampExtensionComparator } from "./sortings/timestamp-extension-comparator"
import Sortings from "./sortings/sortings"

const FilterableList = styled.div`
Expand Down Expand Up @@ -57,15 +56,17 @@ const ExtensionsList = ({ extensions, categories, downloadData }) => {
const allExtensions = extensions.filter(extension => !extension.isSuperseded)

const [filteredExtensions, setExtensions] = useState(allExtensions)
const [extensionComparator, setExtensionComparator] = useState(() => timestampExtensionComparator)
const [extensionComparator, setExtensionComparator] = useState(() => undefined)

if (allExtensions) {
// Exclude unlisted extensions from the count, even though we sometimes show them if there's a direct search for it
const extensionCount = allExtensions.filter(
extension => !extension.metadata.unlisted
).length

filteredExtensions.sort(extensionComparator)
if (extensionComparator) {
filteredExtensions.sort(extensionComparator)
}

const countMessage =
extensionCount === filteredExtensions.length
Expand Down
8 changes: 3 additions & 5 deletions src/components/sortings/sortings.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ const colourStyles = {

const key = "sort"
const downloads = "downloads"

const time = "time"
const sortings = [
{ label: "Most recently released", value: "time", comparator: timestampExtensionComparator },
{ label: "Most recently released", value: time, comparator: timestampExtensionComparator },
{ label: "Alphabetical", value: "alpha", comparator: alphabeticalExtensionComparator },
{ label: "Downloads", value: downloads, comparator: downloadsExtensionComparator }]

const Sortings = ({ sorterAction, downloadData }) => {
const [sort, setSort] = useQueryParamString(key, undefined, true)
const [sort, setSort] = useQueryParamString(key, time, true)

const applySort = (entry) => {
// We need to wrap our comparator functions in functions or they get called, which goes very badly
Expand All @@ -100,10 +100,8 @@ const Sortings = ({ sorterAction, downloadData }) => {
applySort(selected)
}


const formattedDate = downloadData?.date ? format.format(new Date(Number(downloadData.date))) : ""


return (
<SortBar className="sortings">
{sort === downloads &&
Expand Down

0 comments on commit 6eacd0e

Please sign in to comment.