Skip to content
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

Add option to emit discussion metadata #136

Merged
merged 14 commits into from
Jul 11, 2021
Merged

Add option to emit discussion metadata #136

merged 14 commits into from
Jul 11, 2021

Conversation

laymonage
Copy link
Member

@laymonage laymonage commented Jul 11, 2021

Fix #114.

Discussion metadata interface:

export interface IDiscussionData {
  id: string;
  url: string;
  locked: boolean;
  repository: {
    nameWithOwner: string;
  };
  reactionCount: number;
  totalCommentCount: number;
  totalReplyCount: number;
  reactions: IReactionGroups;
}

export interface IUser {
  avatarUrl: string;
  login: string;
  url: string;
}

export interface IMessage<T> {
  giscus: T;
}

export interface IMetadataMessage {
  discussion: IDiscussionData;
  viewer: IUser;
}

The event.data of MessageEvent will be typed as IMessage<IMetadataMessage>, i.e.

function handleMessage(event: MessageEvent) {
  if (event.origin !== 'https://giscus.app') return;
  if (!(typeof event.data === 'object' && event.data.giscus)) return;

  const giscusData = event.data.giscus;

  // Make sure that the message is one that contains the discussion metadata.
  // This is necessary because other message types are also available.
  if ('discussion' in giscusData) {
    const discussion: IDiscussionData = giscusData.discussion;
    const viewer: IUser = giscusData.viewer;
    // Do whatever you want with it
  }
}

window.addEventListener('message', handleMessage);

This can be used to add a link to the discussion on the host page (utterance/utterances#547, utterance/utterances#355, utterance/utterances#301, utterance/utterances#276). It can also be used to display the currently logged-in user (utterance/utterances#313). Also can be used to display the number of comments and replies (utterance/utterances#296, utterance/utterances#36).

This PR also includes major refactoring of the pagination handling logic by extracting it into the useFrontBackDiscussion hook.

@vercel
Copy link

vercel bot commented Jul 11, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/laymonage/giscus/DcVBB8jsKJXeV9Wgoy3RZdBM3f9g
✅ Preview: https://giscus-git-emit-metadata-laymonage.vercel.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Report discussion metadata to the parent window.
1 participant