|
4 | 4 | import FilterStoreDebug from './lib/components/FilterStoreDebug.svelte'; |
5 | 5 | import Tooltip from './lib/components/Tooltip.svelte'; |
6 | 6 | import Scene0Hero from './lib/scenes/Scene0Hero.svelte'; |
7 | | - import Scene1Mediums from './lib/scenes/Scene1Mediums.svelte'; |
8 | | - import Scene2Globe from './lib/scenes/Scene2Globe.svelte'; |
9 | | - import Scene3Gender from './lib/scenes/Scene3Gender.svelte'; |
10 | | - import Scene4Explorer from './lib/scenes/Scene4Explorer.svelte'; |
11 | | - import Scene5Quiz from './lib/scenes/Scene5Quiz.svelte'; |
12 | | - import Scene6Footer from './lib/scenes/Scene6Footer.svelte'; |
13 | 7 | import { initFilterHashSync } from './lib/stores/filters.js'; |
14 | 8 |
|
15 | 9 | let summary = $state(null); |
|
21 | 15 | let countrySummary = $state([]); |
22 | 16 | let genderByCountry = $state([]); |
23 | 17 | let mediumBreakdown = $state([]); |
| 18 | + let sceneModules = $state(null); |
24 | 19 | let showFilterDebug = $state(false); |
25 | 20 | let showComponentDemo = $state(false); |
26 | 21 |
|
| 22 | + function deferStoryLoad(callback) { |
| 23 | + let done = false; |
| 24 | + const events = ['wheel', 'touchstart', 'keydown', 'pointerdown']; |
| 25 | + const cleanup = () => events.forEach((event) => window.removeEventListener(event, run)); |
| 26 | + const run = () => { |
| 27 | + if (done) return; |
| 28 | + done = true; |
| 29 | + window.clearTimeout(timeoutId); |
| 30 | + cleanup(); |
| 31 | + window.setTimeout(callback, 0); |
| 32 | + }; |
| 33 | + const timeoutId = window.setTimeout(run, 5500); |
| 34 | + events.forEach((event) => window.addEventListener(event, run, { once: true, passive: true })); |
| 35 | + return () => { |
| 36 | + done = true; |
| 37 | + window.clearTimeout(timeoutId); |
| 38 | + cleanup(); |
| 39 | + }; |
| 40 | + } |
| 41 | +
|
27 | 42 | onMount(() => { |
28 | 43 | const stopHashSync = initFilterHashSync(); |
29 | 44 | showFilterDebug = import.meta.env.DEV && window.location.pathname.endsWith('/filters'); |
30 | 45 | showComponentDemo = import.meta.env.DEV && window.location.pathname.endsWith('/components'); |
31 | 46 | let cancelled = false; |
32 | 47 | const files = [ |
33 | | - 'summary', |
34 | 48 | 'gender_by_decade', |
35 | 49 | 'timeline', |
36 | 50 | 'medium_totals', |
|
41 | 55 | 'medium_breakdown', |
42 | 56 | ]; |
43 | 57 |
|
44 | | - Promise.all(files.map((file) => fetch(`./data/${file}.json`).then((response) => response.json()))).then( |
45 | | - (results) => { |
46 | | - if (cancelled) return; |
47 | | - [ |
48 | | - summary, |
49 | | - genderByDecade, |
50 | | - timelineData, |
51 | | - mediumTotals, |
52 | | - countryByDecade, |
53 | | - countrySummary, |
54 | | - genderByCountry, |
55 | | - genderByDepartment, |
56 | | - mediumBreakdown, |
57 | | - ] = results; |
58 | | - }, |
59 | | - ); |
| 58 | + fetch('./data/summary.json') |
| 59 | + .then((response) => response.json()) |
| 60 | + .then((result) => { |
| 61 | + if (!cancelled) summary = result; |
| 62 | + }); |
| 63 | +
|
| 64 | + const cancelSceneLoad = deferStoryLoad(async () => { |
| 65 | + const [modules, results] = await Promise.all([ |
| 66 | + Promise.all([ |
| 67 | + import('./lib/scenes/Scene1Mediums.svelte'), |
| 68 | + import('./lib/scenes/Scene2Globe.svelte'), |
| 69 | + import('./lib/scenes/Scene3Gender.svelte'), |
| 70 | + import('./lib/scenes/Scene4Explorer.svelte'), |
| 71 | + import('./lib/scenes/Scene5Quiz.svelte'), |
| 72 | + import('./lib/scenes/Scene6Footer.svelte'), |
| 73 | + ]), |
| 74 | + Promise.all(files.map((file) => fetch(`./data/${file}.json`).then((response) => response.json()))), |
| 75 | + ]); |
| 76 | + if (cancelled) return; |
| 77 | + sceneModules = { |
| 78 | + Scene1Mediums: modules[0].default, |
| 79 | + Scene2Globe: modules[1].default, |
| 80 | + Scene3Gender: modules[2].default, |
| 81 | + Scene4Explorer: modules[3].default, |
| 82 | + Scene5Quiz: modules[4].default, |
| 83 | + Scene6Footer: modules[5].default, |
| 84 | + }; |
| 85 | + [ |
| 86 | + genderByDecade, |
| 87 | + timelineData, |
| 88 | + mediumTotals, |
| 89 | + countryByDecade, |
| 90 | + countrySummary, |
| 91 | + genderByCountry, |
| 92 | + genderByDepartment, |
| 93 | + mediumBreakdown, |
| 94 | + ] = results; |
| 95 | + }); |
60 | 96 |
|
61 | 97 | return () => { |
| 98 | + cancelSceneLoad(); |
62 | 99 | cancelled = true; |
63 | 100 | stopHashSync(); |
64 | 101 | }; |
|
76 | 113 | {:else if summary} |
77 | 114 | <main> |
78 | 115 | <Scene0Hero {summary} /> |
79 | | - <Scene1Mediums data={mediumTotals} /> |
80 | | - <Scene2Globe {countryByDecade} {countrySummary} timeline={timelineData} /> |
81 | | - <Scene3Gender {genderByDecade} {genderByDepartment} {genderByCountry} {countrySummary} timeline={timelineData} /> |
82 | | - <Scene4Explorer data={mediumBreakdown} {countrySummary} timeline={timelineData} /> |
83 | | - <Scene5Quiz /> |
| 116 | + {#if sceneModules} |
| 117 | + {@const Scene1 = sceneModules.Scene1Mediums} |
| 118 | + {@const Scene2 = sceneModules.Scene2Globe} |
| 119 | + {@const Scene3 = sceneModules.Scene3Gender} |
| 120 | + {@const Scene4 = sceneModules.Scene4Explorer} |
| 121 | + {@const Scene5 = sceneModules.Scene5Quiz} |
| 122 | + <Scene1 data={mediumTotals} /> |
| 123 | + <Scene2 {countryByDecade} {countrySummary} timeline={timelineData} /> |
| 124 | + <Scene3 {genderByDecade} {genderByDepartment} {genderByCountry} {countrySummary} timeline={timelineData} /> |
| 125 | + <Scene4 data={mediumBreakdown} {countrySummary} timeline={timelineData} /> |
| 126 | + <Scene5 /> |
| 127 | + {:else} |
| 128 | + <section class="story-loading" aria-live="polite"> |
| 129 | + <p>Preparing the story...</p> |
| 130 | + </section> |
| 131 | + {/if} |
84 | 132 | </main> |
85 | | - <Scene6Footer /> |
| 133 | + {#if sceneModules} |
| 134 | + {@const Scene6 = sceneModules.Scene6Footer} |
| 135 | + <Scene6 /> |
| 136 | + {/if} |
86 | 137 | {:else} |
87 | 138 | <div class="loading"> |
88 | 139 | <div class="loading-spinner"></div> |
|
109 | 160 | animation: spin 800ms linear infinite; |
110 | 161 | } |
111 | 162 |
|
| 163 | + .story-loading { |
| 164 | + min-height: 40vh; |
| 165 | + display: grid; |
| 166 | + place-items: center; |
| 167 | + color: var(--fg-on-light-mute); |
| 168 | + background: var(--bg-light); |
| 169 | + font-family: var(--font-ui); |
| 170 | + } |
| 171 | +
|
112 | 172 | @keyframes spin { |
113 | 173 | to { |
114 | 174 | rotate: 360deg; |
|
0 commit comments