import { query } from '@/modules';
import { message } from 'antd';
import { create } from 'zustand';
export const getCodeFromUrl = () => {
  const searchParams = new URLSearchParams(window.location.search);
  return searchParams.get('code') || '';
};
type GithubStore = {
  githubToken?: string;
  getToken: () => Promise<any>;
};
export const useGithubStore = create<GithubStore>((set) => ({
  githubToken: '',
  getToken: async () => {
    const code = getCodeFromUrl();
    if (!code) {
      message.error('code不存在');
    }
    const loaded = message.loading('获取token中', 0);
    const res = await query.post({
      path: 'github',
      key: 'token',
      code,
    });
    loaded();
    if (res.code === 200) {
      const { githubToken } = res.data;
      set({ githubToken: githubToken });
      localStorage.setItem('githubToken', githubToken);
    }
  },
}));
