2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-13 17:34:32 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import React from "react" ;
2023-08-14 14:32:12 +08:00
import { Card , Col , Row , Spin , Statistic } from "antd" ;
import { ArrowUpOutlined } from "@ant-design/icons" ;
2022-04-25 13:27:26 +08:00
import * as ApplicationBackend from "../backend/ApplicationBackend" ;
2023-08-14 14:32:12 +08:00
import * as DashboardBackend from "../backend/DashboardBackend" ;
import * as echarts from "echarts" ;
2021-02-13 17:34:32 +08:00
import * as Setting from "../Setting" ;
import SingleCard from "./SingleCard" ;
2021-02-19 23:23:59 +08:00
import i18next from "i18next" ;
2021-02-13 17:34:32 +08:00
class HomePage extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
classes : props ,
2022-04-25 13:27:26 +08:00
applications : null ,
2023-08-14 14:32:12 +08:00
dashboardData : null ,
2021-02-13 17:34:32 +08:00
} ;
}
2022-04-25 13:27:26 +08:00
UNSAFE _componentWillMount ( ) {
this . getApplicationsByOrganization ( this . props . account . owner ) ;
2023-08-14 14:32:12 +08:00
this . getDashboard ( ) ;
2022-04-25 13:27:26 +08:00
}
getApplicationsByOrganization ( organizationName ) {
ApplicationBackend . getApplicationsByOrganization ( "admin" , organizationName )
. then ( ( res ) => {
this . setState ( {
2023-07-23 09:49:16 +08:00
applications : res . data || [ ] ,
2022-04-25 13:27:26 +08:00
} ) ;
} ) ;
}
2023-08-14 14:32:12 +08:00
getDashboard ( ) {
DashboardBackend . getDashboard ( )
. then ( ( res ) => {
if ( res . status === "ok" ) {
this . setState ( {
dashboardData : res . data ,
} , ( ) => {
this . renderEChart ( ) ;
} ) ;
} else {
Setting . showMessage ( "error" , res . msg ) ;
}
} ) ;
}
2021-02-13 17:34:32 +08:00
getItems ( ) {
let items = [ ] ;
if ( Setting . isAdminUser ( this . props . account ) ) {
items = [
2021-02-19 23:23:59 +08:00
{ link : "/organizations" , name : i18next . t ( "general:Organizations" ) , organizer : i18next . t ( "general:User containers" ) } ,
{ link : "/users" , name : i18next . t ( "general:Users" ) , organizer : i18next . t ( "general:Users under all organizations" ) } ,
{ link : "/providers" , name : i18next . t ( "general:Providers" ) , organizer : i18next . t ( "general:OAuth providers" ) } ,
2021-08-08 10:03:03 +08:00
{ link : "/applications" , name : i18next . t ( "general:Applications" ) , organizer : i18next . t ( "general:Applications that require authentication" ) } ,
2021-02-13 17:34:32 +08:00
] ;
2022-08-06 23:43:09 +08:00
for ( let i = 0 ; i < items . length ; i ++ ) {
2022-04-25 13:27:26 +08:00
let filename = items [ i ] . link ;
if ( filename === "/account" ) {
filename = "/users" ;
}
2022-08-15 09:18:21 +08:00
items [ i ] . logo = ` ${ Setting . StaticBaseUrl } /img ${ filename } .png ` ;
2022-04-25 13:27:26 +08:00
items [ i ] . createdTime = "" ;
2021-03-28 17:36:31 +08:00
}
2022-04-25 13:27:26 +08:00
} else {
this . state . applications . forEach ( application => {
2023-02-25 10:50:50 +08:00
let homepageUrl = application . homepageUrl ;
if ( homepageUrl === "<custom-url>" ) {
homepageUrl = this . props . account . homepage ;
}
2022-04-25 13:27:26 +08:00
items . push ( {
2023-02-25 10:50:50 +08:00
link : homepageUrl , name : application . displayName , organizer : application . description , logo : application . logo , createdTime : "" ,
2022-04-25 13:27:26 +08:00
} ) ;
} ) ;
2021-02-13 17:34:32 +08:00
}
2022-07-10 15:45:55 +08:00
return items ;
2021-02-13 17:34:32 +08:00
}
2023-08-14 14:32:12 +08:00
renderEChart ( ) {
const data = this . state . dashboardData ;
const chartDom = document . getElementById ( "echarts-chart" ) ;
const myChart = echarts . init ( chartDom ) ;
const currentDate = new Date ( ) ;
const dateArray = [ ] ;
for ( let i = 30 ; i >= 0 ; i -- ) {
const date = new Date ( currentDate ) ;
date . setDate ( date . getDate ( ) - i ) ;
const month = parseInt ( date . getMonth ( ) ) + 1 ;
const day = parseInt ( date . getDate ( ) ) ;
const formattedDate = ` ${ month } - ${ day } ` ;
dateArray . push ( formattedDate ) ;
}
const option = {
title : { text : i18next . t ( "home:Past 30 Days" ) } ,
tooltip : { trigger : "axis" } ,
legend : { data : [
i18next . t ( "general:Users" ) ,
i18next . t ( "general:Providers" ) ,
i18next . t ( "general:Applications" ) ,
i18next . t ( "general:Organizations" ) ,
i18next . t ( "general:Subscriptions" ) ,
] } ,
grid : { left : "3%" , right : "4%" , bottom : "3%" , containLabel : true } ,
xAxis : { type : "category" , boundaryGap : false , data : dateArray } ,
yAxis : { type : "value" } ,
series : [
{ name : i18next . t ( "general:Organizations" ) , type : "line" , data : data ? . organizationCounts } ,
{ name : i18next . t ( "general:Users" ) , type : "line" , data : data ? . userCounts } ,
{ name : i18next . t ( "general:Providers" ) , type : "line" , data : data ? . providerCounts } ,
{ name : i18next . t ( "general:Applications" ) , type : "line" , data : data ? . applicationCounts } ,
{ name : i18next . t ( "general:Subscriptions" ) , type : "line" , data : data ? . subscriptionCounts } ,
] ,
} ;
myChart . setOption ( option ) ;
}
2021-02-13 17:34:32 +08:00
renderCards ( ) {
2023-08-14 14:32:12 +08:00
const data = this . state . dashboardData ;
if ( data === null ) {
return (
< div style = { { display : "flex" , justifyContent : "center" , alignItems : "center" , marginTop : "10%" } } >
< Spin size = "large" tip = { i18next . t ( "login:Loading" ) } style = { { paddingTop : "10%" } } / >
< / d i v >
) ;
2022-04-25 13:27:26 +08:00
}
2021-02-13 17:34:32 +08:00
const items = this . getItems ( ) ;
if ( Setting . isMobile ( ) ) {
return (
< Card bodyStyle = { { padding : 0 } } >
{
items . map ( item => {
return (
2022-08-09 12:19:56 +08:00
< SingleCard key = { item . link } logo = { item . logo } link = { item . link } title = { item . name } desc = { item . organizer } isSingle = { items . length === 1 } / >
2022-07-10 15:45:55 +08:00
) ;
2021-02-13 17:34:32 +08:00
} )
}
< / C a r d >
2022-07-10 15:45:55 +08:00
) ;
2021-02-13 17:34:32 +08:00
} else {
return (
2023-08-14 14:32:12 +08:00
< Row gutter = { 80 } >
< Col span = { 50 } >
< Card bordered = { false } bodyStyle = { { width : "100%" , height : "150px" , display : "flex" , alignItems : "center" , justifyContent : "center" } } >
< Statistic title = { i18next . t ( "home:Total users" ) } fontSize = "100px" value = { data ? . userCounts [ 30 ] } valueStyle = { { fontSize : "30px" } } style = { { width : "200px" , paddingLeft : "10px" } } / >
< / C a r d >
< / C o l >
< Col span = { 50 } >
< Card bordered = { false } bodyStyle = { { width : "100%" , height : "150px" , display : "flex" , alignItems : "center" , justifyContent : "center" } } >
< Statistic title = { i18next . t ( "home:New users today" ) } fontSize = "100px" value = { data ? . userCounts [ 30 ] - data ? . userCounts [ 30 - 1 ] } valueStyle = { { fontSize : "30px" } } prefix = { < ArrowUpOutlined / > } style = { { width : "200px" , paddingLeft : "10px" } } / >
< / C a r d >
< / C o l >
< Col span = { 50 } >
< Card bordered = { false } bodyStyle = { { width : "100%" , height : "150px" , display : "flex" , alignItems : "center" , justifyContent : "center" } } >
< Statistic title = { i18next . t ( "home:New users past 7 days" ) } value = { data ? . userCounts [ 30 ] - data ? . userCounts [ 30 - 7 ] } valueStyle = { { fontSize : "30px" } } prefix = { < ArrowUpOutlined / > } style = { { width : "200px" , paddingLeft : "10px" } } / >
< / C a r d >
< / C o l >
< Col span = { 50 } >
< Card bordered = { false } bodyStyle = { { width : "100%" , height : "150px" , display : "flex" , alignItems : "center" , justifyContent : "center" } } >
< Statistic title = { i18next . t ( "home:New users past 30 days" ) } value = { data ? . userCounts [ 30 ] - data ? . userCounts [ 30 - 30 ] } valueStyle = { { fontSize : "30px" } } prefix = { < ArrowUpOutlined / > } style = { { width : "200px" , paddingLeft : "10px" } } / >
< / C a r d >
< / C o l >
< / R o w >
2022-07-10 15:45:55 +08:00
) ;
2021-02-13 17:34:32 +08:00
}
}
render ( ) {
return (
2023-08-14 14:32:12 +08:00
< div style = { { display : "flex" , justifyContent : "center" , flexDirection : "column" , alignItems : "center" } } >
2021-02-13 17:34:32 +08:00
< Row style = { { width : "100%" } } >
2022-08-06 23:47:28 +08:00
< Col span = { 24 } style = { { display : "flex" , justifyContent : "center" } } >
2021-02-13 17:34:32 +08:00
{
this . renderCards ( )
}
< / C o l >
< / R o w >
2023-08-14 14:32:12 +08:00
< div id = "echarts-chart"
style = { { width : "80%" , height : "400px" , textAlign : "center" , marginTop : "20px" } } > < / d i v >
2021-02-13 17:34:32 +08:00
< / d i v >
2022-07-10 15:45:55 +08:00
) ;
2021-02-13 17:34:32 +08:00
}
}
export default HomePage ;