Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: component manager usecases #2354

Merged
merged 10 commits into from Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/features/src/Domain/Feature/Features.ts
Expand Up @@ -9,6 +9,7 @@ import { experimentalFeatures } from '../Lists/ExperimentalFeatures'
import { IframeEditors } from '../Lists/IframeEditors'
import { themes } from '../Lists/Themes'
import { nativeEditors } from '../Lists/NativeEditors'
import { IframeComponentFeatureDescription } from './IframeComponentFeatureDescription'

export function GetFeatures(): AnyFeatureDescription[] {
return [
Expand All @@ -30,10 +31,14 @@ export function FindNativeTheme(identifier: FeatureIdentifier): ThemeFeatureDesc
return themes().find((t) => t.identifier === identifier)
}

export function GetIframeAndNativeEditors(): EditorFeatureDescription[] {
export function GetIframeAndNativeEditors(): (IframeComponentFeatureDescription | EditorFeatureDescription)[] {
return [...IframeEditors(), ...nativeEditors()]
}

export function GetIframeEditors(): IframeComponentFeatureDescription[] {
return IframeEditors()
}

export function GetSuperNoteFeature(): EditorFeatureDescription {
return FindNativeFeature(FeatureIdentifier.SuperEditor) as EditorFeatureDescription
}
Expand Down
63 changes: 63 additions & 0 deletions packages/features/src/Domain/Feature/TypeGuards.spec.ts
@@ -0,0 +1,63 @@
import { ContentType } from '@standardnotes/domain-core'
import { AnyFeatureDescription } from './AnyFeatureDescription'
import { ComponentArea } from '../Component/ComponentArea'

import {
isThemeFeatureDescription,
isIframeComponentFeatureDescription,
isEditorFeatureDescription,
} from './TypeGuards'
import { ThemeFeatureDescription } from './ThemeFeatureDescription'
import { IframeComponentFeatureDescription } from './IframeComponentFeatureDescription'

describe('TypeGuards', () => {
describe('isThemeFeatureDescription', () => {
it('should return true if feature is ThemeFeatureDescription', () => {
const feature = {
content_type: ContentType.TYPES.Theme,
} as jest.Mocked<ThemeFeatureDescription>
expect(isThemeFeatureDescription(feature)).toBe(true)
})

it('should return false if feature is not ThemeFeatureDescription', () => {
const feature = {
content_type: ContentType.TYPES.Component,
} as jest.Mocked<ThemeFeatureDescription>
expect(isThemeFeatureDescription(feature)).toBe(false)
})
})

describe('isIframeComponentFeatureDescription', () => {
it('should return true if feature is IframeComponentFeatureDescription', () => {
const feature = {
content_type: ContentType.TYPES.Component,
area: ComponentArea.Editor,
} as jest.Mocked<IframeComponentFeatureDescription>
expect(isIframeComponentFeatureDescription(feature)).toBe(true)
})

it('should return false if feature is not IframeComponentFeatureDescription', () => {
const feature = {
content_type: ContentType.TYPES.Theme,
} as jest.Mocked<IframeComponentFeatureDescription>
expect(isIframeComponentFeatureDescription(feature)).toBe(false)
})
})

describe('isEditorFeatureDescription', () => {
it('should return true if feature is EditorFeatureDescription', () => {
const feature = {
note_type: 'test',
area: ComponentArea.Editor,
} as unknown as jest.Mocked<AnyFeatureDescription>
expect(isEditorFeatureDescription(feature)).toBe(true)
})

it('should return false if feature is not EditorFeatureDescription', () => {
const feature = {
content_type: ContentType.TYPES.Theme,
} as jest.Mocked<AnyFeatureDescription>
expect(isEditorFeatureDescription(feature)).toBe(false)
})
})
})
6 changes: 3 additions & 3 deletions packages/mobile/src/Lib/MobileDevice.ts
Expand Up @@ -512,15 +512,15 @@ export class MobileDevice implements MobileDeviceInterface {
)
}

addComponentUrl(componentUuid: UuidString, componentUrl: string) {
registerComponentUrl(componentUuid: UuidString, componentUrl: string) {
this.componentUrls.set(componentUuid, componentUrl)
}

removeComponentUrl(componentUuid: UuidString) {
deregisterComponentUrl(componentUuid: UuidString) {
this.componentUrls.delete(componentUuid)
}

isUrlComponentUrl(url: string): boolean {
isUrlRegisteredComponentUrl(url: string): boolean {
return Array.from(this.componentUrls.values()).includes(url)
}

Expand Down
6 changes: 5 additions & 1 deletion packages/mobile/src/MobileWebAppContainer.tsx
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { ApplicationEvent, ReactNativeToWebEvent } from '@standardnotes/snjs'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Button, Keyboard, Platform, Text, View } from 'react-native'
Expand Down Expand Up @@ -239,6 +241,7 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo
void onFunctionMessage(functionData.functionName, functionData.messageId, functionData.args)
} catch (error) {
if (LoggingEnabled) {
// eslint-disable-next-line no-console
console.log('onGeneralMessage', JSON.stringify(message))
}
}
Expand All @@ -247,6 +250,7 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo
const onFunctionMessage = async (functionName: string, messageId: string, args: any) => {
const returnValue = await (device as any)[functionName](...args)
if (LoggingEnabled && functionName !== 'consoleLog') {
// eslint-disable-next-line no-console
console.log(`Native device function ${functionName} called`)
}
webViewRef.current?.postMessage(JSON.stringify({ messageId, returnValue, messageType: 'reply' }))
Expand All @@ -270,7 +274,7 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo
(Platform.OS === 'ios' && request.navigationType === 'click') ||
(Platform.OS === 'android' && request.url !== sourceUri)

const isComponentUrl = device.isUrlComponentUrl(request.url)
const isComponentUrl = device.isUrlRegisteredComponentUrl(request.url)

if (shouldStopRequest && !isComponentUrl) {
device.openUrl(request.url)
Expand Down
77 changes: 77 additions & 0 deletions packages/models/src/Domain/Runtime/Feature/TypeGuards.spec.ts
@@ -0,0 +1,77 @@
import {
AnyFeatureDescription,
ComponentArea,
EditorFeatureDescription,
IframeComponentFeatureDescription,
NoteType,
UIFeatureDescriptionTypes,
} from '@standardnotes/features'
import {
isUIFeatureAnIframeFeature,
isComponentOrFeatureDescriptionAComponent,
isComponentOrFeatureDescriptionAFeatureDescription,
} from './TypeGuards'
import { UIFeature } from './UIFeature'
import { ComponentInterface } from '../../Syncable/Component'
import { ContentType } from '@standardnotes/domain-core'

describe('TypeGuards', () => {
describe('isUIFeatureAnIframeFeature', () => {
it('should return true if feature is IframeUIFeature', () => {
const x: UIFeature<IframeComponentFeatureDescription> = {
featureDescription: {
content_type: ContentType.TYPES.Component,
area: ComponentArea.Editor,
},
} as jest.Mocked<UIFeature<IframeComponentFeatureDescription>>

expect(isUIFeatureAnIframeFeature(x)).toBe(true)
})

it('should return false if feature is not IframeUIFeature', () => {
const x: UIFeature<EditorFeatureDescription> = {
featureDescription: {
note_type: NoteType.Super,
},
} as jest.Mocked<UIFeature<EditorFeatureDescription>>

expect(isUIFeatureAnIframeFeature(x)).toBe(false)
})
})

describe('isFeatureAComponent', () => {
it('should return true if feature is a Component', () => {
const x: ComponentInterface | UIFeatureDescriptionTypes = {
uuid: 'abc-123',
} as ComponentInterface

expect(isComponentOrFeatureDescriptionAComponent(x)).toBe(true)
})

it('should return false if feature description is not a component', () => {
const x: EditorFeatureDescription = {
note_type: NoteType.Super,
} as jest.Mocked<EditorFeatureDescription>

expect(isComponentOrFeatureDescriptionAComponent(x)).toBe(false)
})
})

describe('isComponentOrFeatureDescriptionAFeatureDescription', () => {
it('should return true if x is a feature description', () => {
const x: AnyFeatureDescription = {
content_type: 'TestContentType',
} as AnyFeatureDescription

expect(isComponentOrFeatureDescriptionAFeatureDescription(x)).toBe(true)
})

it('should return false if x is a component', () => {
const x: ComponentInterface = {
uuid: 'abc-123',
} as ComponentInterface

expect(isComponentOrFeatureDescriptionAFeatureDescription(x)).toBe(false)
})
})
})
27 changes: 27 additions & 0 deletions packages/models/src/Domain/Runtime/Feature/TypeGuards.ts
@@ -0,0 +1,27 @@
import {
AnyFeatureDescription,
EditorFeatureDescription,
IframeComponentFeatureDescription,
UIFeatureDescriptionTypes,
isIframeComponentFeatureDescription,
} from '@standardnotes/features'
import { UIFeatureInterface } from './UIFeatureInterface'
import { ComponentInterface } from '../../Syncable/Component'

export function isUIFeatureAnIframeFeature(
x: UIFeatureInterface<EditorFeatureDescription | IframeComponentFeatureDescription>,
): x is UIFeatureInterface<IframeComponentFeatureDescription> {
return isIframeComponentFeatureDescription(x.featureDescription)
}

export function isComponentOrFeatureDescriptionAComponent(
x: ComponentInterface | UIFeatureDescriptionTypes,
): x is ComponentInterface {
return 'uuid' in x
}

export function isComponentOrFeatureDescriptionAFeatureDescription(
x: ComponentInterface | AnyFeatureDescription,
): x is AnyFeatureDescription {
return !('uuid' in x)
}