mirror of
https://github.com/usememos/memos.git
synced 2025-02-22 22:27:37 +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")
|
||||
}
|
||||
} else if path == "location" {
|
||||
memo.Payload.Location = convertLocationToStore(request.Memo.Location)
|
||||
update.Payload = memo.Payload
|
||||
payload := memo.Payload
|
||||
payload.Location = convertLocationToStore(request.Memo.Location)
|
||||
update.Payload = payload
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Button, IconButton, Input } from "@mui/joy";
|
||||
import { LatLng } from "leaflet";
|
||||
import { MapPinIcon } from "lucide-react";
|
||||
import { MapPinIcon, XIcon } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import LeafletMap from "@/components/LeafletMap";
|
||||
@ -10,10 +10,11 @@ import { useTranslate } from "@/utils/i18n";
|
||||
|
||||
interface Props {
|
||||
location?: Location;
|
||||
onChange: (location: Location) => void;
|
||||
onChange: (location?: Location) => void;
|
||||
}
|
||||
|
||||
interface State {
|
||||
initilized: boolean;
|
||||
placeholder: string;
|
||||
position: LatLng;
|
||||
}
|
||||
@ -21,14 +22,24 @@ interface State {
|
||||
const LocationSelector = (props: Props) => {
|
||||
const t = useTranslate();
|
||||
const [state, setState] = useState<State>({
|
||||
initilized: false,
|
||||
placeholder: props.location?.placeholder || "",
|
||||
position: new LatLng(props.location?.latitude || 0, props.location?.longitude || 0),
|
||||
});
|
||||
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(() => {
|
||||
if (popoverOpen && !props.location) {
|
||||
const handleError = (error: any, errorMessage: string) => {
|
||||
setState({ ...state, initilized: true });
|
||||
toast.error(errorMessage);
|
||||
console.error(error);
|
||||
};
|
||||
@ -38,7 +49,7 @@ const LocationSelector = (props: Props) => {
|
||||
(position) => {
|
||||
const lat = position.coords.latitude;
|
||||
const lng = position.coords.longitude;
|
||||
setState({ ...state, position: new LatLng(lat, lng) });
|
||||
setState({ ...state, position: new LatLng(lat, lng), initilized: true });
|
||||
},
|
||||
(error) => {
|
||||
handleError(error, "Error getting current position");
|
||||
@ -69,21 +80,30 @@ const LocationSelector = (props: Props) => {
|
||||
setState({ ...state, position });
|
||||
};
|
||||
|
||||
const removeLocation = (e: React.MouseEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
props.onChange(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover open={popoverOpen} onOpenChange={setPopoverOpen}>
|
||||
<PopoverTrigger>
|
||||
<IconButton size="sm" component="div">
|
||||
<IconButton className="group" size="sm" component="div">
|
||||
<MapPinIcon className="w-5 h-5 mx-auto shrink-0" />
|
||||
{props.location && (
|
||||
<span className="font-normal ml-0.5 text-ellipsis whitespace-nowrap overflow-hidden max-w-32">
|
||||
{props.location.placeholder}
|
||||
</span>
|
||||
<>
|
||||
<span className="font-normal ml-0.5 text-ellipsis whitespace-nowrap overflow-hidden max-w-32">
|
||||
{props.location.placeholder}
|
||||
</span>
|
||||
<XIcon className="w-5 h-5 mx-auto shrink-0 hidden group-hover:block hover:opacity-80" onClick={removeLocation} />
|
||||
</>
|
||||
)}
|
||||
</IconButton>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="center">
|
||||
<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">
|
||||
<Input
|
||||
placeholder="Choose location"
|
||||
|
@ -45,7 +45,6 @@ export interface Props {
|
||||
}
|
||||
|
||||
interface State {
|
||||
initialized: boolean;
|
||||
memoVisibility: Visibility;
|
||||
resourceList: Resource[];
|
||||
relationList: MemoRelation[];
|
||||
@ -65,7 +64,6 @@ const MemoEditor = (props: Props) => {
|
||||
const resourceStore = useResourceStore();
|
||||
const currentUser = useCurrentUser();
|
||||
const [state, setState] = useState<State>({
|
||||
initialized: false,
|
||||
memoVisibility: Visibility.PRIVATE,
|
||||
resourceList: [],
|
||||
relationList: [],
|
||||
@ -112,16 +110,13 @@ const MemoEditor = (props: Props) => {
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
if (!memoName) {
|
||||
setState((prevState) => ({
|
||||
...prevState,
|
||||
initialized: true,
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
const memo = await memoStore.getOrFetchMemoByName(memoName);
|
||||
if (memo) {
|
||||
handleEditorFocus();
|
||||
setDisplayTime(memo.displayTime);
|
||||
setState((prevState) => ({
|
||||
...prevState,
|
||||
memoVisibility: memo.visibility,
|
||||
@ -129,14 +124,9 @@ const MemoEditor = (props: Props) => {
|
||||
relationList: memo.relations,
|
||||
location: memo.location,
|
||||
}));
|
||||
setDisplayTime(memo.displayTime);
|
||||
if (!contentCache) {
|
||||
editorRef.current?.setContent(memo.content ?? "");
|
||||
}
|
||||
setState((prevState) => ({
|
||||
...prevState,
|
||||
initialized: true,
|
||||
}));
|
||||
}
|
||||
}, [memoName]);
|
||||
|
||||
@ -403,10 +393,6 @@ const MemoEditor = (props: Props) => {
|
||||
|
||||
const allowSave = (hasContent || state.resourceList.length > 0) && !state.isUploadingResource && !state.isRequesting;
|
||||
|
||||
if (!state.initialized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<MemoEditorContext.Provider
|
||||
value={{
|
||||
|
Loading…
x
Reference in New Issue
Block a user