[Glitch] Wrapstodon modal with new share button

Port 31c392b1bc to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Echo
2025-12-03 17:25:36 +01:00
committed by Claire
parent 5a5ba02f96
commit 2d93e63e43
5 changed files with 92 additions and 96 deletions

View File

@@ -1,68 +1,64 @@
import { FormattedMessage } from 'react-intl'; import { defineMessages, useIntl } from 'react-intl';
import type { Archetype as ArchetypeData } from '@/flavours/glitch/models/annual_report';
import booster from '@/images/archetypes/booster.png'; import booster from '@/images/archetypes/booster.png';
import lurker from '@/images/archetypes/lurker.png'; import lurker from '@/images/archetypes/lurker.png';
import oracle from '@/images/archetypes/oracle.png'; import oracle from '@/images/archetypes/oracle.png';
import pollster from '@/images/archetypes/pollster.png'; import pollster from '@/images/archetypes/pollster.png';
import replier from '@/images/archetypes/replier.png'; import replier from '@/images/archetypes/replier.png';
import type { Archetype as ArchetypeData } from 'flavours/glitch/models/annual_report';
export const archetypeNames = defineMessages<ArchetypeData>({
booster: {
id: 'annual_report.summary.archetype.booster',
defaultMessage: 'The cool-hunter',
},
replier: {
id: 'annual_report.summary.archetype.replier',
defaultMessage: 'The social butterfly',
},
pollster: {
id: 'annual_report.summary.archetype.pollster',
defaultMessage: 'The pollster',
},
lurker: {
id: 'annual_report.summary.archetype.lurker',
defaultMessage: 'The lurker',
},
oracle: {
id: 'annual_report.summary.archetype.oracle',
defaultMessage: 'The oracle',
},
});
export const Archetype: React.FC<{ export const Archetype: React.FC<{
data: ArchetypeData; data: ArchetypeData;
}> = ({ data }) => { }> = ({ data }) => {
let illustration, label; const intl = useIntl();
let illustration;
switch (data) { switch (data) {
case 'booster': case 'booster':
illustration = booster; illustration = booster;
label = (
<FormattedMessage
id='annual_report.summary.archetype.booster'
defaultMessage='The cool-hunter'
/>
);
break; break;
case 'replier': case 'replier':
illustration = replier; illustration = replier;
label = (
<FormattedMessage
id='annual_report.summary.archetype.replier'
defaultMessage='The social butterfly'
/>
);
break; break;
case 'pollster': case 'pollster':
illustration = pollster; illustration = pollster;
label = (
<FormattedMessage
id='annual_report.summary.archetype.pollster'
defaultMessage='The pollster'
/>
);
break; break;
case 'lurker': case 'lurker':
illustration = lurker; illustration = lurker;
label = (
<FormattedMessage
id='annual_report.summary.archetype.lurker'
defaultMessage='The lurker'
/>
);
break; break;
case 'oracle': case 'oracle':
illustration = oracle; illustration = oracle;
label = (
<FormattedMessage
id='annual_report.summary.archetype.oracle'
defaultMessage='The oracle'
/>
);
break; break;
} }
return ( return (
<div className='annual-report__bento__box annual-report__summary__archetype'> <div className='annual-report__bento__box annual-report__summary__archetype'>
<div className='annual-report__summary__archetype__label'>{label}</div> <div className='annual-report__summary__archetype__label'>
{intl.formatMessage(archetypeNames[data])}
</div>
<img src={illustration} alt='' /> <img src={illustration} alt='' />
</div> </div>
); );

View File

@@ -1,68 +1,38 @@
import { useState, useEffect } from 'react'; import { useCallback } from 'react';
import type { FC } from 'react';
import { FormattedMessage } from 'react-intl'; import { defineMessage, FormattedMessage, useIntl } from 'react-intl';
import { import { focusCompose, resetCompose } from '@/flavours/glitch/actions/compose';
importFetchedStatuses, import { closeModal } from '@/flavours/glitch/actions/modal';
importFetchedAccounts, import { Button } from '@/flavours/glitch/components/button';
} from 'flavours/glitch/actions/importer'; import { LoadingIndicator } from '@/flavours/glitch/components/loading_indicator';
import { apiRequestGet, apiRequestPost } from 'flavours/glitch/api'; import { me } from '@/flavours/glitch/initial_state';
import { LoadingIndicator } from 'flavours/glitch/components/loading_indicator'; import type { AnnualReport as AnnualReportData } from '@/flavours/glitch/models/annual_report';
import { me } from 'flavours/glitch/initial_state'; import { useAppDispatch, useAppSelector } from '@/flavours/glitch/store';
import type { Account } from 'flavours/glitch/models/account';
import type { AnnualReport as AnnualReportData } from 'flavours/glitch/models/annual_report';
import type { Status } from 'flavours/glitch/models/status';
import { useAppSelector, useAppDispatch } from 'flavours/glitch/store';
import { Archetype } from './archetype'; import { Archetype, archetypeNames } from './archetype';
import { Followers } from './followers'; import { Followers } from './followers';
import { HighlightedPost } from './highlighted_post'; import { HighlightedPost } from './highlighted_post';
import { MostUsedHashtag } from './most_used_hashtag'; import { MostUsedHashtag } from './most_used_hashtag';
import { NewPosts } from './new_posts'; import { NewPosts } from './new_posts';
import { Percentile } from './percentile';
interface AnnualReportResponse { const shareMessage = defineMessage({
annual_reports: AnnualReportData[]; id: 'annual_report.summary.share_message',
accounts: Account[]; defaultMessage: 'I got the {archetype} archetype!',
statuses: Status[]; });
}
export const AnnualReport: React.FC<{ // Share = false when using the embedded version of the report.
year: string; export const AnnualReport: FC<{ share?: boolean }> = ({ share = true }) => {
}> = ({ year }) => {
const [response, setResponse] = useState<AnnualReportResponse | null>(null);
const [loading, setLoading] = useState(true);
const currentAccount = useAppSelector((state) => const currentAccount = useAppSelector((state) =>
me ? state.accounts.get(me) : undefined, me ? state.accounts.get(me) : undefined,
); );
const dispatch = useAppDispatch(); const report = useAppSelector((state) => state.annualReport.report);
useEffect(() => { if (!report) {
apiRequestGet<AnnualReportResponse>(`v1/annual_reports/${year}`)
.then((data) => {
dispatch(importFetchedStatuses(data.statuses));
dispatch(importFetchedAccounts(data.accounts));
setResponse(data);
setLoading(false);
return apiRequestPost(`v1/annual_reports/${year}/read`);
})
.catch(() => {
setLoading(false);
});
}, [dispatch, year, setResponse, setLoading]);
if (loading) {
return <LoadingIndicator />; return <LoadingIndicator />;
} }
const report = response?.annual_reports[0];
if (!report || report.schema_version !== 1) {
return null;
}
return ( return (
<div className='annual-report'> <div className='annual-report'>
<div className='annual-report__header'> <div className='annual-report__header'>
@@ -89,9 +59,37 @@ export const AnnualReport: React.FC<{
total={currentAccount?.followers_count} total={currentAccount?.followers_count}
/> />
<MostUsedHashtag data={report.data.top_hashtags} /> <MostUsedHashtag data={report.data.top_hashtags} />
<Percentile data={report.data.percentiles} />
<NewPosts data={report.data.time_series} /> <NewPosts data={report.data.time_series} />
{share && <ShareButton report={report} />}
</div> </div>
</div> </div>
); );
}; };
const ShareButton: FC<{ report: AnnualReportData }> = ({ report }) => {
const intl = useIntl();
const dispatch = useAppDispatch();
const handleShareClick = useCallback(() => {
// Generate the share message.
const archetypeName = intl.formatMessage(
archetypeNames[report.data.archetype],
);
const shareLines = [
intl.formatMessage(shareMessage, {
archetype: archetypeName,
}),
];
// Share URL is only available for schema version 2.
if (report.schema_version === 2 && report.share_url) {
shareLines.push(report.share_url);
}
shareLines.push(`#Wrapstodon${report.year}`);
// Reset the composer and focus it with the share message, then close the modal.
dispatch(resetCompose());
dispatch(focusCompose(shareLines.join('\n\n')));
dispatch(closeModal({ modalType: 'ANNUAL_REPORT', ignoreFocus: false }));
}, [report, intl, dispatch]);
return <Button text='Share here' onClick={handleShareClick} />;
};

View File

@@ -1,7 +1,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import type { FC } from 'react'; import type { FC } from 'react';
import { showAlert } from '@/flavours/glitch/actions/alerts'; import { openModal } from '@/flavours/glitch/actions/modal';
import { import {
generateReport, generateReport,
selectWrapstodonYear, selectWrapstodonYear,
@@ -20,12 +20,7 @@ export const AnnualReportTimeline: FC = () => {
}, [dispatch]); }, [dispatch]);
const handleOpen = useCallback(() => { const handleOpen = useCallback(() => {
dispatch( dispatch(openModal({ modalType: 'ANNUAL_REPORT', modalProps: {} }));
// TODO: Implement opening the annual report view when components are ready.
showAlert({
message: 'Not yet implemented.',
}),
);
}, [dispatch]); }, [dispatch]);
if (!year || !state || state === 'ineligible') { if (!year || !state || state === 'ineligible') {

View File

@@ -3,16 +3,15 @@ import { useEffect } from 'react';
import { AnnualReport } from 'flavours/glitch/features/annual_report'; import { AnnualReport } from 'flavours/glitch/features/annual_report';
const AnnualReportModal: React.FC<{ const AnnualReportModal: React.FC<{
year: string; onChangeBackgroundColor: (color: string) => void;
onChangeBackgroundColor: (arg0: string) => void; }> = ({ onChangeBackgroundColor }) => {
}> = ({ year, onChangeBackgroundColor }) => {
useEffect(() => { useEffect(() => {
onChangeBackgroundColor('var(--indigo-1)'); onChangeBackgroundColor('var(--indigo-1)');
}, [onChangeBackgroundColor]); }, [onChangeBackgroundColor]);
return ( return (
<div className='modal-root__modal annual-report-modal'> <div className='modal-root__modal annual-report-modal'>
<AnnualReport year={year} /> <AnnualReport />
</div> </div>
); );
}; };

View File

@@ -1,5 +1,9 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import {
importFetchedAccounts,
importFetchedStatuses,
} from '@/flavours/glitch/actions/importer';
import { insertIntoTimeline } from '@/flavours/glitch/actions/timelines'; import { insertIntoTimeline } from '@/flavours/glitch/actions/timelines';
import type { ApiAnnualReportState } from '@/flavours/glitch/api/annual_report'; import type { ApiAnnualReportState } from '@/flavours/glitch/api/annual_report';
import { import {
@@ -114,5 +118,9 @@ export const getReport = createDataLoadingThunk(
} }
return apiGetAnnualReport(year); return apiGetAnnualReport(year);
}, },
(data) => data.annual_reports[0], (data, { dispatch }) => {
dispatch(importFetchedStatuses(data.statuses));
dispatch(importFetchedAccounts(data.accounts));
return data.annual_reports[0];
},
); );