sd-sidebar.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { IconButton } from "@/app/components/button";
  2. import GithubIcon from "@/app/icons/github.svg";
  3. import SDIcon from "@/app/icons/sd.svg";
  4. import ReturnIcon from "@/app/icons/return.svg";
  5. import HistoryIcon from "@/app/icons/history.svg";
  6. import Locale from "@/app/locales";
  7. import { Path, REPO_URL } from "@/app/constant";
  8. import { useNavigate } from "react-router-dom";
  9. import dynamic from "next/dynamic";
  10. import {
  11. SideBarContainer,
  12. SideBarBody,
  13. SideBarHeader,
  14. SideBarTail,
  15. useDragSideBar,
  16. useHotKey,
  17. } from "@/app/components/sidebar";
  18. import { getParams, getModelParamBasicData } from "./sd-panel";
  19. import { useSdStore } from "@/app/store/sd";
  20. import { showToast } from "@/app/components/ui-lib";
  21. import { useMobileScreen } from "@/app/utils";
  22. const SdPanel = dynamic(
  23. async () => (await import("@/app/components/sd")).SdPanel,
  24. {
  25. loading: () => null,
  26. },
  27. );
  28. export function SideBar(props: { className?: string }) {
  29. useHotKey();
  30. const isMobileScreen = useMobileScreen();
  31. const { onDragStart, shouldNarrow } = useDragSideBar();
  32. const navigate = useNavigate();
  33. const sdStore = useSdStore();
  34. const currentModel = sdStore.currentModel;
  35. const params = sdStore.currentParams;
  36. const setParams = sdStore.setCurrentParams;
  37. const handleSubmit = () => {
  38. const columns = getParams?.(currentModel, params);
  39. const reqParams: any = {};
  40. for (let i = 0; i < columns.length; i++) {
  41. const item = columns[i];
  42. reqParams[item.value] = params[item.value] ?? null;
  43. if (item.required) {
  44. if (!reqParams[item.value]) {
  45. showToast(Locale.SdPanel.ParamIsRequired(item.name));
  46. return;
  47. }
  48. }
  49. }
  50. let data: any = {
  51. model: currentModel.value,
  52. model_name: currentModel.name,
  53. status: "wait",
  54. params: reqParams,
  55. created_at: new Date().toLocaleString(),
  56. img_data: "",
  57. };
  58. sdStore.sendTask(data, () => {
  59. setParams(getModelParamBasicData(columns, params, true));
  60. navigate(Path.SdNew);
  61. });
  62. };
  63. return (
  64. <SideBarContainer
  65. onDragStart={onDragStart}
  66. shouldNarrow={shouldNarrow}
  67. {...props}
  68. >
  69. {isMobileScreen ? (
  70. <div
  71. className="window-header"
  72. data-tauri-drag-region
  73. style={{
  74. paddingLeft: 0,
  75. paddingRight: 0,
  76. }}
  77. >
  78. <div className="window-actions">
  79. <div className="window-action-button">
  80. <IconButton
  81. icon={<ReturnIcon />}
  82. bordered
  83. title={Locale.Sd.Actions.ReturnHome}
  84. onClick={() => navigate(Path.Home)}
  85. />
  86. </div>
  87. </div>
  88. <SDIcon width={50} height={50} />
  89. <div className="window-actions">
  90. <div className="window-action-button">
  91. <IconButton
  92. icon={<HistoryIcon />}
  93. bordered
  94. title={Locale.Sd.Actions.History}
  95. onClick={() => navigate(Path.SdNew)}
  96. />
  97. </div>
  98. </div>
  99. </div>
  100. ) : (
  101. <SideBarHeader
  102. title={
  103. <IconButton
  104. icon={<ReturnIcon />}
  105. bordered
  106. title={Locale.Sd.Actions.ReturnHome}
  107. onClick={() => navigate(Path.Home)}
  108. />
  109. }
  110. logo={<SDIcon width={38} height={"100%"} />}
  111. ></SideBarHeader>
  112. )}
  113. <SideBarBody>
  114. <SdPanel />
  115. </SideBarBody>
  116. <SideBarTail
  117. primaryAction={
  118. <a href={REPO_URL} target="_blank" rel="noopener noreferrer">
  119. <IconButton icon={<GithubIcon />} shadow />
  120. </a>
  121. }
  122. secondaryAction={
  123. <IconButton
  124. text={Locale.SdPanel.Submit}
  125. type="primary"
  126. shadow
  127. onClick={handleSubmit}
  128. ></IconButton>
  129. }
  130. />
  131. </SideBarContainer>
  132. );
  133. }