-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87d2b44
commit e7d08e7
Showing
2 changed files
with
100 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use client"; | ||
import { customSession } from "@/@types/customSession"; | ||
import { fileType } from "@/@types/fileType"; | ||
import { | ||
getFileInfo, | ||
getFileContent, | ||
updateFileInfo, | ||
uploadFile, | ||
} from "@/googledrive"; | ||
import { useSession } from "next-auth/react"; | ||
import { useState, useEffect } from "react"; | ||
|
||
export function useFile(fileId: string): { | ||
fileContent: fileType | undefined; | ||
title: string; | ||
updateFileContent: (content: fileType) => void; | ||
updateTitle: (title: string) => void; | ||
loading: boolean; | ||
saving: boolean; | ||
} { | ||
const [fileContent, setFileContent] = useState<fileType | undefined>(); | ||
const [title, setTitle] = useState(""); | ||
const [loading, setLoading] = useState(true); | ||
const { data: session }: { data: customSession | null } = | ||
useSession() as unknown as { data: customSession }; | ||
const token = session?.accessToken; | ||
useEffect(() => { | ||
(async () => { | ||
if (!token) return; | ||
try { | ||
await Promise.all([ | ||
setTitle((await getFileInfo(token, fileId)).name), | ||
setFileContent(JSON.parse(await getFileContent(token, fileId))), | ||
]); | ||
setLoading(false); | ||
} catch (e: any) { | ||
// 空ファイルでは "SyntaxError: Unexpected end of JSON input" を吐くが問題なし | ||
if (e.message !== "Unexpected end of JSON input") console.error(e); | ||
} | ||
})(); | ||
}, [token, fileId]); | ||
const [shouldSaveTitle, setShouldSaveTitle] = useState(false); // タイトルを保存する必要があるときはtrue | ||
const [shouldSaveFileContent, setShouldSaveFileContent] = useState(false); // ファイル本体を保存する必要があるときはtrue | ||
const [saving, setSaving] = useState(false); // 保存中はtrue | ||
const [titleSaving, setTitleSaving] = useState(false); // タイトルを保存中はtrue | ||
const [fileContentSaving, setFileContentSaving] = useState(false); // ファイル本体を保存中はtrue | ||
|
||
/** | ||
* タイトルを更新します | ||
* @returns | ||
*/ | ||
async function updateTitle(title: string) { | ||
if (!token) return; | ||
if (title === "") return; | ||
if (titleSaving) { | ||
setShouldSaveTitle(true); | ||
return; | ||
} | ||
setSaving(true); | ||
setTitleSaving(true); | ||
await updateFileInfo(token, fileId, { | ||
name: title, | ||
}); | ||
if (shouldSaveTitle) updateTitle(title); | ||
setSaving(false); | ||
setTitleSaving(false); | ||
setShouldSaveTitle(false); | ||
} | ||
/** | ||
* ファイル本体を更新します | ||
* @returns | ||
*/ | ||
async function updateFileContent(fileContent: fileType) { | ||
if (!token) return; | ||
if (fileContent?.content?.length === 0) return; | ||
if (fileContentSaving) { | ||
setShouldSaveFileContent(true); | ||
return; | ||
} | ||
setSaving(true); | ||
setFileContentSaving(true); | ||
await uploadFile(token, fileId, JSON.stringify(fileContent)); | ||
if (shouldSaveFileContent) updateFileContent(fileContent); | ||
setSaving(false); | ||
setFileContentSaving(false); | ||
setShouldSaveFileContent(false); | ||
} | ||
return { | ||
fileContent, | ||
title, | ||
updateTitle, | ||
updateFileContent, | ||
loading, | ||
saving, | ||
}; | ||
} |