use-sort-proxy.ts 862 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useMemo } from "react";
  2. import delayManager from "@/services/delay";
  3. // default | delay | alpha
  4. export type ProxySortType = 0 | 1 | 2;
  5. /**
  6. * sort the proxy
  7. */
  8. export default function useSortProxy(
  9. proxies: ApiType.ProxyItem[],
  10. groupName: string,
  11. sortType: ProxySortType
  12. ) {
  13. return useMemo(() => {
  14. if (!proxies) return [];
  15. if (sortType === 0) return proxies;
  16. const list = proxies.slice();
  17. if (sortType === 1) {
  18. list.sort((a, b) => {
  19. const ad = delayManager.getDelay(a.name, groupName);
  20. const bd = delayManager.getDelay(b.name, groupName);
  21. if (ad === -1 || ad === -2) return 1;
  22. if (bd === -1 || bd === -2) return -1;
  23. return ad - bd;
  24. });
  25. } else {
  26. list.sort((a, b) => a.name.localeCompare(b.name));
  27. }
  28. return list;
  29. }, [proxies, groupName, sortType]);
  30. }