|
1 | 1 | <!-- NOT YET MIGRATED TO NEW WIDGET SYSTEM --> |
2 | 2 |
|
3 | 3 | <script lang="ts"> |
4 | | - import { onMount } from 'svelte'; |
5 | | - import { dev } from '$app/environment'; |
6 | 4 | import Widget from '../../common/ModalWidget.svelte'; |
7 | 5 |
|
8 | 6 | interface CpuInfo { |
|
18 | 16 |
|
19 | 17 | interface Props { |
20 | 18 | cpu: CpuInfo; |
| 19 | + cpuMoreInfo: Promise<any>; |
21 | 20 | } |
22 | 21 |
|
23 | 22 | let { |
24 | | - cpu |
| 23 | + cpu, |
| 24 | + cpuMoreInfo |
25 | 25 | }: Props = $props(); |
26 | 26 |
|
27 | | - async function cpuLookup() { |
28 | | - let response; |
29 | | - if (dev) { |
30 | | - console.info('Trying local server for hwapi'); |
31 | | - try { |
32 | | - response = await ( |
33 | | - await fetch( |
34 | | - `http://localhost:3000/api/cpus/?name=${encodeURIComponent(cpu.Name)}`, |
35 | | - { |
36 | | - method: 'GET', |
37 | | - mode: 'cors' |
38 | | - } |
39 | | - ) |
40 | | - ).json(); |
41 | | - } catch (e) { |
42 | | - console.warn( |
43 | | - 'Could not connect to local hwapi instance, falling back to spec-ify.com' |
44 | | - ); |
45 | | - } |
46 | | - } |
47 | | - if (!response) { |
48 | | - response = await ( |
49 | | - await fetch(`https://spec-ify.com/api/cpus/?name=${encodeURIComponent(cpu.Name)}`, { |
50 | | - method: 'GET', |
51 | | - mode: 'cors' |
52 | | - }) |
53 | | - ).json(); |
54 | | - } |
55 | | - // update the title element to reflect the name fetched from the database |
56 | | - let infoTitle = document.getElementById('cpu-info-title'); |
57 | | - if (infoTitle) { |
58 | | - infoTitle.innerHTML = infoTitle.innerHTML.slice(0, -3) + response.name; |
59 | | - } |
60 | | - |
61 | | - let tableContents = ''; |
62 | | - // add new elements to the table for every kv in the database |
63 | | - for (const [key, value] of Object.entries(response.attributes)) { |
64 | | - tableContents += `<tr><td>${key}</td><td>${value}</td></tr>`; |
65 | | - } |
66 | | - // cpuTable.innerHTML = tableContents; |
67 | | - let fetchedTitle = document.getElementById('fetched-cpu-info'); |
68 | | - if (fetchedTitle) { |
69 | | - fetchedTitle.innerHTML = tableContents; |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - onMount(() => { |
74 | | - const observer = new IntersectionObserver((entries) => { |
75 | | - if (entries[0].isIntersecting) { |
76 | | - cpuLookup().then(); |
77 | | - unobserve(); |
78 | | - } |
79 | | - }); |
80 | | - |
81 | | - let cpuDatabase = document.getElementById('cpu-info-title'); |
82 | | - if (cpuDatabase) { |
83 | | - observer.observe(cpuDatabase); |
84 | | - } |
85 | | - |
86 | | - function unobserve() { |
87 | | - if (cpuDatabase) { |
88 | | - observer.unobserve(cpuDatabase); |
89 | | - } |
90 | | - } |
91 | | - }); |
92 | 27 | </script> |
93 | 28 |
|
94 | 29 | <!-- CPU --> |
|
131 | 66 | </table> |
132 | 67 | {/snippet} |
133 | 68 |
|
134 | | - <!-- <div slot="extras" class="modal-body" id="cpu-modal-info-table" style="display:none;"> |
135 | | - <h6 class="modal-title" id="cpu-info-title">Database results for: ...</h6> |
136 | | - <table class="table"> |
137 | | - <tbody id="fetched-cpu-info"> </tbody> |
138 | | - </table> |
139 | | - </div> --> |
| 69 | + {#snippet extraModalContents()} |
| 70 | + {#await cpuMoreInfo} |
| 71 | + <h6 class="modal-title">Database results for: ...</h6> |
| 72 | + {:then response} |
| 73 | + <h6 class="modal-title">Database results for: {response.name}</h6> |
| 74 | + <table class="table"> |
| 75 | + <tbody> |
| 76 | + {#each Object.entries(response.attributes) as [key, value]} |
| 77 | + <tr> |
| 78 | + <td>{key}</td> |
| 79 | + <td>{value}</td> |
| 80 | + </tr> |
| 81 | + {/each} |
| 82 | + </tbody> |
| 83 | + </table> |
| 84 | + {/await} |
| 85 | + {/snippet} |
| 86 | + |
140 | 87 | </Widget> |
141 | 88 |
|
142 | 89 | <style> |
|
0 commit comments