Yuki Takei пре 3 година
родитељ
комит
26917edfeb

+ 21 - 25
packages/app/src/components/ReactMarkdownComponents/Lsx/Lsx.tsx

@@ -6,7 +6,7 @@ import * as url from 'url';
 
 import { pathUtils } from '@growi/core';
 
-import axios from '~/utils/axios';
+import { apiGet } from '~/client/util/apiv1-client';
 
 // eslint-disable-next-line no-unused-vars
 
@@ -66,8 +66,8 @@ function generatePageNode(pathToNodeMap: Record<string, PageNode>, rootPagePath:
     * process recursively for ancestors
     */
   // get or create parent node
-  const parentPath = this.getParentPath(pagePath);
-  const parentNode = this.generatePageNode(pathToNodeMap, rootPagePath, parentPath);
+  const parentPath = getParentPath(pagePath);
+  const parentNode = generatePageNode(pathToNodeMap, rootPagePath, parentPath);
   // associate to patent
   if (parentNode != null) {
     parentNode.children.push(node);
@@ -127,7 +127,7 @@ export const Lsx = ({
     }
 
     return stateCache;
-  }, []);
+  }, [lsxContext]);
 
   const generatePageNodeTree = useCallback((rootPagePath, pages) => {
     const pathToNodeMap: Record<string, PageNode> = {};
@@ -175,19 +175,15 @@ export const Lsx = ({
 
     let newNodeTree: PageNode[] = [];
     try {
-      const res = await axios.get('/_api/plugins/lsx', {
-        params: {
-          pagePath,
-          options: lsxContext.options,
-        },
+      const result: any = await apiGet('/plugins/lsx', {
+        pagePath,
+        options: lsxContext.options,
       });
 
-      if (res.data.ok) {
-        const basisViewersCount = res.data.toppageViewersCount;
-        newNodeTree = generatePageNodeTree(pagePath, res.data.pages);
-        setNodeTree(newNodeTree);
-        setBasisViewersCount(basisViewersCount);
-      }
+      const basisViewersCount = result.toppageViewersCount;
+      newNodeTree = generatePageNodeTree(pagePath, result.pages);
+      setNodeTree(newNodeTree);
+      setBasisViewersCount(basisViewersCount);
     }
     catch (error) {
       setError(true);
@@ -197,15 +193,15 @@ export const Lsx = ({
       setLoading(false);
 
       // store to sessionStorage
-      tagCacheManager.cacheState(lsxContext, {
-        isError,
-        isCacheExists,
-        basisViewersCount,
-        errorMessage,
-        nodeTree: newNodeTree,
-      });
+      // tagCacheManager.cacheState(lsxContext, {
+      //   isError,
+      //   isCacheExists,
+      //   basisViewersCount,
+      //   errorMessage,
+      //   nodeTree: newNodeTree,
+      // });
     }
-  }, [basisViewersCount, errorMessage, generatePageNodeTree, isCacheExists, isError, lsxContext]);
+  }, [generatePageNodeTree, lsxContext]);
 
   useEffect(() => {
     // get state object cache
@@ -231,7 +227,7 @@ export const Lsx = ({
       return (
         <div className="text-warning">
           <i className="fa fa-exclamation-triangle fa-fw"></i>
-          {/* {lsxContext.tagExpression} (-&gt; <small>{this.state.errorMessage}</small>) */}
+          {lsxContext.toString()} (-&gt; <small>{errorMessage}</small>)
         </div>
       );
     }
@@ -242,7 +238,7 @@ export const Lsx = ({
         { isLoading && (
           <div className="text-muted">
             <i className="fa fa-spinner fa-pulse mr-1"></i>
-            {/* {lsxContext.tagExpression} */}
+            {lsxContext.toString()}
             { isCacheExists && <small>&nbsp;(Showing cache..)</small> }
           </div>
         ) }

+ 19 - 0
packages/app/src/components/ReactMarkdownComponents/Lsx/lsx-context.ts

@@ -10,6 +10,10 @@ export class LsxContext {
 
   constructor(pagePath: string, options: Record<string, string|undefined>) {
     this.pagePath = pagePath;
+
+    // remove undefined keys
+    Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
+
     this.options = options;
   }
 
@@ -20,4 +24,19 @@ export class LsxContext {
     return OptionParser.parseRange(this.options.depth);
   }
 
+  /**
+   * for printing errors
+   * @returns
+   */
+  toString(): string {
+    const attributeStrs = [`prefix=${this.pagePath}`];
+    if (this.options != null) {
+      attributeStrs.push(
+        ...Object.entries(this.options).map(([key, val]) => `${key}=${val || 'true'}`),
+      );
+    }
+
+    return `$lsx(${attributeStrs.join(', ')})`;
+  }
+
 }

+ 15 - 16
packages/plugin-lsx/src/server/routes/lsx.js

@@ -135,22 +135,13 @@ class Lsx {
    */
   static addSortCondition(query, pagePath, optionsSortArg, optionsReverse) {
     // init sort key
-    const optionsSort = optionsSortArg || 'path';
+    const optionsSort = optionsSortArg ?? 'path';
 
     // the default sort order
-    let isReversed = false;
+    const isReversed = optionsReverse === 'true';
 
-    if (optionsSort != null) {
-      if (optionsSort !== 'path' && optionsSort !== 'createdAt' && optionsSort !== 'updatedAt') {
-        throw new Error(`The specified value '${optionsSort}' for the sort option is invalid. It must be 'path', 'createdAt' or 'updatedAt'.`);
-      }
-    }
-
-    if (optionsReverse != null) {
-      if (optionsReverse !== 'true' && optionsReverse !== 'false') {
-        throw new Error(`The specified value '${optionsReverse}' for the reverse option is invalid. It must be 'true' or 'false'.`);
-      }
-      isReversed = (optionsReverse === 'true');
+    if (optionsSort !== 'path' && optionsSort !== 'createdAt' && optionsSort !== 'updatedAt') {
+      throw new Error(`The specified value '${optionsSort}' for the sort option is invalid. It must be 'path', 'createdAt' or 'updatedAt'.`);
     }
 
     const sortOption = {};
@@ -162,7 +153,6 @@ class Lsx {
 
 module.exports = (crowi, app) => {
   const Page = crowi.model('Page');
-  const User = crowi.model('User');
   const ApiResponse = crowi.require('../util/apiResponse');
   const actions = {};
 
@@ -203,8 +193,17 @@ module.exports = (crowi, app) => {
 
   actions.listPages = async(req, res) => {
     const user = req.user;
-    const pagePath = req.query.pagePath;
-    const options = JSON.parse(req.query.options);
+
+    let pagePath;
+    let options;
+
+    try {
+      pagePath = req.query.pagePath;
+      options = JSON.parse(req.query.options);
+    }
+    catch (error) {
+      return res.json(ApiResponse.error(error));
+    }
 
     const builder = await generateBaseQueryBuilder(pagePath, user);