Skip to content

Commit

Permalink
chore: revamp PostReplacement settings
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Sep 4, 2020
1 parent 78d5858 commit d6a7472
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
"settings_post_replace_all_posts": "All Posts",
"settings_post_replace_enhanced_posts": "Enhanced Posts",
"settings_post_replace_encrypted_posts": "Encrypted Posts",
"settings_post_replacement_scope": "Post Replacement Scope",
"settings_post_replacement_scope_desc": "Maskbook will render a new post to replace the original one to provide some enhancement.",
"settings_post_replacement": "Substitute Post",
"settings_post_replacement_desc": "All posts on the webpages will be rendered by Maskbook, including those not created by Maskbook.",
"skip": "Skip",
"share": "Share",
"share_to": "Share to…",
Expand Down
4 changes: 2 additions & 2 deletions src/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
"settings_post_replace_all_posts": "すべての投稿",
"settings_post_replace_enhanced_posts": "強化された投稿",
"settings_post_replace_encrypted_posts": "暗号化された投稿",
"settings_post_replacement_scope": "ポスト交換範囲",
"settings_post_replacement_scope_desc": "Maskbook は、新しい投稿をレンダリングして元の投稿を置き換え、いくつかの拡張機能を提供します",
"settings_post_replacement": "代替ポスト",
"settings_post_replacement_desc": "Web ページ上のすべての投稿は、Maskbook で作成されていない投稿を含め、Maskbook によってレンダリングされます",
"skip": "スキップ",
"share": "共有",
"share_to": "共有先",
Expand Down
4 changes: 2 additions & 2 deletions src/_locales/zh/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
"settings_post_replace_all_posts": "所有帖子",
"settings_post_replace_enhanced_posts": "僅增強的帖子",
"settings_post_replace_encrypted_posts": "僅加密的帖子",
"settings_post_replacement_scope": "帖子替換範圍",
"settings_post_replacement_scope_desc": "Maskbook 將呈現一個新帖子以替換原始帖子,以提供一些增強功能",
"settings_post_replacement": "替身帖子",
"settings_post_replacement_desc": "網頁上每個 Post 都由 Maskbook 渲染,包括並非由 Maskbook 創建的",
"skip": "跳過",
"share": "分享",
"share_to": "分享给…",
Expand Down
11 changes: 5 additions & 6 deletions src/components/InjectedComponents/PostReplacer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DefaultTypedMessageRenderer } from './TypedMessageRenderer'
import { PluginUI } from '../../plugins/plugin'
import { makeTypedMessageCompound, isTypedMessageSuspended, isTypedMessageKnown } from '../../protocols/typed-message'
import { useValueRef } from '../../utils/hooks/useValueRef'
import { currentPostReplacementScopeSettings, PostReplacementScope } from '../../settings/settings'
import { allPostReplacementSettings } from '../../settings/settings'
import { makeStyles, Theme } from '@material-ui/core'

