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

DYN-6857-homepage-preferences #13

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState, useEffect } from 'react';
import { IntlProvider } from 'react-intl';
import { getMessagesForLocale } from './localization/localization.js';
import { LayoutContainer } from './components/LayoutContainer.jsx';
import { SettingsProvider } from './components/SettingsContext.jsx';

function App() {
const [locale, setLocale] = useState('en');
Expand All @@ -23,7 +24,9 @@ function App() {

return (
<IntlProvider locale={locale} messages={messages}>
<LayoutContainer id='homeContainer' />
<SettingsProvider>
<LayoutContainer id='homeContainer' />
</SettingsProvider>
</IntlProvider>
);
}
Expand Down
24 changes: 20 additions & 4 deletions src/components/MainContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import { FormattedMessage } from 'react-intl';

export function MainContent({ selectedSidebarItem }){
const [isDisabled, setIsDisabled] = useState(false);
const [settings, setSettings] = useState(null);

const setShowStartPageChanged = (showStartPage) => {
setIsDisabled(!showStartPage);
};

const setHomePageSettings = (settingsJson) => {
try{
if (settingsJson) {
const settingsObject = JSON.parse(settingsJson);
setSettings(settingsObject);
} else {
console.log(`Received null or empty settings`);
}
}catch(exeption){
console.log($`Failed to set the HomePage settings with the following error ${exception}`);
}
}

useEffect(() => {
// Set the setShowStartPageChanged global function
// Set global functions
window.setShowStartPageChanged = setShowStartPageChanged;
window.setHomePageSettings = setHomePageSettings;

return () => {
delete window.setShowStartPageChanged;
delete window.setHomePageSettings;
};
}, [isDisabled]);
}, [isDisabled, settings]);

return (
<>
Expand All @@ -31,10 +47,10 @@ export function MainContent({ selectedSidebarItem }){
)}

<div className={`page-container ${selectedSidebarItem === 'Recent' ? '' : 'hidden'}`}>
<RecentPage setIsDisabled={setIsDisabled} />
<RecentPage setIsDisabled={setIsDisabled} recentPageViewMode={settings?.recentPageViewMode || "grid"} />
</div>
<div className={`page-container ${selectedSidebarItem === 'Samples' ? '' : 'hidden'}`}>
<SamplesPage />
<SamplesPage samplesViewMode={settings?.samplesViewMode || "grid"} />
</div>
<div className={`page-container ${selectedSidebarItem === 'Learning' ? '' : 'hidden'}`}>
<LearningPage />
Expand Down
2 changes: 0 additions & 2 deletions src/components/Recent/CustomAuthorCellRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const CustomAuthorCellRenderer = ({ value, row }) => {
const author = value;
const isOldFormat = author === intl.formatMessage({ id: 'recent.item.old.format' });

console.log(author);
console.log(isOldFormat);
return (
<div className={styles["title-cell"]}>
<p>{author}</p>
Expand Down
24 changes: 21 additions & 3 deletions src/components/Recent/PageRecent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { CustomLocationCellRenderer } from './CustomLocationCellRenderer.jsx';
import { CustomAuthorCellRenderer } from "./CustomAuthorCellRenderer.jsx";
import { GraphTable } from './GraphTable.jsx';
import { GridViewIcon, ListViewIcon } from '../Common/CustomIcons.jsx';
import { openFile } from '../../functions/utility.js';
import { openFile, saveHomePageSettings } from '../../functions/utility.js';
import { FormattedMessage } from 'react-intl';
import { Tooltip } from '../Common/Tooltip.jsx';
import { useSettings } from '../SettingsContext.jsx';

export function RecentPage ({ setIsDisabled }){
const [viewMode, setViewMode] = useState('grid');
export function RecentPage ({ setIsDisabled, recentPageViewMode }){
const { settings, updateSettings } = useSettings();
const [viewMode, setViewMode] = useState(recentPageViewMode);
const [initialized, setInitialized] = useState(false);

// Set a placeholder for the graphs which will be used differently during dev and prod
let initialGraphs = [];
Expand Down Expand Up @@ -48,6 +51,21 @@ export function RecentPage ({ setIsDisabled }){
};
}, []);

useEffect(() => {
// Set the viewMode based on the HomePage preferences
setViewMode(recentPageViewMode);
}, [recentPageViewMode]);

useEffect(() => {
if (initialized || recentPageViewMode !== viewMode) {
setInitialized(true);
updateSettings({ recentPageViewMode: viewMode });

// Send settings to Dynamo to save
saveHomePageSettings({ ...settings, recentPageViewMode: viewMode });
}
}, [viewMode]);

// This variable defins the table structure displaying the graphs
const columns = React.useMemo(() => [
{
Expand Down
25 changes: 22 additions & 3 deletions src/components/Samples/PageSamples.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { GridViewIcon, ListViewIcon } from '../Common/CustomIcons.jsx';
import { Tooltip } from '../Common/Tooltip.jsx';
import { CustomSampleFirstCellRenderer } from "./CustomSampleFirstCellRenderer.jsx";
import { SamplesGrid } from './SamplesGrid.jsx';
import { openFile, showSamplesFilesInFolder } from '../../functions/utility.js';
import { openFile, showSamplesFilesInFolder, saveHomePageSettings } from '../../functions/utility.js';
import { useSettings } from '../SettingsContext.jsx';

export function SamplesPage (){
const [viewMode, setViewMode] = useState('grid');
export function SamplesPage ({ samplesViewMode }){
const { settings, updateSettings } = useSettings();
const [viewMode, setViewMode] = useState(samplesViewMode);
const [collapsedRows, setCollapsedRows] = useState({});
const [initialized, setInitialized] = useState(false);

// Set a placeholder for the graphs which will be used differently during dev and prod
let initialSamples = [];
Expand Down Expand Up @@ -51,6 +54,22 @@ export function SamplesPage (){
};
}, []);


useEffect(() => {
// Set the viewMode based on the HomePage preferences
setViewMode(samplesViewMode);
}, [samplesViewMode]);

useEffect(() => {
if (initialized || samplesViewMode !== viewMode) {
setInitialized(true);
updateSettings({ samplesViewMode: viewMode });

// Send settings to Dynamo to save
saveHomePageSettings({ ...settings, samplesViewMode: viewMode });
}
}, [viewMode]);

// This variable defins the table structure displaying the graphs
const columns = React.useMemo(() => [
{
Expand Down
25 changes: 25 additions & 0 deletions src/components/SettingsContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { createContext, useContext, useState, useEffect } from 'react';

// Create the context
const SettingsContext = createContext();

// Provider component that wraps the app components
export function SettingsProvider({ children }) {
const [settings, setSettings] = useState({});

// Update settings
const updateSettings = (newSettings) => {
setSettings(prev => ({ ...prev, ...newSettings }));
};

return (
<SettingsContext.Provider value={{ settings, updateSettings }}>
{children}
</SettingsContext.Provider>
);
}

// Use settings hook
export function useSettings() {
return useContext(SettingsContext);
}
10 changes: 10 additions & 0 deletions src/functions/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ export function showSamplesFilesInFolder() {
chrome.webview.hostObjects.scriptObject.ShowSampleFilesInFolder();
}
}

/**
* A call to a backend function requesting to save the current HomePage settings
*/
export function saveHomePageSettings(settings) {
if (chrome.webview !== undefined) {
const settingsJson = JSON.stringify(settings);
chrome.webview.hostObjects.scriptObject.SaveSettings(settingsJson);
}
}
Loading