TopBar.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { useEffect, useState } from "preact/hooks";
  2. import { EditorMode } from "./Editor.tsx";
  3. interface TopBarProps {
  4. allowMode: EditorMode;
  5. isLogined: boolean;
  6. shared: boolean;
  7. title: string;
  8. id: string;
  9. }
  10. const settingsData: { [key: string]: string } = {};
  11. const shareData: { [key: string]: boolean } = {};
  12. export default function TopBar(props: TopBarProps) {
  13. const [mode, setMode] = useState(props.allowMode);
  14. const doLogout = async () => {
  15. const resp = await fetch("/api/user/logout");
  16. const respJson = await resp.json();
  17. if (respJson.success) {
  18. location.href = "/login";
  19. return true;
  20. }
  21. return false;
  22. };
  23. const goHome = () => {
  24. location.href = "/";
  25. };
  26. const showShare = () => {
  27. shareData["shared"] = shareData["submittedShared"] !== undefined
  28. ? shareData["submittedShared"]
  29. : props.shared;
  30. window.$modal?.show(
  31. "Share options",
  32. <div style="display: flex; align-items: center">
  33. <input
  34. type="checkbox"
  35. checked={shareData["shared"]}
  36. onChange={(e) => {
  37. shareData["shared"] = (e.target as HTMLInputElement).checked;
  38. (
  39. document.querySelector("#shared-button") as HTMLButtonElement
  40. ).style.visibility = shareData["shared"] ? "visible" : "hidden";
  41. }}
  42. />
  43. <span style="margin: 0 8px">Shared to friends</span>
  44. <button
  45. id="shared-button"
  46. style={`${!shareData["shared"] ? ";visibility: hidden" : ""}`}
  47. onClick={async () => {
  48. await navigator.clipboard.writeText(location.href.split("?")[0]);
  49. }}
  50. >
  51. Copy Link
  52. </button>
  53. </div>,
  54. [
  55. {
  56. text: "Confirm",
  57. onClick: async () => {
  58. const resp = await fetch("/api/share", {
  59. method: "POST",
  60. headers: { "Content-Type": "application/json" },
  61. body: JSON.stringify({
  62. id: props.id,
  63. shared: shareData["shared"],
  64. }),
  65. });
  66. const respJson = await resp.json();
  67. if (respJson.success) {
  68. shareData["submittedShared"] = shareData["shared"];
  69. window.$modal?.hide();
  70. }
  71. },
  72. },
  73. ],
  74. );
  75. };
  76. const showSetting = () => {
  77. settingsData["title"] = settingsData["submittedTitle"] || props.title;
  78. window.$modal?.show(
  79. "Post Settings",
  80. <div style="display: flex; align-items: center">
  81. <span style="margin-right: 8px">Title</span>
  82. <input
  83. placeholder="Post title here"
  84. value={settingsData["title"]}
  85. onInput={(e) => {
  86. settingsData["title"] = (e.target as HTMLInputElement).value;
  87. }}
  88. />
  89. </div>,
  90. [
  91. {
  92. text: "Confirm",
  93. onClick: async () => {
  94. const resp = await fetch("/api/post", {
  95. method: "PUT",
  96. headers: { "Content-Type": "application/json" },
  97. body: JSON.stringify({
  98. id: props.id,
  99. title: settingsData["title"],
  100. }),
  101. });
  102. const respJson = await resp.json();
  103. if (respJson.success) {
  104. settingsData["submittedTitle"] = settingsData["title"];
  105. document.title = settingsData["title"];
  106. window.$modal?.hide();
  107. }
  108. },
  109. },
  110. ],
  111. );
  112. };
  113. // Event listener
  114. const modeChangeListener = (e: CustomEvent) => {
  115. if (
  116. e.detail &&
  117. (props.allowMode === e.detail || props.allowMode === EditorMode.Both)
  118. ) {
  119. setMode(e.detail);
  120. }
  121. };
  122. // Event dispatcher
  123. const modeChangeDispatcher = (mode: EditorMode) => {
  124. dispatchEvent(new CustomEvent("ModeChange", { detail: mode }));
  125. };
  126. // Init event listeners
  127. useEffect(() => {
  128. addEventListener("ModeChange", modeChangeListener);
  129. return () => {
  130. removeEventListener("ModeChange", modeChangeListener);
  131. };
  132. }, []);
  133. return (
  134. <div className="pd-top-bar">
  135. <div
  136. className={`pd-top-bar-mode-switcher${
  137. mode !== EditorMode.Both ? " hidden" : ""
  138. }`}
  139. >
  140. <button
  141. className={`pd-top-bar-btn${
  142. mode === EditorMode.Edit ? " active" : ""
  143. }`}
  144. id="edit"
  145. type="button"
  146. onClick={() => {
  147. modeChangeDispatcher(EditorMode.Edit);
  148. }}
  149. >
  150. Edit
  151. </button>
  152. <button
  153. className={`pd-top-bar-btn${
  154. mode === EditorMode.Read ? " active" : ""
  155. }`}
  156. id="read"
  157. type="button"
  158. onClick={() => {
  159. modeChangeDispatcher(EditorMode.Read);
  160. }}
  161. >
  162. Read
  163. </button>
  164. <button
  165. className={`pd-top-bar-btn${
  166. mode === EditorMode.Both ? " active" : ""
  167. }`}
  168. id="both"
  169. type="button"
  170. onClick={() => {
  171. modeChangeDispatcher(EditorMode.Both);
  172. }}
  173. >
  174. Both
  175. </button>
  176. </div>
  177. {!props.isLogined
  178. ? <span className="pd-top-bar-title">{props.title}</span>
  179. : null}
  180. {props.isLogined
  181. ? (
  182. <div className="pd-top-bar-tool-icons">
  183. <i className="bi bi-box-arrow-left" onClick={doLogout} />
  184. <i className="bi bi-house-door" onClick={goHome} />
  185. <i className="bi bi-share" onClick={showShare} />
  186. <i className="bi bi-gear" onClick={showSetting} />
  187. </div>
  188. )
  189. : null}
  190. </div>
  191. );
  192. }