Skip to content

Commit

Permalink
refactor: simpler enum and exhaustiveness check
Browse files Browse the repository at this point in the history
  • Loading branch information
gavvvr committed Oct 8, 2023
1 parent 85b6d21 commit 216ed6b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': ['warn', { varsIgnorePattern: '_.*' }],
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/ImgurPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Editor, MarkdownView, Notice, Plugin } from 'obsidian'
import ImageUploader from './uploader/ImageUploader'
import ImgurPluginSettingsTab from './ui/ImgurPluginSettingsTab'
import ApiError from './uploader/ApiError'
import UploadStrategy from './UploadStrategy'
import buildUploaderFrom from './uploader/imgUploaderFactory'
import RemoteUploadConfirmationDialog from './ui/RemoteUploadConfirmationDialog'
import PasteEventCopy from './aux-event-classes/PasteEventCopy'
import DragEventCopy from './aux-event-classes/DragEventCopy'
import editorCheckCallbackFor from './imgur/resizing/plugin-callback'
import ImgurSize from './imgur/resizing/ImgurSize'
import { UploadStrategy } from './UploadStrategy'

declare module 'obsidian' {
interface MarkdownSubView {
Expand All @@ -22,13 +22,13 @@ interface ClipboardManager {
}

export interface ImgurPluginSettings {
uploadStrategy: string
uploadStrategy: UploadStrategy
clientId: string
showRemoteUploadConfirmation: boolean
}

const DEFAULT_SETTINGS: ImgurPluginSettings = {
uploadStrategy: UploadStrategy.ANONYMOUS_IMGUR.id,
uploadStrategy: 'ANONYMOUS_IMGUR',
clientId: null,
showRemoteUploadConfirmation: true,
}
Expand Down
20 changes: 5 additions & 15 deletions src/UploadStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
export default class UploadStrategy {
private static readonly valuesArr: UploadStrategy[] = []
export type UploadStrategy = 'ANONYMOUS_IMGUR' | 'AUTHENTICATED_IMGUR'

static get values(): ReadonlyArray<UploadStrategy> {
return this.valuesArr
}
export type StrategyAndDescription = Record<UploadStrategy, string>

static readonly ANONYMOUS_IMGUR = new UploadStrategy('ANONYMOUS_IMGUR', 'Anonymous Imgur upload')

static readonly AUTHENTICATED_IMGUR = new UploadStrategy(
'AUTHENTICATED_IMGUR',
'Authenticated Imgur upload',
)

private constructor(readonly id: string, readonly description: string) {
UploadStrategy.valuesArr.push(this)
}
export const UploadStrategies: StrategyAndDescription = {
ANONYMOUS_IMGUR: 'Anonymous Imgur upload',
AUTHENTICATED_IMGUR: 'Authenticated Imgur upload',
}
27 changes: 13 additions & 14 deletions src/ui/ImgurPluginSettingsTab.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { App, Notice, PluginSettingTab, Setting } from 'obsidian'
import { IMGUR_ACCESS_TOKEN_LOCALSTORAGE_KEY } from 'src/imgur/constants'
import ImgurPlugin, { ImgurPluginSettings } from '../ImgurPlugin'
import UploadStrategy from '../UploadStrategy'
import ImgurAuthModal from './ImgurAuthModal'
import ImgurAuthenticationStatusItem from './ImgurAuthenticationStatus'
import { UploadStrategies, UploadStrategy } from 'src/UploadStrategy'

const REGISTER_CLIENT_URL = 'https://api.imgur.com/oauth2/addclient'

Expand Down Expand Up @@ -53,11 +53,12 @@ export default class ImgurPluginSettingsTab extends PluginSettingTab {
this.strategyDiv = containerEl.createDiv()

new Setting(uploadApproachDiv).setName('Images upload approach').addDropdown((dropdown) => {
UploadStrategy.values.forEach((s) => {
dropdown.addOption(s.id, s.description)
})
for (const key in UploadStrategies) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
dropdown.addOption(key, UploadStrategies[key])
}
dropdown.setValue(this.settings.uploadStrategy)
dropdown.onChange(async (v) => {
dropdown.onChange(async (v: UploadStrategy) => {
this.settings.uploadStrategy = v
this.plugin.setupImagesUploader()
await this.drawSettings(this.strategyDiv)
Expand All @@ -81,15 +82,13 @@ export default class ImgurPluginSettingsTab extends PluginSettingTab {

private async drawSettings(parentEl: HTMLElement) {
parentEl.empty()
switch (this.settings.uploadStrategy) {
case UploadStrategy.ANONYMOUS_IMGUR.id:
this.drawAnonymousClientIdSetting(parentEl)
break
case UploadStrategy.AUTHENTICATED_IMGUR.id:
await new ImgurAuthenticationStatusItem(parentEl, this).display()
break
default:
throw new Error('There must be a bug, this code is not expected to be reached')
if (this.settings.uploadStrategy === 'ANONYMOUS_IMGUR') {
this.drawAnonymousClientIdSetting(parentEl)
} else if (this.settings.uploadStrategy === 'AUTHENTICATED_IMGUR') {
await new ImgurAuthenticationStatusItem(parentEl, this).display()
} else {
const _: never = this.settings.uploadStrategy
return
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/uploader/imgUploaderFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IMGUR_ACCESS_TOKEN_LOCALSTORAGE_KEY, IMGUR_PLUGIN_CLIENT_ID } from 'src/imgur/constants'
import ImgurClient from 'src/imgur/ImgurClient'
import { ImgurPluginSettings } from 'src/ImgurPlugin'
import UploadStrategy from 'src/UploadStrategy'
import ImageUploader from './ImageUploader'
import ImgurAnonymousUploader from './imgur/ImgurAnonymousUploader'
import ImgurAuthenticatedUploader from './imgur/ImgurAuthenticatedUploader'
Expand All @@ -13,20 +12,22 @@ function defaultAnonymousUploader(): ImageUploader {
export default function buildUploaderFrom(
settings: ImgurPluginSettings,
): ImageUploader | undefined {
if (UploadStrategy.AUTHENTICATED_IMGUR.id === settings.uploadStrategy) {
if (settings.uploadStrategy === 'AUTHENTICATED_IMGUR') {
const accessToken = localStorage.getItem(IMGUR_ACCESS_TOKEN_LOCALSTORAGE_KEY)

if (!accessToken) {
return undefined
}

return new ImgurAuthenticatedUploader(new ImgurClient(accessToken))
}
if (settings.uploadStrategy === UploadStrategy.ANONYMOUS_IMGUR.id) {
} else if (settings.uploadStrategy === 'ANONYMOUS_IMGUR') {
if (settings.clientId) {
return new ImgurAnonymousUploader(settings.clientId)
}
return defaultAnonymousUploader()
} else {
const _: never = settings.uploadStrategy
return
}
throw Error('This line of code should never be reached')
}

0 comments on commit 216ed6b

Please sign in to comment.