|
3 | 3 | isValidArchiveIdOrNullish, |
4 | 4 | parseArchiveId, |
5 | 5 | } from "./utils"; |
6 | | -import { WorkSummary } from "types/entities"; |
| 6 | +import { TagSearchFilters, WorkSummary } from "types/entities"; |
7 | 7 |
|
8 | 8 | declare global { |
9 | 9 | var archiveBaseUrl: string; |
@@ -195,3 +195,46 @@ export const getWorkDetailsFromUrl = ({ |
195 | 195 | collectionName: url.match(/collections\/(\w+)/)?.[1], |
196 | 196 | }; |
197 | 197 | }; |
| 198 | + |
| 199 | +const getSearchParamsFromTagFilters = ( |
| 200 | + searchFilters: Partial<TagSearchFilters> |
| 201 | +) => { |
| 202 | + // Prepare the parameters for the search as a map first. This makes them a bit |
| 203 | + // more readable, since these parameters will all need to be wrapped with with |
| 204 | + // "tag_search[]" in the URL. |
| 205 | + const parameters = { |
| 206 | + name: searchFilters.tagName ?? "", |
| 207 | + fandoms: searchFilters.fandoms?.join(",") ?? "", |
| 208 | + type: searchFilters.type?.toLowerCase() ?? "", |
| 209 | + wrangling_status: |
| 210 | + searchFilters.wranglingStatus |
| 211 | + // We remove the _or_ and _and_ that we added for readability |
| 212 | + // so that the values match the expected values for the API. |
| 213 | + ?.replaceAll("_or_", "_") |
| 214 | + .replaceAll("_and_", "_") ?? "any", |
| 215 | + sort_column: |
| 216 | + searchFilters.sortColumn === "works_count" |
| 217 | + ? "uses" |
| 218 | + : searchFilters.sortColumn ?? "name", |
| 219 | + sort_direction: searchFilters.sortDirection ?? "asc", |
| 220 | + }; |
| 221 | + |
| 222 | + const searchParams = new URLSearchParams(); |
| 223 | + if (searchFilters.page) { |
| 224 | + searchParams.set("page", String(searchFilters.page)); |
| 225 | + } |
| 226 | + searchParams.set("commit", "Search Tags"); |
| 227 | + |
| 228 | + // Now add the parameters to the search params, wrapped with "tag_search[]" |
| 229 | + for (const [key, value] of Object.entries(parameters)) { |
| 230 | + searchParams.set(`tag_search[${key}]`, value); |
| 231 | + } |
| 232 | + |
| 233 | + return searchParams; |
| 234 | +}; |
| 235 | + |
| 236 | +export const getSearchUrlFromTagFilters = (searchFilters: TagSearchFilters) => { |
| 237 | + const url = new URL(`tags/search`, getArchiveBaseUrl()); |
| 238 | + url.search = getSearchParamsFromTagFilters(searchFilters).toString(); |
| 239 | + return url.href; |
| 240 | +}; |
0 commit comments