Skip to content

Commit

Permalink
Merge pull request #1483 from OskarDamkjaer/export_folder
Browse files Browse the repository at this point in the history
Add exports to individual folder
  • Loading branch information
OskarDamkjaer authored Jul 30, 2021
2 parents cb202d2 + 5829287 commit b0304b6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/browser/components/SavedScripts/SavedScripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Favorite } from 'shared/modules/favorites/favoritesDuck'
import { useCustomBlur } from './hooks'
import { AddIcon } from 'browser-components/icons/Icons'
import uuid from 'uuid'
import { ExportFormat } from 'services/exporting/favoriteUtils'

interface SavedScriptsProps {
title?: string
Expand All @@ -47,7 +48,11 @@ interface SavedScriptsProps {
selectScript: (script: Favorite) => void
execScript: (script: Favorite) => void
// When optional callbacks aren't provided, respective UI elements are hidden
exportScripts?: (scripts: Favorite[], folders: Folder[]) => void
exportScripts?: (
scripts: Favorite[],
folders: Folder[],
format?: ExportFormat
) => void
renameScript?: (script: Favorite, name: string) => void
moveScript?: (scriptId: string, folderId?: string) => void
addScript?: (content: string) => void
Expand Down Expand Up @@ -245,6 +250,10 @@ export default function SavedScripts({
folder={folder}
renameFolder={renameFolder}
removeFolder={removeFolder}
exportScripts={
exportScripts &&
((format: ExportFormat) => exportScripts(scripts, [], format))
}
moveScript={moveScript}
key={folder.id}
selectedScriptIds={selectedIds}
Expand Down
21 changes: 21 additions & 0 deletions src/browser/components/SavedScripts/SavedScriptsFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ import {
ContextMenuItem
} from './styled'
import { Folder } from 'shared/modules/favorites/foldersDuck'
import { ExportFormat } from 'services/exporting/favoriteUtils'

interface SavedScriptsFolderProps {
folder: Folder
renameFolder?: (folderId: string, name: string) => void
removeFolder?: (folderId: string) => void
exportScripts?: (format: ExportFormat) => void
moveScript?: (scriptId: string, folderId: string) => void
forceEdit: boolean
onDoneEditing: () => void
Expand All @@ -59,6 +61,7 @@ function SavedScriptsFolder({
moveScript,
renameFolder,
removeFolder,
exportScripts,
selectedScriptIds,
forceEdit,
onDoneEditing,
Expand Down Expand Up @@ -116,6 +119,24 @@ function SavedScriptsFolder({
>
Delete folder
</ContextMenuItem>
),
exportScripts && (
<ContextMenuItem
data-testid="contextMenuExportZip"
onClick={() => exportScripts('ZIPFILE')}
key="exportZip"
>
Export scripts as .zip file
</ContextMenuItem>
),
exportScripts && (
<ContextMenuItem
data-testid="contextMenuExportCypherFile"
onClick={() => exportScripts('CYPHERFILE')}
key="exportAsCypher"
>
Export scripts as .cypher file
</ContextMenuItem>
)
].filter(defined => defined)

Expand Down
7 changes: 4 additions & 3 deletions src/browser/modules/Sidebar/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from 'shared/modules/commands/commandsDuck'
import * as favoritesDuck from 'shared/modules/favorites/favoritesDuck'
import * as foldersDuck from 'shared/modules/favorites/foldersDuck'
import { exportFavorites } from 'services/exporting/favoriteUtils'
import { exporters, ExportFormat } from 'services/exporting/favoriteUtils'

const mapFavoritesStateToProps = (state: any) => {
const folders = foldersDuck
Expand Down Expand Up @@ -83,9 +83,10 @@ const mapFavoritesDispatchToProps = (dispatch: any, ownProps: any) => ({
},
exportScripts(
favorites: favoritesDuck.Favorite[],
folders: foldersDuck.Folder[]
folders: foldersDuck.Folder[],
format: ExportFormat
) {
exportFavorites(favorites, folders)
exporters[format](favorites, folders)
},
addScript(content: string) {
dispatch(favoritesDuck.addFavorite(content))
Expand Down
10 changes: 9 additions & 1 deletion src/shared/services/exporting/favoriteUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import { getScriptDisplayName } from 'browser/components/SavedScripts'
import { Folder } from 'shared/modules/favorites/foldersDuck'

export const CYPHER_FILE_EXTENSION = '.cypher'
export type ExportFormat = 'CYPHERFILE' | 'ZIPFILE'
export const exporters: Record<
ExportFormat,
(favorites: Favorite[], folders: Folder[]) => void
> = {
ZIPFILE: exportFavoritesAsZip,
CYPHERFILE: exportFavoritesAsBigCypherFile
}

export function exportFavoritesAsBigCypherFile(favorites: Favorite[]): void {
const fileContent = favorites
Expand All @@ -42,7 +50,7 @@ type WriteableFavorite = {
content: string
fullFilename: string
}
export function exportFavorites(
export function exportFavoritesAsZip(
favorites: Favorite[],
folders: Folder[]
): void {
Expand Down

0 comments on commit b0304b6

Please sign in to comment.