Coder Social home page Coder Social logo

paperkeem / nextron-chatty Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 737 KB

๐Ÿ“ฑ next.js, Electron, firebase๋กœ ๊ตฌํ˜„ํ•œ ๋ฐ์Šคํฌํƒ‘ ์ฑ„ํŒ… ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž…๋‹ˆ๋‹ค.

TypeScript 91.75% JavaScript 0.48% CSS 7.77%
firebase nextron react typescript electron nextjs

nextron-chatty's Introduction

๐Ÿ’ป Chatting Desktop App




๐Ÿ—“ ๊ตฌํ˜„ ์ผ์ • : 2023.02.06 - 2023.02.22

๋ชฉ์ฐจ

  1. ํ”„๋กœ์ ํŠธ ์‹คํ–‰ ๋ฐฉ๋ฒ•
  2. ๊ตฌํ˜„์‚ฌํ•ญ
  3. ํšŒ๊ณ 

ํ”„๋กœ์ ํŠธ ์‹คํ–‰ ๋ฐฉ๋ฒ•


  1. ์˜์กด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
npm install
  1. ํŒŒ์ด์–ด ๋ฒ ์ด์Šค ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ renderer ํด๋” ํ•˜์œ„์— .env ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
NEXT_PUBLIC_API=
NEXT_PUBLIC_FIRE_DOMAIN=
NEXT_PUBLIC_FIRE_DATABASE=
NEXT_PUBLIC_FIRE_PROJECT_ID=
  1. ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
npm run dev

etc. chatty APP์—์„œ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ…Œ์ŠคํŠธ ๊ณ„์ • ์ •๋ณด์ž…๋‹ˆ๋‹ค.

[email protected] / password=1234567
[email protected] / password=1234567
[email protected] / password=1234567

๊ธฐ์ˆ ์Šคํƒ

React, TypeScript, Next.js, Electron, firebase, react-query, ant-design


๊ตฌํ˜„์‚ฌํ•ญ

  • ํšŒ์›๊ฐ€์ž…
  • ๋กœ๊ทธ์ธ
  • ์œ ์ € ๋ชฉ๋ก
  • 1:1 ์ฑ„ํŒ…
  • ๊ทธ๋ฃน์ฑ„ํŒ…

1. firebase authentication์„ ์ด์šฉํ•œ ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ

export const signInEmail = async (
email: string,
password: string,
name: string
) => {
return createUserWithEmailAndPassword(auth, email, password)
.then((res) => {
localStorage.setItem(
"userData",
JSON.stringify({ ...res.user, displayName: name })
);
updateProfile(auth.currentUser, {
displayName: name,
});
return res;
})
.catch((error) => {
const errorMessage = error.message;
throw new Error("ํšŒ์›๊ฐ€์ž… ์—๋Ÿฌ" + errorMessage);
});
};
export const logInEmail = async (email: string, password: string) => {
return signInWithEmailAndPassword(auth, email, password).catch((error) => {
const errorMessage = error.message;
throw new Error("๋กœ๊ทธ์ธ ์—๋Ÿฌ" + errorMessage);
});
};

  • ํšŒ์› ๊ฐ€์ž… ์‹œ, updateProfileํ•จ์ˆ˜๋ฅผ ์ฒด์ด๋‹ ํ•ด firebase user ์ •๋ณด์— ๋‚ด๊ฐ€ ๊ธฐ์ž…ํ•œ ๋ณ„๋ช…์„ displayName์œผ๋กœ ์ €์žฅ
  • ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ, throw new Error๋กœ ์—๋Ÿฌ๋ฅผ ๋„˜๊ฒจ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์—์„œ ์ฒ˜๋ฆฌ

