From 959ac468d85948ef65cb5145660a939f6df1742a Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 8 Apr 2023 20:42:38 +0800 Subject: [PATCH] Allow instance-based hashtags Also change design a little --- src/components/shortcuts-settings.css | 2 + src/components/shortcuts-settings.jsx | 34 +++++++++-- src/components/shortcuts.css | 12 ++++ src/components/shortcuts.jsx | 81 +++++++++++++++++---------- src/pages/hashtag.jsx | 28 +++++++-- 5 files changed, 117 insertions(+), 40 deletions(-) diff --git a/src/components/shortcuts-settings.css b/src/components/shortcuts-settings.css index c3bf0dcd..1036d79b 100644 --- a/src/components/shortcuts-settings.css +++ b/src/components/shortcuts-settings.css @@ -26,6 +26,8 @@ #shortcuts-settings-container .shortcuts-list li .shortcut-text { flex-grow: 1; min-width: 0; + line-height: 1; + word-break: break-word; } #shortcuts-settings-container .shortcuts-list li .shortcut-actions { flex-shrink: 0; diff --git a/src/components/shortcuts-settings.jsx b/src/components/shortcuts-settings.jsx index ed9e189a..7e2c7fef 100644 --- a/src/components/shortcuts-settings.jsx +++ b/src/components/shortcuts-settings.jsx @@ -94,6 +94,13 @@ const TYPE_PARAMS = { placeholder: 'e.g. PixelArt (Max 5, space-separated)', pattern: '[^#]+', }, + { + text: 'Instance', + name: 'instance', + type: 'text', + placeholder: 'Optional, e.g. mastodon.social', + notRequired: true, + }, ], }; export const SHORTCUTS_META = { @@ -131,14 +138,15 @@ export const SHORTCUTS_META = { }, public: { id: 'public', - title: ({ local, instance }) => - `${local ? 'Local' : 'Federated'} (${instance})`, + title: ({ local }) => (local ? 'Local' : 'Federated'), + subtitle: ({ instance }) => instance, path: ({ local, instance }) => `/${instance}/p${local ? '/l' : ''}`, icon: ({ local }) => (local ? 'group' : 'earth'), }, trending: { id: 'trending', title: 'Trending', + subtitle: ({ instance }) => instance, path: ({ instance }) => `/${instance}/trending`, icon: 'chart', }, @@ -177,6 +185,7 @@ export const SHORTCUTS_META = { hashtag: { id: 'hashtag', title: ({ hashtag }) => hashtag, + subtitle: ({ instance }) => instance, path: ({ hashtag }) => `/t/${hashtag.split(/\s+/).join('+')}`, icon: 'hashtag', }, @@ -307,10 +316,13 @@ function ShortcutsSettings() { const key = i + Object.values(shortcut); const { type } = shortcut; if (!SHORTCUTS_META[type]) return null; - let { icon, title } = SHORTCUTS_META[type]; + let { icon, title, subtitle } = SHORTCUTS_META[type]; if (typeof title === 'function') { title = title(shortcut, i); } + if (typeof subtitle === 'function') { + subtitle = subtitle(shortcut, i); + } if (typeof icon === 'function') { icon = icon(shortcut, i); } @@ -319,6 +331,12 @@ function ShortcutsSettings() { {title} + {subtitle && ( + <> + {' '} + {subtitle} + + )} } > - {formattedShortcuts.map(({ path, title, icon }, i) => { + {formattedShortcuts.map(({ path, title, subtitle, icon }, i) => { return ( {' '} - {title} + + {title} + + {subtitle && ( + <> + {' '} + {subtitle} + + )} {i + 1} diff --git a/src/pages/hashtag.jsx b/src/pages/hashtag.jsx index fd0e6f8e..0d46d1af 100644 --- a/src/pages/hashtag.jsx +++ b/src/pages/hashtag.jsx @@ -32,8 +32,10 @@ function Hashtags(props) { hashtags.sort(); hashtag = hashtags[0]; - const { masto, instance } = api({ instance: params.instance }); - const { authenticated } = api(); + const { masto, instance, authenticated } = api({ + instance: props?.instance || params.instance, + }); + const { authenticated: currentAuthenticated } = api(); const hashtagTitle = hashtags.map((t) => `#${t}`).join(' '); const title = instance ? `${hashtagTitle} on ${instance}` : hashtagTitle; useTitle(title, `/:instance?/t/:hashtag`); @@ -99,7 +101,7 @@ function Hashtags(props) { return ( ( { hashtags.splice(i, 1); hashtags.sort(); @@ -252,7 +255,7 @@ function Hashtags(props) { { const shortcut = { type: 'hashtag', @@ -281,6 +284,23 @@ function Hashtags(props) { > Add to Shorcuts + { + let newInstance = prompt( + 'Enter a new instance e.g. "mastodon.social"', + ); + if (!/\./.test(newInstance)) { + if (newInstance) alert('Invalid instance'); + return; + } + if (newInstance) { + newInstance = newInstance.toLowerCase().trim(); + navigate(`/${newInstance}/t/${hashtags.join('+')}`); + } + }} + > + Go to another instance… + } />