MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / Match

Function Match

src/pages/components/ScriptSetting/Match.tsx:22–311  ·  view source on GitHub ↗
({ script })

Source from the content-addressed store, hash-verified

20const Match: React.FC<{
21 script: Script;
22}> = ({ script }) => {
23 const scriptDAO = new ScriptDAO();
24 const [match, setMatch] = useState<MatchItem[]>([]);
25 const [exclude, setExclude] = useState<MatchItem[]>([]);
26 const [matchValue, setMatchValue] = useState<string>("");
27 const [matchVisible, setMatchVisible] = useState<boolean>(false);
28 const [excludeValue, setExcludeValue] = useState<string>("");
29 const [excludeVisible, setExcludeVisible] = useState<boolean>(false);
30 const { t } = useTranslation(); // 使用 react-i18next 的 useTranslation 钩子函数获取翻译函数
31
32 // 自定义的状态更新函数,会在更新后自动刷新数据
33 const updateMatchAndRefresh = (newMatch: MatchItem[]) => {
34 setMatch(newMatch);
35 refreshMatch();
36 };
37
38 const updateExcludeAndRefresh = (newExclude: MatchItem[]) => {
39 setExclude(newExclude);
40 refreshMatch();
41 };
42
43 const refreshMatch = () => {
44 if (script) {
45 // 从数据库中获取是简单处理数据一致性的问题
46 scriptDAO.get(script.uuid).then((res) => {
47 if (!res) {
48 return;
49 }
50 const matchArr = res.selfMetadata?.match || res.metadata.match || [];
51 const matchMap = new Map<string, boolean>();
52 res.metadata.match?.forEach((m) => {
53 matchMap.set(m, true);
54 });
55 const v: MatchItem[] = [];
56 matchArr.forEach((value, index) => {
57 v.push({
58 id: index,
59 match: value,
60 byUser: !matchMap.has(value),
61 hasMatch: false,
62 isExclude: false,
63 });
64 });
65 setMatch(v);
66
67 const excludeArr = res.selfMetadata?.exclude || res.metadata.exclude || [];
68 const excludeMap = new Map<string, boolean>();
69 res.metadata.exclude?.forEach((m) => {
70 excludeMap.set(m, true);
71 });
72 const e: MatchItem[] = [];
73 excludeArr.forEach((value, index) => {
74 const hasMatch = matchMap.has(value);
75 e.push({
76 id: index,
77 match: value,
78 byUser: !excludeMap.has(value),
79 hasMatch,

Callers

nothing calls this directly

Calls 6

refreshMatchFunction · 0.85
updateMatchAndRefreshFunction · 0.85
updateExcludeAndRefreshFunction · 0.85
pushMethod · 0.80
resetMatchMethod · 0.45
resetExcludeMethod · 0.45

Tested by

no test coverage detected