2020-06-30 11:47:21 -07:00
|
|
|
import { IGalleryItem, JunoClient } from "../Juno/JunoClient";
|
|
|
|
import * as ViewModels from "../Contracts/ViewModels";
|
|
|
|
import { NotificationConsoleUtils } from "./NotificationConsoleUtils";
|
|
|
|
import { ConsoleDataType } from "../Explorer/Menus/NotificationConsole/NotificationConsoleComponent";
|
|
|
|
import * as Logger from "../Common/Logger";
|
|
|
|
import {
|
|
|
|
GalleryTab,
|
|
|
|
SortBy,
|
|
|
|
GalleryViewerComponent
|
|
|
|
} from "../Explorer/Controls/NotebookGallery/GalleryViewerComponent";
|
|
|
|
|
|
|
|
export enum NotebookViewerParams {
|
|
|
|
NotebookUrl = "notebookUrl",
|
2020-07-10 14:23:53 -07:00
|
|
|
GalleryItemId = "galleryItemId",
|
|
|
|
HideInputs = "hideInputs"
|
2020-06-30 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface NotebookViewerProps {
|
|
|
|
notebookUrl: string;
|
|
|
|
galleryItemId: string;
|
2020-07-10 14:23:53 -07:00
|
|
|
hideInputs: boolean;
|
2020-06-30 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export enum GalleryViewerParams {
|
|
|
|
SelectedTab = "tab",
|
|
|
|
SortBy = "sort",
|
|
|
|
SearchText = "q"
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GalleryViewerProps {
|
|
|
|
selectedTab: GalleryTab;
|
|
|
|
sortBy: SortBy;
|
|
|
|
searchText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadItem(
|
|
|
|
container: ViewModels.Explorer,
|
|
|
|
junoClient: JunoClient,
|
|
|
|
data: IGalleryItem,
|
|
|
|
onComplete: (item: IGalleryItem) => void
|
|
|
|
): void {
|
|
|
|
const name = data.name;
|
2020-07-15 13:58:43 -07:00
|
|
|
container.showOkCancelModalDialog(
|
|
|
|
"Download to My Notebooks",
|
|
|
|
`Download ${name} from gallery as a copy to your notebooks to run and/or edit the notebook.`,
|
|
|
|
"Download",
|
|
|
|
async () => {
|
|
|
|
const notificationId = NotificationConsoleUtils.logConsoleMessage(
|
|
|
|
ConsoleDataType.InProgress,
|
|
|
|
`Downloading ${name} to My Notebooks`
|
|
|
|
);
|
2020-06-30 11:47:21 -07:00
|
|
|
|
2020-07-15 13:58:43 -07:00
|
|
|
try {
|
|
|
|
const response = await junoClient.getNotebookContent(data.id);
|
|
|
|
if (!response.data) {
|
|
|
|
throw new Error(`Received HTTP ${response.status} when fetching ${data.name}`);
|
|
|
|
}
|
2020-06-30 11:47:21 -07:00
|
|
|
|
2020-07-15 13:58:43 -07:00
|
|
|
await container.importAndOpenContent(data.name, response.data);
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(
|
|
|
|
ConsoleDataType.Info,
|
|
|
|
`Successfully downloaded ${name} to My Notebooks`
|
|
|
|
);
|
2020-06-30 11:47:21 -07:00
|
|
|
|
2020-07-15 13:58:43 -07:00
|
|
|
const increaseDownloadResponse = await junoClient.increaseNotebookDownloadCount(data.id);
|
|
|
|
if (increaseDownloadResponse.data) {
|
|
|
|
onComplete(increaseDownloadResponse.data);
|
2020-06-30 11:47:21 -07:00
|
|
|
}
|
2020-07-15 13:58:43 -07:00
|
|
|
} catch (error) {
|
|
|
|
const message = `Failed to download ${data.name}: ${error}`;
|
|
|
|
Logger.logError(message, "GalleryUtils/downloadItem");
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(ConsoleDataType.Error, message);
|
2020-06-30 11:47:21 -07:00
|
|
|
}
|
2020-07-15 13:58:43 -07:00
|
|
|
|
|
|
|
NotificationConsoleUtils.clearInProgressMessageWithId(notificationId);
|
|
|
|
},
|
|
|
|
"Cancel",
|
|
|
|
undefined
|
|
|
|
);
|
2020-06-30 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function favoriteItem(
|
|
|
|
container: ViewModels.Explorer,
|
|
|
|
junoClient: JunoClient,
|
|
|
|
data: IGalleryItem,
|
|
|
|
onComplete: (item: IGalleryItem) => void
|
|
|
|
): Promise<void> {
|
|
|
|
if (container) {
|
|
|
|
try {
|
|
|
|
const response = await junoClient.favoriteNotebook(data.id);
|
|
|
|
if (!response.data) {
|
|
|
|
throw new Error(`Received HTTP ${response.status} when favoriting ${data.name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
onComplete(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
const message = `Failed to favorite ${data.name}: ${error}`;
|
|
|
|
Logger.logError(message, "GalleryUtils/favoriteItem");
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(ConsoleDataType.Error, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function unfavoriteItem(
|
|
|
|
container: ViewModels.Explorer,
|
|
|
|
junoClient: JunoClient,
|
|
|
|
data: IGalleryItem,
|
|
|
|
onComplete: (item: IGalleryItem) => void
|
|
|
|
): Promise<void> {
|
|
|
|
if (container) {
|
|
|
|
try {
|
|
|
|
const response = await junoClient.unfavoriteNotebook(data.id);
|
|
|
|
if (!response.data) {
|
|
|
|
throw new Error(`Received HTTP ${response.status} when unfavoriting ${data.name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
onComplete(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
const message = `Failed to unfavorite ${data.name}: ${error}`;
|
|
|
|
Logger.logError(message, "GalleryUtils/unfavoriteItem");
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(ConsoleDataType.Error, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteItem(
|
|
|
|
container: ViewModels.Explorer,
|
|
|
|
junoClient: JunoClient,
|
|
|
|
data: IGalleryItem,
|
|
|
|
onComplete: (item: IGalleryItem) => void
|
|
|
|
): void {
|
|
|
|
if (container) {
|
|
|
|
container.showOkCancelModalDialog(
|
|
|
|
"Remove published notebook",
|
|
|
|
`Would you like to remove ${data.name} from the gallery?`,
|
|
|
|
"Remove",
|
|
|
|
async () => {
|
|
|
|
const name = data.name;
|
|
|
|
const notificationId = NotificationConsoleUtils.logConsoleMessage(
|
|
|
|
ConsoleDataType.InProgress,
|
|
|
|
`Removing ${name} from gallery`
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await junoClient.deleteNotebook(data.id);
|
|
|
|
if (!response.data) {
|
|
|
|
throw new Error(`Received HTTP ${response.status} while removing ${name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(ConsoleDataType.Info, `Successfully removed ${name} from gallery`);
|
|
|
|
onComplete(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
const message = `Failed to remove ${name} from gallery: ${error}`;
|
|
|
|
Logger.logError(message, "GalleryUtils/deleteItem");
|
|
|
|
NotificationConsoleUtils.logConsoleMessage(ConsoleDataType.Error, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationConsoleUtils.clearInProgressMessageWithId(notificationId);
|
|
|
|
},
|
|
|
|
"Cancel",
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 12:10:26 -07:00
|
|
|
export function getGalleryViewerProps(search: string): GalleryViewerProps {
|
|
|
|
const params = new URLSearchParams(search);
|
2020-06-30 11:47:21 -07:00
|
|
|
let selectedTab: GalleryTab;
|
|
|
|
if (params.has(GalleryViewerParams.SelectedTab)) {
|
|
|
|
selectedTab = GalleryTab[params.get(GalleryViewerParams.SelectedTab) as keyof typeof GalleryTab];
|
|
|
|
}
|
|
|
|
|
|
|
|
let sortBy: SortBy;
|
|
|
|
if (params.has(GalleryViewerParams.SortBy)) {
|
|
|
|
sortBy = SortBy[params.get(GalleryViewerParams.SortBy) as keyof typeof SortBy];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectedTab,
|
|
|
|
sortBy,
|
|
|
|
searchText: params.get(GalleryViewerParams.SearchText)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-06 12:10:26 -07:00
|
|
|
export function getNotebookViewerProps(search: string): NotebookViewerProps {
|
|
|
|
const params = new URLSearchParams(search);
|
2020-06-30 11:47:21 -07:00
|
|
|
return {
|
|
|
|
notebookUrl: params.get(NotebookViewerParams.NotebookUrl),
|
2020-07-10 14:23:53 -07:00
|
|
|
galleryItemId: params.get(NotebookViewerParams.GalleryItemId),
|
|
|
|
hideInputs: JSON.parse(params.get(NotebookViewerParams.HideInputs))
|
2020-06-30 11:47:21 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTabTitle(tab: GalleryTab): string {
|
|
|
|
switch (tab) {
|
|
|
|
case GalleryTab.OfficialSamples:
|
|
|
|
return GalleryViewerComponent.OfficialSamplesTitle;
|
|
|
|
case GalleryTab.PublicGallery:
|
|
|
|
return GalleryViewerComponent.PublicGalleryTitle;
|
|
|
|
case GalleryTab.Favorites:
|
|
|
|
return GalleryViewerComponent.FavoritesTitle;
|
|
|
|
case GalleryTab.Published:
|
|
|
|
return GalleryViewerComponent.PublishedTitle;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown tab ${tab}`);
|
|
|
|
}
|
|
|
|
}
|