diff --git a/functions/_middleware.js b/functions/_middleware.js index a01ebf8cb8f..1ecc5ac441e 100644 --- a/functions/_middleware.js +++ b/functions/_middleware.js @@ -22,7 +22,8 @@ const MOVED_APP_PAGES = new Set(['/player', '/fullscreen', '/addons', '/credits' const PROJECT_PAGE = /^\/\d+(?:\/(editor|fullscreen))?$/; const PROJECT_PAGE_ASSETS = {editor: '/editor', fullscreen: '/fullscreen'}; const FILE_PATH = /\/[^/]+\.(?!html$)[A-Za-z0-9]+$/; -const PROJECT_CODE_PATH = /^\/code(?:\/(?:commits|branches|pulls|releases)|\/(?:tree|blob)(?:\/[^/]+)+)?$/; +const PROJECT_CODE_PATH = + /^\/code(?:\/(?:commits|branches|pulls|releases|issues)|\/issues\/\d+|\/(?:tree|blob)(?:\/[^/]+)+)?$/; // The app pages live at the root, but people guess /editor/fullscreen and the like from the editor's URL. const EDITOR_PREFIXED_APP_PAGE = /^\/editor(\/[^/]+)$/; // Standalone HTML entries with their own tags; the community SPA is index.html. diff --git a/package.json b/package.json index 0a9d43c2add..2bcb49aa2e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scratch-gui", - "version": "3.10.0", + "version": "3.11.0", "private": true, "description": "MistWarp editor, player, and community site", "author": "MistWarp contributors", diff --git a/src/community/api/comments.js b/src/community/api/comments.js index c07d211ad66..97c6041b428 100644 --- a/src/community/api/comments.js +++ b/src/community/api/comments.js @@ -21,6 +21,8 @@ export default { request(`/projects/${id}/comments/${commentId}/react`, {method: 'POST', body: {type}}), pinComment: (id, commentId, pinned) => request(`/projects/${id}/comments/${commentId}/pin`, {method: 'POST', body: {pinned}}), + setCommentIssue: (id, commentId, state) => + request(`/projects/${id}/comments/${commentId}/issue`, {method: 'PUT', body: {state}}), reactProfileComment: (name, commentId, type) => request(`/users/${encodeURIComponent(name)}/comments/${commentId}/react`, {method: 'POST', body: {type}}), pinProfileComment: (name, commentId, pinned) => diff --git a/src/community/api/query.js b/src/community/api/query.js index 04eb5bf99cd..aed3262ed4f 100644 --- a/src/community/api/query.js +++ b/src/community/api/query.js @@ -1,4 +1,6 @@ -export const commentQuery = ({offset = 0, limit = 20, anchor = '', all = false, sort = 'newest'} = {}) => { +export const commentQuery = ({ + offset = 0, limit = 20, anchor = '', all = false, sort = 'newest', issue = '', number = 0 +} = {}) => { const params = new URLSearchParams(); if (all) { params.set('all', '1'); @@ -8,6 +10,10 @@ export const commentQuery = ({offset = 0, limit = 20, anchor = '', all = false, } if (anchor) params.set('anchor', anchor); if (sort === 'donations') params.set('sort', 'donations'); + if (['open', 'done', 'any'].includes(issue)) { + params.set('issue', issue); + if (Number(number) > 0) params.set('number', String(Math.floor(Number(number)))); + } return params.toString(); }; diff --git a/src/community/comment-issues.js b/src/community/comment-issues.js new file mode 100644 index 00000000000..544d69d1c41 --- /dev/null +++ b/src/community/comment-issues.js @@ -0,0 +1,44 @@ +import {sameUser} from './format'; + +export const ISSUE_FILTERS = ['open', 'done']; + +export const commentIssueState = comment => { + const state = comment && comment.issue && comment.issue.state; + return state === 'open' || state === 'done' ? state : ''; +}; + +export const commentIssueNumber = comment => { + if (!commentIssueState(comment)) return 0; + const number = Number(comment.issue.number); + return Number.isFinite(number) && number > 0 ? number : 0; +}; + +export const issueTitle = (content, limit = 120) => { + const line = String(content || '').split('\n') + .map(part => part.trim()) + .find(Boolean) || ''; + return line.length > limit ? `${line.slice(0, limit - 1).trimEnd()}…` : line; +}; + +export const canChangeIssue = (comment, user, canTriage) => Boolean(user && comment && !comment.parent) && + (Boolean(canTriage) || sameUser(comment.author, user.username)); + +export const mergeCommentUpdate = (comment, update) => { + const next = {...comment, ...update}; + if (!update || !update.issue) delete next.issue; + return next; +}; + +export const issueThreads = comments => { + const replies = new Map(); + for (const comment of comments || []) { + if (!comment.parent) continue; + if (!replies.has(comment.parent)) replies.set(comment.parent, []); + replies.get(comment.parent).push(comment); + } + for (const list of replies.values()) list.sort((a, b) => (a.created || 0) - (b.created || 0)); + return (comments || []) + .filter(comment => !comment.parent && commentIssueState(comment)) + .sort((a, b) => commentIssueNumber(b) - commentIssueNumber(a)) + .map(root => ({root, replies: replies.get(root.id) || []})); +}; diff --git a/src/community/components/CommentThread.jsx b/src/community/components/CommentThread.jsx index 56ae67f09d6..a0bc70675e3 100644 --- a/src/community/components/CommentThread.jsx +++ b/src/community/components/CommentThread.jsx @@ -2,7 +2,10 @@ import {getCommunityLocale} from '../locale.js'; import {useCommunityIntl as useCommunityText} from '../i18n.jsx'; import React, {useEffect, useState, useCallback, useMemo, useRef} from 'react'; import {Link} from 'react-router-dom'; -import {Reply, Search, MoreHorizontal, MessageSquare, Pencil, Flag, Trash2, Coins, Pin, Trophy} from 'lucide-react'; +import { + Reply, Search, MoreHorizontal, MessageSquare, Pencil, Flag, Trash2, Coins, Pin, Trophy, CircleDot, CircleCheck, + CircleSlash, RotateCcw +} from 'lucide-react'; import {useUser} from '../UserContext.jsx'; import useAfterLogin from '../use-after-login.js'; import Avatar from './Avatar.jsx'; @@ -21,6 +24,7 @@ import {timeAgo, sameUser, formatPlaytime} from '../format'; import {payWithRotur} from '../../lib/rotur/payment-window.js'; import useLatest from '../use-latest.js'; import {readSessionDraft, writeSessionDraft} from '../session-draft.js'; +import {commentIssueNumber, commentIssueState, mergeCommentUpdate} from '../comment-issues.js'; import styles from './CommentThread.module.css'; const commentKindOptions = text => [ @@ -87,10 +91,25 @@ const kindLabel = (kind, text) => commentKindOptions(text).find(item => item.val const CommentRow = ({ comment, onReply, onDelete, onEdit, onSaveEdit, onCancelEdit, onReact, onPin, onReport, canReply, canDelete, canEdit, canPin, canReport, deleting, editing, editText, editBusy, onEditTextChange, reacting, pinning, - isReply, id, authorBadge + isReply, id, authorBadge, canIssue, canTrackIssue, onIssue, issueBusy, issueHref }) => { const {text: communityText} = useCommunityText(); - const hasMenu = canEdit || canPin || canReport || canDelete; + const issueState = commentIssueState(comment); + const issueNumber = commentIssueNumber(comment); + const showIssueActions = Boolean(canIssue && (issueState || canTrackIssue)); + const hasMenu = canEdit || canPin || canReport || canDelete || showIssueActions; + const issueLabel = issueState === 'done' ? + communityText('Done #{value1}', {value1: issueNumber}) : + communityText('Issue #{value1}', {value1: issueNumber}); + const issueBadge = issueState ? ( + + {issueState === 'done' ? + + ) : null; + const issueClass = `${styles.issue} ${issueState === 'done' ? styles.issueDone : ''}`; const donationTier = commentDonationTier(donationAmount(comment)); return (
) : null} + {issueState && issueHref ? ( + {issueBadge} + ) : issueState ? {issueBadge} : null} {comment.pinned ? ( @@ -191,6 +213,33 @@ const CommentRow = ({ {comment.pinned ? communityText('Unpin') : communityText('Pin to top')} : null} + {showIssueActions && canTrackIssue && !issueState ? { + close(); onIssue('open'); + }} + >{communityText('Track as issue')} : null} + {showIssueActions && canTrackIssue && issueState === 'open' ? { + close(); onIssue('done'); + }} + >{communityText('Mark as done')} : null} + {showIssueActions && canTrackIssue && issueState === 'done' ? { + close(); onIssue('open'); + }} + >{communityText('Reopen issue')} : null} + {showIssueActions && issueState ? { + close(); onIssue('none'); + }} + > + + {communityText('Stop tracking as issue')} + : null} {canReport ? { close(); onReport(); @@ -327,7 +376,7 @@ const InlineComposer = ({ const CommentThread = ({ source, canModerate, canPin = false, disabled, disabledReason, reportContext, projectComments = false, - composerAction, onCountChange = null, donationRecipient = '', draftKey = '', authorBadges = null + composerAction, onCountChange = null, donationRecipient = '', draftKey = '', authorBadges = null, issues = null }) => { const {text: communityText} = useCommunityText(); const {user} = useUser(); @@ -358,6 +407,7 @@ const CommentThread = ({ const [editBusy, setEditBusy] = useState(false); const [reactingIds, setReactingIds] = useState({}); const [pinningIds, setPinningIds] = useState({}); + const [issueIds, setIssueIds] = useState({}); const [rootLimit, setRootLimit] = useState(ROOT_PAGE); const [totalRoots, setTotalRoots] = useState(0); const [nextOffset, setNextOffset] = useState(0); @@ -532,7 +582,7 @@ const CommentThread = ({ } if (event.type === 'comment_edited' && event.comment) { setComments(current => current.map(comment => ( - comment.id === event.comment.id ? {...comment, ...event.comment} : comment + comment.id === event.comment.id ? mergeCommentUpdate(comment, event.comment) : comment ))); return; } @@ -773,6 +823,32 @@ const CommentThread = ({ } }; + const setIssue = async (commentId, state) => { + if (!source.issue || !user) return; + const actionSource = source; + const actionViewer = viewerName; + const releaseAction = beginAction(actionSource, actionViewer, `issue:${commentId}`); + if (!releaseAction) return; + markPending(setIssueIds, commentId, true); + setError(null); + try { + const result = await actionSource.issue(commentId, state); + if (sourceRef.current !== actionSource || viewerRef.current !== actionViewer) return; + if (result && result.comment) { + setComments(cs => cs.map(c => (c.id === commentId ? mergeCommentUpdate(c, result.comment) : c))); + } + } catch (e) { + if (sourceRef.current === actionSource && viewerRef.current === actionViewer) { + setError(e.message || communityText('Could not update the issue.')); + } + } finally { + releaseAction(); + if (sourceRef.current === actionSource && viewerRef.current === actionViewer) { + markPending(setIssueIds, commentId, false); + } + } + }; + const openReply = (rootId, prefill = '') => { setReplyTo(rootId); setReplyText(prefill); @@ -783,6 +859,8 @@ const CommentThread = ({ (canModerate || user.isAdmin || sameUser(comment.author, user.username)); const canEdit = comment => Boolean(source.edit && user) && sameUser(comment.author, user.username); const canPinComment = comment => Boolean(canPin && source.pin && user) && !comment.parent; + const canIssueComment = comment => Boolean(issues && source.issue && user) && !comment.parent && + (Boolean(issues.canTriage) || sameUser(comment.author, user.username)); const canReport = comment => Boolean(user) && !sameUser(comment.author, user.username); const authorBadgeFor = author => (authorBadges && author ? authorBadges[String(author).toLowerCase()] || '' : ''); const canReply = Boolean(user) && !disabled; @@ -936,6 +1014,11 @@ const CommentThread = ({ canDelete={canDelete(comment)} canEdit={canEdit(comment)} canPin={canPinComment(comment)} + canIssue={canIssueComment(comment)} + canTrackIssue={Boolean(issues && issues.enabled)} + onIssue={state => setIssue(comment.id, state)} + issueBusy={Boolean(issueIds[comment.id])} + issueHref={issues && issues.href} editing={editingId === comment.id} editBusy={editBusy} canReport={canReport(comment)} diff --git a/src/community/components/CommentThread.module.css b/src/community/components/CommentThread.module.css index a40637e16c5..4742189dc91 100644 --- a/src/community/components/CommentThread.module.css +++ b/src/community/components/CommentThread.module.css @@ -102,6 +102,29 @@ font-size: var(--fs-xs); } +.issue { + display: inline-flex; + align-items: center; + gap: 3px; + padding: 2px 7px; + border-radius: var(--radius-2xs); + background: color-mix(in srgb, var(--mw-success) 16%, transparent); + color: var(--mw-success); + font-size: var(--fs-2xs); + font-weight: 700; + white-space: nowrap; + text-decoration: none; +} + +.issueDone { + background: var(--accent-softer); + color: var(--accent-strong); +} + +a.issue:hover { + text-decoration: underline; +} + .pinned { display: inline-flex; align-items: center; diff --git a/src/community/pages/ManageProject.jsx b/src/community/pages/ManageProject.jsx index 8bd6cf4839d..50063b23b10 100644 --- a/src/community/pages/ManageProject.jsx +++ b/src/community/pages/ManageProject.jsx @@ -243,6 +243,7 @@ const ManageProject = () => { brandingTagline: project.branding?.tagline || '', remixShare: project.remixShare || 0, commentsOff: Boolean(project.commentsOff), + issuesOff: Boolean(project.issuesOff), remixable: project.remixable !== false, seeInside: project.seeInside !== false, visibility: project.visibility || (project.shared ? 'public' : 'private') @@ -304,6 +305,7 @@ const ManageProject = () => { tagline: form.brandingTagline.trim() } : {}, commentsOff: form.commentsOff, + issuesOff: form.issuesOff, remixable: form.remixable, seeInside: form.seeInside, remixShare: Math.max(0, Math.min(50, Number(form.remixShare) || 0)) @@ -660,6 +662,7 @@ const ManageProject = () => { set('seeInside', value)} />

{communityText('When enabled, See inside gives access to project files and version history.')}

set('commentsOff', value)} /> + set('issuesOff', value)} />
{status} diff --git a/src/community/pages/Project.jsx b/src/community/pages/Project.jsx index 071e08d6fde..a5a221cbf3c 100644 --- a/src/community/pages/Project.jsx +++ b/src/community/pages/Project.jsx @@ -587,6 +587,7 @@ const Project = () => { edit: (commentId, content) => api.editComment(id, commentId, content), react: (commentId, type) => api.reactComment(id, commentId, type), pin: (commentId, pinned) => api.pinComment(id, commentId, pinned), + issue: (commentId, state) => api.setCommentIssue(id, commentId, state), subscribe: listener => projectRealtime.subscribe( id, listener, diff --git a/src/community/pages/ProjectCode.jsx b/src/community/pages/ProjectCode.jsx index 3f2203c8fb6..e31d97888cc 100644 --- a/src/community/pages/ProjectCode.jsx +++ b/src/community/pages/ProjectCode.jsx @@ -25,6 +25,8 @@ import SelectMenu from '../components/ui/SelectMenu.jsx'; import StatusMessage from '../components/ui/StatusMessage.jsx'; import CodeBrowser from './project/CodeBrowser.jsx'; import HistoryList from './project/HistoryList.jsx'; +import IssueList from './project/IssueList.jsx'; +import IssueThread from './project/IssueThread.jsx'; import ProjectPreview from './project/ProjectPreview.jsx'; import PullList from './project/PullList.jsx'; import ReleaseList from './project/ReleaseList.jsx'; @@ -32,10 +34,11 @@ import useProjectData from './project/use-project-data.js'; import styles from './ProjectCode.module.css'; const TAB_ID_PREFIX = 'project-code'; -export const CODE_TABS = ['code', 'commits', 'branches', 'pulls', 'releases']; +export const CODE_TABS = ['code', 'issues', 'commits', 'branches', 'pulls', 'releases']; export const codeLocation = splat => { const parts = String(splat || '').split('/').filter(Boolean); if (parts[0] === 'tree' || parts[0] === 'blob') return {tab: 'code', kind: parts[0], path: parts.slice(1).join('/')}; + if (parts[0] === 'issues' && /^\d+$/.test(parts[1] || '')) return {tab: 'issues', kind: 'issue', path: '', number: Number(parts[1])}; return {tab: CODE_TABS.includes(parts[0]) ? parts[0] : 'code', kind: 'tree', path: ''}; }; export const codeTabPath = (baseUrl, tab) => (tab === 'code' ? `${baseUrl}/code` : `${baseUrl}/code/${tab}`); @@ -82,6 +85,7 @@ const AboutPanel = ({project, baseUrl, counts, fileCount, contributors}) => { const rows = [ {key: 'commits', tab: 'commits', value: counts.commits, one: communityText('1 commit'), many: communityText('{value1} commits', {value1: counts.commits})}, {key: 'branches', tab: 'branches', value: counts.branches, one: communityText('1 branch'), many: communityText('{value1} branches', {value1: counts.branches})}, + {key: 'issues', tab: 'issues', value: counts.issues, one: communityText('1 open issue'), many: communityText('{value1} open issues', {value1: counts.issues})}, {key: 'pulls', tab: 'pulls', value: counts.pulls, one: communityText('1 open pull request'), many: communityText('{value1} open pull requests', {value1: counts.pulls})}, {key: 'releases', tab: 'releases', value: counts.releases, one: communityText('1 release'), many: communityText('{value1} releases', {value1: counts.releases})} ].filter(row => typeof row.value === 'number'); @@ -136,6 +140,7 @@ const ProjectCodePage = ({id, vanitySlug}) => { const [branches, setBranches] = useState(null); const [openPulls, setOpenPulls] = useState(null); const [releaseCount, setReleaseCount] = useState(null); + const [issueCounts, setIssueCounts] = useState(null); const [fileCount, setFileCount] = useState(null); const beginExtras = useLatest(); const place = codeLocation(params['*']); @@ -156,11 +161,16 @@ const ProjectCodePage = ({id, vanitySlug}) => { api.branches(id).then(fresh(data => setBranches(data.branches || []))).catch(fresh(() => setBranches([]))); api.pulls(id).then(fresh(data => setOpenPulls((data.pulls || []).filter(pull => pull.state === 'open').length))).catch(() => {}); api.releases(id).then(fresh(data => setReleaseCount((data.releases || []).length))).catch(() => {}); + api.getComments(id, {limit: 1, issue: 'open'}).then(fresh(data => setIssueCounts(data.issueCounts || null))).catch(() => {}); }, [beginExtras, canView, id]); + const refreshIssueCounts = useCallback(() => { + api.getComments(id, {limit: 1, issue: 'open'}).then(data => setIssueCounts(data.issueCounts || null)).catch(() => {}); + }, [id]); useEffect(() => { setBranches(null); setOpenPulls(null); setReleaseCount(null); + setIssueCounts(null); setFileCount(null); loadBranches(); }, [loadBranches]); @@ -186,8 +196,12 @@ const ProjectCodePage = ({id, vanitySlug}) => { commits: project ? Number(project.commitCount) || (versionHistory?.commits?.length ?? null) : null, branches: branches ? branches.length : null, pulls: openPulls, - releases: releaseCount + releases: releaseCount, + issues: issueCounts ? issueCounts.open : null }; + const hasIssues = Boolean(issueCounts && (issueCounts.open || issueCounts.done)); + const visibleTabs = CODE_TABS.filter(key => key !== 'issues' || !project || !project.issuesOff || + hasIssues || issueCounts === null || tab === 'issues'); const pickBranch = name => { const next = new URLSearchParams(searchParams); @@ -205,6 +219,7 @@ const ProjectCodePage = ({id, vanitySlug}) => { const tabLabels = { code: communityText('Code'), + issues: {communityText('Issues')}, commits: {communityText('Commits')}, branches: {communityText('Branches')}, pulls: {communityText('Pull requests')}, @@ -252,7 +267,7 @@ const ProjectCodePage = ({id, vanitySlug}) => { idPrefix={TAB_ID_PREFIX} value={tab} onChange={showTab} - items={CODE_TABS.map(key => ({key, label: tabLabels[key]}))} + items={visibleTabs.map(key => ({key, label: tabLabels[key]}))} />
{tab === 'code' ? ( @@ -301,6 +316,12 @@ const ProjectCodePage = ({id, vanitySlug}) => { ) : null}
) : null} + {tab === 'issues' && place.kind === 'issue' ? ( + + ) : null} + {tab === 'issues' && place.kind !== 'issue' ? ( + + ) : null} {tab === 'commits' ? ( ) : null} diff --git a/src/community/pages/ProjectCode.module.css b/src/community/pages/ProjectCode.module.css index 8ebbf081f86..f9c7cb592b5 100644 --- a/src/community/pages/ProjectCode.module.css +++ b/src/community/pages/ProjectCode.module.css @@ -515,6 +515,171 @@ a.rowMessage:hover { outline-offset: -2px; } +.issueRow div > .issueLatest { + display: flex; + gap: var(--space-2); + min-width: 0; + margin-top: var(--space-1); + padding: var(--space-2) var(--space-3); + border-radius: var(--radius-xs); + background: var(--bg-raised); + color: var(--text-dim); + font-size: var(--fs-sm); + font-weight: 400; +} + +.issueLatest b { + flex: none; + color: var(--mw-text); + font-weight: 600; +} + +.issueLatest span { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.issueRow div > .issueLatest:hover { + color: var(--mw-text); +} + +.issueComposer { + display: grid; + gap: var(--space-3); + padding: var(--space-4); + border-radius: var(--radius-sm); + background: var(--bg-card); +} + +.issueComposer textarea { + width: 100%; + min-height: 88px; + padding: var(--space-3); + border: 0; + border-radius: var(--radius-xs); + background: var(--bg-field); + color: var(--mw-text); + font: inherit; + font-size: var(--fs-md); + resize: vertical; +} + +.issueComposerActions { + display: flex; + flex-wrap: wrap; + justify-content: flex-end; + gap: var(--space-2); +} + +.issueThread { + display: grid; + gap: var(--space-4); + max-width: var(--maxw-mid); +} + +.issueBack { + display: inline-flex; + align-items: center; + justify-self: start; + gap: var(--space-1); + color: var(--text-dim); + font-size: var(--fs-sm); + font-weight: 600; +} + +.issueBack:hover { + color: var(--accent-strong); +} + +.issueHead h2 { + margin: 0 0 var(--space-2); + font-size: var(--fs-xl); + font-weight: 600; + overflow-wrap: anywhere; +} + +.issueHead h2 span { + color: var(--text-dim); + font-weight: 400; +} + +.issueMeta { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: var(--space-3); + color: var(--text-dim); + font-size: var(--fs-sm); +} + +.issueMeta > span { + display: inline-flex; + align-items: center; + gap: var(--space-1); +} + +.issueMeta > .issueState { + padding: var(--space-1) var(--space-3); + border-radius: var(--radius); + background: var(--mw-success); + color: #ffffff; + font-size: var(--fs-md); + font-weight: 600; +} + +.issueState.issueStateDone { + background: var(--accent); + color: var(--accent-contrast); + font-size: var(--fs-md); + font-weight: 600; +} + +.issueComments { + display: grid; + gap: var(--space-3); +} + +.issueComment { + display: grid; + grid-template-columns: auto minmax(0, 1fr); + gap: var(--space-3); + padding: var(--space-4); + border-radius: var(--radius-sm); + background: var(--bg-card); +} + +.issueReply { + margin-left: var(--space-6); + background: var(--bg-raised); +} + +.issueComment header { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: var(--space-2); + margin-bottom: var(--space-1); + font-size: var(--fs-sm); +} + +.issueComment header a { + color: var(--mw-text); + font-weight: 600; +} + +.issueComment header span { + color: var(--text-dim); +} + +.issueComment p { + margin: 0; + font-size: var(--fs-md); + line-height: 1.55; + white-space: pre-wrap; + overflow-wrap: anywhere; +} + @media (max-width: 1000px) { .codeLayout { grid-template-columns: minmax(0, 1fr); @@ -528,6 +693,10 @@ a.rowMessage:hover { } @media (max-width: 620px) { + .issueReply { + margin-left: var(--space-4); + } + .header { align-items: flex-start; flex-direction: column; diff --git a/src/community/pages/project/IssueList.jsx b/src/community/pages/project/IssueList.jsx new file mode 100644 index 00000000000..e73aee8c40b --- /dev/null +++ b/src/community/pages/project/IssueList.jsx @@ -0,0 +1,244 @@ +/* eslint-disable max-len */ +import PropTypes from 'prop-types'; +import React, {useCallback, useEffect, useState} from 'react'; +import {Link} from 'react-router-dom'; +import {CircleCheck, CircleDot, MessageSquare, Plus, Search} from 'lucide-react'; +import {useCommunityIntl as useCommunityText} from '../../i18n.jsx'; +import api from '../../api'; +import {timeAgo} from '../../format'; +import useLatest from '../../use-latest.js'; +import {useUser} from '../../UserContext.jsx'; +import {commentIssueNumber, issueThreads, issueTitle} from '../../comment-issues.js'; +import Avatar from '../../components/Avatar.jsx'; +import UserLink from '../../components/UserLink.jsx'; +import Button from '../../components/ui/Button.jsx'; +import EmptyState from '../../components/ui/EmptyState.jsx'; +import StatusMessage from '../../components/ui/StatusMessage.jsx'; +import projectStyles from '../Project.module.css'; +import styles from '../ProjectCode.module.css'; + +const NewIssue = ({id, onCreated, onCancel}) => { + const {text: communityText} = useCommunityText(); + const [text, setText] = useState(''); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + const [posted, setPosted] = useState(null); + const submit = async event => { + event.preventDefault(); + const content = text.trim(); + if ((!content && !posted) || busy) return; + setBusy(true); + setError(''); + let comment = posted; + try { + if (!comment) { + const created = await api.addComment(id, content, ''); + comment = created && created.comment ? created.comment : null; + if (!comment || !comment.id) throw new Error(communityText('Could not open the issue.')); + setPosted(comment); + } + const tracked = await api.setCommentIssue(id, comment.id, 'open'); + onCreated(tracked && tracked.comment ? tracked.comment : comment); + } catch (e) { + setError(comment ? + communityText('Your comment was posted but could not be tracked as an issue. Try again to track it.') : + (e.message || communityText('Could not open the issue.'))); + setBusy(false); + } + }; + return ( +
+