Skip to content

Commit

Permalink
feat: show follow button when anon (#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert authored Oct 1, 2024
1 parent 752a701 commit 4c805f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { ButtonVariant } from '../buttons/Button';
import { useContentPreference } from '../../hooks/contentPreference/useContentPreference';
import SourceActionsNotify from '../sources/SourceActions/SourceActionsNotify';
import SourceActionsFollow from '../sources/SourceActions/SourceActionsFollow';
import { useAuthContext } from '../../contexts/AuthContext';
import { Origin } from '../../lib/log';
import { useIsSpecialUser } from '../../hooks/auth/useIsSpecialUser';

export type FollowButtonProps = {
className?: string;
Expand All @@ -29,7 +29,6 @@ export const FollowButton = ({
type,
origin,
}: FollowButtonProps): ReactElement => {
const { user } = useAuthContext();
const { follow, unfollow, subscribe, unsubscribe } = useContentPreference();

const { mutate: onButtonClick, isLoading: isLoadingFollow } = useMutation(
Expand Down Expand Up @@ -80,7 +79,7 @@ export const FollowButton = ({

const isLoading = isLoadingFollow || isLoadingNotify;

if (!user || user.id === userId) {
if (useIsSpecialUser({ userId })) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useToastNotification } from '../useToastNotification';
import { ContentPreferenceMutation } from './types';
import { useLogContext } from '../../contexts/LogContext';
import { LogEvent } from '../../lib/log';
import { AuthTriggers } from '../../lib/auth';

export type UseContentPreference = {
follow: ContentPreferenceMutation;
Expand All @@ -21,7 +22,7 @@ export type UseContentPreference = {
};

export const useContentPreference = (): UseContentPreference => {
const { user } = useAuthContext();
const { user, showLogin } = useAuthContext();
const { displayToast } = useToastNotification();
const { logEvent } = useLogContext();

Expand All @@ -32,6 +33,12 @@ export const useContentPreference = (): UseContentPreference => {
entityName,
opts,
}: PropsParameters<UseContentPreference['follow']>) => {
if (!user) {
showLogin({ trigger: AuthTriggers.Follow });

throw new Error('not logged in');
}

logEvent({
event_name: LogEvent.Follow,
target_id: id,
Expand Down Expand Up @@ -59,6 +66,12 @@ export const useContentPreference = (): UseContentPreference => {
entityName,
opts,
}: PropsParameters<UseContentPreference['unfollow']>) => {
if (!user) {
showLogin({ trigger: AuthTriggers.Follow });

throw new Error('not logged in');
}

logEvent({
event_name: LogEvent.Unfollow,
target_id: id,
Expand All @@ -85,6 +98,12 @@ export const useContentPreference = (): UseContentPreference => {
entityName,
opts,
}: PropsParameters<UseContentPreference['subscribe']>) => {
if (!user) {
showLogin({ trigger: AuthTriggers.Follow });

throw new Error('not logged in');
}

logEvent({
event_name: LogEvent.Subscribe,
target_id: id,
Expand Down Expand Up @@ -115,6 +134,12 @@ export const useContentPreference = (): UseContentPreference => {
entityName,
opts,
}: PropsParameters<UseContentPreference['subscribe']>) => {
if (!user) {
showLogin({ trigger: AuthTriggers.Follow });

throw new Error('not logged in');
}

logEvent({
event_name: LogEvent.Unsubscribe,
target_id: id,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export enum AuthTriggers {
CommentDownvote = 'comment downvote',
WelcomePage = 'welcome page',
FromNotification = 'from notification',
Follow = 'follow',
}

export type AuthTriggersType =
Expand Down

0 comments on commit 4c805f3

Please sign in to comment.