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

feat: add Labs pane to preferences #892

Merged
merged 18 commits into from
Mar 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: labs pane with experimental features
johnny243 committed Mar 7, 2022

Verified

This commit was signed with the committer’s verified signature.
johnny243 Johnny A.
commit 8b65ff52086ff07540fd7594d857524562439300
96 changes: 48 additions & 48 deletions app/assets/javascripts/preferences/panes/general-segments/Labs.tsx
Original file line number Diff line number Diff line change
@@ -7,70 +7,70 @@ import {
import { WebApplication } from '@/ui_models/application';
import { FeatureIdentifier } from '@standardnotes/snjs';
import { FunctionComponent } from 'preact';
import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
import { useCallback, useEffect, useState } from 'preact/hooks';

type Props = {
application: WebApplication;
};

/** @TODO Remove after adding in snjs */
const storageKey = 'enabled_lab_features';

export const LabsPane: FunctionComponent<Props> = ({ application }) => {
const [enabledLabFeatures, setEnabledLabFeatures] =
const [experimentalFeatures, setExperimentalFeatures] =
useState<FeatureIdentifier[]>();

const reloadEnabledFeatures = useCallback(async () => {
const rawStorageValue =
await application.deviceInterface.getRawStorageValue(storageKey);
if (rawStorageValue) {
const parsedEnabledLabFeatures: FeatureIdentifier[] =
JSON.parse(rawStorageValue);
setEnabledLabFeatures(parsedEnabledLabFeatures);
}
}, [application.deviceInterface]);

useEffect(() => {
reloadEnabledFeatures();
}, [reloadEnabledFeatures]);

const isAccountSwitcherEnabled = useMemo(() => {
return enabledLabFeatures?.includes(FeatureIdentifier.AccountSwitcher);
}, [enabledLabFeatures]);
const reloadExperimentalFeatures = useCallback(() => {
const experimentalFeatures = application.features.getExperimentalFeatures();

const toggleAccountSwitcher = () => {
const currentFeatures = enabledLabFeatures ? enabledLabFeatures : [];
setExperimentalFeatures(
experimentalFeatures.filter(
(feature) => feature !== FeatureIdentifier.AccountSwitcher
)
);
}, [application.features]);

if (isAccountSwitcherEnabled) {
application.deviceInterface.setRawStorageValue(
storageKey,
JSON.stringify(
currentFeatures.filter(
(feature) => feature !== FeatureIdentifier.AccountSwitcher
)
)
);
} else {
application.deviceInterface.setRawStorageValue(
storageKey,
JSON.stringify([...currentFeatures, FeatureIdentifier.AccountSwitcher])
);
}
useEffect(() => {
reloadExperimentalFeatures();
}, [reloadExperimentalFeatures]);

reloadEnabledFeatures();
};
if (!experimentalFeatures) {
return (
<div className="flex items-center justify-between">
No experimental features available.
</div>
);
}

return (
<PreferencesGroup>
<PreferencesSegment>
<Title>Labs</Title>
<div className="flex items-center justify-between">
<div className="font-medium text-sm m-0">Account Switcher</div>
<Switch
onChange={toggleAccountSwitcher}
checked={isAccountSwitcherEnabled}
/>
</div>
{experimentalFeatures?.map((featureIdentifier: FeatureIdentifier) => {
const feature = application.features.getFeature(featureIdentifier);
const isFeatureEnabled =
application.features.isExperimentalFeatureEnabled(
featureIdentifier
);

const toggleFeature = () => {
/** TODO: move this into Features service -> toggleExperimentalFeature(identifier: FeatureIdentifier) */
if (isFeatureEnabled) {
application.features.disableExperimentalFeature(
featureIdentifier
);
} else {
application.features.enableExperimentalFeature(featureIdentifier);
}
reloadExperimentalFeatures();
};

return (
<div className="flex items-center justify-between">
<div className="font-medium text-sm m-0">
{feature?.name ?? featureIdentifier}
</div>
<Switch onChange={toggleFeature} checked={isFeatureEnabled} />
</div>
);
})}
</PreferencesSegment>
</PreferencesGroup>
);