Skip to content

Commit

Permalink
add beta feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
blackforestboi committed Jun 10, 2024
1 parent babc89f commit 1ab41e2
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 69 deletions.
4 changes: 1 addition & 3 deletions src/background-script/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ export function createBackgroundModules(options: {
),
})

const betaFeatures = new BetaFeaturesBackground({
settingsStore: syncSettingsStore,
})
const betaFeatures = null

const localExtSettingStore = new BrowserSettingsStore<
LocalExtensionSettings
Expand Down
21 changes: 0 additions & 21 deletions src/beta-features/background/index.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/beta-features/background/types/remote-interface.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/beta-features/background/types/settings.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import { runInBackground } from 'src/util/webextensionRPC'
import { AuthContextInterface } from 'src/authentication/background/types'
import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section'
import { SyncSettingsStoreInterface } from 'src/sync-settings/types'
import { SyncSettingsByFeature } from 'src/sync-settings/background/types'

const BetaFeaturesData = [
{
id: 'imageOverlay',
id: 'imageOverlay' as keyof SyncSettingsByFeature['betaFeatures'],
title: 'Image Hover Buttons',
description:
'Display an overlay on every image to either save it or analyze it with AI.',
Expand Down Expand Up @@ -79,7 +80,11 @@ export default class BetaFeaturesSettings extends StatefulUIElement<
feature: feature.id,
})
}}
type="primary"
type={
this.state.betaFeaturesSetting[feature.id]
? 'primary'
: 'secondary'
}
size={'medium'}
/>
</RightSide>
Expand Down
32 changes: 24 additions & 8 deletions src/beta-features/ui/containers/beta-features-settings/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {
import { loadInitial, executeUITask } from 'src/util/ui-logic'
import { runInBackground } from 'src/util/webextensionRPC'
import { SyncSettingsStoreInterface } from 'src/sync-settings/types'
import {
SyncSettingsStore,
createSyncSettingsStore,
} from 'src/sync-settings/util'

export const INITIAL_STATE: BetaFeaturesSettingsState = {
betaFeaturesSetting: null,
betaFeaturesSetting: {},
}

type EventHandler<
Expand All @@ -24,10 +28,14 @@ export default class BetaFeaturesSettingsLogic extends UILogic<
BetaFeaturesSettingsState,
BetaFeaturesSettingsEvent
> {
private syncSettingsBG = runInBackground<SyncSettingsStoreInterface>()
syncSettings: SyncSettingsStore<'betaFeatures'>

constructor(protected dependencies: BetaFeaturesSettingsDependencies) {
super()

this.syncSettings = createSyncSettingsStore({
syncSettingsBG: dependencies.syncSettingsBG,
})
}

getInitialState(): BetaFeaturesSettingsState {
Expand All @@ -37,7 +45,18 @@ export default class BetaFeaturesSettingsLogic extends UILogic<
}

init = async () => {
// await loadInitial<BetaFeaturesSettingsState>(this, async () => {})
// Load feature settings
const imageOverlaySetting = await this.syncSettings.betaFeatures.get(
'imageOverlay',
)

let betaFeaturesSetting = {
imageOverlay: imageOverlaySetting,
}

this.emitMutation({
betaFeaturesSetting: { $set: betaFeaturesSetting },
})
}

activateFeature: EventHandler<'activateFeature'> = async ({
Expand All @@ -47,12 +66,9 @@ export default class BetaFeaturesSettingsLogic extends UILogic<
let betaFeaturesSetting = previousState.betaFeaturesSetting
const feature = event.feature
const previousValue = betaFeaturesSetting[feature]
betaFeaturesSetting[feature] = !previousValue

await this.dependencies.syncSettingsBG.extension.set('betaFeatures', {
[feature]: !previousValue,
})

betaFeaturesSetting[feature] = true
await this.syncSettings.betaFeatures.set(feature, !previousValue)

this.emitMutation({
betaFeaturesSetting: { $set: betaFeaturesSetting },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { UIEvent } from 'ui-logic-core'
import { SyncSettingsStoreInterface } from 'src/sync-settings/types'
import {
RemoteSyncSettingsInterface,
SyncSettingsByFeature,
} from 'src/sync-settings/background/types'

export interface BetaFeaturesSettingsState {
betaFeaturesSetting: {
Expand All @@ -8,9 +11,9 @@ export interface BetaFeaturesSettingsState {
}

export interface BetaFeaturesSettingsDependencies {
syncSettingsBG: SyncSettingsStoreInterface
syncSettingsBG: RemoteSyncSettingsInterface
}

export type BetaFeaturesSettingsEvent = UIEvent<{
activateFeature: { feature: string }
activateFeature: { feature: keyof SyncSettingsByFeature['betaFeatures'] }
}>
41 changes: 31 additions & 10 deletions src/search-injection/img-action-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import { TooltipBox } from '@worldbrain/memex-common/lib/common-ui/components/to
interface RootProps {
rootEl: HTMLElement
syncSettingsBG: RemoteSyncSettingsInterface
syncSettings: SyncSettingsStore<'extension'>
syncSettings: SyncSettingsStore<'betaFeatures'>
annotationsFunctions: any
browserAPIs: Browser
contentScriptsBG: ContentScriptsInterface<'caller'>
imageUrl: string
imageData: string
removeElement: () => void
disableImageInjection: () => void
}

interface RootState {
Expand Down Expand Up @@ -66,7 +67,14 @@ class Root extends React.Component<RootProps, RootState> {
<ParentContainer ref={this.parentContainerRef}>
<TooltipBox
getPortalRoot={() => this.props.rootEl}
tooltipText="Remove for this page session"
tooltipText={
<span>
Remove for this page session
<br /> <strong>Shift</strong>Click to
disable completely. <br /> Reenable via Beta
Features Settings
</span>
}
placement="bottom"
>
<PrimaryAction
Expand All @@ -76,7 +84,12 @@ class Root extends React.Component<RootProps, RootState> {
onClick={async (event) => {
event.stopPropagation()
event.preventDefault()
this.props.removeElement()

if (event.shiftKey) {
this.props.disableImageInjection()
} else {
this.props.removeElement()
}
}}
padding={'0 5px'}
iconColor={'white'}
Expand Down Expand Up @@ -132,23 +145,23 @@ class Root extends React.Component<RootProps, RootState> {
}

export const handleRenderImgActionButtons = async (
syncSettings: SyncSettingsStore<'extension'>,
syncSettings: SyncSettingsStore<'betaFeatures'>,
syncSettingsBG: RemoteSyncSettingsInterface,
annotationsFunctions: any,
browserAPIs: Browser,
imageElements: HTMLCollectionOf<HTMLImageElement>,
contentScriptsBG: ContentScriptsInterface<'caller'>,
) => {
const betaFeatures = await syncSettings.extension.get('betaFeatures')
const betaFeatureSetting = await syncSettings.betaFeatures.get(
'imageOverlay',
)
let imageInjectionEnabled = null
if (betaFeatures != null) {
imageInjectionEnabled = betaFeatures.imageInjection
if (betaFeatureSetting != null) {
imageInjectionEnabled = betaFeatureSetting
}

if (imageInjectionEnabled == null) {
await syncSettings.extension.set('betaFeatures', {
imageInjection: false,
})
await syncSettings.betaFeatures.set('imageOverlay', false)
}

if (!imageInjectionEnabled) {
Expand Down Expand Up @@ -284,6 +297,14 @@ export const handleRenderImgActionButtons = async (
ReactDOM.unmountComponentAtNode(target)
shouldShow = false
}}
disableImageInjection={async () => {
ReactDOM.unmountComponentAtNode(target)
await syncSettings.betaFeatures.set(
'imageOverlay',
false,
)
shouldShow = false
}}
/>
</RootPosContainer>,
target,
Expand Down
5 changes: 4 additions & 1 deletion src/sync-settings/background/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const FEATURE_PREFIX = {
READWISE: 'readwise.',
OPENAI: 'openAI.',
HIGHLIGHT_COLORS: 'highlightColors.',
BETA_FEATURES: '@BetaFeatures-',
}

export const SETTING_NAMES: SyncSettingNames = {
Expand All @@ -41,7 +42,9 @@ export const SETTING_NAMES: SyncSettingNames = {
shouldAutoCreateNoteLink:
FEATURE_PREFIX.EXTENSION + 'shouldAutoCreateNoteLink',
shouldAutoAddSpaces: FEATURE_PREFIX.EXTENSION + 'shouldAutoAddSpaces',
betaFeatures: FEATURE_PREFIX.EXTENSION + 'betaFeatures',
},
betaFeatures: {
imageOverlay: FEATURE_PREFIX.BETA_FEATURES + 'imageOverlay',
},
pdfIntegration: {
shouldAutoOpen: FEATURE_PREFIX.PDF_INTEGRATION + 'should_auto_open',
Expand Down
6 changes: 3 additions & 3 deletions src/sync-settings/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ export interface SyncSettingsByFeature {
askAIShortcut: string
sharePageShortcut: string
}
betaFeatures: {
[key: string]: boolean
}
shouldAutoCreateNoteLink: boolean
shouldAutoAddSpaces: boolean
}
betaFeatures: {
imageOverlay: boolean
}
readwise: {
apiKey: string
}
Expand Down
1 change: 1 addition & 0 deletions src/sync-settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface SyncSettingsStoreInterface {
activityIndicator: SettingStore<SyncSettingsByFeature['activityIndicator']>
openAI: SettingStore<SyncSettingsByFeature['openAI']>
highlightColors: SettingStore<SyncSettingsByFeature['highlightColors']>
betaFeatures: SettingStore<SyncSettingsByFeature['betaFeatures']>
}
3 changes: 3 additions & 0 deletions src/sync-settings/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ export const createSyncSettingsStore = <
highlightColors: new BrowserSettingsStore(args.syncSettingsBG, {
prefix: FEATURE_PREFIX.HIGHLIGHT_COLORS,
}),
betaFeatures: new BrowserSettingsStore(args.syncSettingsBG, {
prefix: FEATURE_PREFIX.BETA_FEATURES,
}),
} as SyncSettingsStoreInterface)

0 comments on commit 1ab41e2

Please sign in to comment.