Skip to content

Commit

Permalink
DRY sort logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed May 13, 2024
1 parent ef866e6 commit 748e656
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 66 deletions.
35 changes: 35 additions & 0 deletions frontend/models/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,38 @@ export type Sources = {
sources: Source[]
classifiedSourcesCount: number
}

export const sortSources = (sources: Source[], key: 'sourceName' | 'modules', sort: 'none' | 'asc' | 'desc'): Source[] => {
if (sort === 'none') {
return sources
}

let sorted = [...sources]

const ascString = (a: string, b: string) => {
if (a > b) return 1
if (a < b) return -1
return 0
}

switch (key) {
case 'sourceName': {
sorted = sorted.sort((a, b) => ascString(a.sourceName, b.sourceName))
break
}
case 'modules': {
sorted = sorted.sort((a, b) =>
ascString(
a.modules.map((module) => module.moduleName).join('-'),
b.modules.map((module) => module.moduleName).join('-'),
),
)
}
}

if (sort === 'desc') {
sorted = sorted.reverse()
}

return sorted
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { color } from '@/constants/theme'
import { CombinedDefinition } from '@/models/combinedDefinition'

import { SourceModulesComboBox } from '../SourceModulesComboBox'
import { sortSources } from '@/models/source'

type Props = {
combinedDefinition: CombinedDefinition
Expand Down Expand Up @@ -44,38 +45,7 @@ export const DefinitionSources: FC<Props> = ({ combinedDefinition, mutateCombine
)

const sources: CombinedDefinition['sources'] = useMemo(() => {
let sorted = [...combinedDefinition.sources]

if (sortState.sort === 'none') {
return sorted
}

const ascString = (a: string, b: string) => {
if (a > b) return 1
if (a < b) return -1
return 0
}

switch (sortState.key) {
case 'sourceName': {
sorted = sorted.sort((a, b) => ascString(a.sourceName, b.sourceName))
break
}
case 'modules': {
sorted = sorted.sort((a, b) =>
ascString(
a.modules.map((module) => module.moduleName).join('-'),
b.modules.map((module) => module.moduleName).join('-'),
),
)
}
}

if (sortState.sort === 'desc') {
sorted = sorted.reverse()
}

return sorted
return sortSources(combinedDefinition.sources, sortState.key, sortState.sort)
}, [combinedDefinition.sources, sortState])

return (
Expand Down
37 changes: 3 additions & 34 deletions frontend/pages/Sources/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EmptyTableBody, Heading, Section, Stack, Table, Td, Text, Th } from '@/
import { path } from '@/constants/path'
import { spacing } from '@/constants/theme'
import { useSources } from '@/repositories/sourceRepository'
import { Sources } from '@/models/source'
import { Sources, sortSources } from '@/models/source'

const sortTypes = ['asc', 'desc', 'none'] as const

Expand Down Expand Up @@ -40,41 +40,10 @@ export const List: FC = () => {

const sources: Sources['sources'] = useMemo(() => {
if (!data) {
return null
return []
}

let sorted = [...data.sources]

if (sortState.sort === 'none') {
return sorted
}

const ascString = (a: string, b: string) => {
if (a > b) return 1
if (a < b) return -1
return 0
}

switch (sortState.key) {
case 'sourceName': {
sorted = sorted.sort((a, b) => ascString(a.sourceName, b.sourceName))
break
}
case 'modules': {
sorted = sorted.sort((a, b) =>
ascString(
a.modules.map((module) => module.moduleName).join('-'),
b.modules.map((module) => module.moduleName).join('-'),
),
)
}
}

if (sortState.sort === 'desc') {
sorted = sorted.reverse()
}

return sorted
return sortSources(data.sources, sortState.key, sortState.sort)
}, [data?.sources, sortState])

return (
Expand Down

0 comments on commit 748e656

Please sign in to comment.