-
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
d08b7aa
commit 61221a7
Showing
2 changed files
with
67 additions
and
16 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,48 @@ | ||
"use client"; | ||
|
||
import liff from "@line/liff"; | ||
import React, {createContext, useContext, useEffect, useState, ReactNode} from "react"; | ||
|
||
interface LiffContextType { | ||
liffObject: typeof liff | null; | ||
liffError: string | null; | ||
} | ||
|
||
const LiffContext = createContext<LiffContextType | undefined>(undefined); | ||
|
||
interface LiffProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const LiffProvider: React.FC<LiffProviderProps> = ({children}) => { | ||
const [liffObject, setLiffObject] = useState<typeof liff | null>(null); | ||
const [liffError, setLiffError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
liff | ||
.init({liffId: '2006732958-EAK9vggN'}) | ||
.then(() => { | ||
console.log("LIFF initialization is done"); | ||
setLiffObject(liff); | ||
}) | ||
.catch((error: any) => { | ||
console.error(`LIFF initialization failed: ${error}`); | ||
setLiffError(error.toString()); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<LiffContext.Provider value={{liffObject, liffError} | ||
}> | ||
{children} | ||
</LiffContext.Provider> | ||
); | ||
}; | ||
|
||
export const useLiff = () => { | ||
const context = useContext(LiffContext); | ||
if (!context) { | ||
throw new Error("useLiff must be used within a LiffProvider"); | ||
} | ||
return context; | ||
}; |