|
| 1 | +import { fn } from '@ember/helper'; |
| 2 | +import { on } from '@ember/modifier'; |
| 3 | +import { service } from '@ember/service'; |
| 4 | + |
| 5 | +import Component from '@glimmer/component'; |
| 6 | +import { tracked } from '@glimmer/tracking'; |
| 7 | + |
| 8 | +import TriangleAlert from '@cardstack/boxel-icons/triangle-alert'; |
| 9 | + |
| 10 | +import { dropTask } from 'ember-concurrency'; |
| 11 | +import perform from 'ember-concurrency/helpers/perform'; |
| 12 | + |
| 13 | +import { Accordion, Button } from '@cardstack/boxel-ui/components'; |
| 14 | + |
| 15 | +import SwitchSubmodeCommand from '../../commands/switch-submode'; |
| 16 | +import { type CardError } from '../../resources/card-resource'; |
| 17 | + |
| 18 | +import type CommandService from '../../services/command-service'; |
| 19 | + |
| 20 | +interface Signature { |
| 21 | + Args: { |
| 22 | + error: CardError['errors'][0]; |
| 23 | + title?: string; |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +export default class CardErrorDetail extends Component<Signature> { |
| 28 | + @tracked private showErrorDetail = false; |
| 29 | + @service private declare commandService: CommandService; |
| 30 | + |
| 31 | + private toggleDetail = () => (this.showErrorDetail = !this.showErrorDetail); |
| 32 | + |
| 33 | + private viewInCodeMode = dropTask(async () => { |
| 34 | + let switchSubmodeCommand = new SwitchSubmodeCommand( |
| 35 | + this.commandService.commandContext, |
| 36 | + ); |
| 37 | + const InputType = await switchSubmodeCommand.getInputType(); |
| 38 | + let input = new InputType({ |
| 39 | + submode: 'code', |
| 40 | + codePath: `${this.args.error.id}.json`, |
| 41 | + }); |
| 42 | + await switchSubmodeCommand.execute(input); |
| 43 | + }); |
| 44 | + |
| 45 | + <template> |
| 46 | + <Accordion as |A|> |
| 47 | + <A.Item |
| 48 | + data-test-error-detail-toggle |
| 49 | + @onClick={{fn this.toggleDetail 'schema'}} |
| 50 | + @isOpen={{this.showErrorDetail}} |
| 51 | + > |
| 52 | + <:title> |
| 53 | + <TriangleAlert /> |
| 54 | + An error was encountered on this card: |
| 55 | + <span class='error-detail' data-test-error-title>{{@title}}</span> |
| 56 | + </:title> |
| 57 | + <:content> |
| 58 | + <div class='actions'> |
| 59 | + <Button |
| 60 | + data-test-view-in-code-mode-button |
| 61 | + @kind='primary' |
| 62 | + {{on 'click' (perform this.viewInCodeMode)}} |
| 63 | + >View in Code Mode</Button> |
| 64 | + </div> |
| 65 | + <div class='detail'> |
| 66 | + <div class='detail-item'> |
| 67 | + <div class='detail-title'>Details:</div> |
| 68 | + <div |
| 69 | + class='detail-contents' |
| 70 | + data-test-error-detail |
| 71 | + >{{@error.message}}</div> |
| 72 | + </div> |
| 73 | + {{#if @error.meta.stack}} |
| 74 | + <div class='detail-item'> |
| 75 | + <div class='detail-title'>Stack trace:</div> |
| 76 | + <pre |
| 77 | + data-test-error-stack |
| 78 | + > |
| 79 | +{{@error.meta.stack}} |
| 80 | + </pre> |
| 81 | + </div> |
| 82 | + {{/if}} |
| 83 | + </div> |
| 84 | + </:content> |
| 85 | + </A.Item> |
| 86 | + </Accordion> |
| 87 | + |
| 88 | + <style scoped> |
| 89 | + .actions { |
| 90 | + display: flex; |
| 91 | + justify-content: center; |
| 92 | + margin-top: var(--boxel-sp-lg); |
| 93 | + } |
| 94 | + .detail { |
| 95 | + padding: var(--boxel-sp); |
| 96 | + } |
| 97 | + .detail-item { |
| 98 | + margin-top: var(--boxel-sp); |
| 99 | + } |
| 100 | + .detail-title { |
| 101 | + font: 600 var(--boxel-font); |
| 102 | + } |
| 103 | + .detail-contents { |
| 104 | + font: var(--boxel-font); |
| 105 | + } |
| 106 | + pre { |
| 107 | + margin-top: 0; |
| 108 | + white-space: pre-wrap; |
| 109 | + word-break: break-all; |
| 110 | + } |
| 111 | + </style> |
| 112 | + </template> |
| 113 | +} |
0 commit comments