Skip to content

Commit

Permalink
feat: migrate Polls plugin to new plugin infra (#3500)
Browse files Browse the repository at this point in the history
* feat: migrate Polls plugin to new plugin infra

* fix: add worker

* chore: migrate to new dialog entry

* feat: load worker correctly

* chore: change publisher

* chore: remove message register from old arch

* fixup! chore: remove message register from old arch

Co-authored-by: Jack Works <[email protected]>
  • Loading branch information
UncleBill and Jack-Works committed Jul 15, 2021
1 parent 9caa747 commit a8e0077
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/maskbook/src/plugin-infra/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../plugins/Snapshot'
import '../plugins/Collectible'
import '../plugins/External'
import '../plugins/Gitcoin'
import '../plugins/Polls'
import '../plugins/FileService'
// import '../plugins/NFT'
// import '../plugins/Airdrop'
6 changes: 0 additions & 6 deletions packages/maskbook/src/plugins/PluginSerivce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
*/

// Please make sure you have registered your plugin UI at ./PluginUI
import './Wallet/messages'
import './RedPacket/messages'
import './Gitcoin/messages'
import './Polls/utils'
import './Transak/messages'
import './Trader/messages'
import './Polls/utils'
import './ITO/messages'
import './Airdrop/messages'
import './dHEDGE/messages'
2 changes: 0 additions & 2 deletions packages/maskbook/src/plugins/PluginUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const plugins = new Set<PluginConfig>()
export const PluginUI: ReadonlySet<PluginConfig> = plugins

import { Flags } from '../utils/flags'
import { PollsPluginDefine } from './Polls/define'
import { StorybookPluginDefine } from './Storybook/define'
import { TraderPluginDefine } from './Trader/define'
import { TransakPluginDefine } from './Transak/define'
Expand All @@ -19,7 +18,6 @@ import { DHedgePluginDefine } from './dHEDGE/define'
sideEffect.then(() => {
if (Flags.ito_enabled) plugins.add(ITO_PluginDefine)
if (Flags.vcent_enabled) plugins.add(VCentPluginDefine)
if (Flags.poll_enabled) plugins.add(PollsPluginDefine)
if (Flags.trader_enabled) plugins.add(TraderPluginDefine)
if (Flags.transak_enabled) plugins.add(TransakPluginDefine)
if (Flags.dhedge_enabled) plugins.add(DHedgePluginDefine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useState, useEffect } from 'react'
import {
makeStyles,
DialogContent,
DialogProps,
Typography,
IconButton,
Button,
Expand All @@ -27,7 +26,7 @@ import { PollCardUI } from './Polls'
import type { PollMetaData } from '../types'
import { POLL_META_KEY_1 } from '../constants'
import { InjectedDialog } from '../../../components/shared/InjectedDialog'
import { PluginPollRPC } from '../utils'
import { PluginPollRPC } from '../messages'

const useNewPollStyles = makeStyles((theme) => ({
line: {
Expand Down Expand Up @@ -121,7 +120,7 @@ function NewPollUI(props: PollsDialogProps & NewPollProps) {
return usePortalShadowRoot((container) => (
<Select
variant="standard"
MenuProps={{ container: props.DialogProps?.container ?? container }}
MenuProps={{ container }}
value={defaultIndex}
onChange={(e) => fn(e.target.value as number)}>
{options.map((item, index) => (
Expand Down Expand Up @@ -246,13 +245,10 @@ const useStyles = makeStyles((theme) => {

interface PollsDialogProps extends withClasses<'wrapper'> {
open: boolean
onConfirm: (opt?: any) => void
onDecline: () => void
DialogProps?: Partial<DialogProps>
onClose: () => void
}

export default function PollsDialog(props: PollsDialogProps) {
const classes = useStylesExtends(useStyles(), props)
const state = useState(0)
const [, setTabState] = state
const loading = useState(false)
Expand All @@ -265,7 +261,7 @@ export default function PollsDialog(props: PollsDialogProps) {

const insertPoll = (data?: PollMetaData | null) => {
editActivatedPostMetadata((next) => (data ? next.set(POLL_META_KEY_1, data) : next.delete(POLL_META_KEY_1)))
props.onConfirm()
props.onClose()
}

const senderName = useCurrentIdentity()?.linkedPersona?.nickname
Expand Down Expand Up @@ -302,7 +298,7 @@ export default function PollsDialog(props: PollsDialogProps) {
}

return (
<InjectedDialog open={props.open} onClose={props.onDecline} title={t('plugin_poll_display_name')}>
<InjectedDialog open={props.open} onClose={props.onClose} title={t('plugin_poll_display_name')}>
<DialogContent>
<AbstractTab height={450} {...tabProps} />
</DialogContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from 'react'
import MaskbookPluginWrapper from '../../MaskbookPluginWrapper'
import type { TypedMessage } from '../../../protocols/typed-message'
import { renderWithPollMetadata, PollMetadataReader, PluginPollRPC } from '../utils'
import { PluginPollRPC } from '../messages'
import { renderWithPollMetadata, PollMetadataReader } from '../helpers'
import type { PollGunDB } from '../Services'
import { PollCardUI } from './Polls'
import { PollMetaData, PollStatus } from '../types'
Expand Down
23 changes: 23 additions & 0 deletions packages/maskbook/src/plugins/Polls/SNSAdaptor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '../base'
import { POLL_META_KEY_1 } from '../constants'
import type { PollMetaData } from '../types'
import { PollMetadataReader } from '../helpers'
import PollsDialog from './PollsDialog'
import PollsInPost from './PollsInPost'

const sns: Plugin.SNSAdaptor.Definition = {
...base,
init(signal) {},
DecryptedInspector: function Comp(props) {
const metadata = PollMetadataReader(props.message.meta)
if (!metadata.ok) return null
return <PollsInPost {...props} />
},
CompositionDialogMetadataBadgeRender: new Map([
[POLL_META_KEY_1, (meta: PollMetaData) => `a poll of '${meta.question}' from ${meta.sender}`],
]),
CompositionDialogEntry: { label: '🗳️ Poll', dialog: PollsDialog },
}

export default sns
9 changes: 9 additions & 0 deletions packages/maskbook/src/plugins/Polls/Worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '../base'
import '../messages'

const worker: Plugin.Worker.Definition = {
...base,
init(signal) {},
}
export default worker
15 changes: 15 additions & 0 deletions packages/maskbook/src/plugins/Polls/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Plugin } from '@masknet/plugin-infra'
import { PLUGIN_ID, PLUGIN_ICON, PLUGIN_NAME, PLUGIN_DESCRIPTION } from './constants'

export const base: Plugin.Shared.Definition = {
ID: PLUGIN_ID,
icon: PLUGIN_ICON,
name: { fallback: PLUGIN_NAME },
description: { fallback: PLUGIN_DESCRIPTION },
publisher: { name: { fallback: 'XiChi' }, link: 'https://github.com/xichi' },
enableRequirement: {
architecture: { app: false, web: true },
networks: { type: 'opt-out', networks: {} },
target: 'stable',
},
}
6 changes: 6 additions & 0 deletions packages/maskbook/src/plugins/Polls/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export const pluginName = 'Poll'
export const identifier = 'com.maskbook.poll'
export const POLL_META_KEY_1 = 'com.maskbook.poll:1'
export const PollGunServer = 'com.maskbook.plugin.polls'

export const PLUGIN_META_KEY = 'com.maskbook.poll:1'
export const PLUGIN_ID = 'com.maskbook.poll'
export const PLUGIN_NAME = 'Poll'
export const PLUGIN_ICON = '🗳️'
export const PLUGIN_DESCRIPTION = 'Easily create a Poll for reciving public votes.'
36 changes: 0 additions & 36 deletions packages/maskbook/src/plugins/Polls/define.tsx

This file was deleted.

8 changes: 8 additions & 0 deletions packages/maskbook/src/plugins/Polls/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createTypedMessageMetadataReader, createRenderWithMetadata } from '../../protocols/typed-message/metadata'
import type { PollMetaData } from './types'
import { POLL_META_KEY_1 } from './constants'
import schema from './schema.json'

export const PollMetadataReader = createTypedMessageMetadataReader<PollMetaData>(POLL_META_KEY_1, schema)
export const renderWithPollMetadata = createRenderWithMetadata(PollMetadataReader)
if (import.meta.webpackHot) import.meta.webpackHot.accept()
15 changes: 15 additions & 0 deletions packages/maskbook/src/plugins/Polls/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { registerPlugin } from '@masknet/plugin-infra'
import { base } from './base'

registerPlugin({
...base,
SNSAdaptor: {
load: () => import('./SNSAdaptor'),
hotModuleReload: (hot) =>
import.meta.webpackHot?.accept('./SNSAdaptor/index', () => hot(import('./SNSAdaptor'))),
},
Worker: {
load: () => import('./Worker'),
hotModuleReload: (hot) => import.meta.webpackHot?.accept('./Worker/index', () => hot(import('./Worker'))),
},
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createTypedMessageMetadataReader, createRenderWithMetadata } from '../../protocols/typed-message/metadata'
import type { PollMetaData } from './types'
import { identifier, POLL_META_KEY_1 } from './constants'
import schema from './schema.json'
import { identifier } from './constants'
import { createPluginMessage } from '../utils/createPluginMessage'
import { createPluginRPC } from '../utils/createPluginRPC'
import { OnDemandWorker } from '../../web-workers/OnDemandWorker'
import { AsyncCall } from 'async-call-rpc'
import { WorkerChannel } from 'async-call-rpc/utils/web/worker'

export const PollMetadataReader = createTypedMessageMetadataReader<PollMetaData>(POLL_META_KEY_1, schema)
export const renderWithPollMetadata = createRenderWithMetadata(PollMetadataReader)
if (import.meta.webpackHot) import.meta.webpackHot.accept()
const PollMessage = createPluginMessage<{ _: unknown }>(identifier)
export const PluginPollRPC = createPluginRPC(
identifier,
Expand Down

0 comments on commit a8e0077

Please sign in to comment.