Skip to content

Commit

Permalink
remove unused preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnm committed Aug 21, 2024
1 parent 76034dd commit 56a2ff1
Show file tree
Hide file tree
Showing 13 changed files with 4 additions and 764 deletions.
8 changes: 0 additions & 8 deletions packages/app/src/app/hooks/useIsEditorPage.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/app/src/app/overmind/effects/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
UploadedFilesInfo,
UserQuery,
UserSandbox,
SettingsSync,
ForkSandboxBody,
} from '@codesandbox/common/lib/types';
import { FETCH_TEAM_TEMPLATES } from 'app/components/Create/utils/queries';
Expand Down Expand Up @@ -184,27 +183,6 @@ export default {
},
});
},
createUserSettings({
name,
settings,
}: {
name: string;
settings: string;
}): Promise<SettingsSync> {
return api.post(`/users/current_user/editor_settings`, {
name,
settings,
});
},
getUserSettings(): Promise<SettingsSync[]> {
return api.get(`/users/current_user/editor_settings`);
},
editUserSettings(body: any, id: string): Promise<SettingsSync> {
return api.patch(`/users/current_user/editor_settings/${id}`, body);
},
removeUserSetting(id: string): Promise<SettingsSync> {
return api.delete(`/users/current_user/editor_settings`);
},
sandboxesLimits() {
return api.get<{
sandboxCount: number;
Expand Down
243 changes: 0 additions & 243 deletions packages/app/src/app/overmind/namespaces/preferences/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { convertTypeToStatus } from '@codesandbox/common/lib/utils/notifications';
import { isEqual } from 'lodash-es';
import { saveAs } from 'file-saver';
import { Context } from 'app/overmind';
import { SettingSync } from './state';

export const viewModeChanged = (
{ state }: Context,
Expand Down Expand Up @@ -68,245 +64,6 @@ export const settingChanged = (
});
};

export const zenModeToggled = ({ state }: Context) => {
state.preferences.settings.zenMode = !state.preferences.settings.zenMode;
};

export const codeMirrorForced = ({ state }: Context) => {
state.preferences.settings.codeMirror = true;
};

export const getUserLocalSettings = () => {
const fs = window.BrowserFS.BFSRequire('fs');
const all = fs.readdirSync('/vscode');
const files = {};

const readFromDirectory = (path: string) => {
const filesInDirectory = fs.readdirSync(path);
if (path === '/vscode/userdata/CachedExtensions') {
return;
}

filesInDirectory.forEach(p => {
const newPath = path + '/' + p;
if (fs.statSync(newPath).isDirectory()) {
readFromDirectory(newPath);
} else {
files[newPath] = fs.readFileSync(newPath).toString();
}
});
};

all.forEach(dir => {
const a = `/vscode/` + dir;

if (fs.statSync(a).isDirectory()) {
readFromDirectory(a);
} else {
files[a] = fs.readFileSync(a).toString();
}
});

const LOCAL_STORAGE_KEYS = [
'vs-global://colorThemeData',
'VIEW_MODE_DASHBOARD',
'vs-global://iconThemeData',
...Object.keys(localStorage).filter(key => key.includes('settings.')),
];
const themeData = {};

LOCAL_STORAGE_KEYS.forEach(key => {
themeData[key] = localStorage.getItem(key);
});

return {
themeData,
vscode: files,
};
};

export const renameUserSettings = async (
{ state, effects }: Context,
{
name,
id,
}: {
name: string;
id: string;
}
) => {
const { settingsSync } = state.preferences;
if (!name || !settingsSync.settings) return;

try {
const response = await effects.api.editUserSettings(
{
...settingsSync.settings.find(s => s.id === id),
name,
},
id
);

settingsSync.settings = settingsSync.settings.map(setting => {
if (setting.id === response.id) {
return {
...setting,
name,
};
}

return setting;
});
} catch (e) {
effects.notificationToast.error(
'There has been a problem renaming your profile'
);
}
};

export const getUserSettings = async ({ state, effects }: Context) => {
const { settingsSync } = state.preferences;
settingsSync.fetching = true;

const response = await effects.api.getUserSettings();

settingsSync.settings = response;
settingsSync.fetching = false;
};

export const createPreferencesProfile = async ({
state,
effects,
actions,
}: Context) => {
state.preferences.settingsSync.syncing = true;
try {
const { vscode, themeData } = actions.preferences.getUserLocalSettings();

actions.preferences.updateServerSettings(
JSON.stringify({
themeData,
vscode,
})
);
} catch (e) {
effects.notificationToast.error(
'There has been a problem uploading your preferences'
);
}
};

export const updateServerSettings = async (
{ state, effects }: Context,
settingsStringified: string
) => {
if (!state.user) return;
const {
id,
insertedAt,
name,
settings,
updatedAt,
} = await effects.api.createUserSettings({
name: 'My Profile',
settings: settingsStringified,
});

state.preferences.settingsSync.syncing = false;
state.preferences.settingsSync.settings = [
{
id,
insertedAt,
name,
settings,
updatedAt,
},
];
localStorage.setItem(`profile-${id}`, updatedAt);
effects.notificationToast.success(
'Your preferences have been successfully synced'
);
};

export const checkifSynced = ({ actions }: Context, savedSetting: string) => {
const currentSettings = actions.preferences.getUserLocalSettings();
return isEqual(currentSettings, JSON.parse(savedSetting));
};

export const deleteUserSetting = async (
{ state, effects, actions }: Context,
id: string
) => {
if (!state.preferences.settingsSync.settings) return;
try {
await effects.api.removeUserSetting(id);
const removed = state.preferences.settingsSync.settings.find(
setting => setting.id === id
);

state.preferences.settingsSync.settings = state.preferences.settingsSync.settings.filter(
setting => setting.id !== id
);
effects.notificationToast.add({
title: 'Your profile has been removed',
message: '',
status: convertTypeToStatus('success'),
sticky: false,
actions: {
primary: {
label: 'Undo',
run: () => {
if (removed?.settings) {
actions.preferences.updateServerSettings(removed?.settings);
}
},
},
},
});
} catch (e) {
effects.notificationToast.error(
'There has been a problem removing your profile'
);
}
};

export const downloadPreferences = async (
_: Context,
settings: SettingSync
) => {
const blob = new Blob([settings.settings], { type: 'application/json' });
saveAs(blob, `CodeSandboxSettings-${settings.name}.json`);
};

export const applyPreferences = async (
{ state, effects }: Context,
settings: string
) => {
if (!state.preferences.settingsSync.settings) return;
const fs = window.BrowserFS.BFSRequire('fs');

try {
const parsedSyncedSettings = JSON.parse(settings);

Object.keys(parsedSyncedSettings.themeData).forEach(key => {
localStorage.setItem(key, parsedSyncedSettings.themeData[key]);
});

Object.keys(parsedSyncedSettings.vscode).forEach(key => {
fs.writeFileSync(key, parsedSyncedSettings.vscode[key]);
});
effects.notificationToast.success(
'Your preferences have been applied. The page will now reload'
);

window.setTimeout(() => location.reload(), 1000);
} catch (e) {
effects.notificationToast.error(
'There has been a problem applying your preferences'
);
}
};

export const updateAccountDetails = async (
{ state, effects }: Context,
{
Expand Down

This file was deleted.

Loading

0 comments on commit 56a2ff1

Please sign in to comment.