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:
parent
23701a5261
commit
a39d000ff2
27 changed files with 552 additions and 218 deletions
28
src/hooks/notifications/useNotificationSettings.ts
Normal file
28
src/hooks/notifications/useNotificationSettings.ts
Normal 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,
|
||||
};
|
||||
};
|
36
src/hooks/notifications/useNotificationSettingsRequest.ts
Normal file
36
src/hooks/notifications/useNotificationSettingsRequest.ts
Normal 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),
|
||||
};
|
||||
};
|
20
src/hooks/notifications/useNotificationsList.ts
Normal file
20
src/hooks/notifications/useNotificationsList.ts
Normal 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 };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue