816 lines
21 KiB
TypeScript
816 lines
21 KiB
TypeScript
import type { Labrinth } from '@modrinth/api-client'
|
|
import { ClientIcon, getCategoryIcon, getLoaderIcon, ServerIcon } from '@modrinth/assets'
|
|
import { sortedCategories } from '@modrinth/utils'
|
|
import { type Component, computed, readonly, type Ref, ref } from 'vue'
|
|
import { type LocationQueryRaw, type LocationQueryValue, useRoute } from 'vue-router'
|
|
|
|
import { defineMessage, useVIntl } from '../composables/i18n'
|
|
import {
|
|
DEFAULT_MOD_LOADERS,
|
|
DEFAULT_SHADER_LOADERS,
|
|
formatCategory,
|
|
formatCategoryHeader,
|
|
formatLoader,
|
|
} from './tag-messages.ts'
|
|
|
|
type BaseOption = {
|
|
id: string
|
|
formatted_name?: string
|
|
toggle_group?: string
|
|
icon?: string | Component
|
|
query_value?: string
|
|
group?: string
|
|
}
|
|
|
|
export type FilterOption = BaseOption &
|
|
(
|
|
| { method: 'or' | 'and'; value: string }
|
|
| { method: 'environment'; environment: 'client' | 'server' }
|
|
)
|
|
|
|
export type FilterType = {
|
|
id: string
|
|
formatted_name: string
|
|
options: FilterOption[]
|
|
supported_project_types: ProjectType[]
|
|
query_param: string
|
|
supports_negative_filter: boolean
|
|
toggle_groups?: {
|
|
id: string
|
|
formatted_name: string
|
|
query_param?: string
|
|
}[]
|
|
searchable: boolean
|
|
allows_custom_options?: 'and' | 'or'
|
|
ordering?: number
|
|
} & (
|
|
| {
|
|
display: 'all' | 'scrollable' | 'none'
|
|
}
|
|
| {
|
|
display: 'expandable'
|
|
default_values: string[]
|
|
}
|
|
)
|
|
|
|
export type FilterValue = {
|
|
type: string
|
|
option: string
|
|
negative?: boolean
|
|
}
|
|
|
|
export interface GameVersion {
|
|
version: string
|
|
version_type: 'release' | 'snapshot' | 'alpha' | 'beta'
|
|
date: string
|
|
major: boolean
|
|
}
|
|
|
|
export type ProjectType =
|
|
| 'mod'
|
|
| 'modpack'
|
|
| 'resourcepack'
|
|
| 'shader'
|
|
| 'datapack'
|
|
| 'plugin'
|
|
| 'server'
|
|
| 'project'
|
|
|
|
const ALL_PROJECT_TYPES: ProjectType[] = [
|
|
'mod',
|
|
'modpack',
|
|
'resourcepack',
|
|
'shader',
|
|
'datapack',
|
|
'plugin',
|
|
'server',
|
|
]
|
|
|
|
export interface Tags {
|
|
gameVersions: Labrinth.Tags.v2.GameVersion[]
|
|
loaders: Labrinth.Tags.v2.Loader[]
|
|
categories: Labrinth.Tags.v2.Category[]
|
|
}
|
|
|
|
export interface SortType {
|
|
display: string
|
|
name: string
|
|
}
|
|
|
|
const PLUGIN_PLATFORMS = ['bungeecord', 'waterfall', 'velocity', 'geyser']
|
|
|
|
export function useSearch(
|
|
projectTypes: Ref<ProjectType[]>,
|
|
tags: Ref<Tags>,
|
|
providedFilters: Ref<FilterValue[]>,
|
|
) {
|
|
const query = ref('')
|
|
const maxResults = ref(20)
|
|
|
|
const sortTypes: readonly SortType[] = readonly([
|
|
{ display: 'Relevance', name: 'relevance' },
|
|
{ display: 'Downloads', name: 'downloads' },
|
|
{ display: 'Followers', name: 'follows' },
|
|
{ display: 'Date published', name: 'newest' },
|
|
{ display: 'Date updated', name: 'updated' },
|
|
])
|
|
|
|
const currentSortType: Ref<SortType> = ref(sortTypes[0])
|
|
|
|
const route = useRoute()
|
|
const currentPage = ref(1)
|
|
|
|
const currentFilters: Ref<FilterValue[]> = ref<FilterValue[]>([])
|
|
const toggledGroups = ref<string[]>([])
|
|
const overriddenProvidedFilterTypes = ref<string[]>([])
|
|
|
|
const { formatMessage, locale } = useVIntl()
|
|
const formatCategoryName = (categoryName: string) => {
|
|
return formatCategory(formatMessage, categoryName)
|
|
}
|
|
|
|
const filters = computed(() => {
|
|
const categoryFilters: Record<string, FilterType> = {}
|
|
for (const category of sortedCategories(tags.value, formatCategoryName, locale.value)) {
|
|
const filterTypeId = `category_${category.project_type}_${category.header}`
|
|
if (!categoryFilters[filterTypeId]) {
|
|
categoryFilters[filterTypeId] = {
|
|
id: filterTypeId,
|
|
formatted_name: formatCategoryHeader(formatMessage, category.header),
|
|
supported_project_types:
|
|
category.project_type === 'mod'
|
|
? ['mod', 'plugin', 'datapack']
|
|
: ([category.project_type] as ProjectType[]),
|
|
display: 'all',
|
|
query_param: category.header === 'resolutions' ? 'g' : 'f',
|
|
supports_negative_filter: true,
|
|
searchable: false,
|
|
options: [],
|
|
}
|
|
}
|
|
categoryFilters[filterTypeId].options.push({
|
|
id: category.name,
|
|
formatted_name: formatCategory(formatMessage, category.name),
|
|
icon: getCategoryIcon(category.name),
|
|
value: `categories:${category.name}`,
|
|
method: category.header === 'resolutions' ? 'or' : 'and',
|
|
})
|
|
}
|
|
|
|
const filterTypes: FilterType[] = [
|
|
...Object.values(categoryFilters),
|
|
{
|
|
id: 'environment',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.environment',
|
|
defaultMessage: 'Environment',
|
|
}),
|
|
),
|
|
supported_project_types: ['mod', 'modpack'],
|
|
display: 'all',
|
|
query_param: 'e',
|
|
supports_negative_filter: false,
|
|
searchable: false,
|
|
options: [
|
|
{
|
|
id: 'client',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.environment.client',
|
|
defaultMessage: 'Client',
|
|
}),
|
|
),
|
|
icon: ClientIcon,
|
|
method: 'environment',
|
|
environment: 'client',
|
|
},
|
|
{
|
|
id: 'server',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.environment.server',
|
|
defaultMessage: 'Server',
|
|
}),
|
|
),
|
|
icon: ServerIcon,
|
|
method: 'environment',
|
|
environment: 'server',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: 'game_version',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.game_version',
|
|
defaultMessage: 'Game version',
|
|
}),
|
|
),
|
|
supported_project_types: ALL_PROJECT_TYPES,
|
|
display: 'scrollable',
|
|
query_param: 'v',
|
|
supports_negative_filter: false,
|
|
toggle_groups: [
|
|
{
|
|
id: 'all_versions',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.game_version.all_versions',
|
|
defaultMessage: 'Show all versions',
|
|
}),
|
|
),
|
|
query_param: 'h',
|
|
},
|
|
],
|
|
searchable: true,
|
|
options: tags.value.gameVersions.map((gameVersion) => ({
|
|
id: gameVersion.version,
|
|
toggle_group: gameVersion.version_type !== 'release' ? 'all_versions' : undefined,
|
|
value: `game_versions:${gameVersion.version}`,
|
|
query_value: gameVersion.version,
|
|
method: 'or',
|
|
})),
|
|
ordering: projectTypes.value.includes('mod')
|
|
? 2
|
|
: projectTypes.value.includes('shader')
|
|
? -1
|
|
: undefined,
|
|
},
|
|
{
|
|
id: 'mod_loader',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.mod_loader',
|
|
defaultMessage: 'Loader',
|
|
}),
|
|
),
|
|
supported_project_types: ['mod'],
|
|
display: 'expandable',
|
|
query_param: 'g',
|
|
supports_negative_filter: true,
|
|
default_values: DEFAULT_MOD_LOADERS,
|
|
searchable: false,
|
|
options: tags.value.loaders
|
|
.filter(
|
|
(loader) =>
|
|
loader.supported_project_types.includes('mod') &&
|
|
!loader.supported_project_types.includes('plugin') &&
|
|
!loader.supported_project_types.includes('datapack'),
|
|
)
|
|
.map((loader) => {
|
|
return {
|
|
id: loader.name,
|
|
formatted_name: formatLoader(formatMessage, loader.name),
|
|
icon: getLoaderIcon(loader.name),
|
|
method: 'or',
|
|
value: `categories:${loader.name}`,
|
|
}
|
|
}),
|
|
ordering: projectTypes.value.includes('mod') ? 1 : undefined,
|
|
},
|
|
{
|
|
id: 'modpack_loader',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.modpack_loader',
|
|
defaultMessage: 'Loader',
|
|
}),
|
|
),
|
|
supported_project_types: ['modpack'],
|
|
display: 'all',
|
|
query_param: 'g',
|
|
supports_negative_filter: true,
|
|
searchable: false,
|
|
options: tags.value.loaders
|
|
.filter((loader) => loader.supported_project_types.includes('modpack'))
|
|
.map((loader) => {
|
|
return {
|
|
id: loader.name,
|
|
formatted_name: formatLoader(formatMessage, loader.name),
|
|
icon: getLoaderIcon(loader.name),
|
|
method: 'or',
|
|
value: `categories:${loader.name}`,
|
|
}
|
|
}),
|
|
},
|
|
{
|
|
id: 'plugin_loader',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.plugin_loader',
|
|
defaultMessage: 'Loader',
|
|
}),
|
|
),
|
|
supported_project_types: ['plugin'],
|
|
display: 'all',
|
|
query_param: 'g',
|
|
supports_negative_filter: true,
|
|
searchable: false,
|
|
options: tags.value.loaders
|
|
.filter(
|
|
(loader) =>
|
|
loader.supported_project_types.includes('plugin') &&
|
|
!PLUGIN_PLATFORMS.includes(loader.name),
|
|
)
|
|
.map((loader) => {
|
|
return {
|
|
id: loader.name,
|
|
formatted_name: formatLoader(formatMessage, loader.name),
|
|
icon: getLoaderIcon(loader.name),
|
|
method: 'or',
|
|
value: `categories:${loader.name}`,
|
|
}
|
|
}),
|
|
},
|
|
{
|
|
id: 'plugin_platform',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.plugin_platform',
|
|
defaultMessage: 'Platform',
|
|
}),
|
|
),
|
|
supported_project_types: ['plugin'],
|
|
display: 'all',
|
|
query_param: 'g',
|
|
supports_negative_filter: true,
|
|
searchable: false,
|
|
options: tags.value.loaders
|
|
.filter((loader) => PLUGIN_PLATFORMS.includes(loader.name))
|
|
.map((loader) => {
|
|
return {
|
|
id: loader.name,
|
|
formatted_name: formatLoader(formatMessage, loader.name),
|
|
icon: getLoaderIcon(loader.name),
|
|
method: 'or',
|
|
value: `categories:${loader.name}`,
|
|
}
|
|
}),
|
|
},
|
|
{
|
|
id: 'shader_loader',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.shader_loader',
|
|
defaultMessage: 'Loader',
|
|
}),
|
|
),
|
|
supported_project_types: ['shader'],
|
|
query_param: 'g',
|
|
supports_negative_filter: true,
|
|
searchable: false,
|
|
display: 'expandable',
|
|
default_values: DEFAULT_SHADER_LOADERS,
|
|
options: tags.value.loaders
|
|
.filter((loader) => loader.supported_project_types.includes('shader'))
|
|
.map((loader) => {
|
|
return {
|
|
id: loader.name,
|
|
formatted_name: formatLoader(formatMessage, loader.name),
|
|
icon: getLoaderIcon(loader.name),
|
|
method: 'or',
|
|
value: `categories:${loader.name}`,
|
|
}
|
|
}),
|
|
},
|
|
{
|
|
id: 'license',
|
|
formatted_name: formatMessage(
|
|
defineMessage({ id: 'search.filter_type.license', defaultMessage: 'License' }),
|
|
),
|
|
supported_project_types: ['mod', 'modpack', 'resourcepack', 'shader', 'plugin', 'datapack'],
|
|
query_param: 'l',
|
|
supports_negative_filter: true,
|
|
display: 'all',
|
|
searchable: false,
|
|
options: [
|
|
{
|
|
id: 'open_source',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.license.open_source',
|
|
defaultMessage: 'Open source',
|
|
}),
|
|
),
|
|
method: 'and',
|
|
value: 'open_source:true',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: 'project_id',
|
|
formatted_name: formatMessage(
|
|
defineMessage({
|
|
id: 'search.filter_type.project_id',
|
|
defaultMessage: 'Project ID',
|
|
}),
|
|
),
|
|
supported_project_types: ALL_PROJECT_TYPES,
|
|
query_param: 'pid',
|
|
supports_negative_filter: true,
|
|
display: 'none',
|
|
searchable: false,
|
|
options: [],
|
|
allows_custom_options: 'and',
|
|
},
|
|
]
|
|
|
|
return filterTypes
|
|
.filter((filterType) =>
|
|
filterType.supported_project_types.some((projectType) =>
|
|
projectTypes.value.includes(projectType),
|
|
),
|
|
)
|
|
.sort((a, b) => (b.ordering ?? 0) - (a.ordering ?? 0))
|
|
})
|
|
|
|
const newFilters = computed(() => {
|
|
const validProvidedFilters = providedFilters.value.filter(
|
|
(providedFilter) => !overriddenProvidedFilterTypes.value.includes(providedFilter.type),
|
|
)
|
|
const filteredFilters = currentFilters.value.filter(
|
|
(userFilter) =>
|
|
!validProvidedFilters.some((providedFilter) => providedFilter.type === userFilter.type),
|
|
)
|
|
const filterValues = [...filteredFilters, ...validProvidedFilters]
|
|
|
|
const parts: string[] = []
|
|
const orGroups: Record<string, string[]> = {}
|
|
const negativeByType: Record<string, string[]> = {}
|
|
|
|
for (const filterValue of filterValues) {
|
|
const type = filters.value.find((type) => type.id === filterValue.type)
|
|
if (!type) {
|
|
console.error(`Filter type ${filterValue.type} not found`)
|
|
continue
|
|
}
|
|
let option = type?.options.find((option) => option.id === filterValue.option)
|
|
if (!option && type.allows_custom_options) {
|
|
option = {
|
|
id: filterValue.option,
|
|
formatted_name: filterValue.option,
|
|
icon: undefined,
|
|
method: type.allows_custom_options,
|
|
value: filterValue.option,
|
|
}
|
|
} else if (!option) {
|
|
console.error(`Filter option ${filterValue.option} not found`)
|
|
continue
|
|
}
|
|
|
|
if (option.method === 'or' || option.method === 'and') {
|
|
const [field, val] = option.value.split(':')
|
|
if (!field || !val) continue
|
|
|
|
if (filterValue.negative) {
|
|
if (!negativeByType[field]) {
|
|
negativeByType[field] = []
|
|
}
|
|
negativeByType[field].push(val)
|
|
} else if (option.method === 'or') {
|
|
if (!orGroups[field]) {
|
|
orGroups[field] = []
|
|
}
|
|
orGroups[field].push(val)
|
|
} else {
|
|
parts.push(`${field} = ${enquoteNonBools(val)}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const [field, values] of Object.entries(orGroups)) {
|
|
if (values.length === 1) {
|
|
const val = values[0]
|
|
parts.push(`${field} = ${enquoteNonBools(val)}`)
|
|
} else {
|
|
const quoted = values.map(enquoteNonBools).join(', ')
|
|
parts.push(`${field} IN [${quoted}]`)
|
|
}
|
|
}
|
|
|
|
for (const [field, values] of Object.entries(negativeByType)) {
|
|
const quoted = values.map(enquoteNonBools).join(', ')
|
|
parts.push(`${field} NOT IN [${quoted}]`)
|
|
}
|
|
|
|
// Environment facets
|
|
const client = filterValues.some(
|
|
(filter) => filter.type === 'environment' && filter.option === 'client',
|
|
)
|
|
const server = filterValues.some(
|
|
(filter) => filter.type === 'environment' && filter.option === 'server',
|
|
)
|
|
for (const envGroup of getEnvironmentFilterGroups(client, server)) {
|
|
if (envGroup.length === 1) {
|
|
const [field, val] = envGroup[0].split(':')
|
|
parts.push(`${field} = ${enquoteNonBools(val)}`)
|
|
} else if (envGroup.length > 1) {
|
|
const conditions = envGroup.map((f) => {
|
|
const [field, val] = f.split(':')
|
|
return `${field} = ${enquoteNonBools(val)}`
|
|
})
|
|
parts.push(`(${conditions.join(' OR ')})`)
|
|
}
|
|
}
|
|
|
|
// Project types
|
|
const mappedProjectTypes = projectTypes.value.map(mapProjectTypeToSearch)
|
|
if (mappedProjectTypes.length === 1) {
|
|
parts.push(`project_types = ${enquoteNonBools(mappedProjectTypes[0])}`)
|
|
} else if (mappedProjectTypes.length > 1) {
|
|
const quoted = mappedProjectTypes.map(enquoteNonBools).join(', ')
|
|
parts.push(`project_types IN [${quoted}]`)
|
|
}
|
|
|
|
return parts.join(' AND ')
|
|
})
|
|
|
|
const requestParams: Ref<string> = computed(() => {
|
|
const params = [`limit=${maxResults.value}`, `index=${currentSortType.value.name}`]
|
|
|
|
if (query.value.length > 0) {
|
|
params.push(`query=${encodeURIComponent(query.value)}`)
|
|
}
|
|
|
|
if (newFilters.value) {
|
|
params.push(`new_filters=${encodeURIComponent(newFilters.value)}`)
|
|
}
|
|
|
|
const offset = (currentPage.value - 1) * maxResults.value
|
|
if (currentPage.value !== 1) {
|
|
params.push(`offset=${offset}`)
|
|
}
|
|
|
|
return `?${params.join('&')}`
|
|
})
|
|
|
|
readQueryParams()
|
|
|
|
function readQueryParams() {
|
|
const readParams = new Set<string>()
|
|
|
|
// Load legacy params
|
|
loadQueryParam(['l'], (openSource) => {
|
|
if (
|
|
openSource === 'true' &&
|
|
!currentFilters.value.some(
|
|
(filter) => filter.type === 'license' && filter.option === 'open_source',
|
|
)
|
|
) {
|
|
currentFilters.value.push({
|
|
type: 'license',
|
|
option: 'open_source',
|
|
negative: false,
|
|
})
|
|
readParams.add('l')
|
|
}
|
|
})
|
|
loadQueryParam(['nf'], (filter) => {
|
|
const set = typeof filter === 'string' ? new Set([filter]) : new Set(filter)
|
|
|
|
typesLoop: for (const type of filters.value) {
|
|
for (const option of type.options) {
|
|
const value = getOptionValue(option, false)
|
|
if (
|
|
set.has(value) &&
|
|
!currentFilters.value.some(
|
|
(filter) => filter.type === type.id && filter.option === option.id,
|
|
)
|
|
) {
|
|
currentFilters.value.push({
|
|
type: type.id,
|
|
option: option.id,
|
|
negative: true,
|
|
})
|
|
readParams.add(type.query_param)
|
|
|
|
set.delete(value)
|
|
|
|
if (set.size === 0) {
|
|
break typesLoop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
loadQueryParam(['s'], (sort) => {
|
|
currentSortType.value = sortTypes.find((sortType) => sortType.name === sort) ?? sortTypes[0]
|
|
readParams.add('s')
|
|
})
|
|
loadQueryParam(['m'], (count) => {
|
|
maxResults.value = Number(count)
|
|
readParams.add('m')
|
|
})
|
|
loadQueryParam(['o'], (offset) => {
|
|
currentPage.value = Math.ceil(Number(offset) / maxResults.value) + 1
|
|
readParams.add('o')
|
|
})
|
|
loadQueryParam(['page'], (page) => {
|
|
currentPage.value = Number(page)
|
|
readParams.add('page')
|
|
})
|
|
loadQueryParam(['q'], (queryVal) => {
|
|
query.value = String(queryVal)
|
|
readParams.add('q')
|
|
})
|
|
|
|
for (const key of Object.keys(route.query).filter((key) => !readParams.has(key))) {
|
|
const types = filters.value.filter((type) => type.query_param === key)
|
|
if (types.length === 0) {
|
|
console.error(`Unknown filter type: ${key}`)
|
|
continue
|
|
}
|
|
|
|
const values = getParamValuesAsArray(route.query[key])
|
|
|
|
for (const value of values) {
|
|
const negative = !value.includes(':') && value.includes('!=')
|
|
let matched = false
|
|
|
|
for (const type of types) {
|
|
const option = type.options.find((option) => getOptionValue(option, negative) === value)
|
|
if (!option) {
|
|
continue
|
|
}
|
|
|
|
currentFilters.value.push({
|
|
type: type.id,
|
|
option: option.id,
|
|
negative: negative,
|
|
})
|
|
matched = true
|
|
break
|
|
}
|
|
|
|
if (!matched) {
|
|
const customType = types.find((type) => type.allows_custom_options)
|
|
if (customType) {
|
|
currentFilters.value.push({
|
|
type: customType.id,
|
|
option: value.replace('!=', ':'),
|
|
negative: negative,
|
|
})
|
|
} else {
|
|
console.error(`Unknown filter option for ${key}: ${value}`)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function createPageParams(): LocationQueryRaw {
|
|
const items: Record<string, string[]> = {}
|
|
|
|
if (query.value) {
|
|
items.q = [query.value]
|
|
}
|
|
|
|
currentFilters.value.forEach((filterValue) => {
|
|
const type = filters.value.find((type) => type.id === filterValue.type)
|
|
const option = type?.options.find((option) => option.id === filterValue.option)
|
|
if (type && option) {
|
|
const value = getOptionValue(option, filterValue.negative)
|
|
if (items[type.query_param]) {
|
|
items[type.query_param].push(value)
|
|
} else {
|
|
items[type.query_param] = [value]
|
|
}
|
|
}
|
|
})
|
|
|
|
toggledGroups.value.forEach((groupId) => {
|
|
const group = filters.value
|
|
.flatMap((filter) => filter.toggle_groups)
|
|
.find((group) => group && group.id === groupId)
|
|
|
|
if (group && 'query_param' in group && group.query_param) {
|
|
items[group.query_param] = [String(true)]
|
|
}
|
|
})
|
|
|
|
if (currentSortType.value.name !== 'relevance') {
|
|
items.s = [currentSortType.value.name]
|
|
}
|
|
if (maxResults.value !== 20) {
|
|
items.m = [String(maxResults.value)]
|
|
}
|
|
if (currentPage.value > 1) {
|
|
items.page = [String(currentPage.value)]
|
|
}
|
|
|
|
return items
|
|
}
|
|
|
|
function createPageParamsString(
|
|
pageParams: Record<string, string | string[] | boolean | number>,
|
|
) {
|
|
let url = ``
|
|
|
|
Object.entries(pageParams).forEach(([key, value]) => {
|
|
if (Array.isArray(value)) {
|
|
value.forEach((value) => {
|
|
url = addQueryParam(url, key, value)
|
|
})
|
|
} else {
|
|
url = addQueryParam(url, key, value)
|
|
}
|
|
})
|
|
|
|
return url
|
|
}
|
|
|
|
function loadQueryParam(
|
|
params: string[],
|
|
provider: (param: LocationQueryValue | LocationQueryValue[]) => void,
|
|
) {
|
|
for (const param of params) {
|
|
if (param in route.query) {
|
|
provider(route.query[param])
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
// Selections
|
|
query,
|
|
currentSortType,
|
|
currentFilters,
|
|
toggledGroups,
|
|
currentPage,
|
|
maxResults,
|
|
overriddenProvidedFilterTypes,
|
|
|
|
// Lists
|
|
sortTypes,
|
|
filters,
|
|
|
|
// Computed
|
|
newFilters,
|
|
requestParams,
|
|
|
|
// Functions
|
|
createPageParams,
|
|
createPageParamsString,
|
|
}
|
|
}
|
|
|
|
const PROJECT_TYPE_SEARCH_MAP: Partial<Record<ProjectType, string>> = {
|
|
server: 'minecraft_java_server',
|
|
}
|
|
|
|
function mapProjectTypeToSearch(projectType: ProjectType): string {
|
|
return PROJECT_TYPE_SEARCH_MAP[projectType] ?? projectType
|
|
}
|
|
|
|
function getEnvironmentFilterGroups(client: boolean, server: boolean): string[][] {
|
|
const groups: string[][] = []
|
|
if (client && server) {
|
|
groups.push(
|
|
['client_side:required', 'client_side:optional', 'client_side:unsupported'],
|
|
['server_side:required', 'server_side:optional'],
|
|
)
|
|
} else if (client) {
|
|
groups.push(
|
|
['client_side:optional', 'client_side:required'],
|
|
['server_side:optional', 'server_side:unsupported'],
|
|
)
|
|
} else if (server) {
|
|
groups.push(['server_side:optional', 'server_side:required'])
|
|
}
|
|
return groups
|
|
}
|
|
|
|
function enquoteNonBools(value: string): string {
|
|
if (value === 'true' || value === 'false') {
|
|
return value
|
|
}
|
|
return `"${value}"`
|
|
}
|
|
|
|
function getOptionValue(option: FilterOption, negative?: boolean): string {
|
|
let value = option.method === 'or' || option.method === 'and' ? option.value : option.id
|
|
if (negative === true) {
|
|
value = value.replace(':', '!=')
|
|
}
|
|
if (option.query_value) {
|
|
value = option.query_value
|
|
}
|
|
return value
|
|
}
|
|
|
|
function addQueryParam(existing: string, key: string, value: string | number | boolean) {
|
|
return existing + `${!existing ? '?' : '&'}${key}=${encodeURIComponent(value)}`
|
|
}
|
|
|
|
function getParamValuesAsArray(x: LocationQueryValue | LocationQueryValue[]): string[] {
|
|
if (x === null) {
|
|
return []
|
|
} else if (typeof x === 'string') {
|
|
return [x]
|
|
} else {
|
|
return x.filter((x) => x !== null)
|
|
}
|
|
}
|