Skip to content

Commit

Permalink
Change User instead of Workspace settings. (#56)
Browse files Browse the repository at this point in the history
Before: 
using our dropdown menu to change Location configuration would change
them only in workspace scope, which could cause confusion, after
changing projects.

After: 
dropdown menu changes global configuration.

---------

Co-authored-by: Filip Andrzej Kaminski <[email protected]>
  • Loading branch information
filip131311 and Filip Andrzej Kaminski authored Mar 29, 2024
1 parent 25aed84 commit d008344
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/vscode-extension/src/common/WorkspaceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export interface WorkspaceConfigEventListener<T> {

export interface WorkspaceConfig {
getConfig(): Promise<WorkspaceConfigProps>;
// update method can take any of the keys from WorkspaceConfigProps and appropriate value:
update<K extends keyof WorkspaceConfigProps>(
// updateUserLevel method can take any of the keys from WorkspaceConfigProps and appropriate value:
updateUserLevel<K extends keyof WorkspaceConfigProps>(
key: K,
value: WorkspaceConfigProps[K]
): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ export class WorkspaceConfigController implements Disposable, WorkspaceConfig {
return this.config;
}

async update<K extends keyof WorkspaceConfigProps>(key: K, value: WorkspaceConfigProps[K]) {
async updateUserLevel<K extends keyof WorkspaceConfigProps>(
key: K,
value: WorkspaceConfigProps[K]
) {
const configuration = workspace.getConfiguration("ReactNativeIDE");
await configuration.update(key as string, value);
await configuration.update(key as string, value, true);
}

async addListener<K extends keyof WorkspaceConfigEventMap>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface SettingsDropdownProps {
}

function SettingsDropdown({ project, children, disabled }: SettingsDropdownProps) {
const { panelLocation, update } = useWorkspaceConfig();
const { panelLocation, updateUserLevel } = useWorkspaceConfig();
const { openModal } = useModal();
return (
<DropdownMenu.Root>
Expand Down Expand Up @@ -55,14 +55,14 @@ function SettingsDropdown({ project, children, disabled }: SettingsDropdownProps
alignOffset={-5}>
<DropdownMenu.Item
className="dropdown-menu-item"
onSelect={() => update("panelLocation", "tab")}>
onSelect={() => updateUserLevel("panelLocation", "tab")}>
<span className="codicon codicon-layout-centered" />
Editor tab
{panelLocation === "tab" && <span className="codicon codicon-check right-slot" />}
</DropdownMenu.Item>
<DropdownMenu.Item
className="dropdown-menu-item"
onSelect={() => update("panelLocation", "side-panel")}>
onSelect={() => updateUserLevel("panelLocation", "side-panel")}>
<span className="codicon codicon-layout-sidebar-right" />
Side panel
{panelLocation === "side-panel" && (
Expand All @@ -72,7 +72,7 @@ function SettingsDropdown({ project, children, disabled }: SettingsDropdownProps
<DropdownMenu.Item
className="dropdown-menu-item"
onSelect={() => {
update("panelLocation", "secondary-side-panel");
updateUserLevel("panelLocation", "secondary-side-panel");
openModal(
"Drag and drop to secondary side panel",
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import { WorkspaceConfig, WorkspaceConfigProps } from "../../common/WorkspaceCon
const workspaceConfig = makeProxy<WorkspaceConfig>("WorkspaceConfig");

type WorkspaceConfigContextType = WorkspaceConfigProps & {
update: <K extends keyof WorkspaceConfigProps>(key: K, value: WorkspaceConfigProps[K]) => void;
updateUserLevel: <K extends keyof WorkspaceConfigProps>(
key: K,
value: WorkspaceConfigProps[K]
) => void;
};

const WorkspaceConfigContext = createContext<WorkspaceConfigContextType>({
panelLocation: "tab",
relativeAppLocation: "",
update: () => {},
updateUserLevel: () => {},
});

export default function WorkspaceConfigProvider({ children }: PropsWithChildren) {
Expand All @@ -36,17 +39,21 @@ export default function WorkspaceConfigProvider({ children }: PropsWithChildren)
};
}, []);

const update = useCallback(
<K extends keyof WorkspaceConfigProps>(key: K, value: WorkspaceConfigProps[K]) => {
const updateUserLevel = useCallback(
<K extends keyof WorkspaceConfigProps>(
key: K,
value: WorkspaceConfigProps[K],
configurationTarget?: boolean
) => {
const newState = { ...config, [key]: value };
setConfig(newState);
workspaceConfig.update(key, value);
workspaceConfig.updateUserLevel(key, value);
},
[config, setConfig]
);

return (
<WorkspaceConfigContext.Provider value={{ ...config, update }}>
<WorkspaceConfigContext.Provider value={{ ...config, updateUserLevel }}>
{children}
</WorkspaceConfigContext.Provider>
);
Expand Down

0 comments on commit d008344

Please sign in to comment.