|
7 | 7 | const urlParameter = new URLSearchParams(location.search); |
8 | 8 | const parsedUrlParameter = parseUrl(urlParameter.get('url'), 'https://github.com').href; |
9 | 9 | // Parse partial URL in the parameter so that it's shown as full URL in the field |
10 | | - let urlField = parsedUrlParameter || ''; |
| 10 | + let urlField = $state(parsedUrlParameter || ''); |
11 | 11 |
|
12 | 12 | const allUrls = [...getAllUrls()].sort(); |
13 | 13 |
|
14 | | - let parsedUrl; |
15 | 14 | // Do not use ?? because it should work on empty strings |
16 | | - $: parsedUrl = parseUrl(urlField || defaultUrl); |
| 15 | + let parsedUrl = $derived(parseUrl(urlField || defaultUrl)); |
17 | 16 |
|
18 | | - let detections = []; |
19 | | - $: { |
20 | | - if (parsedUrl) { |
21 | | - if (urlField) { |
22 | | - urlParameter.set('url', urlField.replace('https://github.com', '')); |
23 | | - history.replaceState(null, '', `?${urlParameter}`); |
24 | | - } else { |
25 | | - history.replaceState(null, '', location.pathname); |
26 | | - } |
27 | | - detections = Object.entries(urlDetection) |
28 | | - .map(([name, detect]) => { |
29 | | - if (typeof detect !== 'function') { |
30 | | - return; |
31 | | - } |
| 17 | + let detections = $derived( |
| 18 | + parsedUrl ? Object.entries(urlDetection) |
| 19 | + .map(([name, detect]) => { |
| 20 | + if (typeof detect !== 'function') { |
| 21 | + return; |
| 22 | + } |
32 | 23 |
|
33 | | - if (!String(detect).startsWith('()')) { |
34 | | - return { |
35 | | - name, |
36 | | - detect, |
37 | | - result: detect(parsedUrl) |
38 | | - }; |
39 | | - } else { |
40 | | - return {name}; |
41 | | - } |
42 | | - }) |
43 | | - .filter(Boolean) |
44 | | - .sort((a, b) => { |
45 | | - // Pull true values to the top |
46 | | - if (a.result || b.result) { |
47 | | - return a.result ? b.result ? 0 : -1 : 1; |
48 | | - } |
| 24 | + if (!String(detect).startsWith('()')) { |
| 25 | + return { |
| 26 | + name, |
| 27 | + detect, |
| 28 | + result: detect(parsedUrl) |
| 29 | + }; |
| 30 | + } else { |
| 31 | + return {name}; |
| 32 | + } |
| 33 | + }) |
| 34 | + .filter(Boolean) |
| 35 | + .sort((a, b) => { |
| 36 | + // Pull true values to the top |
| 37 | + if (a.result || b.result) { |
| 38 | + return a.result ? b.result ? 0 : -1 : 1; |
| 39 | + } |
49 | 40 |
|
50 | | - // Push false values to the top |
51 | | - if (a.detect || b.detect) { |
52 | | - return a.detect ? b.detect ? 0 : 1 : -1; |
53 | | - } |
| 41 | + // Push false values to the top |
| 42 | + if (a.detect || b.detect) { |
| 43 | + return a.detect ? b.detect ? 0 : 1 : -1; |
| 44 | + } |
54 | 45 |
|
55 | | - // DOM-based detections should be in the middle |
56 | | - }); |
| 46 | + // DOM-based detections should be in the middle |
| 47 | + }) : [] |
| 48 | + ); |
| 49 | +
|
| 50 | + $effect(() => { |
| 51 | + if (!parsedUrl) return; |
| 52 | +
|
| 53 | + if (urlField) { |
| 54 | + urlParameter.set('url', urlField.replace('https://github.com', '')); |
| 55 | + history.replaceState(null, '', `?${urlParameter}`); |
| 56 | + } else { |
| 57 | + history.replaceState(null, '', location.pathname); |
57 | 58 | } |
58 | | - } |
| 59 | + }); |
59 | 60 | </script> |
60 | 61 |
|
61 | 62 | <style> |
|
0 commit comments