mirror of https://github.com/msgbyte/tailchat
				
				
				
			refactor: 快速切换功能的整理与拆分
							parent
							
								
									40001997a9
								
							
						
					
					
						commit
						64be97d07f
					
				@ -0,0 +1,17 @@
 | 
			
		||||
import { useHistory } from 'react-router';
 | 
			
		||||
 | 
			
		||||
export interface QuickActionContext {
 | 
			
		||||
  navigate: (url: string) => void;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 快速切换操作上下文信息
 | 
			
		||||
 */
 | 
			
		||||
export function useQuickSwitcherActionContext(): QuickActionContext {
 | 
			
		||||
  const history = useHistory();
 | 
			
		||||
  return {
 | 
			
		||||
    navigate: (url) => {
 | 
			
		||||
      history.push(url);
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,42 @@
 | 
			
		||||
import { t } from 'tailchat-shared';
 | 
			
		||||
import _take from 'lodash/take';
 | 
			
		||||
import { useMemo } from 'react';
 | 
			
		||||
import type { QuickActionContext } from './useQuickSwitcherActionContext';
 | 
			
		||||
 | 
			
		||||
interface QuickAction {
 | 
			
		||||
  key: string;
 | 
			
		||||
  label: string;
 | 
			
		||||
  action: (context: QuickActionContext) => void;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const builtinActions: QuickAction[] = [
 | 
			
		||||
  {
 | 
			
		||||
    key: 'personal',
 | 
			
		||||
    label: t('个人主页'),
 | 
			
		||||
    action({ navigate }) {
 | 
			
		||||
      navigate('/main/personal/friends');
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    key: 'plugins',
 | 
			
		||||
    label: t('插件中心'),
 | 
			
		||||
    action({ navigate }) {
 | 
			
		||||
      navigate('/main/personal/plugins');
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 过滤快速搜索操作
 | 
			
		||||
 * @param keyword 关键字
 | 
			
		||||
 */
 | 
			
		||||
export function useQuickSwitcherFilteredActions(keyword: string) {
 | 
			
		||||
  const filteredActions = useMemo(() => {
 | 
			
		||||
    return _take(
 | 
			
		||||
      builtinActions.filter((action) => action.label.includes(keyword)),
 | 
			
		||||
      5
 | 
			
		||||
    );
 | 
			
		||||
  }, [keyword]);
 | 
			
		||||
 | 
			
		||||
  return filteredActions;
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue