-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for showing and accessing custom helpers
- Loading branch information
Showing
8 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<svelte:options accessors={true} immutable={true} /> | ||
|
||
<script lang="ts"> | ||
import SettingsIcon from '@nasa-jpl/stellar/icons/settings.svg?component'; | ||
import { createEventDispatcher } from 'svelte'; | ||
import type { User } from '../../types/app'; | ||
import type { Extension } from '../../types/extension'; | ||
import { permissionHandler } from '../../utilities/permissionHandler'; | ||
import PlanNavButton from '../plan/PlanNavButton.svelte'; | ||
import MenuItem from './MenuItem.svelte'; | ||
export let extensions: Extension[]; | ||
export let title: string; | ||
export let user: User | null; | ||
const DESCRIPTION_MAX_LENGTH = 50; | ||
const dispatch = createEventDispatcher(); | ||
function callExtension(extension: Extension) { | ||
dispatch('callExtension', extension); | ||
} | ||
function formatDescription(description: string): string { | ||
// Truncate the description at DESCRIPTION_MAX_LENGTH and add ellipsis. | ||
if (description !== null && description.length > DESCRIPTION_MAX_LENGTH) { | ||
return `${description.substring(0, Math.min(description.length, DESCRIPTION_MAX_LENGTH))}...`; | ||
} else if (description?.length < DESCRIPTION_MAX_LENGTH) { | ||
return description; | ||
} | ||
return ''; | ||
} | ||
function hasExtensionPermission(extension: Extension): boolean { | ||
for (const extensionRole of extension.extension_roles) { | ||
if (user?.activeRole === extensionRole.role) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
</script> | ||
|
||
{#if extensions.length > 0} | ||
<div class="extension-menu st-typography-medium"> | ||
<PlanNavButton {title} menuTitle="Extensions"> | ||
<SettingsIcon /> | ||
<div class="st-typography-medium" slot="menu"> | ||
{#each extensions as extension} | ||
<MenuItem | ||
on:click={() => callExtension(extension)} | ||
use={[ | ||
[ | ||
permissionHandler, | ||
{ | ||
hasPermission: hasExtensionPermission(extension), | ||
permissionError: 'You do not have permission to call this extension', | ||
}, | ||
], | ||
]} | ||
> | ||
<div class="extension-menu--menu-item"> | ||
{extension.label} | ||
<span class="st-typography-label">{formatDescription(extension.description)}</span> | ||
</div> | ||
</MenuItem> | ||
{/each} | ||
</div> | ||
</PlanNavButton> | ||
</div> | ||
{/if} | ||
|
||
<style> | ||
.extension-menu { | ||
--aerie-menu-item-template-columns: auto; | ||
align-items: center; | ||
cursor: pointer; | ||
display: grid; | ||
height: inherit; | ||
justify-content: center; | ||
position: relative; | ||
} | ||
.extension-menu--menu-item { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { json, type RequestHandler } from '@sveltejs/kit'; | ||
import type { ExtensionResponse } from '../../types/extension'; | ||
import { reqExtension } from '../../utilities/requests'; | ||
|
||
/** | ||
* Used to proxy requests from the UI to an external extension. This avoids any CORS errors we might | ||
* encounter by calling a tool that may or may not be external to Aerie. | ||
*/ | ||
export const POST: RequestHandler = async event => { | ||
const { url, ...body } = await event.request.json(); | ||
const response = await reqExtension(url, body, event.locals.user); | ||
|
||
if (isExtensionResponse(response)) { | ||
return json(response); | ||
} | ||
|
||
return json({ message: response, success: false }); | ||
}; | ||
|
||
function isExtensionResponse(result: any): result is ExtensionResponse { | ||
return 'message' in result && 'success' in result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type Extension = { | ||
description: string; | ||
extension_roles: ExtensionRole[]; | ||
id: number; | ||
label: string; | ||
updated_at: string; | ||
url: string; | ||
}; | ||
|
||
export type ExtensionPayload = { | ||
gateway?: string; | ||
hasura?: string; | ||
planId: number; | ||
selectedActivityDirectiveId: number | null; | ||
simulationDatasetId: number | null; | ||
}; | ||
|
||
export type ExtensionResponse = { | ||
message: string; | ||
success: boolean; | ||
url: string; | ||
}; | ||
|
||
export type ExtensionRole = { | ||
extension_id: number; | ||
id: number; | ||
role: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters