Skip to content

Commit

Permalink
make ChangesList shared
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed May 28, 2024
1 parent e2bed58 commit f8a82c8
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 80 deletions.
78 changes: 5 additions & 73 deletions src/ui/root/DeputyContributionSurveyRevision.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import unwrapWidget from '../../util/unwrapWidget';
import { DeputyMessageEvent, DeputyRevisionStatusUpdateMessage } from '../../DeputyCommunications';
import type DeputyContributionSurveyRow from './DeputyContributionSurveyRow';
import {
ChangesListBytes, ChangesListDate,
ChangesListDiff,
ChangesListLinks,
ChangesListTags, ChangesListTime,
ChangesListUser,
NewPageIndicator
ChangesListLinks, ChangesListMissingRow, ChangesListRow
} from '../shared/ChangesList';
import unwrapElement from '../../util/unwrapElement';
import DeputyLoadingDots from './DeputyLoadingDots';
import MwApi from '../../MwApi';
import classMix from '../../util/classMix';
Expand Down Expand Up @@ -327,64 +321,6 @@ export default class DeputyContributionSurveyRevision
}
}

/**
* Renders revision info. This is only called if the revision exists.
*/
renderRevisionInfo(): HTMLElement {
const commentElement = <span
class="comment comment--without-parentheses"
/** Stranger danger! Yes. */
dangerouslySetInnerHTML={this.revision.parsedcomment}
/>;
const tagMessages = ( this.revision.tags ?? [] ).map(
// eslint-disable-next-line mediawiki/msg-doc
( v ) => [ v, mw.message( `tag-${ v }` ).parse() ] as [string, string]
).filter( v => v[ 1 ] !== '-' );

return <span>
{
!this.revision.parentid && <NewPageIndicator />
} <ChangesListTime
timestamp={ this.revision.timestamp }
/><ChangesListDate
revision={ this.revision }
/> {
window.deputy.config.cci.showUsername.get() && <ChangesListUser
user={ this.revision.user }
/>
} <span
class="mw-changeslist-separator"
/> <ChangesListBytes
size={ this.revision.size }
/> <ChangesListDiff
size={ this.revision.size }
diffsize={ this.revision.diffsize }
/> {
( this.revision.parsedcomment ||
tagMessages.length > 0 ) &&
<span class="mw-changeslist-separator" />
} {
this.revision.parsedcomment &&
commentElement
} {
tagMessages.length > 0 &&
<ChangesListTags tags={tagMessages} />
}
</span> as HTMLElement;
}

/**
* Renders a placeholder for missing revisions.
*/
renderMissingRevisionInfo(): HTMLElement {
return <span>
{' '}<i dangerouslySetInnerHTML={mw.message(
'deputy.session.revision.missing',
this.revision.revid
).parse()}/>
</span> as HTMLElement;
}

/**
* @inheritDoc
*/
Expand All @@ -404,15 +340,11 @@ export default class DeputyContributionSurveyRevision
>
{unwrapWidget( this.completedCheckbox )}
{unwrapWidget( this.diffToggle )}
<ChangesListLinks
revid={ this.revision.revid }
parentid={ this.revision.parentid }
missing={ ( this.revision as any ).missing }
/>{unwrapElement(
{
( this.revision as any ).missing ?
this.renderMissingRevisionInfo() :
this.renderRevisionInfo()
)}{this.diff}
<ChangesListMissingRow revision={this.revision}/> :
<ChangesListRow revision={this.revision}/>
}{this.diff}
</div> as HTMLElement;
}

Expand Down
147 changes: 140 additions & 7 deletions src/ui/shared/ChangesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import getRevisionURL from '../../wiki/util/getRevisionURL';
import getRevisionDiffURL from '../../wiki/util/getRevisionDiffURL';
import nsId from '../../wiki/util/nsId';
import type { ExpandedRevisionData } from '../../api/ExpandedRevisionData';
import { h } from 'tsx-dom';
import { ComponentChild, h } from 'tsx-dom';
import unwrapJQ from '../../util/unwrapJQ';
import msgEval from '../../wiki/util/msgEval';
import classMix from '../../util/classMix';
import { USER_LOCALE } from '../../wiki/Locale';

/**
Expand Down Expand Up @@ -111,12 +113,18 @@ export function ChangesListTime(
*/
export function ChangesListDate(
{ revision, link }: { revision: ExpandedRevisionData, link?: boolean } |
{ revision: { timestamp: string }, link: false }
{ revision: { timestamp: string, texthidden?: true }, link: false }
): JSX.Element {
// `texthidden` would be indeterminate if the `{timestamp}` type was used
if ( revision.texthidden ) {
// Don't give out a link if the revision was deleted
link = false;
}

const time = new Date( revision.timestamp );
let now = window.moment( time );

if ( window.deputy.config.cci.forceUtc.get() ) {
if ( window.deputy && window.deputy.config.cci.forceUtc.get() ) {
now = now.utc();
}
const formattedTime = time.toLocaleTimeString( USER_LOCALE, {
Expand All @@ -137,15 +145,30 @@ export function ChangesListDate(
<a class="mw-changeslist-date" href={
getRevisionURL( revision.revid, revision.page.title )
}>{ formattedTime }{ comma }{ formattedDate }</a> :
<span>{ formattedTime }{ comma }{ formattedDate }</span>;
<span
class={classMix(
'mw-changeslist-date',
revision.texthidden && 'history-deleted'
)}
>{ formattedTime }{ comma }{ formattedDate }</span>;
}

/**
* @param root0
* @param root0.user
* @param root0.revision
* @return HTML element
*/
export function ChangesListUser( { user }: { user: string } ) {
export function ChangesListUser( { revision }: { revision: ExpandedRevisionData } ) {
const { user, userhidden } = revision;

if ( userhidden ) {
return <span class="history-user">
<span class="history-deleted mw-userlink">{
mw.msg( 'deputy.revision.removed.user' )
}</span>
</span>;
}

const userPage = new mw.Title( user, nsId( 'user' ) );
const userTalkPage = new mw.Title( user, nsId( 'user_talk' ) );
const userContribsPage = new mw.Title( 'Special:Contributions/' + user );
Expand Down Expand Up @@ -244,12 +267,32 @@ export function ChangesListDiff(
</DiffTag>;
}

/**
* @param root0
* @param root0.page
* @param root0.page.title
* @param root0.page.ns
* @return HTML element
*/
export function ChangesListPage(
{ page }: { page: { title: string, ns: number } }
): JSX.Element {
const pageTitle =
new mw.Title( page.title, page.ns ).getPrefixedText();

return <a
class="mw-contributions-title"
href={mw.util.getUrl( pageTitle )}
title={pageTitle}
>{pageTitle}</a>;
}

/**
* @param root0
* @param root0.tags
* @return HTML element
*/
export function ChangesListTags( { tags }: { tags: [string, string][] } ): JSX.Element {
export function ChangesListTags( { tags }: { tags: string[] } ): JSX.Element {
return <span class="mw-tag-markers"><a
rel="noopener"
href={mw.format(
Expand Down Expand Up @@ -278,3 +321,93 @@ export function ChangesListTags( { tags }: { tags: [string, string][] } ): JSX.E
}
</span>;
}

/**
*
* @param root0
* @param root0.revision
*/
export function ChangesListMissingRow(
{ revision }: {
revision: ExpandedRevisionData
}
): JSX.Element {
return <span>
<ChangesListLinks
revid={ revision.revid }
parentid={ revision.parentid }
missing={true}
/>{' '}<i dangerouslySetInnerHTML={mw.message(
'deputy.session.revision.missing',
revision.revid
).parse()}/>
</span>;
}

/**
* @param root0
* @param root0.revision
* @param root0.format
* @return A changes list row.
*/
export function ChangesListRow(
{ revision, format }: {
revision: ExpandedRevisionData,
format?: 'history' | 'contribs'
}
): JSX.Element {
if ( !format ) {
format = 'history';
}

let commentElement: ComponentChild = '';
if ( revision.commenthidden ) {
commentElement = <span class="history-deleted comment">{
mw.msg( 'deputy.revision.removed.comment' )
}</span>;
} else if ( revision.parsedcomment ) {
commentElement = <span
class="comment comment--without-parentheses"
/** Stranger danger! Yes. */
dangerouslySetInnerHTML={ revision.parsedcomment }
/>;
} else if ( revision.comment ) {
const comment = revision.comment
// Insert Word-Joiner to avoid parsing "templates".
.replace( /{/g, '{\u2060' )
.replace( /}/g, '\u2060}' );

commentElement = unwrapJQ( <span
class="comment comment--without-parentheses"
/>, msgEval( comment ).parseDom() );
}

return <span>
<ChangesListLinks
revid={ revision.revid }
parentid={ revision.parentid }
/> {
!revision.parentid && <NewPageIndicator />
}<ChangesListTime
timestamp={ revision.timestamp }
/><ChangesListDate
revision={ revision }
/> { format === 'history' && <ChangesListUser
revision={ revision }
/> } <span
class="mw-changeslist-separator"
/> { format === 'history' && <ChangesListBytes
size={ revision.size }
/> } <ChangesListDiff
size={ revision.size }
diffsize={ revision.diffsize }
/> <span
class="mw-changeslist-separator"
/> { format === 'contribs' && <ChangesListPage
page={ revision.page }
/>} { commentElement } {
( revision.tags?.length ?? -1 ) > 0 &&
<ChangesListTags tags={revision.tags} />
}
</span>;
}

0 comments on commit f8a82c8

Please sign in to comment.