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

fix: refresh button also for non-admin user #1045

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
34 changes: 34 additions & 0 deletions src/components/PageRefreshButton/PageRefreshButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';
import { toast } from 'sonner';
import logger from '@/logger';
import { RefreshCw } from 'lucide-react';
import { cn } from '@/lib/utils.ts';
import { Button } from '@/components';

export const PageRefreshButton = () => {
const queryClient = useQueryClient();
const [isRefreshing, setIsRefreshing] = useState(false);

const onRefreshClick = async () => {
setIsRefreshing(true);
try {
// Wait for all queries invalidation (data refetching)
// and a minimum delay of 0.5sec before proceeding to make sure the UI doesn't flicker
await Promise.all([queryClient.invalidateQueries(), new Promise((resolve) => setTimeout(resolve, 500))]);
toast.success('Data refreshed');
} catch {
logger.error('Failed to refresh');
toast.error('Failed to refresh');
} finally {
setIsRefreshing(false);
}
};

return (
<Button variant="ghost" className="ml-auto" disabled={isRefreshing} onClick={onRefreshClick}>
<RefreshCw className={cn('h-4 w-4 mr-2', isRefreshing && 'animate-spin')} />
Refresh
</Button>
);
};
1 change: 1 addition & 0 deletions src/components/PageRefreshButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PageRefreshButton.tsx';
41 changes: 5 additions & 36 deletions src/routes/admin/_admin.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { Button, Logo, ModeToggle, Profile, Sheet, Tooltip, TooltipContent, TooltipTrigger } from '@/components';
import { cn } from '@/lib/utils.ts';
import logger from '@/logger';
import { useQueryClient } from '@tanstack/react-query';
import { Logo, ModeToggle, Profile, Sheet, Tooltip, TooltipContent, TooltipTrigger } from '@/components';
import { createFileRoute, Link, Outlet, redirect, useLocation } from '@tanstack/react-router';
import {
ArrowLeftRight,
KeyRound,
KeySquare,
Mail,
RefreshCw,
Route as RouteIcon,
UsersRound,
Webhook,
} from 'lucide-react';
import { ArrowLeftRight, KeyRound, KeySquare, Mail, Route as RouteIcon, UsersRound, Webhook } from 'lucide-react';

import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import { PageRefreshButton } from '@/components/PageRefreshButton';

export const Route = createFileRoute('/admin/_admin')({
beforeLoad: ({ context, location }) => {
Expand All @@ -29,23 +17,6 @@ export const Route = createFileRoute('/admin/_admin')({
function LayoutComponent() {
const [route, setRoute] = useState<string>('/admin/xpub');
const { pathname } = useLocation();
const queryClient = useQueryClient();
const [isRefreshing, setIsRefreshing] = useState(false);

const onRefreshClick = async () => {
setIsRefreshing(true);
try {
// Wait for all queries invalidation (data refetching)
// and a minimum delay of 0.5sec before proceeding to make sure the UI doesn't flicker
await Promise.all([queryClient.invalidateQueries(), new Promise((resolve) => setTimeout(resolve, 500))]);
toast.success('Data refreshed');
} catch {
logger.error('Failed to refresh');
toast.error('Failed to refresh');
} finally {
setIsRefreshing(false);
}
};

useEffect(() => {
setRoute(pathname);
Expand All @@ -56,6 +27,7 @@ function LayoutComponent() {
return 'bg-accent text-accent-foreground';
}
};

return (
<div className="flex min-h-screen w-full flex-col bg-muted/40">
<aside className="fixed inset-y-0 left-0 z-10 hidden w-14 flex-col border-r bg-background sm:flex">
Expand Down Expand Up @@ -158,10 +130,7 @@ function LayoutComponent() {
<Sheet>
<h1>SPV Wallet Admin</h1>
</Sheet>
<Button variant="ghost" className="ml-auto" disabled={isRefreshing} onClick={onRefreshClick}>
<RefreshCw className={cn('h-4 w-4 mr-2', isRefreshing && 'animate-spin')} />
Refresh
</Button>
<PageRefreshButton />
<ModeToggle />
<Profile />
</header>
Expand Down
6 changes: 4 additions & 2 deletions src/routes/user/_user.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Outlet, createFileRoute, Link, useLocation, redirect } from '@tanstack/react-router';
import { createFileRoute, Link, Outlet, redirect, useLocation } from '@tanstack/react-router';

import { KeySquare, Route as RouteIcon, ArrowLeftRight } from 'lucide-react';
import { ArrowLeftRight, KeySquare, Route as RouteIcon } from 'lucide-react';

import { useEffect, useState } from 'react';

import { Logo, ModeToggle, Profile, Sheet, Tooltip, TooltipContent, TooltipTrigger } from '@/components';
import { PageRefreshButton } from '@/components/PageRefreshButton';

export const Route = createFileRoute('/user/_user')({
beforeLoad: ({ context, location }) => {
Expand Down Expand Up @@ -82,6 +83,7 @@ function LayoutComponent() {
<Sheet>
<h1>SPV Wallet Admin</h1>
</Sheet>
<PageRefreshButton />
<ModeToggle />
<Profile />
</header>
Expand Down
Loading