mirror of
https://github.com/usememos/memos.git
synced 2025-02-23 14:47:44 +01:00
chore: fix component state
This commit is contained in:
parent
b144faf43a
commit
2837816ff7
@ -331,8 +331,9 @@ func (s *APIV1Service) UpdateMemo(ctx context.Context, request *v1pb.UpdateMemoR
|
|||||||
return nil, errors.Wrap(err, "failed to set memo relations")
|
return nil, errors.Wrap(err, "failed to set memo relations")
|
||||||
}
|
}
|
||||||
} else if path == "location" {
|
} else if path == "location" {
|
||||||
memo.Payload.Location = convertLocationToStore(request.Memo.Location)
|
payload := memo.Payload
|
||||||
update.Payload = memo.Payload
|
payload.Location = convertLocationToStore(request.Memo.Location)
|
||||||
|
update.Payload = payload
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Button, IconButton, Input } from "@mui/joy";
|
import { Button, IconButton, Input } from "@mui/joy";
|
||||||
import { LatLng } from "leaflet";
|
import { LatLng } from "leaflet";
|
||||||
import { MapPinIcon } from "lucide-react";
|
import { MapPinIcon, XIcon } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import LeafletMap from "@/components/LeafletMap";
|
import LeafletMap from "@/components/LeafletMap";
|
||||||
@ -10,10 +10,11 @@ import { useTranslate } from "@/utils/i18n";
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
location?: Location;
|
location?: Location;
|
||||||
onChange: (location: Location) => void;
|
onChange: (location?: Location) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
|
initilized: boolean;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
position: LatLng;
|
position: LatLng;
|
||||||
}
|
}
|
||||||
@ -21,14 +22,24 @@ interface State {
|
|||||||
const LocationSelector = (props: Props) => {
|
const LocationSelector = (props: Props) => {
|
||||||
const t = useTranslate();
|
const t = useTranslate();
|
||||||
const [state, setState] = useState<State>({
|
const [state, setState] = useState<State>({
|
||||||
|
initilized: false,
|
||||||
placeholder: props.location?.placeholder || "",
|
placeholder: props.location?.placeholder || "",
|
||||||
position: new LatLng(props.location?.latitude || 0, props.location?.longitude || 0),
|
position: new LatLng(props.location?.latitude || 0, props.location?.longitude || 0),
|
||||||
});
|
});
|
||||||
const [popoverOpen, setPopoverOpen] = useState<boolean>(false);
|
const [popoverOpen, setPopoverOpen] = useState<boolean>(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setState((state) => ({
|
||||||
|
...state,
|
||||||
|
placeholder: props.location?.placeholder || "",
|
||||||
|
position: new LatLng(props.location?.latitude || 0, props.location?.longitude || 0),
|
||||||
|
}));
|
||||||
|
}, [props.location]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (popoverOpen && !props.location) {
|
if (popoverOpen && !props.location) {
|
||||||
const handleError = (error: any, errorMessage: string) => {
|
const handleError = (error: any, errorMessage: string) => {
|
||||||
|
setState({ ...state, initilized: true });
|
||||||
toast.error(errorMessage);
|
toast.error(errorMessage);
|
||||||
console.error(error);
|
console.error(error);
|
||||||
};
|
};
|
||||||
@ -38,7 +49,7 @@ const LocationSelector = (props: Props) => {
|
|||||||
(position) => {
|
(position) => {
|
||||||
const lat = position.coords.latitude;
|
const lat = position.coords.latitude;
|
||||||
const lng = position.coords.longitude;
|
const lng = position.coords.longitude;
|
||||||
setState({ ...state, position: new LatLng(lat, lng) });
|
setState({ ...state, position: new LatLng(lat, lng), initilized: true });
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
handleError(error, "Error getting current position");
|
handleError(error, "Error getting current position");
|
||||||
@ -69,21 +80,30 @@ const LocationSelector = (props: Props) => {
|
|||||||
setState({ ...state, position });
|
setState({ ...state, position });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const removeLocation = (e: React.MouseEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
props.onChange(undefined);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popover open={popoverOpen} onOpenChange={setPopoverOpen}>
|
<Popover open={popoverOpen} onOpenChange={setPopoverOpen}>
|
||||||
<PopoverTrigger>
|
<PopoverTrigger>
|
||||||
<IconButton size="sm" component="div">
|
<IconButton className="group" size="sm" component="div">
|
||||||
<MapPinIcon className="w-5 h-5 mx-auto shrink-0" />
|
<MapPinIcon className="w-5 h-5 mx-auto shrink-0" />
|
||||||
{props.location && (
|
{props.location && (
|
||||||
|
<>
|
||||||
<span className="font-normal ml-0.5 text-ellipsis whitespace-nowrap overflow-hidden max-w-32">
|
<span className="font-normal ml-0.5 text-ellipsis whitespace-nowrap overflow-hidden max-w-32">
|
||||||
{props.location.placeholder}
|
{props.location.placeholder}
|
||||||
</span>
|
</span>
|
||||||
|
<XIcon className="w-5 h-5 mx-auto shrink-0 hidden group-hover:block hover:opacity-80" onClick={removeLocation} />
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
<PopoverContent align="center">
|
<PopoverContent align="center">
|
||||||
<div className="min-w-80 sm:w-128 flex flex-col justify-start items-start">
|
<div className="min-w-80 sm:w-128 flex flex-col justify-start items-start">
|
||||||
<LeafletMap key={JSON.stringify(state.position)} latlng={state.position} onChange={onPositionChanged} />
|
<LeafletMap key={JSON.stringify(state.initilized)} latlng={state.position} onChange={onPositionChanged} />
|
||||||
<div className="mt-2 w-full flex flex-row justify-between items-center gap-2">
|
<div className="mt-2 w-full flex flex-row justify-between items-center gap-2">
|
||||||
<Input
|
<Input
|
||||||
placeholder="Choose location"
|
placeholder="Choose location"
|
||||||
|
@ -45,7 +45,6 @@ export interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
initialized: boolean;
|
|
||||||
memoVisibility: Visibility;
|
memoVisibility: Visibility;
|
||||||
resourceList: Resource[];
|
resourceList: Resource[];
|
||||||
relationList: MemoRelation[];
|
relationList: MemoRelation[];
|
||||||
@ -65,7 +64,6 @@ const MemoEditor = (props: Props) => {
|
|||||||
const resourceStore = useResourceStore();
|
const resourceStore = useResourceStore();
|
||||||
const currentUser = useCurrentUser();
|
const currentUser = useCurrentUser();
|
||||||
const [state, setState] = useState<State>({
|
const [state, setState] = useState<State>({
|
||||||
initialized: false,
|
|
||||||
memoVisibility: Visibility.PRIVATE,
|
memoVisibility: Visibility.PRIVATE,
|
||||||
resourceList: [],
|
resourceList: [],
|
||||||
relationList: [],
|
relationList: [],
|
||||||
@ -112,16 +110,13 @@ const MemoEditor = (props: Props) => {
|
|||||||
|
|
||||||
useAsyncEffect(async () => {
|
useAsyncEffect(async () => {
|
||||||
if (!memoName) {
|
if (!memoName) {
|
||||||
setState((prevState) => ({
|
|
||||||
...prevState,
|
|
||||||
initialized: true,
|
|
||||||
}));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const memo = await memoStore.getOrFetchMemoByName(memoName);
|
const memo = await memoStore.getOrFetchMemoByName(memoName);
|
||||||
if (memo) {
|
if (memo) {
|
||||||
handleEditorFocus();
|
handleEditorFocus();
|
||||||
|
setDisplayTime(memo.displayTime);
|
||||||
setState((prevState) => ({
|
setState((prevState) => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
memoVisibility: memo.visibility,
|
memoVisibility: memo.visibility,
|
||||||
@ -129,14 +124,9 @@ const MemoEditor = (props: Props) => {
|
|||||||
relationList: memo.relations,
|
relationList: memo.relations,
|
||||||
location: memo.location,
|
location: memo.location,
|
||||||
}));
|
}));
|
||||||
setDisplayTime(memo.displayTime);
|
|
||||||
if (!contentCache) {
|
if (!contentCache) {
|
||||||
editorRef.current?.setContent(memo.content ?? "");
|
editorRef.current?.setContent(memo.content ?? "");
|
||||||
}
|
}
|
||||||
setState((prevState) => ({
|
|
||||||
...prevState,
|
|
||||||
initialized: true,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}, [memoName]);
|
}, [memoName]);
|
||||||
|
|
||||||
@ -403,10 +393,6 @@ const MemoEditor = (props: Props) => {
|
|||||||
|
|
||||||
const allowSave = (hasContent || state.resourceList.length > 0) && !state.isUploadingResource && !state.isRequesting;
|
const allowSave = (hasContent || state.resourceList.length > 0) && !state.isUploadingResource && !state.isRequesting;
|
||||||
|
|
||||||
if (!state.initialized) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MemoEditorContext.Provider
|
<MemoEditorContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user