MCPcopy
hub / github.com/jwngr/sdow / PageInput

Function PageInput

website/src/components/PageInput.tsx:106–192  ·  view source on GitHub ↗
({title, setTitle, placeholderText, setPlaceholderText})

Source from the content-addressed store, hash-verified

104 readonly placeholderText: string;
105 readonly setPlaceholderText: (placeholderText: string) => void;
106}> = ({title, setTitle, placeholderText, setPlaceholderText}) => {
107 const [suggestions, setSuggestions] = useState<readonly PageSuggestion[]>([]);
108
109 // Update placeholder text every 5 seconds when the title is empty.
110 useEffect(() => {
111 if (title !== '') return;
112
113 const intervalId = setInterval(() => {
114 setPlaceholderText(getRandomPageTitle());
115 }, 5_000);
116
117 return () => clearInterval(intervalId);
118 }, [setPlaceholderText, title]);
119
120 const loadSuggestions = useCallback(async (query: string) => {
121 const url = new URL(WIKIPEDIA_API_URL);
122 url.search = new URLSearchParams({
123 action: 'query',
124 format: 'json',
125 gpssearch: query,
126 generator: 'prefixsearch',
127 prop: 'pageprops|pageimages|pageterms',
128 redirects: '',
129 ppprop: 'displaytitle',
130 piprop: 'thumbnail',
131 pithumbsize: '160',
132 pilimit: '30',
133 wbptterms: 'description',
134 gpsnamespace: '0',
135 gpslimit: '5',
136 origin: '*',
137 }).toString();
138
139 try {
140 const response = await fetch(url, {
141 method: 'GET',
142 headers: {'Api-User-Agent': SDOW_USER_AGENT},
143 });
144
145 const data = await response.json();
146
147 const pageResults = get(data, 'query.pages', {});
148 const newSuggestions: PageSuggestion[] = [];
149 forEach(pageResults, (all) => {
150 const {ns, id, title, terms, thumbnail} = all;
151 if (ns === 0) {
152 let description = get(terms, 'description.0', '');
153 description = description.charAt(0).toUpperCase() + description.slice(1);
154 newSuggestions.push({id, title, description, thumbnailUrl: get(thumbnail, 'source')});
155 }
156 });
157 setSuggestions(filter(newSuggestions));
158 } catch (error) {
159 const defaultErrorMessage = 'Failed to fetch page suggestions from Wikipedia API.';
160 // eslint-disable-next-line @typescript-eslint/no-explicit-any
161 (window as any).ga('send', 'exception', {
162 exDescription: get(error, 'response.data.error', defaultErrorMessage),
163 exFatal: false,

Callers

nothing calls this directly

Calls 1

getRandomPageTitleFunction · 0.90

Tested by

no test coverage detected