atmosphere explorer pds.ls
tool typescript atproto

refactor labels

handle.invalid 1bb22c59 bb9374d3

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