export const onAuth = (callback) => {
onAuthStateChanged(auth, async (user) => {
let updatedUser;
if (user != null) {
updatedUser = user;
localStorage.setItem("userData", JSON.stringify(updatedUser));
} else if (user == null) {
updatedUser = JSON.parse(localStorage.getItem("userData")) || {};
}
callback(updatedUser);
});
};
const AuthContext = createContext<TAuth | null>(null);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<any>({});
useEffect(() => {
onAuth(setUser);
}, []);
return (
<AuthContext.Provider
value={{
user,
uid: user?.uid,
name: user?.displayName,
email: user?.email,
}}
>
{children}
</AuthContext.Provider>
);
}

  • ๋กœ๊ทธ์ธ ์‹œ firebase์˜ onAuthStateChanged ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด current user ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋„๋ก ๊ตฌํ˜„
  • ์—๋Ÿฌ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์œ ์ € ์ •๋ณด๋ฅผ localStorage์— ์ด์ค‘์œผ๋กœ ์ €์žฅ
  • context api๋ฅผ ์ด์šฉํ•ด ์œ ์ € ์ •๋ณด๋ฅผ ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌ

2. react-query๋ฅผ ์ด์šฉํ•ด firebase api ํ˜ธ์ถœ / refetchInterval ์„ค์ •์„ ํ†ตํ•ด ํด๋ง ๊ตฌํ˜„

const {
chatQuery: { data: chat },
sendMsgQuery,
} = useChat(roomId as string);
export default function useChat(roomId: string) {
const queryClicent = useQueryClient();
const chatQuery = useQuery<IChat[] | unknown[]>(
["chat", roomId],
() => getChatMsg(roomId),
{ refetchInterval: 1000 }
);
const sendMsgQuery = useMutation(
({ roomId, uid, name, message }) => {
setChatMsg(roomId, uid, name, message);
},
{
onSuccess: () => {
queryClicent.invalidateQueries(["chat", roomId]);
},
}
);
return { chatQuery, sendMsgQuery };
}

  • react query์˜ refetchInterval์„ 1000์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์„œ๋ฒ„ polling์„ ๊ตฌํ˜„
  • ๊ฐ™์€ query key๋ฅผ ์“ฐ๋Š” ํ•จ์ˆ˜๋ผ๋ฆฌ custom hook์œผ๋กœ ๋ฌถ์–ด ๊ด€๋ฆฌ ์šฉ์ด์„ฑ ์ฆ๋Œ€
  • ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ผ ์‹œ์—๋Š” useMutation์„ ์ด์šฉํ•ด ์ฑ„ํŒ…๋ฐฉ ๋ฐ์ดํ„ฐ ๋™๊ธฐํ™”๋ฅผ ๊ตฌํ˜„

3. ์ฑ„ํŒ…๋ฐฉ ๋‚ด ์ค‘๋ณต๋˜๋Š” ๋กœ์ง์€ custom hook์œผ๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌ

export default function useMsg(callback, obj) {
const [message, setMessage] = useState("");
const handleChange = (e: any) => {
const { value } = e.target;
setMessage(value);
};
const handleSubmit = async (e: any) => {
e.preventDefault();
if (!message || message.trim() === "") {
return;
}
setMessage(null);
callback.mutate({ ...obj, message });
};
return { message, handleChange, handleSubmit };
}
export default function useScroll(chat) {
const chatWindowRef = useRef(null);
useEffect(() => {
chatWindowRef.current.scrollTop = chatWindowRef.current.scrollHeight;
}, [chat]);
return { chatWindowRef };
}

  • 1:1 ์ฑ„ํŒ…๋ฐฉ๊ณผ ๊ทธ๋ฃน ์ฑ„ํŒ…๋ฐฉ์—์„œ ๋™์ผํ•˜๊ฒŒ ์“ฐ์ด๋Š” input event์™€ scroll ์ด๋ฒคํŠธ๋ฅผ custom hook์œผ๋กœ ๊ด€๋ฆฌ

const { chatWindowRef } = useScroll(chat);
const { message, handleChange, handleSubmit } = useMsg(sendMsgQuery, {
roomId,
uid,
name,
});
const { chatWindowRef } = useScroll(groupChat);
const { message, handleChange, handleSubmit } = useMsg(sendGroupQuery, {
idx,
uid,
name,
});

  • useMutation ํ•จ์ˆ˜๋ฅผ callback์œผ๋กœ ๋„˜๊ธฐ๊ณ  obj๋ฅผ Value ๊ฐ’์œผ๋กœ ๋„˜๊ฒจ ์ด์šฉ

nextron-chatty's People

Contributors

paperkeem avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.