Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以添加LeetCode的LinkCard #438

Closed
1 task done
liyown opened this issue Aug 21, 2024 · 3 comments
Closed
1 task done

是否可以添加LeetCode的LinkCard #438

liyown opened this issue Aug 21, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@liyown
Copy link
Contributor

liyown commented Aug 21, 2024

Feature description

可以根据题目获取相关信息,展示卡片,例如
<LinkCard source="leetcode" id="median-of-two-sorted-arrays" />

我大概参考源码的数据获取函数写了一个函数,但是开发环境不太好搭建,可以麻烦大佬测试加上去嘛。

const fetchLeetCodeQuestionData: FetchObject = {
    isValid: (id) => {
      // 检查 titleSlug 是否是一个有效的字符串
      return typeof id === 'string' && id.length > 0;
    },
    fetch: async (id, setCardInfo, setFullUrl) => {
      try {
        // 第一个请求:获取题目基本信息
        const questionTitleResponse = await fetchLeetCodeApi({
          query: `
            query questionTitle($titleSlug: String!) {
              question(titleSlug: $titleSlug) {
                questionId
                title
                isPaidOnly
                difficulty
                likes
                dislikes
                categoryTitle
              }
            }
          `,
          variables: { titleSlug: id },
        });
        const questionTitleData = camelcaseKeys(questionTitleResponse.data.question);
  
        // 第二个请求:获取题目翻译
        const translationResponse = await fetchLeetCodeApi({
          query: `
            query questionTranslations($titleSlug: String!) {
              question(titleSlug: $titleSlug) {
                translatedTitle
                translatedContent
              }
            }
          `,
          variables: { titleSlug: id },
        });
        const translationData = camelcaseKeys(translationResponse.data.question);
  
        // 第三个请求:获取题目标签
        const tagsResponse = await fetchLeetCodeApi({
          query: `
            query singleQuestionTopicTags($titleSlug: String!) {
              question(titleSlug: $titleSlug) {
                topicTags {
                  name
                  translatedName
                }
              }
            }
        `,
        variables: { titleSlug: id },
      });
      const tagsData = camelcaseKeys(tagsResponse.data.question);
      
      // 设置卡片信息
      setCardInfo({
        title: (
          <span className="flex items-center gap-2">
            <span className="flex-1">{translationData.translatedTitle || questionTitleData.title}</span>
            <span className="shrink-0 self-end justify-self-end">
              {questionTitleData.likes > 0 && (
                <span className="inline-flex shrink-0 items-center gap-1 self-center text-sm text-orange-400 dark:text-yellow-500">
                  <i className="icon-[mingcute--thumb-up-line]" />
                  <span className="font-sans font-medium">
                    {questionTitleData.likes}
                  </span>
                </span>
              )}
            </span>
          </span>
        ),
        desc: tagsData.topicTags.map(tag => tag.translatedName || tag.name).join(', '),
        image: "https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png",
        color: getDifficultyColor(questionTitleData.difficulty), 
      });

      setFullUrl(`https://leetcode.cn/problems/${id}/`);
    } catch (err) {
      console.error('Error fetching LeetCode question data:', err);
      throw err;
    }
  },
};

// 通用的 GraphQL 请求函数
async function fetchLeetCodeApi(requestBody) {
  const response = await fetch('https://leetcode.cn/graphql/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(requestBody),
  });

  if (!response.ok) {
    throw new Error('Failed to fetch data from LeetCode API');
  }

  return await response.json();
}

// 映射难度到颜色的函数
function getDifficultyColor(difficulty) {
  switch (difficulty) {
    case 'Easy':
      return '#00BFA5';
    case 'Medium':
      return '#FFA726';
    case 'Hard':
      return '#F44336';
    default:
      return '#757575';
  }
}
  

Validations

  • You know your feature request won't be accepted.
@liyown liyown added the enhancement New feature or request label Aug 21, 2024
@Innei
Copy link
Owner

Innei commented Aug 21, 2024

你可以使用你自己的 api 进行测试,PR welcome

@liyown
Copy link
Contributor Author

liyown commented Aug 21, 2024

PR 已经提交,请大佬 review and decide merge

@Innei
Copy link
Owner

Innei commented Aug 24, 2024

Merged

@Innei Innei closed this as completed Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants