From 67e8f566ae7a33849fae11e14c6897129df22f7a Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 17 May 2025 11:10:55 +0800 Subject: [PATCH] Add reset button to Sandbox --- src/pages/sandbox.css | 6 ++++ src/pages/sandbox.jsx | 83 ++++++++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/pages/sandbox.css b/src/pages/sandbox.css index 15ef2e21..3d59353a 100644 --- a/src/pages/sandbox.css +++ b/src/pages/sandbox.css @@ -127,6 +127,12 @@ background-color: var(--bg-blur-color); box-shadow: 0 0 0 1px var(--outline-color); + header { + display: flex; + align-items: center; + justify-content: space-between; + } + h2 { margin-top: 0; padding-top: 0; diff --git a/src/pages/sandbox.jsx b/src/pages/sandbox.jsx index a45ac0a8..94e818fe 100644 --- a/src/pages/sandbox.jsx +++ b/src/pages/sandbox.jsx @@ -291,33 +291,36 @@ const MOCK_STATUS = ({ toggles = {} } = {}) => { return base; }; +// Initial state +const INITIAL_STATE = { + loading: false, + mediaFirst: false, + hasContent: true, + contentType: 'short', + visibility: 'public', // Default visibility + hasSpoiler: false, + spoilerType: 'all', + mediaCount: '0', + mediaTypes: [], + pollCount: '0', + pollMultiple: false, + pollExpired: false, + showCard: false, + showQuotes: false, + quotesCount: '1', + quoteNestingLevel: '0', + size: 'medium', + filters: [false, false, false], // hide, blur, warn + mediaPreference: 'default', + expandWarnings: false, + contextType: 'none', // Default context type +}; + export default function Sandbox() { useTitle('Sandbox', '/_sandbox'); // Consolidated state for all toggles - const [toggleState, setToggleState] = useState({ - loading: false, - mediaFirst: false, - hasContent: true, - contentType: 'short', - visibility: 'public', // Default visibility - hasSpoiler: false, - spoilerType: 'all', - mediaCount: '0', - mediaTypes: [], - pollCount: '0', - pollMultiple: false, - pollExpired: false, - showCard: false, - showQuotes: false, - quotesCount: '1', - quoteNestingLevel: '0', - size: 'medium', - filters: [false, false, false], // hide, blur, warn - mediaPreference: 'default', - expandWarnings: false, - contextType: 'none', // Default context type - }); + const [toggleState, setToggleState] = useState(INITIAL_STATE); // Update function with view transitions const updateToggles = (updates) => { @@ -674,6 +677,28 @@ export default function Sandbox() { updateToggles({ filters: newFilters }); }; + // Function to check if the current state is different from the initial state + const hasChanges = () => { + return Object.keys(INITIAL_STATE).some((key) => { + if (Array.isArray(INITIAL_STATE[key])) { + if (INITIAL_STATE[key].length !== toggleState[key].length) return true; + return INITIAL_STATE[key].some((val, i) => val !== toggleState[key][i]); + } + return INITIAL_STATE[key] !== toggleState[key]; + }); + }; + + // Function to reset state to initial values + const resetToInitialState = () => { + if (document.startViewTransition) { + document.startViewTransition(() => { + setToggleState({ ...INITIAL_STATE }); + }); + } else { + setToggleState({ ...INITIAL_STATE }); + } + }; + return (
@@ -729,7 +754,17 @@ export default function Sandbox() {
e.preventDefault()}> -

Post Controls

+
+

Post Controls

+ +
  • Miscellaneous