From 8d1ebba2e1e3aabec7b2fcaa9510646edb03c571 Mon Sep 17 00:00:00 2001 From: ajnart Date: Thu, 23 Mar 2023 01:28:17 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=92=84=20Style=20and=20usability=20im?= =?UTF-8?q?provements=20to=20RSS=20widget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/widgets/rss/useGetRssFeed.tsx | 10 --- src/widgets/rss/RssWidgetTile.tsx | 115 ++++++++++++++---------- 2 files changed, 70 insertions(+), 55 deletions(-) delete mode 100644 src/hooks/widgets/rss/useGetRssFeed.tsx diff --git a/src/hooks/widgets/rss/useGetRssFeed.tsx b/src/hooks/widgets/rss/useGetRssFeed.tsx deleted file mode 100644 index 2fc9e07e3..000000000 --- a/src/hooks/widgets/rss/useGetRssFeed.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import { useQuery } from '@tanstack/react-query'; - -export const useGetRssFeed = (feedUrl: string) => - useQuery({ - queryKey: ['rss-feed', feedUrl], - queryFn: async () => { - const response = await fetch('/api/modules/rss'); - return response.json(); - }, - }); diff --git a/src/widgets/rss/RssWidgetTile.tsx b/src/widgets/rss/RssWidgetTile.tsx index 6c6e53b4e..0b2156f0f 100644 --- a/src/widgets/rss/RssWidgetTile.tsx +++ b/src/widgets/rss/RssWidgetTile.tsx @@ -14,7 +14,6 @@ import { Stack, Text, Title, - UnstyledButton, } from '@mantine/core'; import { IconBulldozer, @@ -25,11 +24,11 @@ import { IconRss, IconSpeakerphone, } from '@tabler/icons'; +import { useQuery } from '@tanstack/react-query'; +import dayjs from 'dayjs'; import { useTranslation } from 'next-i18next'; import Link from 'next/link'; import { useState } from 'react'; -import { useGetRssFeed } from '../../hooks/widgets/rss/useGetRssFeed'; -import { sleep } from '../../tools/client/time'; import { defineWidget } from '../helper'; import { IWidget } from '../widgets'; @@ -57,6 +56,15 @@ interface RssTileProps { widget: IRssWidget; } +const useGetRssFeed = (feedUrl: string) => + useQuery({ + queryKey: ['rss-feed', feedUrl], + queryFn: async () => { + const response = await fetch('/api/modules/rss'); + return response.json(); + }, + }); + function RssTile({ widget }: RssTileProps) { const { t } = useTranslation('modules/rss'); const { data, isLoading, isFetching, isError, refetch } = useGetRssFeed( @@ -65,9 +73,27 @@ function RssTile({ widget }: RssTileProps) { const { classes } = useStyles(); const [loadingOverlayVisible, setLoadingOverlayVisible] = useState(false); + function formatDate(input: string): string { + // Parse the input date as a local date + const inputDate = dayjs(new Date(input)); + const now = dayjs(); // Current date and time + + const diffInHours = now.diff(inputDate, 'hour'); + const diffInDays = now.diff(inputDate, 'day'); + + // If the input date is more than 2 weeks ago, return the formatted date + if (diffInDays > 14) { + return inputDate.format('DD MMM YYYY'); + } + if (diffInDays >= 1) { + return `${diffInDays} days ago`; + } + return `${diffInHours} hours ago`; + } + if (!data || isLoading) { return ( -
+
); @@ -87,32 +113,8 @@ function RssTile({ widget }: RssTileProps) { return ( - - - {data.feed.image ? ( - {data.feed.image.title} - ) : ( - {data.feed.title} - )} - { - setLoadingOverlayVisible(true); - await Promise.all([sleep(1500), refetch()]); - setLoadingOverlayVisible(false); - }} - disabled={isFetching || isLoading} - > - - - - - + + {data.feed.title && {data.feed.title}} {data.feed.items.map((item: any, index: number) => ( @@ -150,7 +152,7 @@ function RssTile({ widget }: RssTileProps) { {item.categories && ( {item.categories.map((category: any, categoryIndex: number) => ( - {category._} + {category} ))} )} @@ -160,7 +162,7 @@ function RssTile({ widget }: RssTileProps) { {item.content} - {item.pubDate && } + {item.pubDate && } @@ -169,23 +171,27 @@ function RssTile({ widget }: RssTileProps) { - - - - {data.feed.copyright} - - - - - - {data.feed.pubDate} - - + {data.feed.copyright && ( + + + + {data.feed.copyright} + + + )} + {data.feed.pubDate && ( + + + + {data.feed.pubDate} + + + )} {data.feed.lastBuildDate && ( - {data.feed.lastBuildDate} + {formatDate(data.feed.lastBuildDate)} )} @@ -204,6 +210,25 @@ function RssTile({ widget }: RssTileProps) { )} + refetch()} + bottom={10} + styles={{ + root: { + borderColor: 'red', + }, + }} + > + {data.feed.image ? ( + {data.feed.image.title} + ) : ( + + )} + ); From c7317c7278a982fcdb32ed31e57eded9bdbe7036 Mon Sep 17 00:00:00 2001 From: ajnart Date: Mon, 27 Mar 2023 11:42:17 +0900 Subject: [PATCH 2/4] Address PR comments --- src/widgets/rss/RssWidgetTile.tsx | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/widgets/rss/RssWidgetTile.tsx b/src/widgets/rss/RssWidgetTile.tsx index 0b2156f0f..b594803e6 100644 --- a/src/widgets/rss/RssWidgetTile.tsx +++ b/src/widgets/rss/RssWidgetTile.tsx @@ -78,17 +78,11 @@ function RssTile({ widget }: RssTileProps) { const inputDate = dayjs(new Date(input)); const now = dayjs(); // Current date and time - const diffInHours = now.diff(inputDate, 'hour'); - const diffInDays = now.diff(inputDate, 'day'); - - // If the input date is more than 2 weeks ago, return the formatted date - if (diffInDays > 14) { - return inputDate.format('DD MMM YYYY'); - } - if (diffInDays >= 1) { - return `${diffInDays} days ago`; - } - return `${diffInHours} hours ago`; + // The difference between the input date and now + const difference = now.diff(inputDate, 'ms'); + const duration = dayjs.duration(difference, 'ms'); + const humanizedDuration = duration.humanize(); + return `${humanizedDuration} ago`; } if (!data || isLoading) { From 64e8e85aacb49d93da3d8ad368c6aad0c71e3143 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 27 Mar 2023 06:10:26 +0000 Subject: [PATCH 3/4] refactor: Replace short hand type conversions with function calls Prefer using explicit casts by calling `Number`, `Boolean`, or `String` over using operators like `+`, `!!` or `"" +`. This is considered best practice as it improves readability. --- src/components/Dashboard/Views/DashboardView.tsx | 2 +- src/hooks/widgets/dashDot/api.ts | 2 +- src/widgets/weather/useWeatherForCity.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Dashboard/Views/DashboardView.tsx b/src/components/Dashboard/Views/DashboardView.tsx index 194098857..202232e45 100644 --- a/src/components/Dashboard/Views/DashboardView.tsx +++ b/src/components/Dashboard/Views/DashboardView.tsx @@ -60,7 +60,7 @@ const usePrepareGridstack = () => { }, [width]); return { - isReady: !!mainAreaWidth, + isReady: Boolean(mainAreaWidth), mainAreaRef, }; }; diff --git a/src/hooks/widgets/dashDot/api.ts b/src/hooks/widgets/dashDot/api.ts index 34a290475..fd9af4209 100644 --- a/src/hooks/widgets/dashDot/api.ts +++ b/src/hooks/widgets/dashDot/api.ts @@ -29,7 +29,7 @@ export const useGetUsenetInfo = (params: UsenetInfoRequestParams) => refetchInterval: POLLING_INTERVAL, keepPreviousData: true, retry: 2, - enabled: !!params.appId, + enabled: Boolean(params.appId), } ); diff --git a/src/widgets/weather/useWeatherForCity.ts b/src/widgets/weather/useWeatherForCity.ts index 3f21ebad6..be7a28ac0 100644 --- a/src/widgets/weather/useWeatherForCity.ts +++ b/src/widgets/weather/useWeatherForCity.ts @@ -20,7 +20,7 @@ export const useWeatherForCity = (cityName: string) => { const weatherQuery = useQuery({ queryKey: ['weather', { cityName }], queryFn: () => fetchWeather(city?.results[0]), - enabled: !!city, + enabled: Boolean(city), cacheTime: 1000 * 60 * 60 * 6, // the weather is cached for 6 hours staleTime: 1000 * 60 * 5, // the weather is considered stale after 5 minutes }); From 69dd4ea75c76ac26278c8deb01855ed1f2ea14bb Mon Sep 17 00:00:00 2001 From: ajnart Date: Sat, 18 Mar 2023 18:16:21 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20proxied?= =?UTF-8?q?=20docker=20#758?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/pages/api/docker/DockerSingleton.ts | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 66a0673f5..991371dfe 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ yarn-debug.log* yarn-error.log* # local env files +.env .env.local .env.development.local .env.test.local diff --git a/src/pages/api/docker/DockerSingleton.ts b/src/pages/api/docker/DockerSingleton.ts index 804444274..9694059be 100644 --- a/src/pages/api/docker/DockerSingleton.ts +++ b/src/pages/api/docker/DockerSingleton.ts @@ -9,7 +9,12 @@ export default class DockerSingleton extends Docker { public static getInstance(): DockerSingleton { if (!DockerSingleton.dockerInstance) { - DockerSingleton.dockerInstance = new DockerSingleton(); + DockerSingleton.dockerInstance = new Docker({ + // If env variable DOCKER_HOST is not set, it will use the default socket + ...(process.env.DOCKER_HOST && { host: process.env.DOCKER_HOST }), + // Same thing for docker port + ...(process.env.DOCKER_PORT && { port: process.env.DOCKER_PORT }), + }); } return DockerSingleton.dockerInstance; }