Browse Source

避免sidebar 中点击我的应用 无数据的情况跳出 error组件的问题

Ryuiso 3 months ago
parent
commit
3683797b41
1 changed files with 8 additions and 3 deletions
  1. 8 3
      app/components/sidebar.tsx

+ 8 - 3
app/components/sidebar.tsx

@@ -231,13 +231,18 @@ const AppDrawer: React.FC<AppDrawerProps> = (props) => {
     setListLoading(true);
     try {
       const res = await api.get(`/deepseek/api/project/app`);
+      // 确保 res.data 是数组,如果不是则设为空数组
+      const data = Array.isArray(res.data) ? res.data : [];
+      
       if (type === 'all') {
-        setList(res.data);
+        setList(data);
       } else {
-        setList(res.data.filter((item: any) => item.isCollect));
+        setList(data.filter((item: any) => item.isCollect));
       }
     } catch (error) {
       console.error(error);
+      // 出错时设置为空数组,避免渲染错误
+      setList([]);
     } finally {
       setListLoading(false);
     }
@@ -284,7 +289,7 @@ const AppDrawer: React.FC<AppDrawerProps> = (props) => {
       onClose={onClose}
     >
       {
-        list.length > 0 ?
+        Array.isArray(list) && list.length > 0 ?
           list.map((item, index) => {
             return <div
               style={{