const useStlyes = makeStyles((theme: Theme) => ({
Expand All @@ -23,7 +23,7 @@ export function PostReplacer(props: PostReplacerProps) {
const postContent = usePostInfoDetails('postContent')
const postMessage = usePostInfoDetails('postMessage')
const postPayload = usePostInfoDetails('postPayload')
const postRepalcementScope = useValueRef(currentPostReplacementScopeSettings)
const allPostReplacement = useValueRef(allPostReplacementSettings)

const plugins = [...PluginUI.values()]
const processedPostMessage = useMemo(
Expand All @@ -32,12 +32,11 @@ export function PostReplacer(props: PostReplacerProps) {
)
const shouldReplacePost =
// replace all posts
postRepalcementScope === PostReplacementScope.all ||
allPostReplacement ||
// replace posts which enhanced by plugins
(postRepalcementScope === PostReplacementScope.enhancedOnly &&
processedPostMessage.items.some((x) => !isTypedMessageKnown(x))) ||
(!allPostReplacement && processedPostMessage.items.some((x) => !isTypedMessageKnown(x))) ||
// replace posts which encrypted by maskbook
(postRepalcementScope === PostReplacementScope.encryptedOnly && postPayload.ok)
(!allPostReplacement && postPayload.ok)

// zip/unzip original post
useEffect(() => {
Expand Down
21 changes: 6 additions & 15 deletions src/extension/options-page/DashboardRouters/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
languageSettings,
Language,
renderInShadowRootSettings,
currentPostReplacementScopeSettings,
allPostReplacementSettings,
currentLocalWalletEthereumNetworkSettings,
appearanceSettings,
Appearance,
PostReplacementScope,
} from '../../../settings/settings'
import { useValueRef } from '../../../utils/hooks/useValueRef'

Expand Down Expand Up @@ -118,7 +117,6 @@ export default function DashboardSettingsRouter() {
const { t } = useI18N()
const currentLang = useValueRef(languageSettings)
const currentApperance = useValueRef(appearanceSettings)
const currentPostReplacementScope = useValueRef(currentPostReplacementScopeSettings)
const langMapper = React.useRef((x: Language) => {
if (x === Language.en) return t('language_en')
if (x === Language.zh) return t('language_zh')
Expand All @@ -130,11 +128,6 @@ export default function DashboardSettingsRouter() {
if (x === Appearance.light) return t('settings_appearance_light')
return t('settings_appearance_default')
}).current
const postReplacerMapper = React.useRef((x: PostReplacementScope) => {
if (x === PostReplacementScope.all) return t('settings_post_replace_all_posts')
if (x === PostReplacementScope.encryptedOnly) return t('settings_post_replace_encrypted_posts')
return t('settings_post_replace_enhanced_posts')
}).current
const classes = useStyles()
const shadowRoot = useValueRef(renderInShadowRootSettings)
const theme = useTheme()
Expand Down Expand Up @@ -182,13 +175,6 @@ export default function DashboardSettingsRouter() {
value={currentLocalWalletEthereumNetworkSettings}
/>
) : null}
<SettingsUIEnum
classes={listStyle}
enumObject={PostReplacementScope}
getText={postReplacerMapper}
icon={<FlipToFrontIcon />}
value={currentPostReplacementScopeSettings}
/>
</List>
</Card>
</Paper>
Expand All @@ -215,6 +201,11 @@ export default function DashboardSettingsRouter() {
icon={<MemoryOutlinedIcon />}
value={debugModeSetting}
/>
<SettingsUI
classes={listStyle}
icon={<FlipToFrontIcon />}
value={allPostReplacementSettings}
/>
</List>
</Card>
</Paper>
Expand Down
23 changes: 8 additions & 15 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export const renderInShadowRootSettings = createGlobalSettings<boolean>(
},
)

/**
* Whether if create substitute post for all posts
*/
export const allPostReplacementSettings = createGlobalSettings<boolean>('post replacement all', false, {
primary: () => i18n.t('settings_post_replacement'),
secondary: () => i18n.t('settings_post_replacement_desc'),
})

export enum Appearance {
default = 'default',
light = 'light',
Expand All @@ -44,21 +52,6 @@ export const appearanceSettings = createGlobalSettings<Appearance>('apperance',
primary: () => i18n.t('settings_appearance'),
})

export enum PostReplacementScope {
all = 'all',
enhancedOnly = 'enhancedOnly',
encryptedOnly = 'encryptedOnly',
}

export const currentPostReplacementScopeSettings = createGlobalSettings<PostReplacementScope>(
'post replacement scope',
PostReplacementScope.enhancedOnly,
{
primary: () => i18n.t('settings_post_replacement_scope'),
secondary: () => i18n.t('settings_post_replacement_scope_desc'),
},
)

export const currentLocalWalletEthereumNetworkSettings = createGlobalSettings<EthereumNetwork>(
'eth network',
EthereumNetwork.Mainnet,
Expand Down

0 comments on commit d6a7472

Please sign in to comment.