Skip to content

Commit

Permalink
🎨 拆分index中推荐工具的生成算法
Browse files Browse the repository at this point in the history
  • Loading branch information
neila-a committed Feb 5, 2024
1 parent 828bfbd commit 10827aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/app/index/generateTries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use client";
import {
tool
} from "../tools/info";
import {
not
} from '../components/TransferList';
const generateTries = (mostUsed: string, realTools: tool[]) => {
const unUsed = not(realTools.map(single => single.to), Object.keys(JSON.parse(mostUsed))).slice(0, 3), isUnFull = unUsed.length < 3, // 判断没用过的工具有没有三个
toFill = (Object.entries(JSON.parse(mostUsed)) as [string, number][]).sort((r, g) => {
if (r[1] < g[1]) {
return -1;
} if (r[1] > g[1]) {
return 1;
}
return 0;
}).map(single => single[0]).slice(0, 3 - unUsed.length); // 如果没用过的工具连三个都没有,那么就从使用最少的工具里选几个
return (isUnFull ? unUsed : unUsed.concat(toFill)).map(to => {
var tool: tool;
realTools.forEach(single => {
if (single.to === to) {
tool = single;
}
});
return tool;
});
};
export default generateTries;
26 changes: 2 additions & 24 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ import {
} from './layout/layoutClient';
import stringToBoolean from './setting/stringToBoolean';
import getParamTools from './index/getParamTools';
import {
not
} from './components/TransferList';
import VerkfiIcon from './components/verkfiIcon/verkfiIcon';
import generateTries from './index/generateTries';
export default function Index(props: {
/**
* 是否为嵌入
Expand Down Expand Up @@ -89,27 +87,7 @@ export default function Index(props: {
[showTries, setShowTries] = useState<boolean>(false),
[tools, setTools] = useState(toolsList),
[show, setShow] = useState<"tools" | "home">(props.isImplant ? "tools" : "home"),
tries = useMemo(() => {
const unUsed = not(realTools.map(single => single.to), Object.keys(JSON.parse(mostUsed))).slice(0, 3),
isUnFull = unUsed.length < 3, // 判断没用过的工具有没有三个
toFill = (Object.entries(JSON.parse(mostUsed)) as [string, number][]).sort((r, g) => {
if (r[1] < g[1]) {
return -1;
} if (r[1] > g[1]) {
return 1;
}
return 0;
}).map(single => single[0]).slice(0, 3 - unUsed.length);// 如果没用过的工具连三个都没有,那么就从使用最少的工具里选几个
return (isUnFull ? unUsed : unUsed.concat(toFill)).map(to => {
var tool: tool;
realTools.forEach(single => {
if (single.to === to) {
tool = single;
}
});
return tool;
});
}, [mostUsed]),
tries = useMemo(() => generateTries(mostUsed, realTools), [mostUsed, realTools]),
[sortingFor, setSortingFor] = useState<string>(props.isImplant ? "__global__" : "__home__");
if (props.setExpand) {
var {
Expand Down

0 comments on commit 10827aa

Please sign in to comment.