From 1336d96a354d15f82c04dc478f17ca0a3e54e914 Mon Sep 17 00:00:00 2001 From: ZLY201 <951711127@qq.com> Date: Fri, 7 Jun 2024 14:23:14 +0800 Subject: [PATCH] feat: add share button --- config/i18n.json | 16 +++++++++++++ src/modules/Question/Solution.tsx | 2 +- src/modules/Results/index.module.less | 3 +++ src/modules/Results/index.tsx | 34 ++++++++++++++++++++------- src/utils/type-challenges.ts | 17 ++++++++++---- 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/config/i18n.json b/config/i18n.json index b0c8912..104f337 100644 --- a/config/i18n.json +++ b/config/i18n.json @@ -58,5 +58,21 @@ "no_question": { "en": "No Result", "zh-CN": "暂无数据" + }, + "compilation_error": { + "en": "Compilation Error", + "zh-CN": "编译失败" + }, + "compilation_success": { + "en": "Compilation Successful", + "zh-CN": "编译成功" + }, + "compilation_success_info": { + "en": "\uD83C\uDF89 Yay! You have finished this challenge.", + "zh-CN": "\uD83C\uDF89 恭喜你完成了这个挑战!" + }, + "share_solution": { + "en": "Share your Solution", + "zh-CN": "分享你的解答" } } diff --git a/src/modules/Question/Solution.tsx b/src/modules/Question/Solution.tsx index 32df415..e4488ef 100644 --- a/src/modules/Question/Solution.tsx +++ b/src/modules/Question/Solution.tsx @@ -28,7 +28,7 @@ const Solution = function () { return (
- +
diff --git a/src/modules/Results/index.module.less b/src/modules/Results/index.module.less index 01efeb8..8d9e383 100644 --- a/src/modules/Results/index.module.less +++ b/src/modules/Results/index.module.less @@ -31,6 +31,9 @@ border-radius: 8px; font-size: 16px; } + .result-accept-btns { + margin-top: 24px; + } } .result-accept { .result-accept-title { diff --git a/src/modules/Results/index.tsx b/src/modules/Results/index.tsx index c466686..cb3b7e0 100644 --- a/src/modules/Results/index.tsx +++ b/src/modules/Results/index.tsx @@ -1,12 +1,14 @@ import type { editor } from 'monaco-editor'; import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; -import { Skeleton } from '@arco-design/web-react'; +import { Button, Skeleton } from '@arco-design/web-react'; import localCache, { QUESTION_STATUS } from '@src/utils/local-cache'; import emitter from '@src/utils/emit'; import Context from '@src/utils/context'; import { monacoEditorLoaded, monacoInstance } from '@src/utils/monaco'; import { QuestionFiles } from '@src/utils/type-challenges'; import SubmitStatus from '@src/components/SubmitStatus'; +import i18n from '@config/i18n.json'; +import { Setting } from '@src/utils/setting'; import styles from './index.module.less'; function formatErrorFromMarkers(markers: editor.IMarker[]) { @@ -17,12 +19,12 @@ function formatErrorFromMarkers(markers: editor.IMarker[]) { }); } -function createResultError(status: string[]) { +function createResultError(status: string[], language: Setting['language']) { return (
- Compilation Error + {i18n['compilation_error'][language]}
{status.map(function (error) { @@ -38,10 +40,15 @@ function createResultError(status: string[]) { } const Results = function () { - const [{ currentQuestion }] = useContext(Context); + const [{ currentQuestion, setting: { language } }] = useContext(Context); const [loading, setLoading] = useState(true); const [status, setStatus] = useState([]); + const shareSolutionHref = useMemo(function () { + const questionNum = currentQuestion.match(/[0-9]+(?=-)/)?.[0]; + return `https://tsch.js.org/${Number(questionNum)}/answer/${language === 'en' ? '' : language}`; + }, [currentQuestion, language]); + const resultContent = useMemo( function () { if (status.length === 0) { @@ -49,18 +56,29 @@ const Results = function () {
- Compilation Successful + {i18n['compilation_success'][language]}
- 🎉 Yay! You have finished this challenge. + {i18n['compilation_success_info'][language]} +
+
+
); } else { - return createResultError(status); + return createResultError(status, language); } }, - [status], + [status, language], ); const validate = useCallback( diff --git a/src/utils/type-challenges.ts b/src/utils/type-challenges.ts index 1925bd5..e7c62f8 100644 --- a/src/utils/type-challenges.ts +++ b/src/utils/type-challenges.ts @@ -131,24 +131,31 @@ class TypeChallenges { } return this.info || {}; } - async getSolution(cnt: number = 0): Promise { + async getSolution(cnt: number = 3): Promise { if (this.solution) { return this.solution; } - if (cnt === 3) { + if (cnt === 0) { return 'Get solution failed!'; } - cnt += 1; const id = this.id; const index = id.match(/[0-9]+(?=-)/)?.[0]; if (!index) { return 'Get solution failed!'; } + const controller = new AbortController(); const res = await fetch( // eslint-disable-next-line max-len 'https://api.github.com/repos/type-challenges/type-challenges/issues?&sort=reactions-+1-desc&per_page=1&labels=answer,' + - String(Number(index)), + String(Number(index)),{ + headers: { + Accept: 'application/vnd.github+json', + 'X-GitHub-Api-Version': '2022-11-28' + }, + signal: controller.signal, + } ); + setTimeout(() => controller.abort(), 5000); try { const solutions = await res.json(); const solution = solutions?.[0]; @@ -156,7 +163,7 @@ class TypeChallenges { this.solution = `${html_url}\n${body}`; return this.solution; } catch { - return await this.getSolution(cnt); + return await this.getSolution(cnt - 1); } } }