1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

add user notifications (#148)

* added notification settings

* notifications: added list to profile

* notifications: changed appearance for comment notifications
This commit is contained in:
muerwre 2023-03-11 17:16:31 +06:00 committed by GitHub
parent 23701a5261
commit a39d000ff2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 552 additions and 218 deletions

View file

@ -0,0 +1,28 @@
import { isAfter, isValid, parse, parseISO } from 'date-fns';
import { useAuth } from '../auth/useAuth';
import { useNotificationSettingsRequest } from './useNotificationSettingsRequest';
export const useNotificationSettings = () => {
const { isUser } = useAuth();
const {
error: settingsError,
enabled: settingsEnabled,
lastSeen,
lastDate,
isLoading: isLoadingSettings,
} = useNotificationSettingsRequest();
const enabled = !isLoadingSettings && !settingsError && settingsEnabled;
const hasNew =
enabled && !!lastDate && (!lastSeen || isAfter(lastDate, lastSeen));
return {
enabled,
hasNew,
available: isUser,
};
};

View file

@ -0,0 +1,36 @@
import { isValid, parseISO } from 'date-fns';
import useSWR from 'swr';
import { apiGetNotificationSettings } from '~/api/notifications/settings';
import { API } from '~/constants/api';
import { useAuth } from '../auth/useAuth';
const refreshInterval = 60e3; // 1min
export const useNotificationSettingsRequest = () => {
const { isUser } = useAuth();
const {
data,
isValidating: isLoading,
error,
} = useSWR(
isUser ? API.NOTIFICATIONS.SETTINGS : null,
async () => apiGetNotificationSettings(),
{ refreshInterval },
);
return {
isLoading,
error,
lastSeen:
data?.last_seen && isValid(parseISO(data.last_seen))
? parseISO(data?.last_seen)
: undefined,
lastDate:
data?.last_date && isValid(parseISO(data.last_date))
? parseISO(data?.last_date)
: undefined,
enabled: !!data?.enabled && (data.flow || data.comments),
};
};

View file

@ -0,0 +1,20 @@
import useSWR from 'swr';
import { apiGetNotifications } from '~/api/notifications/settings';
import { API } from '~/constants/api';
import { useAuth } from '../auth/useAuth';
export const useNotificationsList = () => {
const { isUser } = useAuth();
const {
data,
isValidating: isLoading,
error,
} = useSWR(isUser ? API.NOTIFICATIONS.LIST : null, async () =>
apiGetNotifications(),
);
return { isLoading, error, ...data };
};