Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scratch-gui",
"version": "3.7.0",
"version": "3.8.0",
"private": true,
"description": "MistWarp editor, player, and community site",
"author": "MistWarp contributors",
Expand Down
7 changes: 7 additions & 0 deletions src/community/api/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export default {
`/projects/${id}/commits/${encodeURIComponent(sha)}/changes${projectAccessQuery()}`,
{cache: false, timeoutMs: SLOW_GET_TIMEOUT}
),
commitSummaries: (id, shas) => {
const params = new URLSearchParams(projectAccessQuery().replace(/^\?/, ''));
params.set('shas', shas.join(','));
return request(`/projects/${id}/commit-summaries?${params.toString()}`, {
cache: false, timeoutMs: SLOW_GET_TIMEOUT
});
},
commitTree: (id, sha) => request(
`/projects/${id}/commits/${encodeURIComponent(sha)}/tree${projectAccessQuery()}`,
{cache: false, timeoutMs: SLOW_GET_TIMEOUT}
Expand Down
99 changes: 99 additions & 0 deletions src/community/commit-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const DEFAULT_MESSAGES = new Set(['', 'initial version', 'updated project', 'save project', 'untitled commit']);

export const SUMMARY_BATCH = 50;
export const SUMMARY_LIMIT = 100;

export const commitSubject = message => String(message || '').split('\n')[0].trim();

export const isDefaultCommitMessage = message => DEFAULT_MESSAGES.has(commitSubject(message).toLowerCase());

const formatList = (names, locale) => {
try {
return new Intl.ListFormat(locale ? [locale] : [], {style: 'long', type: 'conjunction'}).format(names);
} catch {
return names.join(', ');
}
};

export const summaryPlace = (summary, text, locale) => {
const names = Array.isArray(summary?.sprites) ? summary.sprites.filter(Boolean) : [];
const count = Math.max(Number(summary?.spriteCount) || 0, names.length);
if (!count) return '';
if (count <= 3 && names.length === count) return formatList(names, locale);
return text('{value1} sprites', {value1: count});
};

const sum = values => values.reduce((total, value) => total + (Number(value) || 0), 0);

export const commitSummarySentence = (summary, text, locale) => {
if (!summary || summary.ok === false || summary.legacy) return '';
const totals = summary.totals || {};
const scripts = totals.scripts || {};
const assets = totals.assets || {};
const scriptCount = sum([scripts.added, scripts.removed, scripts.changed, scripts.moved]);
const assetCount = sum([assets.added, assets.removed, assets.modified]);
const files = Number(summary.authoredFiles) || 0;
const place = summaryPlace(summary, text, locale);
if (scriptCount && place) {
if (scriptCount === Number(scripts.added)) {
return scriptCount === 1 ?
text('Added 1 script in {value1}', {value1: place}) :
text('Added {value1} scripts in {value2}', {value1: scriptCount, value2: place});
}
if (scriptCount === Number(scripts.removed)) {
return scriptCount === 1 ?
text('Removed 1 script in {value1}', {value1: place}) :
text('Removed {value1} scripts in {value2}', {value1: scriptCount, value2: place});
}
return scriptCount === 1 ?
text('Changed 1 script in {value1}', {value1: place}) :
text('Changed {value1} scripts in {value2}', {value1: scriptCount, value2: place});
}
if (assetCount && place) {
return assetCount === 1 ?
text('Changed 1 costume or sound in {value1}', {value1: place}) :
text('Changed {value1} costumes and sounds in {value2}', {value1: assetCount, value2: place});
}
if (assetCount) {
return assetCount === 1 ?
text('Changed 1 asset file') :
text('Changed {value1} asset files', {value1: assetCount});
}
if (Number(totals.items) && place) {
return text('Changed variables, lists or settings in {value1}', {value1: place});
}
if (summary.tooLarge && place) {
return text('Changed {value1}, too large to summarize', {value1: place});
}
if (files) {
return files === 1 ?
text('Changed 1 project file') :
text('Changed {value1} project files', {value1: files});
}
if (totals.stateOnly || Number(totals.state)) return text('Only saved state changed');
return text('No changes to how the project runs');
};

export const commitHeadline = (message, summary, text, locale) => {
const subject = commitSubject(message);
const sentence = commitSummarySentence(summary, text, locale);
if (isDefaultCommitMessage(subject) && sentence) return {title: sentence, detail: ''};
return {title: subject || text('Untitled commit'), detail: sentence};
};

export const pendingSummaryShas = (shas, known, limit = SUMMARY_LIMIT) => {
const seen = new Set();
const pending = [];
for (const sha of shas.slice(0, limit)) {
if (!/^[a-f0-9]{40}$/.test(String(sha)) || seen.has(sha) || known.has(sha)) continue;
seen.add(sha);
pending.push(sha);
}
return pending;
};

export const chunk = (values, size = SUMMARY_BATCH) => {
const chunks = [];
for (let index = 0; index < values.length; index += size) chunks.push(values.slice(index, index + size));
return chunks;
};
12 changes: 9 additions & 3 deletions src/community/components/GitGraph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {useCommunityIntl as useCommunityText} from '../i18n.jsx';
import React, {useMemo} from 'react';
import {Link} from 'react-router-dom';
import {safeDate} from '../format.js';
import {commitHeadline} from '../commit-summary.js';
import UserLink from './UserLink.jsx';
import {LANE_WIDTH, PADDING, layoutGraph} from '../../lib/git/graph-layout.js';
import styles from './GitGraph.module.css';
Expand All @@ -15,7 +16,7 @@ const point = node => ({
y: (node.row * ROW_HEIGHT) + (ROW_HEIGHT / 2)
});

const GitGraph = ({graph, currentBranch = 'main', onRestore, restoring, projectId = ''}) => {
const GitGraph = ({graph, currentBranch = 'main', onRestore, restoring, projectId = '', summaries = null}) => {
const {text: communityText} = useCommunityText();
const layout = useMemo(() => layoutGraph(graph, currentBranch), [graph, currentBranch]);
if (!layout.rows.length) return null;
Expand Down Expand Up @@ -60,6 +61,8 @@ const GitGraph = ({graph, currentBranch = 'main', onRestore, restoring, projectI
graph.branchLogs?.find(entry => entry.branch === branch)?.oids?.[0] === node.sha);
const nodeDate = safeDate(node.date);
const commitUrl = projectId ? `/project/${projectId}/commits/${node.sha}` : '';
const summary = summaries?.[node.sha];
const headline = commitHeadline(node.message, summary, communityText, getCommunityLocale());
return (
<li key={node.sha} className={styles.commit} style={{height: ROW_HEIGHT}}>
{commitUrl ? (
Expand All @@ -68,15 +71,15 @@ const GitGraph = ({graph, currentBranch = 'main', onRestore, restoring, projectI
to={commitUrl}
aria-current={node.sha === currentHead ? 'page' : null}
aria-label={communityText('View commit {value1}', {
value1: (node.message || node.sha).split('\n')[0]
value1: headline.title
})}
/>
) : null}
<div className={styles.subject}>
{tipBranches.map(branch => (
<span key={branch} className={styles.branch}>{branch}</span>
))}
<span>{(node.message || 'Untitled commit').split('\n')[0]}</span>
<span>{headline.title}</span>
</div>
<div className={styles.meta}>
<code>{node.sha.slice(0, 7)}</code>
Expand All @@ -90,6 +93,9 @@ const GitGraph = ({graph, currentBranch = 'main', onRestore, restoring, projectI
{nodeDate.toLocaleString(getCommunityLocale())}
</time>
) : null}
{headline.detail ? (
<span className={styles.detail}>{headline.detail}</span>
) : null}
{onRestore && node.sha !== currentHead ? (
<button
type="button"
Expand Down
9 changes: 9 additions & 0 deletions src/community/components/GitGraph.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,21 @@
.meta {
color: var(--text-dim);
font-size: var(--fs-2xs);
white-space: nowrap;
}

.meta code {
color: inherit;
}

.detail {
flex: 0 1 auto;
min-width: 0;
overflow: hidden;
color: var(--text-faint);
text-overflow: ellipsis;
}

.restore {
position: relative;
z-index: 2;
Expand Down
5 changes: 5 additions & 0 deletions src/community/pages/Project.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@
margin-inline-end: 10px;
}

.commitDetail {
color: var(--text-faint);
font-size: var(--fs-sm);
}

.pullBrowser {
display: grid;
gap: 12px;
Expand Down
33 changes: 23 additions & 10 deletions src/community/pages/project/HistoryList.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-len */
import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import {Link} from 'react-router-dom';
import {History} from 'lucide-react';
import {useCommunityIntl as useCommunityText} from '../../i18n.jsx';
Expand All @@ -12,6 +12,9 @@ import Notice from '../../components/ui/Notice.jsx';
import StatusMessage from '../../components/ui/StatusMessage.jsx';
import UserLink from '../../components/UserLink.jsx';
import styles from '../Project.module.css';
import {getCommunityLocale} from '../../locale.js';
import {commitHeadline} from '../../commit-summary.js';
import useCommitSummaries from './use-commit-summaries.js';

const HistoryList = ({id, history, canRestore, onChange, baseUrl}) => {
const {text: communityText} = useCommunityText();
Expand All @@ -21,6 +24,11 @@ const HistoryList = ({id, history, canRestore, onChange, baseUrl}) => {
const restoreLocks = useRef(new Set());
const idRef = useRef(id);
idRef.current = id;
const summaryShas = useMemo(() => {
const nodes = history?.graph?.nodes?.length ? history.graph.nodes : (history?.commits || []);
return nodes.map(node => node.sha).filter(Boolean);
}, [history]);
const summaries = useCommitSummaries(id, summaryShas);
useEffect(() => {
restoreLocks.current.clear();
setRestoring(null);
Expand Down Expand Up @@ -90,6 +98,7 @@ const HistoryList = ({id, history, canRestore, onChange, baseUrl}) => {
currentBranch={history.branch}
onRestore={canRestore ? requestRestore : null}
restoring={restoring}
summaries={summaries}
/>
{restoreCandidate ? (
<ConfirmModal
Expand All @@ -113,15 +122,19 @@ const HistoryList = ({id, history, canRestore, onChange, baseUrl}) => {
return (
<React.Fragment>
<ul className={styles.commitList}>
{commits.map(commit => (
<li key={commit.sha}>
<Link className={styles.commitLink} to={`${baseUrl || projectUrl(id)}/commits/${commit.sha}`}>
<code>{commit.sha.slice(0, 7)}</code>
<span className={styles.commitMsg}>{commit.message.split('\n')[0]}</span>
</Link>
<UserLink className={styles.muted} username={commit.author}>{commit.author}</UserLink>
</li>
))}
{commits.map(commit => {
const headline = commitHeadline(commit.message, summaries[commit.sha], communityText, getCommunityLocale());
return (
<li key={commit.sha}>
<Link className={styles.commitLink} to={`${baseUrl || projectUrl(id)}/commits/${commit.sha}`}>
<code>{commit.sha.slice(0, 7)}</code>
<span className={styles.commitMsg}>{headline.title}</span>
{headline.detail ? <span className={styles.commitDetail}>{headline.detail}</span> : null}
</Link>
<UserLink className={styles.muted} username={commit.author}>{commit.author}</UserLink>
</li>
);
})}
</ul>
</React.Fragment>
);
Expand Down
46 changes: 46 additions & 0 deletions src/community/pages/project/use-commit-summaries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {useEffect, useRef, useState} from 'react';
import api from '../../api';
import {chunk, pendingSummaryShas} from '../../commit-summary.js';

const useCommitSummaries = (projectId, shas) => {
const [summaries, setSummaries] = useState({});
const requested = useRef({projectId: '', shas: new Set()});
const mounted = useRef(true);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
const key = shas.join(',');
useEffect(() => {
if (requested.current.projectId !== projectId) {
requested.current = {projectId, shas: new Set()};
setSummaries({});
}
if (!projectId) return;
const state = requested.current;
const pending = pendingSummaryShas(shas, state.shas);
if (!pending.length) return;
pending.forEach(sha => state.shas.add(sha));
const current = () => mounted.current && requested.current === state;
(async () => {
const batches = chunk(pending);
for (let index = 0; index < batches.length; index++) {
if (!current()) return;
try {
const data = await api.commitSummaries(projectId, batches[index]);
const found = data && typeof data.summaries === 'object' ? data.summaries : null;
if (found && current()) setSummaries(known => ({...known, ...found}));
} catch {
batches.slice(index).flat()
.forEach(sha => state.shas.delete(sha));
return;
}
}
})();
}, [projectId, key]);
return summaries;
};

export default useCommitSummaries;
Loading