Skip to content

Commit

Permalink
Made sound volume optional, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevintjuhz committed Apr 7, 2022
1 parent 3a39f32 commit 03d5d4c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Through exports:
-- msgType: string
-- -options: success, error, warning, primary, default
-- msg: string
exports.rr_uilib:NotifyS(msgType, msg)
-- useSound | optional: boolean
exports.rr_uilib:NotifyS(msgType, msg, useSound)
```

`Serversided`
Expand All @@ -49,7 +50,8 @@ exports.rr_uilib:NotifyS(msgType, msg)
-- msgType: string
-- -options: success, error, warning, primary, default
-- msg: string
exports.rr_uilib:NotifyS(source, msgType, msg)
-- useSound | optional: boolean
exports.rr_uilib:NotifyS(source, msgType, msg, useSound)
```

Through event:
Expand All @@ -59,7 +61,8 @@ Through event:
-- msgType: string
-- -options: success, error, warning, primary, default
-- msg: string
TriggerClientEvent("rr_uilib:NotifyS", source, msgType, msg)
-- useSound | optional: boolean
TriggerClientEvent("rr_uilib:NotifyS", source, msgType, msg, useSound)
```

### **Notifications**
Expand Down Expand Up @@ -161,9 +164,10 @@ You can import your .mp3 files into the web/public/sounds path. To retrieve them
```lua
local data = {
sound = "soundName",
volume = 0.1
volume = 0.1 -- Optional / Default 0.5
}
```

Options: `"success", "info", "error"`

### **DrawText**
Expand Down
19 changes: 16 additions & 3 deletions resources/client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ AddEventHandler('rr_uilib:Notify', function(props)
Notify(props)
end)

function NotifyS(msgType, msg)
function NotifyS(msgType, msg, sound)
local useSound = false

local data = {
type = msgType,
msg = msg,
}

if sound then
useSound = true
if msgType == "success" then
data.sound = "success"
elseif msgType == "error" then
data.sound = "error"
else
data.sound = "info"
end
end

if msgType == "success" then
data.icon = "fa-solid fa-check"
elseif msgType == "error" then
Expand All @@ -31,8 +44,8 @@ function NotifyS(msgType, msg)
end

RegisterNetEvent('rr_uilib:NotifyS')
AddEventHandler('rr_uilib:NotifyS', function(msgType, msg)
NotifyS(msgType, msg)
AddEventHandler('rr_uilib:NotifyS', function(msgType, msg, sound)
NotifyS(msgType, msg, sound)
end)

exports("NotifyS", NotifyS)
Expand Down
4 changes: 2 additions & 2 deletions resources/server/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ function Notify(source, props)
end
exports('Notify', Notify)

function NotifyS(source, msgType, msg)
TriggerClientEvent('rr_uilib:NotifyS', source, msgType, msg)
function NotifyS(source, msgType, msg, useSound)
TriggerClientEvent('rr_uilib:NotifyS', source, msgType, msg, useSound)
end
exports('NotifyS', NotifyS)

Expand Down
33 changes: 13 additions & 20 deletions web/src/components/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,24 @@ const Notification: React.FC = () => {
const handleBackgroundClasses = (data: any) => {
if (!data.type) data.type = "success";

let oldClass =
"inline-flex flex-shrink-0 justify-center items-center w-8 h-8 rounded-lg ";
let oldClass = "inline-flex flex-shrink-0 justify-center items-center w-8 h-8 rounded-lg ";

if (data.style && data.style !== "colored") {
switch (data.type) {
case "success":
oldClass +=
"text-green-500 bg-green-200 dark:bg-green-800 dark:text-green-200";
oldClass += "text-green-500 bg-green-200 dark:bg-green-800 dark:text-green-200";
break;
case "error":
oldClass +=
"text-red-600 bg-red-200 dark:bg-red-800 dark:text-red-200";
oldClass += "text-red-600 bg-red-200 dark:bg-red-800 dark:text-red-200";
break;
case "primary":
oldClass +=
"text-blue-600 bg-blue-200 dark:bg-blue-800 dark:text-blue-200";
oldClass += "text-blue-600 bg-blue-200 dark:bg-blue-800 dark:text-blue-200";
break;
case "warning":
oldClass +=
"text-yellow-600 bg-yellow-200 dark:bg-yellow-600 dark:text-yellow-200";
oldClass += "text-yellow-600 bg-yellow-200 dark:bg-yellow-600 dark:text-yellow-200";
break;
case "default":
oldClass +=
"text-slate-600 bg-slate-200 dark:text-slate-200 dark:bg-slate-700";
oldClass += "text-slate-600 bg-slate-200 dark:text-slate-200 dark:bg-slate-700";
break;
}
} else if (data.style === "colored") {
Expand All @@ -44,8 +38,7 @@ const Notification: React.FC = () => {
};

const handleWrapperClasses = (data: any) => {
let className =
"flex items-center p-3 mb-0.5 w-full max-w-xs shadow rounded-lg ";
let className = "flex items-center p-3 mb-0.5 w-full max-w-xs shadow rounded-lg ";

if (!data.style) data.style = "default";

Expand Down Expand Up @@ -92,10 +85,10 @@ const Notification: React.FC = () => {
return "";
};

const handleSoundClass = (data: any) => {
const handleSoundClass = (soundName: string, soundVolume: number = 0.5) => {
const soundFolder = process.env.PUBLIC_URL + "/sounds/";
let sound = new Audio(soundFolder + data.sound + '.mp3');
sound.volume = data.volume;
let sound = new Audio(`${soundFolder + soundName}.mp3`);
sound.volume = soundVolume;
sound.play();
};

Expand All @@ -120,10 +113,10 @@ const Notification: React.FC = () => {
);
};

useNuiEvent<any>("notify", data => {
useNuiEvent<any>("notify", (data) => {
handleNotification(data);
if(data.sound && data.sound !== null && data.sound !== "" && data.volume && data.volume !== null && data.volume !== ""){
handleSoundClass(data);
if (data.sound && data.sound !== null && data.sound !== "") {
handleSoundClass(data.sound, data.volume);
}
});
return (
Expand Down
3 changes: 0 additions & 3 deletions web/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./components/App";
// import { VisibilityProvider } from "./providers/VisibilityProvider";

ReactDOM.render(
<React.StrictMode>
{/* <VisibilityProvider> */}
<App />
{/* </VisibilityProvider> */}
</React.StrictMode>,
document.getElementById("root")
);

0 comments on commit 03d5d4c

Please sign in to comment.