Shelly011

Untitled

Feb 28th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable jsx-a11y/anchor-is-valid,camelcase */
  2.  
  3. import * as React from "react";
  4. import { createRoot } from "react-dom/client";
  5. import * as Sentry from "@sentry/react";
  6.  
  7. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  8. import { IconProp } from "@fortawesome/fontawesome-svg-core";
  9. import {
  10.   faHeart,
  11.   faHeartBroken,
  12.   faThumbtack,
  13. } from "@fortawesome/free-solid-svg-icons";
  14. import { clone, get, has, isNaN } from "lodash";
  15. import { Integrations } from "@sentry/tracing";
  16. import GlobalAppContext, { GlobalAppContextT } from "../utils/GlobalAppContext";
  17. import {
  18.   withAlertNotifications,
  19.   WithAlertNotificationsInjectedProps,
  20. } from "../notifications/AlertNotificationsHOC";
  21.  
  22. import APIServiceClass from "../utils/APIService";
  23. import BrainzPlayer from "../brainzplayer/BrainzPlayer";
  24. import ErrorBoundary from "../utils/ErrorBoundary";
  25. import PinRecordingModal from "../pins/PinRecordingModal";
  26. import { getListenablePin, getPageProps } from "../utils/utils";
  27. import SimpleModal from "../utils/SimpleModal";
  28. import UserFeedback from "./UserFeedback";
  29. import UserPins from "../pins/UserPins";
  30.  
  31. export type UserTasteProps = {
  32.   feedback?: Array<FeedbackResponseWithTrackMetadata>;
  33.   totalFeedbackCount: number;
  34.   pins: PinnedRecording[];
  35.   totalPinsCount: number;
  36.   user: ListenBrainzUser;
  37. } & WithAlertNotificationsInjectedProps;
  38.  
  39. export interface UserTasteState {
  40.   recordingToPin?: BaseListenFormat;
  41. }
  42.  
  43. export default class UserTaste extends React.Component<
  44.   UserTasteProps,
  45.   UserTasteState
  46. > {
  47.   static contextType = GlobalAppContext;
  48.   static RecordingMetadataToListenFormat = (
  49.     feedbackItem: FeedbackResponseWithTrackMetadata
  50.   ): Listen => {
  51.     return {
  52.       listened_at: feedbackItem.created ?? 0,
  53.       track_metadata: { ...feedbackItem.track_metadata },
  54.     };
  55.   };
  56.  
  57.   declare context: React.ContextType<typeof GlobalAppContext>;
  58.  
  59.   constructor(props: UserTasteProps) {
  60.     super(props);
  61.     this.state = {};
  62.   }
  63.  
  64.   updateRecordingToPin = (recordingToPin: BaseListenFormat) => {
  65.     this.setState({ recordingToPin });
  66.   };
  67.  
  68.   render() {
  69.     const { recordingToPin } = this.state;
  70.     const {
  71.       feedback,
  72.       user,
  73.       newAlert,
  74.       totalFeedbackCount,
  75.       pins,
  76.       totalPinsCount,
  77.     } = this.props;
  78.     const { APIService, currentUser } = this.context;
  79.     const listensFromFeedback: BaseListenFormat[] =
  80.       feedback
  81.         // remove feedback items for which track metadata wasn't found. this usually means bad
  82.         // msid or mbid data was submitted by the user.
  83.         ?.filter((item) => item?.track_metadata)
  84.         .map((feedbackItem) =>
  85.           UserTaste.RecordingMetadataToListenFormat(feedbackItem)
  86.         ) ?? [];
  87.     const listensFromPins = pins.map((pin) => {
  88.       return getListenablePin(pin);
  89.     });
  90.     console.log(listensFromPins);
  91.     const listenables = [...listensFromFeedback, ...listensFromPins];
  92.     return (
  93.       <div role="main">
  94.         <div className="row">
  95.           <div className="col-md-7">
  96.             <UserFeedback
  97.               feedback={feedback}
  98.               newAlert={newAlert}
  99.               totalCount={totalFeedbackCount}
  100.               user={user}
  101.               updateRecordingToPin={this.updateRecordingToPin}
  102.             />
  103.           </div>
  104.           <div className="col-md-5">
  105.             <UserPins
  106.               user={user}
  107.               newAlert={newAlert}
  108.               pins={pins}
  109.               totalCount={totalPinsCount}
  110.             />
  111.           </div>
  112.         </div>
  113.         {currentUser && (
  114.           <PinRecordingModal
  115.             recordingToPin={recordingToPin || listenables[0]}
  116.             newAlert={newAlert}
  117.           />
  118.         )}
  119.         <BrainzPlayer
  120.           listens={listenables}
  121.           newAlert={newAlert}
  122.           listenBrainzAPIBaseURI={APIService.APIBaseURI}
  123.           refreshSpotifyToken={APIService.refreshSpotifyToken}
  124.           refreshYoutubeToken={APIService.refreshYoutubeToken}
  125.         />
  126.       </div>
  127.     );
  128.   }
  129. }
  130.  
  131. document.addEventListener("DOMContentLoaded", () => {
  132.   const {
  133.     domContainer,
  134.     reactProps,
  135.     globalReactProps,
  136.     optionalAlerts,
  137.   } = getPageProps();
  138.   const {
  139.     api_url,
  140.     sentry_dsn,
  141.     current_user,
  142.     spotify,
  143.     youtube,
  144.     sentry_traces_sample_rate,
  145.   } = globalReactProps;
  146.   const { feedback, feedback_count, user, pins, pin_count } = reactProps;
  147.  
  148.   const apiService = new APIServiceClass(
  149.     api_url || `${window.location.origin}/1`
  150.   );
  151.  
  152.   if (sentry_dsn) {
  153.     Sentry.init({
  154.       dsn: sentry_dsn,
  155.       integrations: [new Integrations.BrowserTracing()],
  156.       tracesSampleRate: sentry_traces_sample_rate,
  157.     });
  158.   }
  159.  
  160.   const UserTasteWithAlertNotifications = withAlertNotifications(UserTaste);
  161.  
  162.   const modalRef = React.createRef<SimpleModal>();
  163.   const globalProps: GlobalAppContextT = {
  164.     APIService: apiService,
  165.     currentUser: current_user,
  166.     spotifyAuth: spotify,
  167.     youtubeAuth: youtube,
  168.     modal: modalRef,
  169.   };
  170.  
  171.   const renderRoot = createRoot(domContainer!);
  172.   renderRoot.render(
  173.     <ErrorBoundary>
  174.       <SimpleModal ref={modalRef} />
  175.       <GlobalAppContext.Provider value={globalProps}>
  176.         <UserTasteWithAlertNotifications
  177.           initialAlerts={optionalAlerts}
  178.           user={user}
  179.           feedback={feedback}
  180.           totalFeedbackCount={feedback_count}
  181.           pins={pins}
  182.           totalPinsCount={pin_count}
  183.         />
  184.       </GlobalAppContext.Provider>
  185.     </ErrorBoundary>
  186.   );
  187. });
  188.  
Advertisement
Add Comment
Please, Sign In to add comment