atmosphere explorer pds.ls
tool typescript atproto

refactor labels

handle.invalid 1bb22c59 bb9374d3

verified
+18 -29
+18 -29
src/views/labels.tsx
··· 14 14 import { localDateFromTimestamp } from "../utils/date.js"; 15 15 16 16 const LABELS_PER_PAGE = 50; 17 + const DEFAULT_LABELER_DID = "did:plc:ar7c4by46qjdydhdevvrndac"; 17 18 18 19 const LabelCard = (props: { label: ComAtprotoLabelDefs.Label }) => { 19 20 const label = props.label; ··· 76 77 .map((f) => f.trim()) 77 78 .filter((f) => f.length > 0); 78 79 79 - const exclusions: { pattern: string; hasWildcard: boolean }[] = []; 80 - const inclusions: { pattern: string; hasWildcard: boolean }[] = []; 80 + const toMatcher = (pattern: string): ((value: string) => boolean) => { 81 + if (pattern.includes("*")) { 82 + const regexPattern = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*"); 83 + const regex = new RegExp(`^${regexPattern}$`); 84 + return (value) => regex.test(value); 85 + } 86 + return (value) => value === pattern; 87 + }; 88 + 89 + const exclusions: ((value: string) => boolean)[] = []; 90 + const inclusions: ((value: string) => boolean)[] = []; 81 91 82 92 filters.forEach((f) => { 83 93 if (f.startsWith("-")) { 84 - const lower = f.slice(1).toLowerCase(); 85 - exclusions.push({ 86 - pattern: lower, 87 - hasWildcard: lower.includes("*"), 88 - }); 94 + exclusions.push(toMatcher(f.slice(1).toLowerCase())); 89 95 } else { 90 - const lower = f.toLowerCase(); 91 - inclusions.push({ 92 - pattern: lower, 93 - hasWildcard: lower.includes("*"), 94 - }); 96 + inclusions.push(toMatcher(f.toLowerCase())); 95 97 } 96 98 }); 97 99 98 - const matchesPattern = (value: string, filter: { pattern: string; hasWildcard: boolean }) => { 99 - if (filter.hasWildcard) { 100 - // Convert wildcard pattern to regex 101 - const regexPattern = filter.pattern 102 - .replace(/[.+?^${}()|[\]\\]/g, "\\$&") // Escape special regex chars except * 103 - .replace(/\*/g, ".*"); // Replace * with .* 104 - const regex = new RegExp(`^${regexPattern}$`); 105 - return regex.test(value); 106 - } else { 107 - return value === filter.pattern; 108 - } 109 - }; 110 - 111 100 return labels().filter((label) => { 112 101 const labelValue = label.val.toLowerCase(); 113 102 114 - if (exclusions.some((exc) => matchesPattern(labelValue, exc))) { 103 + if (exclusions.some((exc) => exc(labelValue))) { 115 104 return false; 116 105 } 117 106 118 - // If there are inclusions, at least one must match 119 107 if (inclusions.length > 0) { 120 - return inclusions.some((inc) => matchesPattern(labelValue, inc)); 108 + return inclusions.some((inc) => inc(labelValue)); 121 109 } 122 110 123 111 // If only exclusions were specified, include everything not excluded ··· 137 125 }); 138 126 139 127 const fetchLabels = async (formData: FormData, reset?: boolean) => { 140 - let did = formData.get("did")?.toString()?.trim() || "did:plc:ar7c4by46qjdydhdevvrndac"; 128 + let did = formData.get("did")?.toString()?.trim() || DEFAULT_LABELER_DID; 141 129 const uriPatterns = formData.get("uriPatterns")?.toString()?.trim(); 142 130 143 131 if (!did || !uriPatterns) { ··· 170 158 uriPatterns: uriPatterns.split(",").map((p) => p.trim()), 171 159 sources: [did as `did:${string}:${string}`], 172 160 cursor: cursor(), 161 + limit: LABELS_PER_PAGE, 173 162 }, 174 163 }); 175 164