Browse Source

mode 不用useState

visuddhinanda 2 years ago
parent
commit
ff4223a671
1 changed files with 38 additions and 13 deletions
  1. 38 13
      dashboard/src/pages/library/article/show.tsx

+ 38 - 13
dashboard/src/pages/library/article/show.tsx

@@ -40,9 +40,6 @@ const Widget = () => {
   const { type, id, mode = "read" } = useParams(); //url 参数
   const { type, id, mode = "read" } = useParams(); //url 参数
   const navigate = useNavigate();
   const navigate = useNavigate();
   console.log("mode", mode);
   console.log("mode", mode);
-  const [articleMode, setArticleMode] = useState<ArticleMode>(
-    mode as ArticleMode
-  );
   const [rightPanel, setRightPanel] = useState<TPanelName>("close");
   const [rightPanel, setRightPanel] = useState<TPanelName>("close");
   const [searchParams, setSearchParams] = useSearchParams();
   const [searchParams, setSearchParams] = useSearchParams();
 
 
@@ -60,6 +57,12 @@ const Widget = () => {
   }, []);
   }, []);
   const rightBarWidth = "48px";
   const rightBarWidth = "48px";
   const channelId = id?.split("_").slice(1);
   const channelId = id?.split("_").slice(1);
+  let currMode: ArticleMode;
+  if (searchParams.get("mode") !== null) {
+    currMode = searchParams.get("mode") as ArticleMode;
+  } else {
+    currMode = "read";
+  }
   return (
   return (
     <div>
     <div>
       <Affix offsetTop={0}>
       <Affix offsetTop={0}>
@@ -76,8 +79,16 @@ const Widget = () => {
           <div></div>
           <div></div>
           <div key="right" style={{ display: "flex" }}>
           <div key="right" style={{ display: "flex" }}>
             <ModeSwitch
             <ModeSwitch
+              initMode={currMode}
               onModeChange={(e: ArticleMode) => {
               onModeChange={(e: ArticleMode) => {
-                setArticleMode(e);
+                let output: any = { mode: e };
+                searchParams.forEach((value, key) => {
+                  console.log(value, key);
+                  if (key !== "mode") {
+                    output[key] = value;
+                  }
+                });
+                setSearchParams(output);
               }}
               }}
             />
             />
             <Divider type="vertical" />
             <Divider type="vertical" />
@@ -132,13 +143,16 @@ const Widget = () => {
               para={searchParams.get("par")}
               para={searchParams.get("par")}
               channelId={searchParams.get("channel")}
               channelId={searchParams.get("channel")}
               articleId={id}
               articleId={id}
-              mode={articleMode}
+              mode={searchParams.get("mode") as ArticleMode}
               onArticleChange={(article: string) => {
               onArticleChange={(article: string) => {
                 console.log("article change", article);
                 console.log("article change", article);
-                let url = `/article/${type}/${article}/${articleMode}?mode=${articleMode}`;
-                if (searchParams.get("channel")) {
-                  url += "&channel=" + searchParams.get("channel");
-                }
+                let url = `/article/${type}/${article}?mode=${currMode}`;
+                searchParams.forEach((value, key) => {
+                  console.log(value, key);
+                  if (key !== "mode") {
+                    url += `&${key}=${value}`;
+                  }
+                });
                 navigate(url);
                 navigate(url);
               }}
               }}
             />
             />
@@ -150,12 +164,23 @@ const Widget = () => {
               articleId={id ? id : ""}
               articleId={id ? id : ""}
               selectedChannelKeys={channelId}
               selectedChannelKeys={channelId}
               onChannelSelect={(e: IChannel[]) => {
               onChannelSelect={(e: IChannel[]) => {
+                //channel 改变
                 console.log("onChannelSelect", e);
                 console.log("onChannelSelect", e);
                 const channels = e.map((item) => item.id).join("_");
                 const channels = e.map((item) => item.id).join("_");
-                let url = `/article/${type}/${id}/${articleMode}`;
-                if (e.length > 0) {
-                  url += `?channel=${channels}`;
-                }
+                let url = `/article/${type}/${id}?mode=${currMode}`;
+                searchParams.forEach((value, key) => {
+                  console.log(value, key);
+                  if (key !== "mode") {
+                    if (key === "channel") {
+                      if (e.length > 0) {
+                        url += `&channel=${channels}`;
+                      }
+                    } else {
+                      url += `&${key}=${value}`;
+                    }
+                  }
+                });
+
                 navigate(url);
                 navigate(url);
               }}
               }}
             />
             />