use-sort-proxy.ts 888 B

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