这是indexloc提供的服务,不要输入任何密码
Skip to content
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
5 changes: 3 additions & 2 deletions console/src/Endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const Endpoints = {
hasuractlMetadata: `${hasuractlUrl}/apis/metadata`,
hasuractlMigrateSettings: `${hasuractlUrl}/apis/migrate/settings`,
telemetryServer: 'wss://telemetry.hasura.io/v1/ws',
consoleNotificationsStg: 'https://data.hasura-stg.hasura-app.io/v1/query',
consoleNotificationsProd: 'https://data.hasura.io/v1/query',
consoleNotificationsStg:
'https://notifications.hasura-stg.hasura-app.io/v1/graphql',
consoleNotificationsProd: 'https://notifications.hasura.io/v1/graphql',
};

const globalCookiePolicy = 'same-origin';
Expand Down
2 changes: 1 addition & 1 deletion console/src/components/App/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const CONNECTION_FAILED = 'App/CONNECTION_FAILED';
* title: string null
* message: string null
* autoDismiss: integer 5, set to 0 to not auto-dismiss
* dismissible: bool true, set if user can dismiss notification
* dismissible: 'button', set if the user can dismiss the notification, false otherwise
* action: object null, action button with label string and callback function
* children: element/string, null, add custom element, over-rides action
* onAdd: function, null, called when notification is successfully created, 1st argument is the notification
Expand Down
65 changes: 26 additions & 39 deletions console/src/components/Common/utils/v1QueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,47 +700,34 @@ export const getConsoleNotificationQuery = (
time: Date | string | number,
userType?: Nullable<ConsoleScope>
) => {
let consoleUserScope = {
$ilike: `%${userType}%`,
};
let consoleUserScopeVar = `%${userType}%`;
if (!userType) {
consoleUserScope = {
$ilike: '%OSS%',
};
consoleUserScopeVar = '%OSS%';
}

return {
args: {
table: 'console_notification',
columns: ['*'],
where: {
$or: [
{
expiry_date: {
$gte: time,
},
},
{
expiry_date: {
$eq: null,
},
},
],
scope: consoleUserScope,
start_date: { $lte: time },
},
order_by: [
{
type: 'asc',
nulls: 'last',
column: 'priority',
},
{
type: 'desc',
column: 'start_date',
},
],
},
type: 'select',
const query = `query fetchNotifications($currentTime: timestamptz, $userScope: String) {
console_notifications(
where: {start_date: {_lte: $currentTime}, scope: {_ilike: $userScope}, _or: [{expiry_date: {_gte: $currentTime}}, {expiry_date: {_eq: null}}]},
order_by: {priority: asc_nulls_last, start_date: desc}
) {
content
created_at
external_link
expiry_date
id
is_active
priority
scope
start_date
subject
type
}
}`;

const variables = {
userScope: consoleUserScopeVar,
currentTime: time,
};

return { query, variables };
};
195 changes: 119 additions & 76 deletions console/src/components/Main/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ const fetchConsoleNotifications = () => (dispatch, getState) => {
const consoleId = window.__env.consoleId;
const consoleScope = getConsoleScope(serverVersion, consoleId);
let userType = 'admin';
if (headers.hasOwnProperty(HASURA_COLLABORATOR_TOKEN)) {
const collabToken = headers[HASURA_COLLABORATOR_TOKEN];
const headerHasCollabToken = Object.keys(headers).find(
header => header.toLowerCase() === HASURA_COLLABORATOR_TOKEN
);

if (headerHasCollabToken) {
const collabToken = headers[headerHasCollabToken];
userType = getUserType(collabToken);
}

Expand All @@ -94,12 +98,14 @@ const fetchConsoleNotifications = () => (dispatch, getState) => {
}

const now = new Date().toISOString();
const query = getConsoleNotificationQuery(now, consoleScope);
const payload = getConsoleNotificationQuery(now, consoleScope);
const options = {
body: JSON.stringify(query),
body: JSON.stringify(payload),
method: 'POST',
headers: {
'content-type': 'application/json',
// temp. change until Auth is added
'x-hasura-role': 'user',
},
};

Expand All @@ -108,94 +114,131 @@ const fetchConsoleNotifications = () => (dispatch, getState) => {
const lastSeenNotifications = JSON.parse(
window.localStorage.getItem('notifications:lastSeen')
);
if (!data.length) {
dispatch({ type: FETCH_CONSOLE_NOTIFICATIONS_SET_DEFAULT });
dispatch(
updateConsoleNotificationsState({
read: 'default',
date: now,
showBadge: false,
})
);
if (!lastSeenNotifications) {
window.localStorage.setItem(
'notifications:lastSeen',
JSON.stringify(0)
);
}
return;
}
if (data.data.console_notifications) {
const fetchedData = data.data.console_notifications;

const uppercaseScopedData = makeUppercaseScopes(data);
let filteredData = filterScope(uppercaseScopedData, consoleScope);

if (
!lastSeenNotifications ||
lastSeenNotifications !== filteredData.length
) {
window.localStorage.setItem(
'notifications:lastSeen',
JSON.stringify(filteredData.length)
);
}

if (previousRead) {
if (!consoleStateDB.console_notifications) {
if (!fetchedData.length) {
dispatch({ type: FETCH_CONSOLE_NOTIFICATIONS_SET_DEFAULT });
dispatch(
updateConsoleNotificationsState({
read: [],
read: 'default',
date: now,
showBadge: true,
showBadge: false,
})
);
} else {
let newReadValue;

if (previousRead === 'default' || previousRead === 'error') {
newReadValue = [];
toShowBadge = false;
} else if (previousRead === 'all') {
const previousList = JSON.parse(
localStorage.getItem('notifications:data')
if (!lastSeenNotifications) {
window.localStorage.setItem(
'notifications:lastSeen',
JSON.stringify(0)
);
if (previousList.length) {
const resDiff = filteredData.filter(
newNotif =>
!previousList.find(oldNotif => oldNotif.id === newNotif.id)
}
return;
}

// NOTE: these 2 steps may not be required if the table in the DB
// enforces the usage of `enums` and we're sure that the notification scope
// is only from the allowed permutations of scope. We aren't doing that yet
// because within the GQL query, I can't be using the `_ilike` operator during
// filtering. Hence I'm keeping it here since this is a new feature and
// mistakes can happen while adding data into the DB.

// TODO: is to remove these once things are more streamlined
const uppercaseScopedData = makeUppercaseScopes(fetchedData);
let filteredData = filterScope(uppercaseScopedData, consoleScope);

if (
lastSeenNotifications &&
lastSeenNotifications > filteredData.length
) {
window.localStorage.setItem(
'notifications:lastSeen',
JSON.stringify(filteredData.length)
);
}

if (previousRead) {
if (!consoleStateDB.console_notifications) {
dispatch(
updateConsoleNotificationsState({
read: [],
date: now,
showBadge: true,
})
);
} else {
let newReadValue;
if (previousRead === 'default' || previousRead === 'error') {
newReadValue = [];
toShowBadge = false;
} else if (previousRead === 'all') {
const previousList = JSON.parse(
localStorage.getItem('notifications:data')
);
if (!resDiff.length) {
// since the data hasn't changed since the last call
newReadValue = previousRead;
if (!previousList) {
// we don't have a record of the IDs that were marked as read previously
newReadValue = [];
toShowBadge = true;
} else if (previousList.length) {
const readNotificationsDiff = filteredData.filter(
newNotif =>
!previousList.find(oldNotif => oldNotif.id === newNotif.id)
);
if (!readNotificationsDiff.length) {
// since the data hasn't changed since the last call
newReadValue = previousRead;
toShowBadge = false;
} else {
newReadValue = [...previousList.map(notif => `${notif.id}`)];
toShowBadge = true;
filteredData = [...readNotificationsDiff, ...previousList];
}
}
} else {
newReadValue = previousRead;
if (
previousRead.length &&
lastSeenNotifications >= filteredData.length
) {
toShowBadge = false;
} else {
newReadValue = [...previousList.map(notif => `${notif.id}`)];
} else if (lastSeenNotifications < filteredData.length) {
toShowBadge = true;
filteredData = [...resDiff, ...previousList];
}
}
} else {
newReadValue = previousRead;
if (
previousRead.length &&
lastSeenNotifications === filteredData.length
) {
toShowBadge = false;
}
dispatch(
updateConsoleNotificationsState({
read: newReadValue,
date: consoleStateDB.console_notifications[userType].date,
showBadge: toShowBadge,
})
);
}
dispatch(
updateConsoleNotificationsState({
read: newReadValue,
date: consoleStateDB.console_notifications[userType].date,
showBadge: toShowBadge,
})
}

dispatch({
type: FETCH_CONSOLE_NOTIFICATIONS_SUCCESS,
data: filteredData,
});

// update/set the lastSeen value upon data is set
if (
!lastSeenNotifications ||
lastSeenNotifications !== filteredData.length
) {
window.localStorage.setItem(
'notifications:lastSeen',
JSON.stringify(filteredData.length)
);
}
return;
}

dispatch({
type: FETCH_CONSOLE_NOTIFICATIONS_SUCCESS,
data: filteredData,
});
dispatch({ type: FETCH_CONSOLE_NOTIFICATIONS_ERROR });
dispatch(
updateConsoleNotificationsState({
read: 'error',
date: now,
showBadge: false,
})
);
})
.catch(err => {
console.error(err);
Expand Down
1 change: 0 additions & 1 deletion console/src/components/Main/ConsoleNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type ConsoleNotification = {
scope?: NotificationScope;
};

// FIXME? : we may have to remove this
export const defaultNotification: ConsoleNotification = {
subject: 'No updates available at the moment',
created_at: Date.now(),
Expand Down
Loading