Skip to content

Commit

Permalink
fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Dec 20, 2023
1 parent 170fe36 commit cac1df9
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/shared/hooks/useAddTiddlerToServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,33 @@ export function useAddTiddlerToServer() {
},
);

// const getusername = async () => {
// const baseURL = new URL('status', addProtocolToUrl(onlineServers[0].uri));
// try {
// const data = await fetch(baseURL, {
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json',
// 'x-requested-with': 'TiddlyWiki',
// },
// });
// const { username } = await data.json() as IServerStatus;
// return username;
// } catch (error) {
// console.error('[获取用户名]:', baseURL, error);
// }
// };
const getusername = async (server: IServerInfo) => {
const baseURL = new URL('status', addProtocolToUrl(server.uri));
try {
const data = await fetch(baseURL);
const { username } = await data.json() as IServerStatus;
return username;
} catch (error) {
// 即使报错, 控制台也看不到, 因为点击保存后窗口就关闭了, 也许可以使用notify
console.error('[获取用户名]:', error);
}
};

const addTiddlerToServer = useCallback(
async (server: IServerInfo, tiddler: ITiddlerToAdd): Promise<void> => {
const putTiddlerUrl = new URL(
`recipes/default/tiddlers/${tiddler.title as string}`,
addProtocolToUrl(server.uri),
);
// const username = await getusername();
const username = await getusername(server);

try {
tiddler.created = toTWUTCString(new Date());
// tiddler.creator = (username != null) || t('TWCollector');
tiddler.creator = t('TWCollector');
tiddler.creator = username ?? t('TWCollector');
tiddler.modifier = username ?? t('TWCollector');
// Recent tab need `modified` field to work
tiddler.modified = toTWUTCString(new Date());
// user name on the view template is `modifier`
tiddler.modifier = t('TWCollector');
await fetch(putTiddlerUrl, {
method: 'PUT',
headers: {
Expand Down

0 comments on commit cac1df9

Please sign in to comment.