im working on a decky plugin why does my custom websites variable get reset once the…
- User
im working on a decky plugin why does my custom websites variable get reset once the user enters for example google.com? import { ButtonItem, definePlugin, findSP, Menu, MenuItem, ModalRoot, ModalRootProps, PanelSection, PanelSectionRow, ServerAPI, showContextMenu, showModal, staticClasses, TextField, ToggleField, } from "decky-frontend-lib"; import { useContext, useEffect, useReducer, useState, VFC, createContext } from "react"; import { FaRocket } from "react-icons/fa";
// Define an action type for updating customWebsites type UpdateCustomWebsitesAction = { type: 'UPDATE_CUSTOM_WEBSITES'; customWebsites: string[]; };
// Define a reducer function for updating customWebsites const customWebsitesReducer = ( state: string[], action: UpdateCustomWebsitesAction ) => { console.log(
action: ${JSON.stringify(action)}); switch (action.type) { case 'UPDATE_CUSTOM_WEBSITES': return action.customWebsites; default: return state; } };// Create a context with an empty array as the default value const CustomWebsitesContext = createContext<{ customWebsites: string[]; dispatch: React.Dispatch; }>({ customWebsites: [], dispatch: () => {}, });
// Create a provider component that takes in children as a prop const CustomWebsitesProvider: React.FC = ({ children }) => { // Create a state variable and dispatch function for customWebsites const [customWebsites, dispatch] = useReducer(customWebsitesReducer, []);
// Render the provider and pass in the customWebsites state and dispatch function as the value return ( <CustomWebsitesContext.Provider value={{ customWebsites, dispatch }}> {children} </CustomWebsitesContext.Provider> ); };
type SearchModalProps = ModalRootProps & { setModalResult?(result: string[]): void; promptText: string; };
const SearchModal: VFC = ({ closeModal, setModalResult, promptText }) => { console.log('SearchModal rendered');
const [searchText, setSearchText] = useState('');
const handleTextChange = (e: React.ChangeEvent) => { setSearchText(e.target.value); };
const handleSubmit = () => { // Split the entered text by commas and trim any whitespace const websites = searchText.split(',').map((website) => website.trim()); console.log(
websites: ${JSON.stringify(websites)}); setModalResult && setModalResult(websites); closeModal && closeModal(); };return ( You can separate multiple websites by using commas. Submit ); };
const Content: VFC<{ serverAPI: ServerAPI }> = ({ serverAPI }) => { console.log('Content rendered');
// Use the useContext hook to access customWebsites and dispatch from the context const { customWebsites, dispatch } = useContext(CustomWebsitesContext);
const [options, setOptions] = useState({ epicGames: false, gogGalaxy: false, origin: false, uplay: false, battleNet: false, amazonGames: false, eaApp: false, legacyGames: false, itchIo: false, humbleGames: false, indieGala: false, rockstar: false, glyph: false, minecraft: false, psPlus: false, dmm: false, xboxGamePass: false, geforceNow: false, amazonLuna:false, netflix:false, hulu:false, disneyPlus:false, amazonPrimeVideo:false, youtube:false });
- Assistant
The
customWebsitesvariable is getting reset because it is not persisted or stored anywhere when the user enters a different website. In your code, thecustomWebsitesvariable is only stored in the state of theCustomWebsitesProvidercomponent, which gets reset whenever the component is unmounted or re-rendered.To persist the
customWebsitesvariable across different pages or website entries, you can use browser storage mechanisms such aslocalStorageor .
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.