You've already forked AstralRinth
7d92e4ec7f
* feat: base content card component * fix: tooltips + colors * feat: fix orgs * feat: base content tab internals rewrite * feat: fix invalidmodal * feat: add ContentModpackCard * fix: extract types * draft: layout * feat: unlink modal * feat: impl content tab * fix: lint * fix: toggling * temp: disable updating stuff * feat: selection v-model * feat: bulk selection * feat: mods tab rough draft * feat: use fuse.js * feat: add project combobox * clean up project combobox * feat: start install to play modal * fix: events * feat: use v-on * feat: bulk actions + fix floating action bar width * feat: figma alignments * feat: migrate toggle to tailwind * fix: row borders * feat: disabled state * feat: virtual list impl for card table based on window scroll * fix: lint * feat: virtualization + smaller contentcard items * feat: use ContentCardTable + ContentCardItems * feat: fix gap + border issues on last elm * feat: cleanup + use proper searching * fix: use TeleportOverflowMenu * fix: fallback to svg if src is invalid on avatar component * fix: storybook * feat: start on updater modal * feat: finish content updater modal * feat: i18n pass * feat: impl modal * feat(app): backend changes for content tab refactor (#5237) * feat: include_changelog=false for updater modal * fix: hash overrides * feat: update checking for modpack * feat: qa * feat: modpack content modal * fix: padding in table to match modals + tightness * fix: lint * feat: delete modal * feat: fix toggle bugs * fix: prepr * fix: duplicate messages * qa: full width search * qa: use bg-surface-1.5 * qa: animation for filter pills * qa: standardize hover colors * fix: border-[1px] is border * qa: mass de-select actually mass selecting * qa: match figma designs for floating action bar * qa: modal fixes * q: modal fixes x2 * fix: table border * qa: confirm modals * qa: modal alignment * qa: re-add stuck heading + dedupe logic * qa: dedupe virtual scrolling + remove dead components * qa: responsiveness for content table + link fixes * qa: version column link, tooltips + lint fixes * qa: instance busy protections * fix: installation freeze bug * chore: remove old mods page * refactor: deduplicate layout * chore: delete old content page(s) * qa * qa * qa * feat: sort btn - to iterate * fix: ml * feat: date added * fix: lint * fix: formatting.ts removal * feat: get_dependencies_as_content_items * qa: final QA changes * refactor: deduplicate + polish content.rs * feat: hook up content.vue with v1 * feat: hide v1 content api behind frontend feature flag * fix: query keys + copy on empty state * chore: i18n pass * feat: reimpl unlink + upload endpoint * feat: use bulk endpoints v1 * fix: lint * fix: flags * fix: responsiveness via container queries * fix: lint * qa: 1 * qa: fixes * qa: fix ssr issues with browse content * qa: header page divider * qa: modals * fix: prepr * fix: issues * fix: lint * fix: toggle v1 ff * qa: 5 * qa: delete modal copy * feat: creation flow modals (#5383) * refactor: delete content v0 usages + impl * feat: qa + fixes * feat: installing banner using state event * feat: fix modpack card bugs + filtering issues * refactor: delete backups v0 api module * feat: v1 servers GET endpoint * fix: backups * feat: swap to kyros upload v1 addon * fix: use tanstack for loader.vue * feat: finish install from discovery modal * qa: bug fixes * feat: set up installation settings * fix: lint * fix: typos * fix: bugs * fix: disable inline content * feat: content tab improvements — upload UX, installation settings, and client-only indicators Upload cancellation and navigation guard: - Add ConfirmLeaveModal that prompts when navigating away during upload - Cancel in-flight XHR uploads when user confirms leaving the page - Add beforeunload handler to warn on browser/tab close during upload - Track uploadedBytes/totalBytes in UploadState for progress display - Replace Collapsible with Transition for upload progress admonition - Show byte progress and percentage in upload banner - Clamp upload progress to prevent exceeding 100% Installation settings (server.properties): - Add KnownPropertiesFields and PropertiesFields types to Archon types - Add buildProperties() to creation flow context to collect gamemode, difficulty, seed, world type, structures, and generator settings - Pass properties through installContent on onboarding, discovery, and ServerSetupModal flows Server setup and discovery flow improvements: - Migrate ServerSetupModal from servers_v0.reinstall to content_v1.installContent - Replace loaderApiNames lookup with toApiLoader() helper - Remove eraseDataOnInstall toggle — always use soft_override: false - Simplify modpack install on discovery page to use first available version and route through creation flow modal for both onboarding and non-onboarding - Differentiate post-install navigation: content page for onboarding, loader options for existing servers Modpack update flow: - Replace updateModpack() call with installContent() using soft_override: true to support version selection in the content updater modal Client-only mod indicators: - Add environment field to AddonVersion (reuses Labrinth.Projects.v3.Environment) - Add environment to ContentItem and isClientOnly to ContentCardTableItem - Show orange TriangleAlertIcon with tooltip on client-only mods in content table - Add "Client-only" filter pill to content filtering (controlled via showClientOnlyFilter on ContentManagerContext) - Apply client-only indicators in both ContentPageLayout and ModpackContentModal Misc: - Add CLAUDE.md note about using prepr commands for lint checks - Export ConfirmLeaveModal from instances barrel * fix: piping * fix: switch content disable for linked server instances * feat: client only filter * fix: prepr * feat: hasUpdate shape update * feat: bulk update endpoint impl for content in panel * feat: websocket state impl again with new phases * fix: ws * fix: use timeout fn for sync admon + fix content card layout scroll for browsers with overflow anchor bug * fix: qa bugs * fix: lint, a11y and i18n * refactor: set up layouts folder properly * fix: linked data cache stuff + lint * feat: move installationsettings to shared layout * fix: lint * fix: issues * feat: temp fuck staging up * fix: lockfile * fix: data sync issues on loader.vue * fix: lint * Hide shader configuration files from content list (#5499) * feat: workaround search problem + split out reset * fix: qa * fix: changelog not showing on first open * fix: qa + optimistic updating improvements * fix: prepr+lint * fix: qa * feat: qa * fix: lint * fix: lint * fix: build * fix: build * fix: type errors * fix: fade and JAVA_HOME passthrough * feat: qa * feat: impl diff shit * fix: qa * fix: app qa * feat: update diff modal * fix: endpoint * fix: qa * fix: qa * fix: use bulk in modpack modal * feat: abort signal impl + fix issues * fix: diff modal trunc * feat: qa * fix: qa * feat: tooltip content tab * fix: prepr * fix: dismiss on settings btn * feat: qa * feat: dont clear handlers on disconnect * fix: lint * fix: wrangler + introduce staging-archon env file --------- Signed-off-by: Calum H. <calum@modrinth.com> Co-authored-by: tdgao <mr.trumgao@gmail.com> Co-authored-by: Artyom Ezri <61311568+Artezon@users.noreply.github.com>
770 lines
20 KiB
TypeScript
770 lines
20 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({ name: 'relevance', display: 'Relevance' })
|
|
|
|
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: `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 facets = 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 andFacets: string[][] = []
|
|
const orFacets: 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') {
|
|
if (filterValue.negative) {
|
|
andFacets.push([option.value.replace(':', '!=')])
|
|
} else {
|
|
if (option.method === 'or') {
|
|
if (!orFacets[type.id]) {
|
|
orFacets[type.id] = []
|
|
}
|
|
orFacets[type.id].push(option.value)
|
|
} else if (option.method === 'and') {
|
|
andFacets.push([option.value])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Object.values(orFacets).forEach((facets) => andFacets.push(facets))
|
|
|
|
/*
|
|
Add environment facets, separate from the rest because it oddly depends on the combination
|
|
of filters selected to determine which facets to add.
|
|
Uses filterValues (merged user + provided filters) so server-provided environment
|
|
filters are respected.
|
|
*/
|
|
const client = filterValues.some(
|
|
(filter) => filter.type === 'environment' && filter.option === 'client',
|
|
)
|
|
const server = filterValues.some(
|
|
(filter) => filter.type === 'environment' && filter.option === 'server',
|
|
)
|
|
andFacets.push(...createEnvironmentFacets(client, server))
|
|
|
|
const projectType = projectTypes.value.map((projectType) => `project_type:${projectType}`)
|
|
if (andFacets.length > 0) {
|
|
return [projectType, ...andFacets]
|
|
} else {
|
|
return [projectType]
|
|
}
|
|
})
|
|
|
|
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)}`)
|
|
}
|
|
|
|
params.push(`facets=${encodeURIComponent(JSON.stringify(facets.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
|
|
facets,
|
|
requestParams,
|
|
|
|
// Functions
|
|
createPageParams,
|
|
createPageParamsString,
|
|
}
|
|
}
|
|
|
|
export function createEnvironmentFacets(client: boolean, server: boolean): string[][] {
|
|
const facets: string[][] = []
|
|
if (client && server) {
|
|
facets.push(['client_side:required'], ['server_side:required'])
|
|
} else if (client) {
|
|
facets.push(
|
|
['client_side:optional', 'client_side:required'],
|
|
['server_side:optional', 'server_side:unsupported'],
|
|
)
|
|
} else if (server) {
|
|
facets.push(
|
|
['client_side:optional', 'client_side:unsupported'],
|
|
['server_side:optional', 'server_side:required'],
|
|
)
|
|
}
|
|
return facets
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|