TopBar.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. globalThis.$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. type="button"
  46. id="shared-button"
  47. style={`${!shareData["shared"] ? ";visibility: hidden" : ""}`}
  48. onClick={async () => {
  49. await navigator.clipboard.writeText(location.href.split("?")[0]);
  50. }}
  51. >
  52. Copy Link
  53. </button>
  54. </div>,
  55. [
  56. {
  57. text: "Confirm",
  58. onClick: async () => {
  59. const resp = await fetch("/api/share", {
  60. method: "POST",
  61. headers: { "Content-Type": "application/json" },
  62. body: JSON.stringify({
  63. id: props.id,
  64. shared: shareData["shared"],
  65. }),
  66. });
  67. const respJson = await resp.json();
  68. if (respJson.success) {
  69. shareData["submittedShared"] = shareData["shared"];
  70. globalThis.$modal?.hide();
  71. }
  72. },
  73. },
  74. ],
  75. );
  76. };
  77. const showSetting = () => {
  78. settingsData["title"] = settingsData["submittedTitle"] || props.title;
  79. globalThis.$modal?.show(
  80. "Post Settings",
  81. <div style="display: flex; align-items: center">
  82. <span style="margin-right: 8px">Title</span>
  83. <input
  84. placeholder="Post title here"
  85. value={settingsData["title"]}
  86. onInput={(e) => {
  87. settingsData["title"] = (e.target as HTMLInputElement).value;
  88. }}
  89. />
  90. </div>,
  91. [
  92. {
  93. text: "Confirm",
  94. onClick: async () => {
  95. const resp = await fetch("/api/post", {
  96. method: "PUT",
  97. headers: { "Content-Type": "application/json" },
  98. body: JSON.stringify({
  99. id: props.id,
  100. title: settingsData["title"],
  101. }),
  102. });
  103. const respJson = await resp.json();
  104. if (respJson.success) {
  105. settingsData["submittedTitle"] = settingsData["title"];
  106. document.title = settingsData["title"];
  107. globalThis.$modal?.hide();
  108. }
  109. },
  110. },
  111. ],
  112. );
  113. };
  114. // Event listener
  115. const modeChangeListener = (e: CustomEvent) => {
  116. if (
  117. e.detail &&
  118. (props.allowMode === e.detail || props.allowMode === EditorMode.Both)
  119. ) {
  120. setMode(e.detail);
  121. }
  122. };
  123. // Event dispatcher
  124. const modeChangeDispatcher = (mode: EditorMode) => {
  125. dispatchEvent(new CustomEvent("ModeChange", { detail: mode }));
  126. };
  127. // Init event listeners
  128. useEffect(() => {
  129. addEventListener("ModeChange", modeChangeListener);
  130. return () => {
  131. removeEventListener("ModeChange", modeChangeListener);
  132. };
  133. }, []);
  134. return (
  135. <div className="pd-top-bar">
  136. <div
  137. className={`pd-top-bar-mode-switcher${
  138. props.allowMode !== EditorMode.Both ? " hidden" : ""
  139. }`}
  140. >
  141. <button
  142. className={`pd-top-bar-btn${
  143. mode === EditorMode.Edit ? " active" : ""
  144. }`}
  145. id="edit"
  146. type="button"
  147. onClick={() => {
  148. modeChangeDispatcher(EditorMode.Edit);
  149. }}
  150. >
  151. Edit
  152. </button>
  153. <button
  154. className={`pd-top-bar-btn${
  155. mode === EditorMode.Read ? " active" : ""
  156. }`}
  157. id="read"
  158. type="button"
  159. onClick={() => {
  160. modeChangeDispatcher(EditorMode.Read);
  161. }}
  162. >
  163. Read
  164. </button>
  165. <button
  166. className={`pd-top-bar-btn${
  167. mode === EditorMode.Both ? " active" : ""
  168. }`}
  169. id="both"
  170. type="button"
  171. onClick={() => {
  172. modeChangeDispatcher(EditorMode.Both);
  173. }}
  174. >
  175. Both
  176. </button>
  177. </div>
  178. {!props.isLogined
  179. ? <span className="pd-top-bar-title">{props.title}</span>
  180. : null}
  181. {props.isLogined
  182. ? (
  183. <div className="pd-top-bar-tool-icons">
  184. <i className="bi bi-box-arrow-left" onClick={doLogout} />
  185. <i className="bi bi-house-door" onClick={goHome} />
  186. <i className="bi bi-share" onClick={showShare} />
  187. <i className="bi bi-gear" onClick={showSetting} />
  188. </div>
  189. )
  190. : null}
  191. </div>
  192. );
  193. }