You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, {useContext, useEffect, useState} from 'react';
  2. import "./Bois.css";
  3. import {StatusContext} from "./Contexts";
  4. import {CalorieScore, CpmScore, DistanceScore, RpmScore, Timer} from "./Score";
  5. import calculateDiff from "../helpers/diff";
  6. import useKey from "../hooks/useKey";
  7. import {Milestones} from "./Milestones";
  8. import {Info, InfoTable, StateFilter, Warning} from "./Misc";
  9. const Boi = ({type, children}) => {
  10. return (
  11. <div className={`Boi Boi-${type}`}>
  12. {children}
  13. </div>
  14. )
  15. };
  16. export const LeftBoi = () => {
  17. const {prevLongDiff, workoutStatus, program, hidden} = useContext(StatusContext);
  18. if (workoutStatus === null || program === null || hidden) {
  19. return null;
  20. }
  21. const {minutes, seconds, calories, distance, rpm} = workoutStatus;
  22. const diff = calculateDiff(program, minutes, seconds, calories);
  23. const cpm = calories / (minutes + (seconds / 60));
  24. return (
  25. <Boi type="left">
  26. <Timer minutes={minutes} seconds={seconds}/>
  27. <CalorieScore calories={calories} diff={diff} prevDiff={prevLongDiff}/>
  28. <DistanceScore distance={distance}/>
  29. <RpmScore rpm={rpm}/>
  30. <CpmScore cpm={cpm}/>
  31. </Boi>
  32. );
  33. };
  34. export const CentreBoi = () => {
  35. const {
  36. state, program, setProgram, bike, setBike, bikes, programs,
  37. start, pause, stop, create, workoutStatus,
  38. hidden, setHidden,
  39. } = useContext(StatusContext);
  40. const [options, setOptions] = useState(null);
  41. const [current, setCurrent] = useState(0);
  42. useEffect(() => {
  43. if (bikes === null && programs === null) {
  44. return;
  45. }
  46. setOptions(state === "bike" ? bikes : programs);
  47. }, [state, bike, program, bikes, programs]);
  48. useKey("-", () => {
  49. if (options === null) {
  50. return false;
  51. }
  52. if (current > 0) {
  53. setCurrent(current - 1);
  54. } else {
  55. setCurrent(options.length - 1);
  56. }
  57. return true;
  58. });
  59. useKey("+", () => {
  60. if (options === null) {
  61. return false;
  62. }
  63. if (current < options.length - 1) {
  64. setCurrent(current + 1);
  65. } else {
  66. setCurrent(0);
  67. }
  68. return true;
  69. });
  70. useKey("Enter", () => {
  71. if (state === "bike") {
  72. setBike(options[current]);
  73. return true;
  74. } else if (state === "program") {
  75. setProgram(options[current]);
  76. create(options[current]);
  77. return true;
  78. } else if (state === "connected") {
  79. start();
  80. return true;
  81. } else if (state === "started") {
  82. pause();
  83. return true;
  84. }
  85. return false;
  86. });
  87. useKey("Escape", () => {
  88. if (state === "connected") {
  89. stop();
  90. return true;
  91. }
  92. });
  93. useKey(["H", "h"], () => showHide());
  94. function showHide() {
  95. setHidden(!hidden);
  96. }
  97. if (hidden) {
  98. return null;
  99. }
  100. if (state === "started" && workoutStatus !== null) {
  101. const {minutes, seconds, calories} = workoutStatus;
  102. const diff = calculateDiff(program, minutes, seconds, calories);
  103. if (diff < 0) {
  104. return (
  105. <Boi type="centre">
  106. <Warning>{diff}</Warning>
  107. </Boi>
  108. );
  109. } else {
  110. return null;
  111. }
  112. }
  113. if (bikes === null || programs === null) {
  114. return <Boi type="centre"><Info>Laster inn...</Info></Boi>;
  115. }
  116. function currentOptionName() {
  117. if (options === null) {
  118. return "";
  119. }
  120. if (options[current] === void(0)) {
  121. return "";
  122. }
  123. return options[current].name;
  124. }
  125. console.log(state);
  126. return (
  127. <Boi type="centre">
  128. <StateFilter current={state} required="stopped">
  129. <Info>Inaktiv</Info>
  130. </StateFilter>
  131. <StateFilter current={state} required="bike">
  132. <Info>
  133. Velg sykkel (+/-):
  134. <br/>
  135. <b>{currentOptionName()}</b>
  136. </Info>
  137. </StateFilter>
  138. <StateFilter current={state} required="program">
  139. <Info>
  140. Velg program (+/-):
  141. <br/>
  142. <b>{currentOptionName()}</b>
  143. </Info>
  144. </StateFilter>
  145. <StateFilter current={state} required="connected">
  146. <InfoTable keyValuePairs={[
  147. {key: "Enter", value: "Start"},
  148. {key: "Escape", value: "Avslutt"},
  149. ]}/>
  150. </StateFilter>
  151. <StateFilter current={state} required="disconnected">
  152. <Info>Kobler til...</Info>
  153. </StateFilter>
  154. <StateFilter current={state} required="started">
  155. <Info>Abonnerer WS...</Info>
  156. </StateFilter>
  157. <StateFilter current={state} required="unknown">
  158. <Info>Noe gikk galt!</Info>
  159. </StateFilter>
  160. </Boi>
  161. );
  162. };
  163. export const RightBoi = () => {
  164. const {milestones, hidden} = useContext(StatusContext);
  165. if (hidden || (milestones || []).length === 0) {
  166. return null;
  167. }
  168. return (
  169. <Boi type="right">
  170. <Milestones milestones={milestones.sort((i, j) => j.minutes - i.minutes)}/>
  171. </Boi>
  172. );
  173. };