From c06a31dfbb35537379defabe84701f9355a105a5 Mon Sep 17 00:00:00 2001
From: Lim Chee Aun
Date: Sat, 8 Apr 2023 22:16:13 +0800
Subject: [PATCH] Allow edit Shortcuts now woot
---
src/components/columns.jsx | 1 +
src/components/shortcuts-settings.css | 4 ++
src/components/shortcuts-settings.jsx | 82 ++++++++++++++++++++++-----
3 files changed, 74 insertions(+), 13 deletions(-)
diff --git a/src/components/columns.jsx b/src/components/columns.jsx
index ad8d4aec..da5ca8db 100644
--- a/src/components/columns.jsx
+++ b/src/components/columns.jsx
@@ -17,6 +17,7 @@ function Columns() {
const { shortcuts } = snapStates;
const components = shortcuts.map((shortcut) => {
+ if (!shortcut) return null;
const { type, ...params } = shortcut;
const Component = {
following: Following,
diff --git a/src/components/shortcuts-settings.css b/src/components/shortcuts-settings.css
index 1036d79b..5e141930 100644
--- a/src/components/shortcuts-settings.css
+++ b/src/components/shortcuts-settings.css
@@ -121,3 +121,7 @@
min-width: 0;
max-width: 320px;
}
+#shortcut-settings-form form footer {
+ display: flex;
+ gap: 16px;
+}
diff --git a/src/components/shortcuts-settings.jsx b/src/components/shortcuts-settings.jsx
index 7e2c7fef..7f92f521 100644
--- a/src/components/shortcuts-settings.jsx
+++ b/src/components/shortcuts-settings.jsx
@@ -1,7 +1,7 @@
import './shortcuts-settings.css';
import mem from 'mem';
-import { useEffect, useState } from 'preact/hooks';
+import { useEffect, useRef, useState } from 'preact/hooks';
import { useSnapshot } from 'valtio';
import floatingButtonUrl from '../assets/floating-button.svg';
@@ -312,7 +312,7 @@ function ShortcutsSettings() {
*/}
{shortcuts.length > 0 ? (
- {shortcuts.map((shortcut, i) => {
+ {shortcuts.filter(Boolean).map((shortcut, i) => {
const key = i + Object.values(shortcut);
const { type } = shortcut;
if (!SHORTCUTS_META[type]) return null;
@@ -372,6 +372,18 @@ function ShortcutsSettings() {
+ {/*
+ */}
);
@@ -420,12 +432,18 @@ function ShortcutsSettings() {
}}
>
= SHORTCUTS_LIMIT}
lists={lists}
followedHashtags={followedHashtags}
- onSubmit={(data) => {
- console.log('onSubmit', data);
- states.shortcuts.push(data);
+ onSubmit={({ result, mode }) => {
+ console.log('onSubmit', result);
+ if (mode === 'edit') {
+ states.shortcuts[showForm.shortcutIndex] = result;
+ } else {
+ states.shortcuts.push(result);
+ }
}}
onClose={() => setShowForm(false)}
/>
@@ -436,21 +454,40 @@ function ShortcutsSettings() {
}
function ShortcutForm({
- type,
lists,
followedHashtags,
onSubmit,
disabled,
+ shortcut,
+ shortcutIndex,
onClose = () => {},
}) {
- const [currentType, setCurrentType] = useState(type);
+ console.log('shortcut', shortcut);
+ const editMode = !!shortcut;
+ const [currentType, setCurrentType] = useState(shortcut?.type || null);
+
+ const formRef = useRef();
+ useEffect(() => {
+ if (editMode && currentType && TYPE_PARAMS[currentType]) {
+ // Populate form
+ const form = formRef.current;
+ TYPE_PARAMS[currentType].forEach(({ name }) => {
+ const input = form.querySelector(`[name="${name}"]`);
+ if (input && shortcut[name]) {
+ input.value = shortcut[name];
+ }
+ });
+ }
+ }, [editMode, currentType]);
+
return (
- Add shortcut
+ {editMode ? 'Edit' : 'Add'} shortcut