This account resolving thingie is getting ridiculous

This commit is contained in:
Lim Chee Aun 2023-01-22 20:29:48 +08:00
parent a522511e0e
commit 4c05692ef5
3 changed files with 64 additions and 36 deletions

View file

@ -3,6 +3,7 @@ import './account.css';
import { useEffect, useState } from 'preact/hooks'; import { useEffect, useState } from 'preact/hooks';
import enhanceContent from '../utils/enhance-content'; import enhanceContent from '../utils/enhance-content';
import handleAccountLinks from '../utils/handle-account-links';
import shortenNumber from '../utils/shorten-number'; import shortenNumber from '../utils/shorten-number';
import store from '../utils/store'; import store from '../utils/store';
@ -27,12 +28,29 @@ function Account({ account }) {
setInfo(info); setInfo(info);
setUIState('default'); setUIState('default');
} catch (e) { } catch (e) {
alert(e); try {
const result = await masto.v2.search({
q: account,
type: 'accounts',
limit: 1,
resolve: true,
});
if (result.accounts.length) {
setInfo(result.accounts[0]);
setUIState('default');
return;
}
alert('Account not found');
setUIState('error'); setUIState('error');
} catch (err) {
alert(err);
console.error(err);
setUIState('error');
}
} }
})(); })();
} }
}, []); }, [account]);
const { const {
acct, acct,
@ -138,6 +156,7 @@ function Account({ account }) {
)} )}
<div <div
class="note" class="note"
onClick={handleAccountLinks()}
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: enhanceContent(note, { emojis }), __html: enhanceContent(note, { emojis }),
}} }}

View file

@ -20,6 +20,7 @@ import Loader from '../components/loader';
import Modal from '../components/modal'; import Modal from '../components/modal';
import NameText from '../components/name-text'; import NameText from '../components/name-text';
import enhanceContent from '../utils/enhance-content'; import enhanceContent from '../utils/enhance-content';
import handleAccountLinks from '../utils/handle-account-links';
import htmlContentLength from '../utils/html-content-length'; import htmlContentLength from '../utils/html-content-length';
import shortenNumber from '../utils/shorten-number'; import shortenNumber from '../utils/shorten-number';
import states, { saveStatus } from '../utils/states'; import states, { saveStatus } from '../utils/states';
@ -343,39 +344,7 @@ function Status({
lang={language} lang={language}
ref={contentRef} ref={contentRef}
data-read-more={readMoreText} data-read-more={readMoreText}
onClick={(e) => { onClick={handleAccountLinks({ mentions })}
let { target } = e;
if (target.parentNode.tagName.toLowerCase() === 'a') {
target = target.parentNode;
}
if (
target.tagName.toLowerCase() === 'a' &&
target.classList.contains('u-url')
) {
const targetText = (
target.querySelector('span') || target
).innerText.trim();
const username = targetText.replace(/^@/, '');
const url = target.getAttribute('href');
const mention = mentions.find(
(mention) =>
mention.username === username ||
mention.acct === username ||
mention.url === url,
);
if (mention) {
e.preventDefault();
e.stopPropagation();
states.showAccount = mention.acct;
} else if (!/^http/i.test(targetText)) {
console.log('mention not found', targetText);
e.preventDefault();
e.stopPropagation();
const href = target.getAttribute('href');
states.showAccount = href;
}
}
}}
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: enhanceContent(content, { __html: enhanceContent(content, {
emojis, emojis,

View file

@ -0,0 +1,40 @@
import states from './states';
function handleAccountLinks(opts) {
const { mentions = [] } = opts || {};
return (e) => {
let { target } = e;
if (target.parentNode.tagName.toLowerCase() === 'a') {
target = target.parentNode;
}
if (
target.tagName.toLowerCase() === 'a' &&
target.classList.contains('u-url')
) {
const targetText = (
target.querySelector('span') || target
).innerText.trim();
const username = targetText.replace(/^@/, '');
const url = target.getAttribute('href');
const mention = mentions.find(
(mention) =>
mention.username === username ||
mention.acct === username ||
mention.url === url,
);
if (mention) {
e.preventDefault();
e.stopPropagation();
states.showAccount = mention.acct;
} else if (!/^http/i.test(targetText)) {
console.log('mention not found', targetText);
e.preventDefault();
e.stopPropagation();
const href = target.getAttribute('href');
states.showAccount = href;
}
}
};
}
export default handleAccountLinks;