-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Platform-independent frontend slots #940
Draft
eritbh
wants to merge
55
commits into
master
Choose a base branch
from
feat/platform-observers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 24 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
1300326
make helper for subtree mutationobservers that handle mutations serially
eritbh 755cb1f
just uh. just kinda shove it all in there i guess
eritbh 3dc35da
erin react doesnt even have slots what does this comment mean
eritbh 92d5e2f
add key to mapped slot components
eritbh 69e2907
update log names for consistency
eritbh 0c8db28
ensure we use a sufficiently different jsapi consumer name
eritbh 147531f
don't include details we can't get easily for now
eritbh 3dff633
oldreddit observer based on oldreddit module code
eritbh b017242
more context data model revisions
eritbh 557a955
modmail observer pulled even harder from tblistener
eritbh e73d528
fix unrelated bug with admin usernames in modmail sidebars
eritbh 67b3ca6
put modnotes in the user hovercard location too
eritbh e80a144
guess we just dont need a logger there
eritbh b233f35
i am dumb
eritbh d1e2331
Merge branch 'master' into feat/platform-observers
eritbh d4735ad
convert usernotes to slots
eritbh ac59a9b
Use display:contents for jQ renderers
eritbh 8da95e7
don't initialize observers until after all registrations
eritbh fff814c
use contextFullname when available for modnotes
eritbh 900a6ac
return null for no render instead of empty fragment
eritbh fc2c7ec
Handle onlyshowInhover behavior at the listener level
eritbh 7ebf831
convert usernotes module
eritbh 22c4ccf
i knew it was gonna be a circular dependency issue
eritbh d60d042
have toolbox-generated things manage their own slots
eritbh 499fa92
clean up import list since this is my fault
eritbh aadb07d
convert mod button to slots
eritbh 4bf2571
Bump react and @types/react (#936)
dependabot[bot] 37c1d21
Bump framer-motion from 11.0.3 to 11.2.2 (#944)
dependabot[bot] f94b21d
Bump webextension-polyfill from 0.10.0 to 0.12.0 (#942)
dependabot[bot] 5fc0ad1
Bump react-dom and @types/react-dom (#945)
dependabot[bot] 6ba2850
Bump dprint from 0.45.0 to 0.46.2 (#950)
dependabot[bot] 1652c52
Bump @rollup/plugin-commonjs from 25.0.7 to 26.0.1 (#948)
dependabot[bot] 645892d
Bump braces from 3.0.2 to 3.0.3 (#951)
dependabot[bot] 182cee6
Bump inquirer from 9.2.15 to 9.3.2 (#955)
dependabot[bot] 396e71e
Bump dprint from 0.46.2 to 0.47.0 (#956)
dependabot[bot] c92cf47
Bump typescript from 5.4.3 to 5.5.3 (#957)
dependabot[bot] 6ec88fb
Bump iter-ops from 3.1.1 to 3.2.0 (#958)
dependabot[bot] b9c6025
Bump framer-motion from 11.2.2 to 11.3.2 (#962)
dependabot[bot] 2c5a268
Bump inquirer from 9.3.2 to 10.0.1 (#960)
dependabot[bot] 86e1b3c
Fix macros in modmail (#963)
eritbh ba20f72
More release meta fixes (#965)
eritbh 3aaa711
v7.0.0-beta.4
eritbh 01712b0
firefox is so picky about its version strings
eritbh 8b6b5df
v7.0.0-beta.5
eritbh cd54af4
please accept this string i am begging you
eritbh 820294e
v7.0.0-beta.6
eritbh 7f55c1e
Bump iter-ops from 3.2.0 to 3.4.0 (#973)
dependabot[bot] 66274d1
Bump inquirer from 10.0.1 to 10.1.7 (#972)
dependabot[bot] 2efd9c9
Bump eslint-plugin-react from 7.34.0 to 7.35.0 (#966)
dependabot[bot] 2ab3bca
fail CI if NOMERGE/XXX comments are present (#974)
eritbh ccc865f
fix up imports, add notes for removed settings
eritbh 951b942
convert history button to slots
eritbh b0660cf
kinda start converting nuke button badly
eritbh 852d157
Merge branch 'master' into feat/platform-observers
eritbh 43a2606
fix import file extension
eritbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Defines a system of "slots" which modules can use to render interface | ||
// elements within the page. Slot locations are standardized for consumers (e.g. | ||
// a module says it wants to display a button next to comment author usernames) | ||
// and their actual position in the DOM is controlled by platform-specific | ||
// observers responding to changes in the page and dynamically creating React | ||
// roots which this code then populates with the appropriate contents. | ||
|
||
// TODO: this file probably needs to be explained a lot better im in | ||
// functionality hyperfocus mode not documentation hyperfocus mode | ||
|
||
import {type ComponentType} from 'react'; | ||
|
||
import {currentPlatform, RedditPlatform} from '../util/platform'; | ||
import {reactRenderer} from '../util/ui_interop'; | ||
|
||
import modmailObserver from './modmail'; | ||
import newRedditObserver from './newreddit'; | ||
import oldRedditObserver from './oldreddit'; | ||
import shredditObserver from './shreddit'; | ||
|
||
// FIXME: document all of these | ||
interface PlatformSlotDetailsSubreddit { | ||
fullname?: string; | ||
name: string; | ||
} | ||
|
||
export type PlatformSlotDetailsUser = { | ||
deleted: true; | ||
} | { | ||
deleted: false; | ||
fullname?: string; | ||
name: string; | ||
}; | ||
|
||
interface PlatformSlotDetailsSubmission { | ||
fullname: string; | ||
} | ||
|
||
interface PlatformSlotDetailsComment { | ||
fullname: string; | ||
} | ||
|
||
// Slot names and the type of associated contextual information | ||
// FIXME: document | ||
export interface PlatformSlotDetails { | ||
submissionAuthor: { | ||
user: PlatformSlotDetailsUser; | ||
submission?: PlatformSlotDetailsSubmission; | ||
subreddit: PlatformSlotDetailsSubreddit; | ||
// distinguishType: null | 'moderator' | 'employee' | 'alumnus'; | ||
// stickied: boolean; | ||
}; | ||
commentAuthor: { | ||
user: PlatformSlotDetailsUser; | ||
comment: PlatformSlotDetailsComment; | ||
submission?: PlatformSlotDetailsSubmission; | ||
subreddit: PlatformSlotDetailsSubreddit; | ||
// distinguished: boolean; | ||
// stickied: boolean; | ||
}; | ||
modmailAuthor: { | ||
user: PlatformSlotDetailsUser; | ||
subreddit: PlatformSlotDetailsSubreddit; | ||
thread: {fullname: string}; | ||
message: {fullname: string}; | ||
// authorIsModerator: boolean; | ||
// repliedAsSubreddit: boolean; | ||
}; | ||
userHovercard: { | ||
user: PlatformSlotDetailsUser; | ||
subreddit: PlatformSlotDetailsSubreddit; | ||
contextFullname?: string; | ||
}; | ||
} | ||
export type PlatformSlotLocation = keyof PlatformSlotDetails; | ||
|
||
// Consumer code (used by toolbox modules) | ||
|
||
// A consumer of a particular slot location which gets appropriate context and | ||
// returns React content to be rendered in the slot | ||
export type PlatformSlotContent<Location extends keyof PlatformSlotDetails> = ComponentType<{ | ||
details: PlatformSlotDetails[Location]; | ||
location: Location; | ||
}>; | ||
|
||
// Map of slot locations to consumers of the slot | ||
const slotConsumers: { | ||
[K in keyof PlatformSlotDetails]?: PlatformSlotContent<K>[]; | ||
} = Object.create(null); | ||
|
||
// FIXME: document | ||
export function renderInSlots<K extends keyof PlatformSlotDetails> (locations: K[], render: PlatformSlotContent<K>) { | ||
if (!Array.isArray(locations)) { | ||
locations = []; | ||
} | ||
for (const location of locations) { | ||
if (!slotConsumers[location]) { | ||
slotConsumers[location] = []; | ||
} | ||
slotConsumers[location]?.push(render); | ||
} | ||
} | ||
|
||
// Observer code (used by platform-specific observers in this directory) | ||
|
||
// FIXME: document | ||
export type PlatformObserver = ( | ||
/** | ||
* Creates a React root for a slot which will be populated with the | ||
* appropriate contents. Observers are responsible for calling this function | ||
* and inserting the resulting element into the DOM wherever the slot should | ||
* be rendered. | ||
*/ | ||
createRenderer: <Location extends keyof PlatformSlotDetails>( | ||
location: Location, | ||
details: PlatformSlotDetails[Location], | ||
) => HTMLElement, | ||
) => void; | ||
|
||
// the actual `createRenderer` function observers get - returns a new react root | ||
// which will contain all the contents different modules have registered for the | ||
// given slot location | ||
// NOTE: Exported because tbui builders need to manually emit their own slots. | ||
// Should we just import this from the platform-specific bits instead of | ||
// passing this function in to them? | ||
export const createRenderer = <K extends keyof PlatformSlotDetails>(location: K, details: PlatformSlotDetails[K]) => | ||
reactRenderer( | ||
<div | ||
className='tb-platform-slot' | ||
style={{display: 'inline-flex'}} // FIXME: do this in CSS | ||
data-location={location} | ||
> | ||
{/* TODO: Do we want to do anything more sophisticated here? */} | ||
{slotConsumers[location]?.map((Component, i) => ( | ||
<Component | ||
key={i} | ||
details={details} | ||
location={location} | ||
/> | ||
))} | ||
</div>, | ||
); | ||
|
||
// Initialize the appropriate observer for the platform we've loaded into | ||
let observers = { | ||
[RedditPlatform.OLD]: oldRedditObserver, | ||
[RedditPlatform.NEW]: newRedditObserver, | ||
[RedditPlatform.SHREDDIT]: shredditObserver, | ||
[RedditPlatform.MODMAIL]: modmailObserver, | ||
}; | ||
|
||
/** | ||
* Start the platform observer, which will cause slots to be identified and | ||
* populated. To be called as part of the init process after all slot consumers | ||
* have been registered via {@linkcode renderInSlots}. | ||
*/ | ||
export function initializeObserver () { | ||
if (currentPlatform == null) { | ||
return; | ||
} | ||
observers[currentPlatform](createRenderer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {getThingInfo} from '../tbcore'; | ||
import {PlatformObserver} from '.'; | ||
|
||
const MESSAGE_SEEN_CLASS = 'tb-observer-modmail-message-seen'; | ||
|
||
const SIDEBAR_SEEN_CLASS = 'tb-observer-modmail-sidebar-seen'; | ||
|
||
export default (createRenderer => { | ||
function newModmailConversationAuthors () { | ||
const $body = $('body'); | ||
const subreddit = $body.find('.ThreadTitle__community').text(); | ||
$body.find(`.Thread__message:not(.${MESSAGE_SEEN_CLASS})`).each(function () { | ||
const $this = $(this); | ||
this.classList.add(MESSAGE_SEEN_CLASS); | ||
|
||
// Get information | ||
const authorHref = $this.find('.Message__header .Message__author').attr('href'); | ||
const idDetails = $this.find('.m-link').attr('href')!.match(/\/mail\/.*?\/(.*?)\/(.*?)$/i)!; | ||
|
||
this.querySelector('.Message__divider')?.after(createRenderer('modmailAuthor', { | ||
user: authorHref === undefined | ||
? {deleted: true} | ||
: {deleted: false, name: authorHref.replace(/.*\/user\/([^/]+).*/, '$1')}, | ||
subreddit: { | ||
name: subreddit, | ||
}, | ||
thread: { | ||
fullname: idDetails[1], | ||
}, | ||
message: { | ||
fullname: idDetails[2], | ||
}, | ||
})); | ||
}); | ||
} | ||
|
||
/** | ||
* Makes sure to fire a jsAPI `TBuserHovercard` event for new modmail sidebar instances. | ||
* @function | ||
*/ | ||
function newModmailSidebar () { | ||
const $body = $('body'); | ||
if ($body.find('.ThreadViewer').length) { | ||
const $modmailSidebar = $body.find( | ||
`:is(.ThreadViewer__infobar, .ThreadViewerHeader__infobar, .InfoBar__idCard):not(.${SIDEBAR_SEEN_CLASS})`, | ||
); | ||
const jsApiPlaceHolder = ` | ||
<div class="tb-jsapi-container tb-modmail-sidebar-container"> | ||
<div class="InfoBar__recentsTitle">Toolbox functions:</div> | ||
<span data-name="toolbox"></span> | ||
</div> | ||
`; | ||
$modmailSidebar.each(function () { | ||
getThingInfo(this, true).then(info => { | ||
this.classList.add(SIDEBAR_SEEN_CLASS); | ||
|
||
const $jsApiThingPlaceholder = $(jsApiPlaceHolder).appendTo(this); | ||
const jsApiThingPlaceholder = $jsApiThingPlaceholder[0]; | ||
|
||
jsApiThingPlaceholder.appendChild(createRenderer('userHovercard', { | ||
user: (info.user && info.user !== '[deleted]') | ||
? {deleted: false, name: info.user} | ||
: {deleted: true}, | ||
subreddit: { | ||
name: info.subreddit, | ||
}, | ||
})); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
const $body = $('body'); | ||
|
||
$body.on('click', '.icon-user', () => { | ||
setTimeout(() => { | ||
newModmailSidebar(); | ||
}, 500); | ||
}); | ||
|
||
$body.on('click', '.Thread__grouped', () => { | ||
setTimeout(() => { | ||
newModmailConversationAuthors(); | ||
}, 500); | ||
}); | ||
|
||
window.addEventListener('TBNewPage', event => { | ||
// TODO: augh | ||
if ((event as any).detail.pageType === 'modmailConversation') { | ||
setTimeout(() => { | ||
newModmailSidebar(); | ||
newModmailConversationAuthors(); | ||
}, 500); | ||
} | ||
}); | ||
}) satisfies PlatformObserver; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
location
is a global name why did i think it was a good idea to name a parameter that. call itslotName
or something