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 live data indicator #1056

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions apps/web/components/Gw2Api/Gw2AccountSubscriptionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type Cache = {

type SubscriptionContext<T extends SubscriptionType> = (type: T, accountId: string, callback: SubscriptionCallback<T>) => CancelSubscription;

const context = createContext<SubscriptionContext<SubscriptionType>>(() => () => {});
const subscriptionContext = createContext<SubscriptionContext<SubscriptionType>>(() => () => {});
const hasActiveSubscriptionContext = createContext(false);

function hasType<T extends SubscriptionType>(type: T) {
return (subscription: ActiveSubscription<SubscriptionType>): subscription is ActiveSubscription<T> => subscription.type === type;
Expand Down Expand Up @@ -175,14 +176,16 @@ export const Gw2AccountSubscriptionProvider: FC<Gw2AccountSubscriptionProviderPr
}, []);

return (
<context.Provider value={subscribe}>
{children}
</context.Provider>
<subscriptionContext.Provider value={subscribe}>
<hasActiveSubscriptionContext.Provider value={activeSubscriptions.length > 0}>
{children}
</hasActiveSubscriptionContext.Provider>
</subscriptionContext.Provider>
);
};

export function useSubscribe<T extends SubscriptionType>(type: T, accountId: string, callback: SubscriptionCallback<T>) {
const subscribe = useContext(context);
const subscribe = useContext(subscriptionContext);

useEffect(
() => subscribe(type, accountId, callback),
Expand All @@ -200,6 +203,10 @@ export function useSubscription<T extends SubscriptionType>(type: T, accountId:
return data === undefined ? { loading: true } : { loading: false, ...data };
}

export function usePageHasActiveSubscriptions() {
return useContext(hasActiveSubscriptionContext);
}

function useInterval(active: boolean = false, seconds: number, callback: () => void) {
const callbackRef = useRefTo(callback);
const activeRef = useRefTo(active);
Expand Down
16 changes: 16 additions & 0 deletions apps/web/components/Layout/Header/LiveUpdateStatus.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.live {
display: inline-block;
width: 8px;
height: 8px;
color: #ef5350;
background-color: currentColor;
border-radius: 4px;
vertical-align: 2px;
margin-right: 8px;
animation: live ease-in 2s infinite;
}

@keyframes live {
0% { box-shadow: 0 0 0 0 currentColor; }
40% { box-shadow: 0 0 0 4px transparent; }
}
23 changes: 23 additions & 0 deletions apps/web/components/Layout/Header/LiveUpdateStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client';

import { usePageHasActiveSubscriptions } from '@/components/Gw2Api/Gw2AccountSubscriptionProvider';
import { Button } from '@gw2treasures/ui/components/Form/Button';
import type { FC } from 'react';
import styles from './LiveUpdateStatus.module.css';
import { DropDown } from '@gw2treasures/ui/components/DropDown/DropDown';

export interface LiveUpdateStatusProps {}

export const LiveUpdateStatus: FC<LiveUpdateStatusProps> = ({}) => {
const live = usePageHasActiveSubscriptions();

if(!live) {
return null;
}

return (
<DropDown hideTop={false} preferredPlacement="bottom" button={<Button appearance="menu"><span className={styles.live}/> Live</Button>}>
<p>Your account data on this page<br/> is automatically updating.</p>
</DropDown>
);
};
2 changes: 2 additions & 0 deletions apps/web/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ReviewCountBadge } from './Header/ReviewCountBadge';
import { UserButton } from './Header/UserButton';
import { translations as itemTypeTranslations } from '../Item/ItemType.translations';
import { Rarity } from '@gw2treasures/database';
import { LiveUpdateStatus } from './Header/LiveUpdateStatus';

interface LayoutProps {
children: ReactNode;
Expand Down Expand Up @@ -44,6 +45,7 @@ const Layout: FC<LayoutProps> = ({ children }) => {
<span>gw2treasures.com</span>
</Link>
<Search translations={searchTranslations}/>
<LiveUpdateStatus/>
<div className={styles.right}>
<LinkButton appearance="menu" href="/review">
<Icon icon="review-queue"/><span className={styles.responsive}> Review<ReviewCountBadge/></span>
Expand Down
Loading