- 
                Notifications
    You must be signed in to change notification settings 
- Fork 277
feat(gathering): message copyier #5013
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
          
     Open
      
      
            sshanzel
  wants to merge
  21
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat-message-copyier
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
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.
          
          
      
        
          +726
        
        
          −44
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            21 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      04c29b7
              
                feat: poc
              
              
                sshanzel f7c74a3
              
                refactor: copy to clipboard
              
              
                sshanzel 51a96f3
              
                fix: response after clicking - show toast
              
              
                sshanzel f9bea65
              
                feat: support switching threads
              
              
                sshanzel cca5f45
              
                refactor: moved to common file for reusing
              
              
                sshanzel abe572f
              
                feat: suggestion on feed popups
              
              
                sshanzel 0d55dd7
              
                refactor: reuse hook
              
              
                sshanzel e1cd0db
              
                fix: duplication bug
              
              
                sshanzel 726e835
              
                refactor: toast to trigger event
              
              
                sshanzel f3bda13
              
                Update packages/extension/src/companion/suggestion/MessageSuggestion.tsx
              
              
                sshanzel 4e50fd5
              
                Update packages/extension/src/companion/suggestion/MessageSuggestion.tsx
              
              
                sshanzel 7e9130c
              
                Update packages/extension/src/companion/suggestion/MessageSuggestionP…
              
              
                sshanzel 4f1411e
              
                refactor: no need to copy to clipboard
              
              
                sshanzel c3d8dc1
              
                fix: lint
              
              
                sshanzel b43a4ac
              
                fix: lint
              
              
                sshanzel facbde4
              
                Merge branch 'main' into feat-message-copyier
              
              
                sshanzel 52b7289
              
                feat: experimentation through gb
              
              
                sshanzel 8f94e96
              
                Merge branch 'main' into feat-message-copyier
              
              
                sshanzel 03c68b3
              
                refactor: experimentation on the button and request on click only (#5…
              
              
                sshanzel eb18db2
              
                Merge branch 'main' into feat-message-copyier
              
              
                sshanzel e4ec582
              
                feat: create refer-a-recruiter page (#5020)
              
              
                omBratteng 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
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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
    
  
  
    
              
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
  
    
      
          
            82 changes: 82 additions & 0 deletions
          
          82 
        
  packages/extension/src/companion/suggestion/MessageSuggestion.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,82 @@ | ||
| import React, { useState } from 'react'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { ONE_SECOND, sleep } from '@dailydotdev/shared/src/lib/func'; | ||
| import { TargetId } from '@dailydotdev/shared/src/lib/log'; | ||
| import { useThreadPageObserver } from './useThreadPageObserver'; | ||
| import { MessageSuggestionPortal } from './MessageSuggestionPortal'; | ||
|  | ||
| const bubbleClass = 'msg-overlay-conversation-bubble'; | ||
| const titleClass = 'msg-overlay-bubble-header__title'; | ||
|  | ||
| interface MessagePopup { | ||
| id: string; | ||
| bubble: HTMLElement; | ||
| } | ||
|  | ||
| export function MessageSuggestion() { | ||
| const { id: threadUserId } = useThreadPageObserver(); | ||
| const [popups, setPopups] = useState<MessagePopup[]>([]); | ||
| const [observer] = useState( | ||
| new MutationObserver(async () => { | ||
| await sleep(10); // wait for the dom to update | ||
| const bubbles = document.querySelectorAll(`.${bubbleClass}`); | ||
| const toUpdate = []; | ||
|  | ||
| bubbles.forEach((bubble) => { | ||
| const header = bubble.querySelector(`.${titleClass}`); | ||
| const anchor = header?.firstElementChild as HTMLAnchorElement; | ||
| const id = anchor?.getAttribute('href')?.split('/in/')[1]; | ||
| const cleanId = id?.replace(/\//g, ''); | ||
|  | ||
| if (cleanId) { | ||
| toUpdate.push({ id: cleanId, bubble }); | ||
| } | ||
| }); | ||
|  | ||
| setPopups(toUpdate); | ||
| }), | ||
| ); | ||
|  | ||
| useQuery({ | ||
| queryKey: ['msg-overlay'], | ||
| queryFn: () => null, | ||
| refetchInterval: (cache) => { | ||
| const retries = cache.state.dataUpdateCount; | ||
|  | ||
| if (retries >= 3) { | ||
| return false; | ||
| } | ||
|  | ||
| const container = document.querySelector('#msg-overlay'); | ||
|  | ||
| if (!container) { | ||
| return ONE_SECOND; | ||
| } | ||
|  | ||
| observer.observe(container, { childList: true, subtree: false }); | ||
|  | ||
| return false; | ||
| }, | ||
| }); | ||
|  | ||
| return ( | ||
| <> | ||
| {popups.map(({ id, bubble }) => ( | ||
| <MessageSuggestionPortal | ||
| key={id} | ||
| id={id} | ||
| bubble={bubble} | ||
| target_id={TargetId.ThreadPopup} | ||
| /> | ||
| ))} | ||
| {threadUserId && ( | ||
| <MessageSuggestionPortal | ||
| key={threadUserId} | ||
| id={threadUserId} | ||
| bubble={globalThis?.document} | ||
| target_id={TargetId.ThreadPage} | ||
| /> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
        
          
  
    
      
          
            122 changes: 122 additions & 0 deletions
          
          122 
        
  packages/extension/src/companion/suggestion/MessageSuggestionPortal.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,122 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { createPortal } from 'react-dom'; | ||
| import { inviteRecruiterFeature } from '@dailydotdev/shared/src/lib/featureManagement'; | ||
| import { useConditionalFeature } from '@dailydotdev/shared/src/hooks'; | ||
| import { useRequestProtocol } from '@dailydotdev/shared/src/hooks/useRequestProtocol'; | ||
| import user from '@dailydotdev/shared/__tests__/fixture/loggedUser'; | ||
| import { | ||
| generateQueryKey, | ||
| RequestKey, | ||
| } from '@dailydotdev/shared/src/lib/query'; | ||
| import { USER_REFERRAL_RECRUITER_QUERY } from '@dailydotdev/shared/src/graphql/users'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import { useBackgroundRequest } from '@dailydotdev/shared/src/hooks/companion'; | ||
| import { CoreIcon } from '@dailydotdev/shared/src/components/icons'; | ||
| import { useLogContext } from '@dailydotdev/shared/src/contexts/LogContext'; | ||
| import type { TargetId } from '@dailydotdev/shared/src/lib/log'; | ||
| import { LogEvent, TargetType } from '@dailydotdev/shared/src/lib/log'; | ||
| import { useGenerateSuggestionContainer } from './useGenerateSuggestionContainer'; | ||
|  | ||
| interface MessageSuggestionPortalProps { | ||
| bubble: HTMLElement | Document; | ||
| id: string; | ||
| target_id: TargetId.ThreadPage | TargetId.ThreadPopup; | ||
| } | ||
|  | ||
| const input = '.msg-form__contenteditable'; | ||
|  | ||
| export function MessageSuggestionPortal({ | ||
| bubble, | ||
| id, | ||
| target_id, | ||
| }: MessageSuggestionPortalProps) { | ||
| const { logEvent } = useLogContext(); | ||
| const { injectedElement } = useGenerateSuggestionContainer({ | ||
| id, | ||
| container: bubble, | ||
| }); | ||
| const { value, isLoading } = useConditionalFeature({ | ||
| feature: inviteRecruiterFeature, | ||
| shouldEvaluate: !!injectedElement, | ||
| }); | ||
| const { cta, message } = value; | ||
|  | ||
| const mutationKey = generateQueryKey(RequestKey.InviteRecruiter, user, id); | ||
| const { requestMethod } = useRequestProtocol(); | ||
| const { mutateAsync } = useMutation({ | ||
| mutationFn: () => | ||
| requestMethod( | ||
| USER_REFERRAL_RECRUITER_QUERY, | ||
| { toReferExternalId: id }, | ||
| { requestKey: JSON.stringify(mutationKey) }, | ||
| ), | ||
| }); | ||
|  | ||
| useBackgroundRequest(mutationKey, { | ||
| callback: async ({ res }) => { | ||
| const url = res?.userReferralRecruiter?.url; | ||
|  | ||
| if (!url) { | ||
| return; | ||
| } | ||
|  | ||
| const formattedMessage = message.replace('{{url}}', url); | ||
| const replyBox = bubble.querySelector(input) as HTMLElement; | ||
| const p = document.createElement('p'); | ||
| p.innerText = formattedMessage; | ||
|  | ||
| replyBox.innerText = ''; | ||
| replyBox.appendChild(p); | ||
|  | ||
| const inputEvent = new InputEvent('input', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| inputType: 'insertText', | ||
| data: formattedMessage, | ||
| }); | ||
|  | ||
| replyBox.dispatchEvent(inputEvent); | ||
| replyBox.focus(); | ||
| }, | ||
| }); | ||
|  | ||
| const shouldShow = !!injectedElement && !isLoading && !!id; | ||
| const isLoggedRef = React.useRef(false); | ||
|  | ||
| useEffect(() => { | ||
| if (shouldShow && !isLoggedRef.current) { | ||
| logEvent({ | ||
| event_name: LogEvent.Impression, | ||
| target_type: TargetType.InviteRecruiter, | ||
| target_id, | ||
| }); | ||
| isLoggedRef.current = true; | ||
| } | ||
| }, [logEvent, target_id, shouldShow]); | ||
|  | ||
| if (!injectedElement || isLoading || !id) { | ||
| return null; | ||
| } | ||
|  | ||
| const handleClick = () => { | ||
| logEvent({ | ||
| event_name: LogEvent.Click, | ||
| target_type: TargetType.InviteRecruiter, | ||
| target_id, | ||
| }); | ||
| mutateAsync(); | ||
| }; | ||
|  | ||
| return createPortal( | ||
| <button | ||
| type="button" | ||
| className="artdeco-button artdeco-button--2 artdeco-button--primary" | ||
| style={{ fontSize: '14px' }} | ||
| onClick={handleClick} | ||
| > | ||
| <CoreIcon style={{ width: '20px', height: '20px', marginRight: '4px' }} /> | ||
| {cta} | ||
| </button>, | ||
| injectedElement, | ||
| ); | ||
| } | 
      
      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.
does bubbles.map work? Then we can just do
const toUpdate = bubbles.map.....Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah that can work. Will just have to add
.filterfor ids that are not found. The additional iteration shouldn't impace the performance that much.