|
| 1 | +import { action } from '@ember/object'; |
| 2 | +import { inject as service } from '@ember/service'; |
| 3 | +import Component from '@glimmer/component'; |
| 4 | +import { tracked } from '@glimmer/tracking'; |
| 5 | + |
| 6 | +import window from 'ember-window-mock'; |
| 7 | + |
| 8 | +const REASONS = [ |
| 9 | + { |
| 10 | + reason: 'spam', |
| 11 | + description: 'it contains spam', |
| 12 | + }, |
| 13 | + { |
| 14 | + reason: 'name-squatting', |
| 15 | + description: 'it is name-squatting (reserving a crate name without content)', |
| 16 | + }, |
| 17 | + { |
| 18 | + reason: 'abuse', |
| 19 | + description: 'it is abusive or otherwise harmful', |
| 20 | + }, |
| 21 | + { |
| 22 | + reason: 'security', |
| 23 | + description: 'it contains a vulnerability (please try to contact the crate author first)', |
| 24 | + }, |
| 25 | + { |
| 26 | + reason: 'other', |
| 27 | + description: 'it is violating the usage policy in some other way (please specify below)', |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +export default class CrateReportForm extends Component { |
| 32 | + @service store; |
| 33 | + |
| 34 | + @tracked crate = ''; |
| 35 | + @tracked selectedReasons = []; |
| 36 | + @tracked detail = ''; |
| 37 | + @tracked crateInvalid = false; |
| 38 | + @tracked reasonsInvalid = false; |
| 39 | + @tracked detailInvalid = false; |
| 40 | + |
| 41 | + reasons = REASONS; |
| 42 | + |
| 43 | + constructor() { |
| 44 | + super(...arguments); |
| 45 | + this.crate = this.args.crate; |
| 46 | + } |
| 47 | + |
| 48 | + validate() { |
| 49 | + this.crateInvalid = !this.crate || !this.crate.trim(); |
| 50 | + this.reasonsInvalid = this.selectedReasons.length === 0; |
| 51 | + this.detailInvalid = this.selectedReasons.includes('other') && !this.detail?.trim(); |
| 52 | + return !this.crateInvalid && !this.reasonsInvalid && !this.detailInvalid; |
| 53 | + } |
| 54 | + |
| 55 | + @action resetCrateValidation() { |
| 56 | + this.crateInvalid = false; |
| 57 | + } |
| 58 | + |
| 59 | + @action resetDetailValidation() { |
| 60 | + this.detailInvalid = false; |
| 61 | + } |
| 62 | + |
| 63 | + @action isReasonSelected(reason) { |
| 64 | + return this.selectedReasons.includes(reason); |
| 65 | + } |
| 66 | + |
| 67 | + @action toggleReason(reason) { |
| 68 | + this.selectedReasons = this.selectedReasons.includes(reason) |
| 69 | + ? this.selectedReasons.filter(it => it !== reason) |
| 70 | + : [...this.selectedReasons, reason]; |
| 71 | + this.reasonsInvalid = false; |
| 72 | + } |
| 73 | + |
| 74 | + @action |
| 75 | + submit() { |
| 76 | + if (!this.validate()) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + let mailto = this.composeMail(); |
| 81 | + window.open(mailto, '_self'); |
| 82 | + } |
| 83 | + |
| 84 | + composeMail() { |
| 85 | + let crate = this.crate; |
| 86 | + let reasons = this.reasons |
| 87 | + .map(({ reason, description }) => { |
| 88 | + let selected = this.isReasonSelected(reason); |
| 89 | + return `${selected ? '- [x]' : '- [ ]'} ${description}`; |
| 90 | + }) |
| 91 | + .join('\n'); |
| 92 | + let body = `I'm reporting the https://crates.io/crates/${crate} crate because: |
| 93 | +
|
| 94 | +${reasons} |
| 95 | +
|
| 96 | +Additional details: |
| 97 | +
|
| 98 | +${this.detail} |
| 99 | +`; |
| 100 | + let subject = `The "${crate}" crate`; |
| 101 | + let address = '[email protected]'; |
| 102 | + let mailto = `mailto:${address}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`; |
| 103 | + return mailto; |
| 104 | + } |
| 105 | +} |
0 commit comments