From 837243313be3d8438c4855fb488cf52e4ac2d09e Mon Sep 17 00:00:00 2001 From: Kavi Gupta Date: Sun, 11 Aug 2024 22:58:35 -0700 Subject: [PATCH] use context for universe rather than passing it through (#285) --- react/src/article.js | 8 +- react/src/comparison.js | 12 +- react/src/components/article-panel.js | 40 +++--- react/src/components/comparison-panel.js | 24 ++-- react/src/components/header.tsx | 42 +++--- react/src/components/map.tsx | 9 +- react/src/components/mapper-panel.js | 2 - react/src/components/related-button.tsx | 12 +- react/src/components/statistic-panel.js | 156 ++++++++++++++--------- react/src/components/table.tsx | 36 +++--- react/src/page_template/template.abc | 64 ++++++++++ react/src/page_template/template.js | 18 +-- react/src/quiz/quiz-question.js | 2 - react/src/statistic.js | 41 +++--- react/src/universe.ts | 6 + 15 files changed, 280 insertions(+), 192 deletions(-) create mode 100644 react/src/page_template/template.abc diff --git a/react/src/article.js b/react/src/article.js index cb4c1ae3..51aee19e 100644 --- a/react/src/article.js +++ b/react/src/article.js @@ -7,7 +7,7 @@ import { data_link } from "./navigation/links"; import { ArticlePanel } from './components/article-panel'; import { loadProtobuf } from './load_json'; -import { default_article_universe, get_universe, remove_universe_if_default, remove_universe_if_not_in } from './universe'; +import { default_article_universe, get_universe, remove_universe_if_default, remove_universe_if_not_in, UNIVERSE_CONTEXT } from './universe'; async function loadPage() { @@ -20,7 +20,11 @@ async function loadPage() { remove_universe_if_not_in(data.universes) const default_universe = default_article_universe(longname); remove_universe_if_default(default_universe); - root.render(); + root.render( + + + + ); } loadPage(); \ No newline at end of file diff --git a/react/src/comparison.js b/react/src/comparison.js index d0362398..5f08315d 100644 --- a/react/src/comparison.js +++ b/react/src/comparison.js @@ -7,7 +7,7 @@ import { data_link } from "./navigation/links"; import { loadProtobuf } from './load_json'; import { ComparisonPanel } from './components/comparison-panel.js'; -import { default_comparison_universe, get_universe, remove_universe_if_default, remove_universe_if_not_in } from './universe'; +import { default_comparison_universe, get_universe, remove_universe_if_default, remove_universe_if_not_in, UNIVERSE_CONTEXT } from './universe'; async function loadPage() { @@ -24,11 +24,11 @@ async function loadPage() { remove_universe_if_not_in(universes) const default_universe = default_comparison_universe(names); remove_universe_if_default(default_universe); - root.render(); + root.render( + + + + ); } loadPage(); \ No newline at end of file diff --git a/react/src/components/article-panel.js b/react/src/components/article-panel.js index 8b187e05..201b6d79 100644 --- a/react/src/components/article-panel.js +++ b/react/src/components/article-panel.js @@ -12,7 +12,7 @@ import { load_article } from './load-article'; import { comparisonHeadStyle, headerTextClass, subHeaderTextClass } from '../utils/responsive'; import { SearchBox } from './search'; import { article_link, comparison_link, sanitize } from '../navigation/links'; -import { longname_is_exclusively_american } from '../universe'; +import { longname_is_exclusively_american, useUniverse } from '../universe'; import { useSetting, useTableCheckboxSettings } from '../page_template/settings'; class ArticlePanel extends PageTemplate { @@ -39,9 +39,8 @@ class ArticlePanel extends PageTemplate {
- + @@ -55,7 +54,6 @@ class ArticlePanel extends PageTemplate { related={this.props.related} article_type={this.props.articleType} basemap={{ type: "osm" }} - universe={this.state.current_universe} />
@@ -66,15 +64,7 @@ class ArticlePanel extends PageTemplate {
Compare to:
- { - document.location.href = comparison_link( - this.state.current_universe, - [this.props.longname, x]); - }} - /> +
@@ -83,7 +73,6 @@ class ArticlePanel extends PageTemplate { ); @@ -106,23 +95,36 @@ class ArticlePanel extends PageTemplate { } } -function StatisticRowHeader(props) { +function ComparisonSearchBox(props) { + const curr_universe = useUniverse(); + return { + document.location.href = comparison_link( + curr_universe, + [props.longname, x]); + }} + /> +} + +function StatisticRowHeader() { const [simple_ordinals, _] = useSetting("simple_ordinals"); - return + return } function ArticlePanelRows(props) { + const curr_universe = useUniverse(); const settings = useTableCheckboxSettings(); const [simple_ordinals, _set_simple_ordinals] = useSetting("simple_ordinals"); - const [filtered_rows, _] = load_article(props.current_universe, props.article_row, settings, + const [filtered_rows, _] = load_article(curr_universe, props.article_row, settings, longname_is_exclusively_american(props.longname)); return <> {filtered_rows.map((row, i) => { document.location = article_link(props.current_universe, x) }} + onReplace={x => { document.location = article_link(curr_universe, x) }} simple={simple_ordinals} longname={props.longname} - universe={props.current_universe} />)} } \ No newline at end of file diff --git a/react/src/components/comparison-panel.js b/react/src/components/comparison-panel.js index 4425e4a6..246f676b 100644 --- a/react/src/components/comparison-panel.js +++ b/react/src/components/comparison-panel.js @@ -12,7 +12,7 @@ import { comparisonHeadStyle, headerTextClass, mobileLayout, subHeaderTextClass import { SearchBox } from './search'; import { article_link, sanitize } from '../navigation/links'; import { lighten } from '../utils/color'; -import { longname_is_exclusively_american } from '../universe'; +import { longname_is_exclusively_american, useUniverse } from '../universe'; import { useTableCheckboxSettings } from '../page_template/settings'; const main_columns = ["statval", "statval_unit", "statistic_ordinal", "statistic_percentile"]; @@ -57,9 +57,6 @@ class ComparisonPanel extends PageTemplate { main_content() { const self = this; - if (this.state.current_universe == undefined) { - throw new Error("ComparisonPanel: current_universe not set"); - } if (this.props.names == undefined) { throw new Error("ComparisonPanel: names not set"); } @@ -100,7 +97,6 @@ class ComparisonPanel extends PageTemplate { on_click={() => on_delete(self.props.names, i)} on_change={(x) => on_change(self.props.names, i, x)} screenshot_mode={this.state.screenshot_mode} - universe={this.state.current_universe} /> ) )} @@ -110,7 +106,6 @@ class ComparisonPanel extends PageTemplate { )} @@ -123,7 +118,6 @@ class ComparisonPanel extends PageTemplate { id="map_combined" article_type={undefined} basemap={{ type: "osm" }} - universe={this.state.current_universe} /> @@ -222,12 +216,13 @@ function all_data_types_same(datas) { } -function ComparsionPageRows({ names, datas, current_universe }) { +function ComparsionPageRows({ names, datas }) { + const curr_universe = useUniverse(); var rows = []; var idxs = []; const exclusively_american = datas.every(x => longname_is_exclusively_american(x.longname)); for (let i in datas) { - const [r, idx] = load_article(current_universe, datas[i], useTableCheckboxSettings(), + const [r, idx] = load_article(curr_universe, datas[i], useTableCheckboxSettings(), exclusively_american); rows.push(r); idxs.push(idx); @@ -238,7 +233,6 @@ function ComparsionPageRows({ names, datas, current_universe }) { const header_row = { return { is_header: true } }} datas={datas} - current_universe={current_universe} names={names} />; const render_rows = rows[0].map((_, row_idx) => @@ -249,7 +243,6 @@ function ComparsionPageRows({ names, datas, current_universe }) { } }} datas={datas} - current_universe={current_universe} names={names} /> ); @@ -266,7 +259,7 @@ function ComparsionPageRows({ names, datas, current_universe }) { ) } -function ComparisonRow({ names, params, datas, current_universe }) { +function ComparisonRow({ names, params, datas }) { if (names == undefined) { throw new Error("ComparisonRow: names is undefined"); } @@ -300,7 +293,6 @@ function ComparisonRow({ names, params, datas, current_universe }) { row_overall.push(...StatisticRowRawCellContents( { ...param_vals[0], only_columns: ["statname"], _idx: -1, simple: true, longname: datas[0].longname, - universe: current_universe, total_width: 100 * (left_margin_pct - left_bar_margin) } )); @@ -312,7 +304,6 @@ function ComparisonRow({ names, params, datas, current_universe }) { ...param_vals[i], only_columns: only_columns, _idx: i, simple: true, statistic_style: highlight_idx == i ? { backgroundColor: lighten(color(i), 0.7) } : {}, onReplace: x => on_change(names, i, x), - universe: current_universe, total_width: each(datas) } )); @@ -339,9 +330,10 @@ function ManipulationButton({ color, on_click, text }) { } -function HeadingDisplay({ universe, longname, include_delete, on_click, on_change, screenshot_mode }) { +function HeadingDisplay({ longname, include_delete, on_click, on_change, screenshot_mode }) { const [is_editing, set_is_editing] = React.useState(false); + const curr_universe = useUniverse(); const manipulation_buttons =
@@ -359,7 +351,7 @@ function HeadingDisplay({ universe, longname, include_delete, on_click, on_chang return
{screenshot_mode ? undefined : manipulation_buttons}
-
{longname}
+
{longname}
{is_editing ? void, has_universe_selector: boolean, - current_universe: string, all_universes: string[], - on_universe_update: (universe: string) => void, has_screenshot: boolean, screenshot_mode: boolean, - initiate_screenshot: () => void + initiate_screenshot: (curr_universe: string) => void }) { + const curr_universe = useUniverse(); return (
{/* flex but stretch to fill */} @@ -38,9 +36,7 @@ export function Header(props: { {!mobileLayout() && props.has_universe_selector ?
: undefined} @@ -48,7 +44,7 @@ export function Header(props: { props.has_screenshot ? props.initiate_screenshot(curr_universe)} /> : undefined }
@@ -57,7 +53,7 @@ export function Header(props: { on_change={ new_location => { window.location.href = article_link( - props.current_universe, new_location + curr_universe, new_location ) } } @@ -83,9 +79,7 @@ function TopLeft(props: { hamburger_open: boolean, set_hamburger_open: (newValue: boolean) => void, has_universe_selector: boolean, - current_universe: string, all_universes: string[], - on_universe_update: (universe: string) => void }) { if (mobileLayout()) { return ( @@ -95,9 +89,7 @@ function TopLeft(props: { { props.has_universe_selector ? : } @@ -122,9 +114,10 @@ function HeaderImage() { } function UniverseSelector( - { current_universe, all_universes, on_universe_update } - : { current_universe: string, all_universes: string[], on_universe_update: (universe: string) => void } + { all_universes } + : { all_universes: string[] } ) { + const curr_universe = useUniverse(); // button to select universe. Image is icons/flags/${universe}.png // when clicked, a dropdown appears with all universes, labeled by their flags @@ -135,10 +128,7 @@ function UniverseSelector( let dropdown = dropdown_open ? { - set_dropdown_open(false); - on_universe_update(universe); - }} /> : undefined; + /> : undefined; // wrap dropdown in a div to place it in front of everything else and let it spill out of the header // do NOT use class @@ -165,7 +155,7 @@ function UniverseSelector( alignItems: "center", } }> - {current_universe} set_dropdown_open(!dropdown_open)} /> @@ -176,8 +166,8 @@ function UniverseSelector( } function UniverseDropdown( - { all_universes, on_universe_update, flag_size } - : { all_universes: string[], on_universe_update: (universe: string) => void, flag_size: string } + { all_universes, flag_size } + : { all_universes: string[], flag_size: string } ) { return (
@@ -188,9 +178,9 @@ function UniverseDropdown( > Select universe for statistics
- {all_universes.map(universe => { + {all_universes.map(alt_universe => { return ( -
on_universe_update(universe)}> +
set_universe(alt_universe)}>
- {universe}
- {universe == "world" ? "World" : universe} + {alt_universe == "world" ? "World" : alt_universe}
diff --git a/react/src/components/map.tsx b/react/src/components/map.tsx index ef7802c2..ba3ec504 100644 --- a/react/src/components/map.tsx +++ b/react/src/components/map.tsx @@ -14,12 +14,12 @@ import { NormalizeProto } from "../utils/types"; import { Feature, IRelatedButton, IRelatedButtons } from "../utils/protos"; import { Basemap } from "../mapper/settings"; import { relationship_key, useRelatedCheckboxSettings, useSetting } from '../page_template/settings'; +import { UNIVERSE_CONTEXT } from '../universe'; export interface MapGenericProps { height?: string, id: string, basemap: Basemap, - universe: string, } class MapGeneric

extends React.Component

{ @@ -291,7 +291,7 @@ class MapGeneric

extends React.Component

{ }); if (add_callback) { polygon = polygon.on("click", () => { - window.location.href = article_link(this.props.universe, name); + window.location.href = article_link(this.context!, name); }); } @@ -318,13 +318,16 @@ class MapGeneric

extends React.Component

{ console.log("zoom to", name); this.map!.fitBounds(this.polygon_by_name.get(name)!.getBounds()); } + + static contextType = UNIVERSE_CONTEXT; + + declare context: React.ContextType; } interface MapProps extends MapGenericProps { longname: string, related: NormalizeProto[], article_type: string, - universe: string, } interface ArticleMapProps extends MapProps { diff --git a/react/src/components/mapper-panel.js b/react/src/components/mapper-panel.js index ba3ab7e2..11cebe88 100644 --- a/react/src/components/mapper-panel.js +++ b/react/src/components/mapper-panel.js @@ -180,7 +180,6 @@ function MapComponent(props) { line_style={props.line_style} basemap={props.basemap} height={props.height} - universe={props.universe} />

@@ -367,7 +366,6 @@ class MapperPanel extends PageTemplate { line_style={this.state.map_settings.line_style} basemap={this.state.map_settings.basemap} height={height} - universe={this.state.current_universe} /> } diff --git a/react/src/components/related-button.tsx b/react/src/components/related-button.tsx index acb79523..fe0cbff4 100644 --- a/react/src/components/related-button.tsx +++ b/react/src/components/related-button.tsx @@ -8,6 +8,7 @@ import "./related.css"; import { mobileLayout } from '../utils/responsive'; import { lighten } from '../utils/color'; import { useSetting, relationship_key } from '../page_template/settings'; +import { useUniverse } from '../universe'; const type_ordering_idx = require("../data/type_ordering_idx.json"); @@ -38,8 +39,9 @@ const colorsEach: Record = { "Native": GREEN, }; -function RelatedButton(props: { region: Region, universe: string }) { +function RelatedButton(props: { region: Region }) { + const curr_universe = useUniverse(); const type_category = type_to_type_category[props.region.rowType]; let classes = `serif button_related` @@ -55,13 +57,13 @@ function RelatedButton(props: { region: Region, universe: string }) { {props.region.shortname} + href={article_link(curr_universe, props.region.longname)}>{props.region.shortname} ); } -function RelatedList(props: { articleType: string, buttonType: string, regions: Record, universe: string }) { +function RelatedList(props: { articleType: string, buttonType: string, regions: Record }) { if (props.articleType == undefined) { throw new Error("articleType is undefined; shoud be defined"); } @@ -108,7 +110,6 @@ function RelatedList(props: { articleType: string, buttonType: string, regions: ) } @@ -123,7 +124,7 @@ function RelatedList(props: { articleType: string, buttonType: string, regions: ); } -export function Related(props: { article_type: string, related: { relationshipType: string, buttons: Region[] }[], universe: string }) { +export function Related(props: { article_type: string, related: { relationshipType: string, buttons: Region[] }[] }) { // buttons[rowType][relationshipType] = const [showHistoricalCds] = useSetting("show_historical_cds"); @@ -160,7 +161,6 @@ export function Related(props: { article_type: string, related: { relationshipTy buttonType={key} regions={buttons[key]} articleType={props.article_type} - universe={props.universe} /> ); } diff --git a/react/src/components/statistic-panel.js b/react/src/components/statistic-panel.js index b79171d3..28c17067 100644 --- a/react/src/components/statistic-panel.js +++ b/react/src/components/statistic-panel.js @@ -10,6 +10,7 @@ import { article_link, explanation_page_link, sanitize, statistic_link } from '. import { Percentile, Statistic } from './table'; import { display_type } from '../utils/text'; import { useSetting } from '../page_template/settings'; +import { useUniverse } from '../universe'; const table_style = { display: "flex", flexDirection: "column", padding: "1px" }; const column_names = ["Ordinal", "Name", "Value", "", "Percentile"]; @@ -55,10 +56,10 @@ class StatisticPanel extends PageTemplate { return this.is_ascending() ? "ascending" : "descending"; } - swap_ascending_descending() { + swap_ascending_descending(curr_universe) { var new_order = this.is_ascending() ? "descending" : "ascending"; document.location = statistic_link( - this.state.current_universe, + curr_universe, this.props.statname, this.props.article_type, 1, this.props.amount, new_order, undefined @@ -112,7 +113,10 @@ class StatisticPanel extends PageTemplate {
{this.props.rendered_statname}
{/* // TODO plural */} -
{display_type(this.state.current_universe, this.props.article_type)} ({this.rendered_order()})
+
@@ -121,7 +125,7 @@ class StatisticPanel extends PageTemplate { if (i === 0) { return
{name}
- this.swap_ascending_descending()} is_ascending={this.is_ascending()} /> + this.swap_ascending_descending(curr_universe)} is_ascending={this.is_ascending()} />
} return
{name}
@@ -133,7 +137,7 @@ class StatisticPanel extends PageTemplate { }}>
{i + 1}
- -
- Page: - { - if (e.key === "Enter") { - var new_page = e.target.value; - if (typeof new_page === "string") { - new_page = parseInt(new_page); - } - if (typeof new_page === "number") { - if (new_page < 1) { - new_page = 1; - } - if (new_page > max_pages) { - new_page = max_pages; - } - const new_start = (new_page - 1) * per_page + 1; - self.change_start(new_start); - } - } - }} /> - of {max_pages} -
- -
; + const select_page = self.change_start(curr_universe, new_start)} + current_page={current_page} + max_pages={max_pages} + prev_page={prev} + next_page={next} + per_page={per_page} + /> // align the entire div to the center. not flex. return
-
- per page -
+ self.change_amount(curr_universe, new_amount)} + />
} - change_start(new_start) { + change_start(curr_universe, new_start) { document.location.href = statistic_link( - this.state.current_universe, + curr_universe, this.props.statname, this.props.article_type, new_start, this.props.amount, this.props.order, undefined ); } - change_amount(new_amount) { + change_amount(curr_universe, new_amount) { const new_amount_str = new_amount; var start = this.props.start; if (new_amount === "All") { @@ -274,7 +244,7 @@ class StatisticPanel extends PageTemplate { } if (typeof new_amount === "number") { document.location.href = statistic_link( - this.state.current_universe, + curr_universe, this.props.statname, this.props.article_type, start, @@ -286,6 +256,77 @@ class StatisticPanel extends PageTemplate { } } +function PerPageSelector(props) { + const curr_universe = useUniverse(); + return
+ per page +
+} + +function SelectPage(props) { + // low-key style for the buttons + const button_style = { + backgroundColor: "#f7f1e8", + border: "1px solid #000", + padding: "0 0.5em", + margin: "0.5em" + }; + + const curr_universe = useUniverse(); + return
+ +
+ Page: + { + if (e.key === "Enter") { + var new_page = e.target.value; + if (typeof new_page === "string") { + new_page = parseInt(new_page); + } + if (typeof new_page === "number") { + if (new_page < 1) { + new_page = 1; + } + if (new_page > props.max_pages) { + new_page = props.max_pages; + } + const new_start = (new_page - 1) * props.per_page + 1; + props.change_start(curr_universe, new_start); + } + } + }} /> + of {props.max_pages} +
+ +
+} + +function ArticleLink(props) { + const curr_universe = useUniverse(); + return {props.longname} +} + +function StatisticPanelSubhead(props) { + const curr_universe = useUniverse(); + return
+ {display_type(curr_universe, props.article_type)} ({props.rendered_order}) +
+} + function AutoPercentile(props) { const [simple_ordinals, _] = useSetting("simple_ordinals"); return -
+
on_click(curr_universe)}> {is_ascending ? "▲" : "▼"}
diff --git a/react/src/components/table.tsx b/react/src/components/table.tsx index 48f7a05a..70e25a6c 100644 --- a/react/src/components/table.tsx +++ b/react/src/components/table.tsx @@ -8,6 +8,7 @@ import { is_historical_cd } from '../utils/is_historical'; import { display_type } from '../utils/text'; import { ArticleRow } from './load-article'; import { useSetting } from '../page_template/settings'; +import { useUniverse } from '../universe'; const table_row_style: React.CSSProperties = { display: "flex", @@ -30,17 +31,15 @@ export type StatisticRowRawProps = { } ) -export function StatisticRowRaw(props: StatisticRowRawProps & { index: number, universe: string, longname?: string }) { +export function StatisticRowRaw(props: StatisticRowRawProps & { index: number, longname?: string }) { const cell_contents = StatisticRowRawCellContents({ ...props, total_width: 100 }); return ; } -export function StatisticRowRawCellContents(props: StatisticRowRawProps & { total_width: number, universe: string, longname?: string }) { - if (props.universe == undefined) { - throw "StatisticRowRawCellContents: universe is undefined"; - } +export function StatisticRowRawCellContents(props: StatisticRowRawProps & { total_width: number, longname?: string }) { + const curr_universe = useUniverse(); const alignStyle: React.CSSProperties = { textAlign: props.is_header ? "center" : "right" }; let value_columns: [number, string, React.ReactNode][] = [ [15, @@ -85,7 +84,7 @@ export function StatisticRowRawCellContents(props: StatisticRowRawProps & { tota props.is_header ? "Statistic" : } ], @@ -133,7 +131,6 @@ export function StatisticRowRawCellContents(props: StatisticRowRawProps & { tota statpath={props.statpath} type={props.article_type} total={props.total_count_in_class} - universe={props.universe} /> ], @@ -147,7 +144,6 @@ export function StatisticRowRawCellContents(props: StatisticRowRawProps & { tota statpath={props.statpath} type="overall" total={props.total_count_overall} - universe={props.universe} /> ] @@ -337,10 +333,8 @@ function ElectionResult(props: { value: number }) { return {party}+{text}; } -export function Ordinal(props: { ordinal: number, total: number, type: string, statpath: string, onReplace?: (newValue: string) => void, simple: boolean, universe: string }) { - if (props.universe == undefined) { - throw "Ordinal: universe is undefined"; - } +export function Ordinal(props: { ordinal: number, total: number, type: string, statpath: string, onReplace?: (newValue: string) => void, simple: boolean }) { + const curr_universe = useUniverse(); const onNewNumber = async (number: number) => { let num = number; if (num < 0) { @@ -353,7 +347,7 @@ export function Ordinal(props: { ordinal: number, total: number, type: string, s if (num <= 0) { num = 1; } - const data = await load_ordering(props.universe, props.statpath, props.type); + const data = await load_ordering(curr_universe, props.statpath, props.type); props.onReplace?.(data[num - 1]) } const ordinal = props.ordinal; @@ -370,7 +364,7 @@ export function Ordinal(props: { ordinal: number, total: number, type: string, s return right_align(en); } return
- {en} of {total} {display_type(props.universe, type)} + {en} of {total} {display_type(curr_universe, type)}
; } @@ -428,8 +422,9 @@ export function Percentile(props: { ordinal: number, total: number, percentile_b return
{text}
; } -function PointerButtonsIndex(props: { ordinal: number, statpath: string, type: string, total: number, universe: string }) { - const get_data = async () => await load_ordering(props.universe, props.statpath, props.type); +function PointerButtonsIndex(props: { ordinal: number, statpath: string, type: string, total: number }) { + const curr_universe = useUniverse(); + const get_data = async () => await load_ordering(curr_universe, props.statpath, props.type); const [settings_show_historical_cds] = useSetting("show_historical_cds"); const show_historical_cds = settings_show_historical_cds || is_historical_cd(props.type); return ( @@ -441,7 +436,6 @@ function PointerButtonsIndex(props: { ordinal: number, statpath: string, type: s direction={-1} total={props.total} show_historical_cds={show_historical_cds} - universe={props.universe} /> ); } -function PointerButtonIndex(props: { text: string, get_data: () => Promise, original_pos: number, direction: number, total: number, show_historical_cds: boolean, universe: string }) { +function PointerButtonIndex(props: { text: string, get_data: () => Promise, original_pos: number, direction: number, total: number, show_historical_cds: boolean }) { + const curr_universe = useUniverse(); const out_of_bounds = (pos: number) => pos < 0 || pos >= props.total const onClick = async (pos: number) => { { @@ -467,7 +461,7 @@ function PointerButtonIndex(props: { text: string, get_data: () => Promise React.ReactNode, screencapElements?: ScreencapElements }) { + const [hamburger_open, set_hamburger_open] = useState(false); + const [screenshot_mode, set_screenshot_mode] = useState(false); + + const responsive = useResponsive() + + const initiateScreenshot = () => { + set_screenshot_mode(true) + setTimeout(async () => { + await create_screenshot(props.screencapElements!) + set_screenshot_mode(false) + }) + } + + return ( + + +
+
+
+ +
+
+ ); +} + +function BodyPanel(props: { mainContent: React.ReactNode, hamburger_open: boolean }) { + const responsive = useResponsive() + if (props.hamburger_open) { + return ; + } + return
+} + +function LeftPanel() { + const responsive = useResponsive() + return ( +
+ +
+ ) +} \ No newline at end of file diff --git a/react/src/page_template/template.js b/react/src/page_template/template.js index 56aff563..4491ab6b 100644 --- a/react/src/page_template/template.js +++ b/react/src/page_template/template.js @@ -33,7 +33,6 @@ class PageTemplate extends React.Component { settings: settings, hamburger_open: false, screenshot_mode: false, - current_universe: this.props.universe, } } @@ -41,15 +40,10 @@ class PageTemplate extends React.Component { const self = this; - const set_this_universe = universe => { - self.setState({ current_universe: universe }); - set_universe(universe); - } - - const initiate_screenshot = async () => { + const initiate_screenshot = async curr_universe => { self.setState({ screenshot_mode: true }); setTimeout(async () => { - await self.screencap(); + await self.screencap(curr_universe); self.setState({ screenshot_mode: false }); }) } @@ -63,11 +57,9 @@ class PageTemplate extends React.Component { set_hamburger_open={x => this.setState({ hamburger_open: x })} has_screenshot={this.has_screenshot_button()} has_universe_selector={this.has_universe_selector()} - current_universe={this.state.current_universe} all_universes={this.props.universes} - on_universe_update={set_this_universe} screenshot_mode={this.state.screenshot_mode} - initiate_screenshot={() => initiate_screenshot()} + initiate_screenshot={curr_universe => initiate_screenshot(curr_universe)} />
} @@ -209,7 +208,6 @@ class JuxtastatQuizQuestion extends QuizQuestion { longname={this.props.longname_b} article_type={undefined} basemap={{ type: "osm" }} - universe={undefined} /> } } diff --git a/react/src/statistic.js b/react/src/statistic.js index 45f8e141..05e4d6e9 100644 --- a/react/src/statistic.js +++ b/react/src/statistic.js @@ -7,7 +7,7 @@ import "./common.css"; import { load_ordering_protobuf, load_ordering } from './load_json'; import { StatisticPanel } from './components/statistic-panel.js'; import { for_type, render_statname } from './components/load-article'; -import { get_universe, longname_is_exclusively_american, remove_universe_if_default } from './universe'; +import { get_universe, longname_is_exclusively_american, remove_universe_if_default, UNIVERSE_CONTEXT } from './universe'; async function loadPage() { @@ -42,24 +42,27 @@ async function loadPage() { const root = ReactDOM.createRoot(document.getElementById("root")); const universes = require("./data/universes_ordered.json"); const exclusively_american = article_names.every(longname_is_exclusively_american); - root.render(); + root.render( + + + + ); } loadPage(); \ No newline at end of file diff --git a/react/src/universe.ts b/react/src/universe.ts index 98ee916a..77e17ea1 100644 --- a/react/src/universe.ts +++ b/react/src/universe.ts @@ -1,4 +1,10 @@ +import { createContext, useContext } from "react"; +export const UNIVERSE_CONTEXT = createContext(undefined); + +export function useUniverse(): string { + return useContext(UNIVERSE_CONTEXT)!; +} export function get_universe(default_universe: string | undefined) { return new URLSearchParams(window.location.search).get("universe") || default_universe