-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flat-pages): add sensitive page (#1902)
- Loading branch information
Showing
8 changed files
with
352 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import "./style.less"; | ||
|
||
import React, { useCallback, useContext, useEffect, useMemo, useState } from "react"; | ||
import { observer } from "mobx-react-lite"; | ||
import { useSearchParam } from "react-use"; | ||
import { Redirect } from "react-router-dom"; | ||
|
||
import type { ColumnsType } from "antd/lib/table"; | ||
import { subWeeks, subMonths, subYears } from "date-fns"; | ||
import { Select, Table } from "antd"; | ||
|
||
import { useSafePromise } from "flat-components"; | ||
import { FlatI18n, useTranslate } from "@netless/flat-i18n"; | ||
import { SensitiveType, listSensitive } from "@netless/flat-server-api"; | ||
import { GlobalStoreContext } from "../components/StoreProvider"; | ||
import { NEED_BINDING_PHONE } from "../constants/config"; | ||
|
||
export type SensitiveRange = "1 week" | "1 month" | "1 year"; | ||
|
||
export interface SensitiveData { | ||
type: SensitiveType; | ||
name: string; | ||
purpose: string; | ||
description: string; | ||
count: number; | ||
content: string; | ||
} | ||
|
||
const SensitiveTypes: SensitiveType[] = [ | ||
SensitiveType.Avatar, | ||
SensitiveType.Phone, | ||
SensitiveType.Name, | ||
SensitiveType.WeChatName, | ||
SensitiveType.CloudStorage, | ||
SensitiveType.Recording, | ||
]; | ||
|
||
const BlankSensitiveTypes: SensitiveType[] = [SensitiveType.CloudStorage, SensitiveType.Recording]; | ||
|
||
export const SensitivePage = observer(function SensitivePage() { | ||
const t = useTranslate(); | ||
const sp = useSafePromise(); | ||
const globalStore = useContext(GlobalStoreContext); | ||
const token = useSearchParam("token") || globalStore.userInfo?.token; | ||
|
||
const defaultSensitiveData = useCallback( | ||
() => | ||
SensitiveTypes.map(type => ({ | ||
type, | ||
name: t("sensitive.name." + type), | ||
purpose: t("sensitive.purpose." + type), | ||
description: t("sensitive.description." + type), | ||
count: BlankSensitiveTypes.includes(type) ? -1 : 0, | ||
content: "", | ||
})), | ||
[t], | ||
); | ||
|
||
const [range, setRange] = useState<SensitiveRange>("1 week"); | ||
const [data, setData] = useState<SensitiveData[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
if (NEED_BINDING_PHONE) { | ||
FlatI18n.changeLanguage("zh-CN"); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
let from = new Date(); | ||
if (range === "1 week") { | ||
from = subWeeks(from, 1); | ||
} else if (range === "1 month") { | ||
from = subMonths(from, 1); | ||
} else if (range === "1 year") { | ||
from = subYears(from, 1); | ||
} | ||
|
||
setLoading(true); | ||
sp(listSensitive({ from, to: new Date() })) | ||
.then(data => { | ||
const newData = data.reduce((acc, cur) => { | ||
const index = acc.findIndex(e => e.type === cur.type); | ||
if (index !== -1) { | ||
acc[index].count += 1; | ||
acc[index].content = reduceContent( | ||
cur.type, | ||
acc[index].content, | ||
cur.content, | ||
); | ||
} | ||
return acc; | ||
}, defaultSensitiveData()); | ||
if (NEED_BINDING_PHONE) { | ||
const phoneIndex = newData.findIndex(e => e.type === SensitiveType.Phone); | ||
if (phoneIndex !== -1) { | ||
newData[phoneIndex].count = Math.max(newData[phoneIndex].count, 1); | ||
} | ||
} | ||
setData(newData); | ||
}) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}, [defaultSensitiveData, range, sp, t]); | ||
|
||
const columns = useMemo<ColumnsType<SensitiveData>>( | ||
() => [ | ||
{ | ||
title: t("sensitive.name.column"), | ||
dataIndex: "name", | ||
align: "center", | ||
}, | ||
{ | ||
title: t("sensitive.purpose.column"), | ||
dataIndex: "purpose", | ||
}, | ||
{ | ||
title: t("sensitive.description.column"), | ||
dataIndex: "description", | ||
}, | ||
{ | ||
title: t("sensitive.count.column"), | ||
dataIndex: "count", | ||
align: "center", | ||
render: (count: number) => | ||
count < 0 ? "" : count === 0 ? t("sensitive.no-data") : String(count), | ||
}, | ||
{ | ||
title: t("sensitive.content.column"), | ||
dataIndex: "content", | ||
align: "center", | ||
}, | ||
], | ||
[t], | ||
); | ||
|
||
if (!token) { | ||
return <Redirect to="/login" />; | ||
} | ||
|
||
return ( | ||
<div className="sensitive-page-container"> | ||
<div className="sensitive-page"> | ||
<div className="sensitive-page-actions"> | ||
<Select loading={loading} value={range} onChange={setRange}> | ||
<Select.Option key="1 week" value="1 week"> | ||
{t("sensitive-range.week")} | ||
</Select.Option> | ||
<Select.Option key="1 month" value="1 month"> | ||
{t("sensitive-range.month")} | ||
</Select.Option> | ||
<Select.Option key="1 year" value="1 year"> | ||
{t("sensitive-range.year")} | ||
</Select.Option> | ||
</Select> | ||
</div> | ||
<Table | ||
className="sensitive-page-table" | ||
columns={columns} | ||
dataSource={data} | ||
loading={loading} | ||
locale={{ emptyText: t("no-data") }} | ||
pagination={false} | ||
rowKey="type" | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
|
||
export default SensitivePage; | ||
|
||
function reduceContent(type: SensitiveType, _content: string, input: string): string { | ||
if (type === SensitiveType.Avatar) { | ||
return "/"; | ||
} | ||
// otherwise always return the last one | ||
return input; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.sensitive-page-container { | ||
background-color: #edf0f6; | ||
padding: 8px 0; | ||
} | ||
|
||
.sensitive-page { | ||
background-color: #fff; | ||
padding: 16px; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.sensitive-page-actions { | ||
text-align: right; | ||
padding-bottom: 12px; | ||
|
||
.ant-select { | ||
text-align: left; | ||
} | ||
} | ||
|
||
.sensitive-page-subtitle { | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.