Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/common/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ export function buildReactionFromUrl(reaction) {

return reaction.from_info?.service_external_url;
}

export function truncateString(str, countDots = null) {
if (!str || str.length <= 1) {
return str;
}

return str[0] + '.'.repeat(countDots || str.length - 1);
}
4 changes: 1 addition & 3 deletions src/entries/contentScript/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ function App() {

<ReactionPolicyNotice />

{state !== STATE_LIVE && (
<ReactionsHistoryNotice />
)}
<ReactionsHistoryNotice truncateNames={state === STATE_LIVE} />

{state !== STATE_LIVE && (
<SubmitSuggestionNotice />
Expand Down
32 changes: 25 additions & 7 deletions src/entries/contentScript/components/ReactionsHistoryNotice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { useMemo, Fragment } from 'react';
import moment from 'moment';
import { useTranslation } from 'react-i18next';
import PropTypes from 'prop-types';
import Card from '~/entries/contentScript/components/Card';
import { useReactions } from '~/common/bridge';
import { reactionShape } from '~/shapes';
Expand All @@ -11,10 +12,14 @@
import { hasSubscriptionFeature } from '~/entries/contentScript/hooks/useSubscription';
import { subscriptionIds, subscriptionFeatures } from '~/enums';
import PremiumCtaLabel from '~/entries/contentScript/components/PremiumCtaLabel';
import { buildReactionFromUrl } from '~/common/pretty';
import { buildReactionFromUrl, truncateString } from '~/common/pretty';

Check warning on line 15 in src/entries/contentScript/components/ReactionsHistoryNotice.jsx

View workflow job for this annotation

GitHub Actions / Build (chrome MV2)

'truncateString' is defined but never used

Check warning on line 15 in src/entries/contentScript/components/ReactionsHistoryNotice.jsx

View workflow job for this annotation

GitHub Actions / ESLint

'truncateString' is defined but never used

Check warning on line 15 in src/entries/contentScript/components/ReactionsHistoryNotice.jsx

View workflow job for this annotation

GitHub Actions / Build (firefox MV2)

'truncateString' is defined but never used

Check warning on line 15 in src/entries/contentScript/components/ReactionsHistoryNotice.jsx

View workflow job for this annotation

GitHub Actions / Build (chrome MV3)

'truncateString' is defined but never used

Check warning on line 15 in src/entries/contentScript/components/ReactionsHistoryNotice.jsx

View workflow job for this annotation

GitHub Actions / Build (firefox MV3)

'truncateString' is defined but never used
import imageOnError from '~/common/imageOnError';
import TruncatedString from '~/entries/contentScript/components/TruncatedString';

function ReactionPreview({ reaction }) {
function ReactionPreview({
reaction,
truncateNames = false,
}) {
const { t } = useTranslation();

const showAvatar = useMemo(() => reaction.from_info?.avatar_url, [reaction]);
Expand Down Expand Up @@ -56,7 +61,9 @@
rel="noreferrer"
className="font-bold text-primary-gradient-hc-from dark:text-primary-gradient-from"
>
{reaction.from_info?.display_name}
{truncateNames
? <TruncatedString value={reaction.from_info?.display_name} />
: reaction.from_info?.display_name}
</a>

{' '}
Expand Down Expand Up @@ -106,9 +113,12 @@

ReactionPreview.propTypes = {
reaction: reactionShape.isRequired,
truncateNames: PropTypes.bool,
};

function ReactionsHistoryNotice() {
function ReactionsHistoryNotice({
truncateNames = false,
}) {
const { t } = useTranslation();
const currentUrl = useAppStore((state) => state.currentUrl);
const compact = useAppStore((state) => state.isCompact);
Expand Down Expand Up @@ -196,7 +206,10 @@
<div className="flex flex-col gap-1">
{videoReactions.map((reaction) => (
<Fragment key={reaction.id}>
<ReactionPreview reaction={reaction} />
<ReactionPreview
reaction={reaction}
truncateNames={truncateNames}
/>
</Fragment>
))}
</div>
Expand All @@ -212,7 +225,10 @@
<div className="flex flex-col gap-1">
{liveReactions.map((reaction) => (
<Fragment key={reaction.id}>
<ReactionPreview reaction={reaction} />
<ReactionPreview
reaction={reaction}
truncateNames={truncateNames}
/>
</Fragment>
))}
</div>
Expand All @@ -224,7 +240,9 @@
);
}

ReactionsHistoryNotice.propTypes = {};
ReactionsHistoryNotice.propTypes = {
truncateNames: PropTypes.bool,
};

ReactionsHistoryNotice.defaultProps = {};

Expand Down
20 changes: 20 additions & 0 deletions src/entries/contentScript/components/TruncatedString.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMemo } from 'react';
import PropTypes from 'prop-types';
import { truncateString } from '~/common/pretty';

function TruncatedString({ value }) {
const truncated = useMemo(() => truncateString(value, 100), [value]);

return (
<span className="group/tstr relative overflow-hidden">
<span className="invisible group-hover/tstr:visible">{value}</span>
<span className="visible absolute left-0 w-full overflow-hidden group-hover/tstr:invisible">{truncated}</span>
</span>
);
}

TruncatedString.propTypes = {
value: PropTypes.string,
};

export default TruncatedString;